parse_ls_vga.c 21 KB

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