parse_ls_vga.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. /*
  2. Routines for parsing output from the 'ls' command.
  3. Copyright (C) 1988-2015
  4. Free Software Foundation, Inc.
  5. Copyright (C) 1995, 1996 Miguel de Icaza
  6. Written by:
  7. Miguel de Icaza, 1995, 1996
  8. Slava Zanko <slavazanko@gmail.com>, 2011
  9. This file is part of the Midnight Commander.
  10. The Midnight Commander is free software: you can redistribute it
  11. and/or modify it under the terms of the GNU General Public License as
  12. published by the Free Software Foundation, either version 3 of the License,
  13. or (at your option) any later version.
  14. The Midnight Commander is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file
  23. * \brief Source: Utilities for VFS modules
  24. * \author Miguel de Icaza
  25. * \date 1995, 1996
  26. */
  27. #include <config.h>
  28. #include <ctype.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include "lib/global.h"
  32. #include "lib/unixcompat.h" /* makedev */
  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. /* cppcheck-suppress invalidscanf */
  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. /* cppcheck-suppress invalidscanf */
  485. if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3)
  486. {
  487. /* Months are zero based */
  488. if (d[0] > 0)
  489. d[0]--;
  490. if (d[2] > 1900)
  491. {
  492. d[2] -= 1900;
  493. }
  494. else
  495. {
  496. /* Y2K madness */
  497. if (d[2] < 70)
  498. d[2] += 100;
  499. }
  500. tim.tm_mon = d[0];
  501. tim.tm_mday = d[1];
  502. tim.tm_year = d[2];
  503. got_year = 1;
  504. }
  505. else
  506. return 0; /* sscanf failed */
  507. }
  508. else
  509. {
  510. /* Locale's abbreviated month name followed by day number */
  511. if (is_localized_month (p) && (is_num (idx++)))
  512. l10n = 1;
  513. else
  514. return 0; /* unsupported format */
  515. }
  516. }
  517. /* Here we expect to find time or year */
  518. if (is_num (idx) && (is_time (columns[idx], &tim) || (got_year = is_year (columns[idx], &tim))))
  519. idx++;
  520. else
  521. return 0; /* Neither time nor date */
  522. /*
  523. * If the date is less than 6 months in the past, it is shown without year
  524. * other dates in the past or future are shown with year but without time
  525. * This does not check for years before 1900 ... I don't know, how
  526. * to represent them at all
  527. */
  528. if (!got_year && local_time->tm_mon < 6
  529. && local_time->tm_mon < tim.tm_mon && tim.tm_mon - local_time->tm_mon >= 6)
  530. tim.tm_year--;
  531. *t = mktime (&tim);
  532. if (l10n || (*t < 0))
  533. *t = 0;
  534. return idx;
  535. }
  536. /* --------------------------------------------------------------------------------------------- */
  537. int
  538. vfs_split_text (char *p)
  539. {
  540. char *original = p;
  541. int numcols;
  542. memset (columns, 0, sizeof (columns));
  543. for (numcols = 0; *p && numcols < MAXCOLS; numcols++)
  544. {
  545. while (*p == ' ' || *p == '\r' || *p == '\n')
  546. {
  547. *p = 0;
  548. p++;
  549. }
  550. columns[numcols] = p;
  551. column_ptr[numcols] = p - original;
  552. while (*p && *p != ' ' && *p != '\r' && *p != '\n')
  553. p++;
  554. }
  555. return numcols;
  556. }
  557. /* --------------------------------------------------------------------------------------------- */
  558. void
  559. vfs_parse_ls_lga_init (void)
  560. {
  561. vfs_parce_ls_final_num_spaces = 1;
  562. }
  563. /* --------------------------------------------------------------------------------------------- */
  564. size_t
  565. vfs_parse_ls_lga_get_final_spaces (void)
  566. {
  567. return vfs_parce_ls_final_num_spaces;
  568. }
  569. /* --------------------------------------------------------------------------------------------- */
  570. gboolean
  571. vfs_parse_ls_lga (const char *p, struct stat * s, char **filename, char **linkname,
  572. size_t * num_spaces)
  573. {
  574. int idx, idx2, num_cols;
  575. int i;
  576. char *p_copy = NULL;
  577. char *t = NULL;
  578. const char *line = p;
  579. size_t skipped;
  580. if (strncmp (p, "total", 5) == 0)
  581. return FALSE;
  582. if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
  583. goto error;
  584. p += skipped;
  585. if (*p == ' ') /* Notwell 4 */
  586. p++;
  587. if (*p == '[')
  588. {
  589. if (strlen (p) <= 8 || p[8] != ']')
  590. goto error;
  591. /* Should parse here the Notwell permissions :) */
  592. if (S_ISDIR (s->st_mode))
  593. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
  594. else
  595. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  596. p += 9;
  597. }
  598. else
  599. {
  600. size_t lc_skipped;
  601. mode_t perms;
  602. if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
  603. goto error;
  604. p += lc_skipped;
  605. s->st_mode |= perms;
  606. }
  607. p_copy = g_strdup (p);
  608. num_cols = vfs_split_text (p_copy);
  609. s->st_nlink = atol (columns[0]);
  610. if (s->st_nlink <= 0)
  611. goto error;
  612. if (!is_num (1))
  613. s->st_uid = vfs_finduid (columns[1]);
  614. else
  615. s->st_uid = (uid_t) atol (columns[1]);
  616. /* Mhm, the ls -lg did not produce a group field */
  617. for (idx = 3; idx <= 5; idx++)
  618. if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
  619. || is_dos_date (columns[idx]) || is_localized_month (columns[idx]))
  620. break;
  621. if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
  622. goto error;
  623. /* We don't have gid */
  624. if (idx == 3 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
  625. idx2 = 2;
  626. else
  627. {
  628. /* We have gid field */
  629. if (is_num (2))
  630. s->st_gid = (gid_t) atol (columns[2]);
  631. else
  632. s->st_gid = vfs_findgid (columns[2]);
  633. idx2 = 3;
  634. }
  635. /* This is device */
  636. if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))
  637. {
  638. int maj, min;
  639. /* Corner case: there is no whitespace(s) between maj & min */
  640. if (!is_num (idx2) && idx2 == 2)
  641. {
  642. /* cppcheck-suppress invalidscanf */
  643. if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &maj, &min) != 2)
  644. goto error;
  645. }
  646. else
  647. {
  648. /* cppcheck-suppress invalidscanf */
  649. if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
  650. goto error;
  651. /* cppcheck-suppress invalidscanf */
  652. if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
  653. goto error;
  654. }
  655. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  656. s->st_rdev = makedev (maj, min);
  657. #endif
  658. s->st_size = 0;
  659. }
  660. else
  661. {
  662. /* Common file size */
  663. if (!is_num (idx2))
  664. goto error;
  665. s->st_size = (off_t) g_ascii_strtoll (columns[idx2], NULL, 10);
  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 (DIR_IS_DOTDOT (columns[idx]))
  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. /* --------------------------------------------------------------------------------------------- */