parse_ls_vga.c 20 KB

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