parse_ls_vga.c 21 KB

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