parse_ls_vga.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. /*
  2. Routines for parsing output from the `ls' command.
  3. Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. 2005, 2006, 2007, 2011
  5. The Free Software Foundation, Inc.
  6. Copyright (C) 1995, 1996 Miguel de Icaza
  7. Written by:
  8. Miguel de Icaza, 1995, 1996
  9. Slava Zanko <slavazanko@gmail.com>, 2011
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file
  24. * \brief Source: Utilities for VFS modules
  25. * \author Miguel de Icaza
  26. * \date 1995, 1996
  27. */
  28. #include <config.h>
  29. #include <ctype.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include "lib/global.h"
  33. #include "lib/widget.h" /* message() */
  34. #include "utilvfs.h"
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. /* Parsing code is used by ftpfs, fish and extfs */
  38. #define MAXCOLS 30
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. static char *columns[MAXCOLS]; /* Points to the string in column n */
  42. static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */
  43. static size_t vfs_parce_ls_final_num_spaces = 0;
  44. /*** file scope functions ************************************************************************/
  45. /* --------------------------------------------------------------------------------------------- */
  46. static int
  47. is_num (int idx)
  48. {
  49. char *column = columns[idx];
  50. if (!column || column[0] < '0' || column[0] > '9')
  51. return 0;
  52. return 1;
  53. }
  54. /* --------------------------------------------------------------------------------------------- */
  55. /* Return 1 for MM-DD-YY and MM-DD-YYYY */
  56. static int
  57. is_dos_date (const char *str)
  58. {
  59. int len;
  60. if (!str)
  61. return 0;
  62. len = strlen (str);
  63. if (len != 8 && len != 10)
  64. return 0;
  65. if (str[2] != str[5])
  66. return 0;
  67. if (!strchr ("\\-/", (int) str[2]))
  68. return 0;
  69. return 1;
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. static int
  73. is_week (const char *str, struct tm *tim)
  74. {
  75. static const char *week = "SunMonTueWedThuFriSat";
  76. const char *pos;
  77. if (!str)
  78. return 0;
  79. pos = strstr (week, str);
  80. if (pos != NULL)
  81. {
  82. if (tim != NULL)
  83. tim->tm_wday = (pos - week) / 3;
  84. return 1;
  85. }
  86. return 0;
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. static int
  90. is_month (const char *str, struct tm *tim)
  91. {
  92. static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
  93. const char *pos;
  94. if (!str)
  95. return 0;
  96. pos = strstr (month, str);
  97. if (pos != NULL)
  98. {
  99. if (tim != NULL)
  100. tim->tm_mon = (pos - month) / 3;
  101. return 1;
  102. }
  103. return 0;
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. /**
  107. * Check for possible locale's abbreviated month name (Jan..Dec).
  108. * Any 3 bytes long string without digit, control and punctuation characters.
  109. * isalpha() is locale specific, so it cannot be used if current
  110. * locale is "C" and ftp server use Cyrillic.
  111. * NB: It is assumed there are no whitespaces in month.
  112. */
  113. static int
  114. is_localized_month (const char *month)
  115. {
  116. int i = 0;
  117. if (!month)
  118. return 0;
  119. while ((i < 3) && *month && !isdigit ((unsigned char) *month)
  120. && !iscntrl ((unsigned char) *month) && !ispunct ((unsigned char) *month))
  121. {
  122. i++;
  123. month++;
  124. }
  125. return ((i == 3) && (*month == 0));
  126. }
  127. /* --------------------------------------------------------------------------------------------- */
  128. static int
  129. is_time (const char *str, struct tm *tim)
  130. {
  131. const char *p, *p2;
  132. if (str == NULL)
  133. return 0;
  134. p = strchr (str, ':');
  135. p2 = strrchr (str, ':');
  136. if (p != NULL && p2 != NULL)
  137. {
  138. if (p != p2)
  139. {
  140. if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
  141. return 0;
  142. }
  143. else
  144. {
  145. if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
  146. return 0;
  147. }
  148. }
  149. else
  150. return 0;
  151. return 1;
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. static int
  155. is_year (char *str, struct tm *tim)
  156. {
  157. long year;
  158. if (!str)
  159. return 0;
  160. if (strchr (str, ':'))
  161. return 0;
  162. if (strlen (str) != 4)
  163. return 0;
  164. if (sscanf (str, "%ld", &year) != 1)
  165. return 0;
  166. if (year < 1900 || year > 3000)
  167. return 0;
  168. tim->tm_year = (int) (year - 1900);
  169. return 1;
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. /*** public functions ****************************************************************************/
  173. /* --------------------------------------------------------------------------------------------- */
  174. gboolean
  175. vfs_parse_filetype (const char *s, size_t * ret_skipped, mode_t * ret_type)
  176. {
  177. mode_t type;
  178. switch (*s)
  179. {
  180. case 'd':
  181. type = S_IFDIR;
  182. break;
  183. case 'b':
  184. type = S_IFBLK;
  185. break;
  186. case 'c':
  187. type = S_IFCHR;
  188. break;
  189. case 'l':
  190. type = S_IFLNK;
  191. break;
  192. #ifdef S_IFSOCK
  193. case 's':
  194. type = S_IFSOCK;
  195. break;
  196. #else
  197. case 's':
  198. type = S_IFIFO;
  199. break;
  200. #endif
  201. #ifdef S_IFDOOR /* Solaris door */
  202. case 'D':
  203. type = S_IFDOOR;
  204. break;
  205. #else
  206. case 'D':
  207. type = S_IFIFO;
  208. break;
  209. #endif
  210. case 'p':
  211. type = S_IFIFO;
  212. break;
  213. #ifdef S_IFNAM /* Special named files */
  214. case 'n':
  215. type = S_IFNAM;
  216. break;
  217. #else
  218. case 'n':
  219. type = S_IFREG;
  220. break;
  221. #endif
  222. case 'm': /* Don't know what these are :-) */
  223. case '-':
  224. case '?':
  225. type = S_IFREG;
  226. break;
  227. default:
  228. return FALSE;
  229. }
  230. *ret_type = type;
  231. *ret_skipped = 1;
  232. return TRUE;
  233. }
  234. /* --------------------------------------------------------------------------------------------- */
  235. gboolean
  236. vfs_parse_fileperms (const char *s, size_t * ret_skipped, mode_t * ret_perms)
  237. {
  238. const char *p;
  239. mode_t perms;
  240. p = s;
  241. perms = 0;
  242. switch (*p++)
  243. {
  244. case '-':
  245. break;
  246. case 'r':
  247. perms |= S_IRUSR;
  248. break;
  249. default:
  250. return FALSE;
  251. }
  252. switch (*p++)
  253. {
  254. case '-':
  255. break;
  256. case 'w':
  257. perms |= S_IWUSR;
  258. break;
  259. default:
  260. return FALSE;
  261. }
  262. switch (*p++)
  263. {
  264. case '-':
  265. break;
  266. case 'S':
  267. perms |= S_ISUID;
  268. break;
  269. case 's':
  270. perms |= S_IXUSR | S_ISUID;
  271. break;
  272. case 'x':
  273. perms |= S_IXUSR;
  274. break;
  275. default:
  276. return FALSE;
  277. }
  278. switch (*p++)
  279. {
  280. case '-':
  281. break;
  282. case 'r':
  283. perms |= S_IRGRP;
  284. break;
  285. default:
  286. return FALSE;
  287. }
  288. switch (*p++)
  289. {
  290. case '-':
  291. break;
  292. case 'w':
  293. perms |= S_IWGRP;
  294. break;
  295. default:
  296. return FALSE;
  297. }
  298. switch (*p++)
  299. {
  300. case '-':
  301. break;
  302. case 'S':
  303. perms |= S_ISGID;
  304. break;
  305. case 'l':
  306. perms |= S_ISGID;
  307. break; /* found on Solaris */
  308. case 's':
  309. perms |= S_IXGRP | S_ISGID;
  310. break;
  311. case 'x':
  312. perms |= S_IXGRP;
  313. break;
  314. default:
  315. return FALSE;
  316. }
  317. switch (*p++)
  318. {
  319. case '-':
  320. break;
  321. case 'r':
  322. perms |= S_IROTH;
  323. break;
  324. default:
  325. return FALSE;
  326. }
  327. switch (*p++)
  328. {
  329. case '-':
  330. break;
  331. case 'w':
  332. perms |= S_IWOTH;
  333. break;
  334. default:
  335. return FALSE;
  336. }
  337. switch (*p++)
  338. {
  339. case '-':
  340. break;
  341. case 'T':
  342. perms |= S_ISVTX;
  343. break;
  344. case 't':
  345. perms |= S_IXOTH | S_ISVTX;
  346. break;
  347. case 'x':
  348. perms |= S_IXOTH;
  349. break;
  350. default:
  351. return FALSE;
  352. }
  353. if (*p == '+')
  354. { /* ACLs on Solaris, HP-UX and others */
  355. p++;
  356. }
  357. *ret_skipped = p - s;
  358. *ret_perms = perms;
  359. return TRUE;
  360. }
  361. /* --------------------------------------------------------------------------------------------- */
  362. gboolean
  363. vfs_parse_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode)
  364. {
  365. const char *p;
  366. mode_t type, perms;
  367. size_t skipped;
  368. p = s;
  369. if (!vfs_parse_filetype (p, &skipped, &type))
  370. return FALSE;
  371. p += skipped;
  372. if (!vfs_parse_fileperms (p, &skipped, &perms))
  373. return FALSE;
  374. p += skipped;
  375. *ret_skipped = p - s;
  376. *ret_mode = type | perms;
  377. return TRUE;
  378. }
  379. /* --------------------------------------------------------------------------------------------- */
  380. gboolean
  381. vfs_parse_raw_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode)
  382. {
  383. const char *p;
  384. mode_t remote_type = 0, local_type, perms = 0;
  385. p = s;
  386. /* isoctal */
  387. while (*p >= '0' && *p <= '7')
  388. {
  389. perms *= 010;
  390. perms += (*p - '0');
  391. ++p;
  392. }
  393. if (*p++ != ' ')
  394. return FALSE;
  395. while (*p >= '0' && *p <= '7')
  396. {
  397. remote_type *= 010;
  398. remote_type += (*p - '0');
  399. ++p;
  400. }
  401. if (*p++ != ' ')
  402. return FALSE;
  403. /* generated with:
  404. $ perl -e 'use Fcntl ":mode";
  405. my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
  406. foreach $t (@modes) { printf ("%o\n", $t); };'
  407. TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
  408. (see vfs_parse_filetype)
  409. */
  410. switch (remote_type)
  411. {
  412. case 020000:
  413. local_type = S_IFCHR;
  414. break;
  415. case 040000:
  416. local_type = S_IFDIR;
  417. break;
  418. case 060000:
  419. local_type = S_IFBLK;
  420. break;
  421. case 0120000:
  422. local_type = S_IFLNK;
  423. break;
  424. case 0100000:
  425. default: /* don't know what is it */
  426. local_type = S_IFREG;
  427. break;
  428. }
  429. *ret_skipped = p - s;
  430. *ret_mode = local_type | perms;
  431. return TRUE;
  432. }
  433. /* --------------------------------------------------------------------------------------------- */
  434. /** This function parses from idx in the columns[] array */
  435. int
  436. vfs_parse_filedate (int idx, time_t * t)
  437. {
  438. char *p;
  439. struct tm tim;
  440. int d[3];
  441. int got_year = 0;
  442. int l10n = 0; /* Locale's abbreviated month name */
  443. time_t current_time;
  444. struct tm *local_time;
  445. /* Let's setup default time values */
  446. current_time = time (NULL);
  447. local_time = localtime (&current_time);
  448. tim.tm_mday = local_time->tm_mday;
  449. tim.tm_mon = local_time->tm_mon;
  450. tim.tm_year = local_time->tm_year;
  451. tim.tm_hour = 0;
  452. tim.tm_min = 0;
  453. tim.tm_sec = 0;
  454. tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
  455. p = columns[idx++];
  456. /* We eat weekday name in case of extfs */
  457. if (is_week (p, &tim))
  458. p = columns[idx++];
  459. /* Month name */
  460. if (is_month (p, &tim))
  461. {
  462. /* And we expect, it followed by day number */
  463. if (is_num (idx))
  464. tim.tm_mday = (int) atol (columns[idx++]);
  465. else
  466. return 0; /* No day */
  467. }
  468. else
  469. {
  470. /* We expect:
  471. 3 fields max or we'll see oddities with certain file names.
  472. So both year and time is not allowed.
  473. Mon DD hh:mm[:ss]
  474. Mon DD YYYY
  475. But in case of extfs we allow these date formats:
  476. MM-DD-YY hh:mm[:ss]
  477. where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
  478. YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
  479. /* Special case with MM-DD-YY or MM-DD-YYYY */
  480. if (is_dos_date (p))
  481. {
  482. p[2] = p[5] = '-';
  483. if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3)
  484. {
  485. /* Months are zero based */
  486. if (d[0] > 0)
  487. d[0]--;
  488. if (d[2] > 1900)
  489. {
  490. d[2] -= 1900;
  491. }
  492. else
  493. {
  494. /* Y2K madness */
  495. if (d[2] < 70)
  496. d[2] += 100;
  497. }
  498. tim.tm_mon = d[0];
  499. tim.tm_mday = d[1];
  500. tim.tm_year = d[2];
  501. got_year = 1;
  502. }
  503. else
  504. return 0; /* sscanf failed */
  505. }
  506. else
  507. {
  508. /* Locale's abbreviated month name followed by day number */
  509. if (is_localized_month (p) && (is_num (idx++)))
  510. l10n = 1;
  511. else
  512. return 0; /* unsupported format */
  513. }
  514. }
  515. /* Here we expect to find time or year */
  516. if (is_num (idx) && (is_time (columns[idx], &tim) || (got_year = is_year (columns[idx], &tim))))
  517. idx++;
  518. else
  519. return 0; /* Neither time nor date */
  520. /*
  521. * If the date is less than 6 months in the past, it is shown without year
  522. * other dates in the past or future are shown with year but without time
  523. * This does not check for years before 1900 ... I don't know, how
  524. * to represent them at all
  525. */
  526. if (!got_year && local_time->tm_mon < 6
  527. && local_time->tm_mon < tim.tm_mon && tim.tm_mon - local_time->tm_mon >= 6)
  528. tim.tm_year--;
  529. *t = mktime (&tim);
  530. if (l10n || (*t < 0))
  531. *t = 0;
  532. return idx;
  533. }
  534. /* --------------------------------------------------------------------------------------------- */
  535. int
  536. vfs_split_text (char *p)
  537. {
  538. char *original = p;
  539. int numcols;
  540. memset (columns, 0, sizeof (columns));
  541. for (numcols = 0; *p && numcols < MAXCOLS; numcols++)
  542. {
  543. while (*p == ' ' || *p == '\r' || *p == '\n')
  544. {
  545. *p = 0;
  546. p++;
  547. }
  548. columns[numcols] = p;
  549. column_ptr[numcols] = p - original;
  550. while (*p && *p != ' ' && *p != '\r' && *p != '\n')
  551. p++;
  552. }
  553. return numcols;
  554. }
  555. /* --------------------------------------------------------------------------------------------- */
  556. void
  557. vfs_parse_ls_lga_init (void)
  558. {
  559. vfs_parce_ls_final_num_spaces = 1;
  560. }
  561. /* --------------------------------------------------------------------------------------------- */
  562. size_t
  563. vfs_parse_ls_lga_get_final_spaces (void)
  564. {
  565. return vfs_parce_ls_final_num_spaces;
  566. }
  567. /* --------------------------------------------------------------------------------------------- */
  568. gboolean
  569. vfs_parse_ls_lga (const char *p, struct stat * s, char **filename, char **linkname,
  570. size_t * num_spaces)
  571. {
  572. int idx, idx2, num_cols;
  573. int i;
  574. char *p_copy = NULL;
  575. char *t = NULL;
  576. const char *line = p;
  577. size_t skipped;
  578. if (strncmp (p, "total", 5) == 0)
  579. return FALSE;
  580. if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
  581. goto error;
  582. p += skipped;
  583. if (*p == ' ') /* Notwell 4 */
  584. p++;
  585. if (*p == '[')
  586. {
  587. if (strlen (p) <= 8 || p[8] != ']')
  588. goto error;
  589. /* Should parse here the Notwell permissions :) */
  590. if (S_ISDIR (s->st_mode))
  591. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
  592. else
  593. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  594. p += 9;
  595. }
  596. else
  597. {
  598. size_t lc_skipped;
  599. mode_t perms;
  600. if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
  601. goto error;
  602. p += lc_skipped;
  603. s->st_mode |= perms;
  604. }
  605. p_copy = g_strdup (p);
  606. num_cols = vfs_split_text (p_copy);
  607. s->st_nlink = atol (columns[0]);
  608. if (s->st_nlink <= 0)
  609. goto error;
  610. if (!is_num (1))
  611. s->st_uid = vfs_finduid (columns[1]);
  612. else
  613. s->st_uid = (uid_t) atol (columns[1]);
  614. /* Mhm, the ls -lg did not produce a group field */
  615. for (idx = 3; idx <= 5; idx++)
  616. if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
  617. || is_dos_date (columns[idx]) || is_localized_month (columns[idx]))
  618. break;
  619. if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
  620. goto error;
  621. /* We don't have gid */
  622. if (idx == 3 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
  623. idx2 = 2;
  624. else
  625. {
  626. /* We have gid field */
  627. if (is_num (2))
  628. s->st_gid = (gid_t) atol (columns[2]);
  629. else
  630. s->st_gid = vfs_findgid (columns[2]);
  631. idx2 = 3;
  632. }
  633. /* This is device */
  634. if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))
  635. {
  636. int maj, min;
  637. /* Corner case: there is no whitespace(s) between maj & min */
  638. if (!is_num (idx2) && idx2 == 2)
  639. {
  640. if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &maj, &min) != 2)
  641. goto error;
  642. }
  643. else
  644. {
  645. if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
  646. goto error;
  647. if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
  648. goto error;
  649. }
  650. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  651. s->st_rdev = makedev (maj, min);
  652. #endif
  653. s->st_size = 0;
  654. }
  655. else
  656. {
  657. /* Common file size */
  658. if (!is_num (idx2))
  659. goto error;
  660. #ifdef HAVE_ATOLL
  661. s->st_size = (off_t) atoll (columns[idx2]);
  662. #else
  663. s->st_size = (off_t) atof (columns[idx2]);
  664. #endif
  665. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  666. s->st_rdev = 0;
  667. #endif
  668. }
  669. idx = vfs_parse_filedate (idx, &s->st_mtime);
  670. if (!idx)
  671. goto error;
  672. /* Use resulting time value */
  673. s->st_atime = s->st_ctime = s->st_mtime;
  674. /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
  675. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  676. s->st_blksize = 512;
  677. #endif
  678. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  679. s->st_blocks = (s->st_size + 511) / 512;
  680. #endif
  681. if (num_spaces != NULL)
  682. {
  683. *num_spaces = column_ptr[idx] - column_ptr[idx - 1] - strlen (columns[idx - 1]);
  684. if (strcmp (columns[idx], "..") == 0)
  685. vfs_parce_ls_final_num_spaces = *num_spaces;
  686. }
  687. for (i = idx + 1, idx2 = 0; i < num_cols; i++)
  688. if (strcmp (columns[i], "->") == 0)
  689. {
  690. idx2 = i;
  691. break;
  692. }
  693. if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
  694. && idx2)
  695. {
  696. if (filename)
  697. {
  698. *filename = g_strndup (p + column_ptr[idx], column_ptr[idx2] - column_ptr[idx] - 1);
  699. }
  700. if (linkname)
  701. {
  702. t = g_strdup (p + column_ptr[idx2 + 1]);
  703. *linkname = t;
  704. }
  705. }
  706. else
  707. {
  708. /* Extract the filename from the string copy, not from the columns
  709. * this way we have a chance of entering hidden directories like ". ."
  710. */
  711. if (filename)
  712. {
  713. /*
  714. * filename = g_strdup (columns [idx++]);
  715. */
  716. t = g_strdup (p + column_ptr[idx]);
  717. *filename = t;
  718. }
  719. if (linkname)
  720. *linkname = NULL;
  721. }
  722. if (t)
  723. {
  724. int p2 = strlen (t);
  725. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  726. t[p2] = 0;
  727. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  728. t[p2] = 0;
  729. }
  730. g_free (p_copy);
  731. return TRUE;
  732. error:
  733. {
  734. static int errorcount = 0;
  735. if (++errorcount < 5)
  736. {
  737. message (D_ERROR, _("Cannot parse:"), "%s", (p_copy && *p_copy) ? p_copy : line);
  738. }
  739. else if (errorcount == 5)
  740. message (D_ERROR, MSG_ERROR, _("More parsing errors will be ignored."));
  741. }
  742. g_free (p_copy);
  743. return FALSE;
  744. }
  745. /* --------------------------------------------------------------------------------------------- */