parse_ls_vga.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. /*
  2. Routines for parsing output from the 'ls' command.
  3. Copyright (C) 1988-2017
  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. /*
  461. ALLOWED DATE FORMATS
  462. We expect 3 fields max or we'll see oddities with certain file names.
  463. Formats that contain either year or time (the default 'ls' formats):
  464. * Mon DD hh:mm[:ss]
  465. * Mon DD YYYY
  466. Formats that contain both year and time, to make it easier to write
  467. extfs scripts:
  468. * MM-DD-YYYY hh:mm[:ss]
  469. * MM-DD-YY hh:mm[:ss]
  470. ('/' and '\' can be used instead of '-'.)
  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. (As for the "3 fields max" restriction: this prevents, for example, a
  474. file name "13:48" from being considered part of a "Sep 19 2016" date
  475. string preceding it.)
  476. */
  477. /* Month name */
  478. if (is_month (p, &tim))
  479. {
  480. /* And we expect, it followed by day number */
  481. if (is_num (idx))
  482. tim.tm_mday = (int) atol (columns[idx++]);
  483. else
  484. return 0; /* No day */
  485. }
  486. else
  487. {
  488. /* Case with MM-DD-YY or MM-DD-YYYY */
  489. if (is_dos_date (p))
  490. {
  491. p[2] = p[5] = '-';
  492. /* cppcheck-suppress invalidscanf */
  493. if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3)
  494. {
  495. /* Months are zero based */
  496. if (d[0] > 0)
  497. d[0]--;
  498. if (d[2] > 1900)
  499. {
  500. d[2] -= 1900;
  501. }
  502. else
  503. {
  504. /* Y2K madness */
  505. if (d[2] < 70)
  506. d[2] += 100;
  507. }
  508. tim.tm_mon = d[0];
  509. tim.tm_mday = d[1];
  510. tim.tm_year = d[2];
  511. got_year = 1;
  512. }
  513. else
  514. return 0; /* sscanf failed */
  515. }
  516. else
  517. {
  518. /* Locale's abbreviated month name followed by day number */
  519. if (is_localized_month (p) && (is_num (idx++)))
  520. l10n = 1;
  521. else
  522. return 0; /* unsupported format */
  523. }
  524. }
  525. /* Here we expect to find time or year */
  526. if (is_num (idx) && (is_time (columns[idx], &tim) || (got_year = is_year (columns[idx], &tim))))
  527. idx++;
  528. else
  529. return 0; /* Neither time nor date */
  530. /*
  531. * If the date is less than 6 months in the past, it is shown without year
  532. * other dates in the past or future are shown with year but without time
  533. * This does not check for years before 1900 ... I don't know, how
  534. * to represent them at all
  535. */
  536. if (!got_year && local_time->tm_mon < 6
  537. && local_time->tm_mon < tim.tm_mon && tim.tm_mon - local_time->tm_mon >= 6)
  538. tim.tm_year--;
  539. *t = mktime (&tim);
  540. if (l10n || (*t < 0))
  541. *t = 0;
  542. return idx;
  543. }
  544. /* --------------------------------------------------------------------------------------------- */
  545. int
  546. vfs_split_text (char *p)
  547. {
  548. char *original = p;
  549. int numcols;
  550. memset (columns, 0, sizeof (columns));
  551. for (numcols = 0; *p && numcols < MAXCOLS; numcols++)
  552. {
  553. while (*p == ' ' || *p == '\r' || *p == '\n')
  554. {
  555. *p = 0;
  556. p++;
  557. }
  558. columns[numcols] = p;
  559. column_ptr[numcols] = p - original;
  560. while (*p && *p != ' ' && *p != '\r' && *p != '\n')
  561. p++;
  562. }
  563. return numcols;
  564. }
  565. /* --------------------------------------------------------------------------------------------- */
  566. void
  567. vfs_parse_ls_lga_init (void)
  568. {
  569. vfs_parce_ls_final_num_spaces = 1;
  570. }
  571. /* --------------------------------------------------------------------------------------------- */
  572. size_t
  573. vfs_parse_ls_lga_get_final_spaces (void)
  574. {
  575. return vfs_parce_ls_final_num_spaces;
  576. }
  577. /* --------------------------------------------------------------------------------------------- */
  578. gboolean
  579. vfs_parse_ls_lga (const char *p, struct stat * s, char **filename, char **linkname,
  580. size_t * num_spaces)
  581. {
  582. int idx, idx2, num_cols;
  583. int i;
  584. char *p_copy = NULL;
  585. char *t = NULL;
  586. const char *line = p;
  587. size_t skipped;
  588. if (strncmp (p, "total", 5) == 0)
  589. return FALSE;
  590. if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
  591. goto error;
  592. p += skipped;
  593. if (*p == ' ') /* Notwell 4 */
  594. p++;
  595. if (*p == '[')
  596. {
  597. if (strlen (p) <= 8 || p[8] != ']')
  598. goto error;
  599. /* Should parse here the Notwell permissions :) */
  600. if (S_ISDIR (s->st_mode))
  601. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
  602. else
  603. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  604. p += 9;
  605. }
  606. else
  607. {
  608. size_t lc_skipped;
  609. mode_t perms;
  610. if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
  611. goto error;
  612. p += lc_skipped;
  613. s->st_mode |= perms;
  614. }
  615. p_copy = g_strdup (p);
  616. num_cols = vfs_split_text (p_copy);
  617. s->st_nlink = atol (columns[0]);
  618. if (s->st_nlink <= 0)
  619. goto error;
  620. if (!is_num (1))
  621. s->st_uid = vfs_finduid (columns[1]);
  622. else
  623. s->st_uid = (uid_t) atol (columns[1]);
  624. /* Mhm, the ls -lg did not produce a group field */
  625. for (idx = 3; idx <= 5; idx++)
  626. if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
  627. || is_dos_date (columns[idx]) || is_localized_month (columns[idx]))
  628. break;
  629. if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
  630. goto error;
  631. /* We don't have gid */
  632. if (idx == 3 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
  633. idx2 = 2;
  634. else
  635. {
  636. /* We have gid field */
  637. if (is_num (2))
  638. s->st_gid = (gid_t) atol (columns[2]);
  639. else
  640. s->st_gid = vfs_findgid (columns[2]);
  641. idx2 = 3;
  642. }
  643. /* This is device */
  644. if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))
  645. {
  646. int maj, min;
  647. /* Corner case: there is no whitespace(s) between maj & min */
  648. if (!is_num (idx2) && idx2 == 2)
  649. {
  650. /* cppcheck-suppress invalidscanf */
  651. if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &maj, &min) != 2)
  652. goto error;
  653. }
  654. else
  655. {
  656. /* cppcheck-suppress invalidscanf */
  657. if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
  658. goto error;
  659. /* cppcheck-suppress invalidscanf */
  660. if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
  661. goto error;
  662. }
  663. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  664. s->st_rdev = makedev (maj, min);
  665. #endif
  666. s->st_size = 0;
  667. }
  668. else
  669. {
  670. /* Common file size */
  671. if (!is_num (idx2))
  672. goto error;
  673. s->st_size = (off_t) g_ascii_strtoll (columns[idx2], NULL, 10);
  674. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  675. s->st_rdev = 0;
  676. #endif
  677. }
  678. idx = vfs_parse_filedate (idx, &s->st_mtime);
  679. if (!idx)
  680. goto error;
  681. /* Use resulting time value */
  682. s->st_atime = s->st_ctime = s->st_mtime;
  683. #ifdef HAVE_STRUCT_STAT_ST_MTIM
  684. s->st_atim.tv_nsec = s->st_mtim.tv_nsec = s->st_ctim.tv_nsec = 0;
  685. #endif
  686. /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
  687. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  688. s->st_blksize = 512;
  689. #endif
  690. vfs_adjust_stat (s);
  691. if (num_spaces != NULL)
  692. {
  693. *num_spaces = column_ptr[idx] - column_ptr[idx - 1] - strlen (columns[idx - 1]);
  694. if (DIR_IS_DOTDOT (columns[idx]))
  695. vfs_parce_ls_final_num_spaces = *num_spaces;
  696. }
  697. for (i = idx + 1, idx2 = 0; i < num_cols; i++)
  698. if (strcmp (columns[i], "->") == 0)
  699. {
  700. idx2 = i;
  701. break;
  702. }
  703. if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
  704. && idx2)
  705. {
  706. if (filename)
  707. {
  708. *filename = g_strndup (p + column_ptr[idx], column_ptr[idx2] - column_ptr[idx] - 1);
  709. }
  710. if (linkname)
  711. {
  712. t = g_strdup (p + column_ptr[idx2 + 1]);
  713. *linkname = t;
  714. }
  715. }
  716. else
  717. {
  718. /* Extract the filename from the string copy, not from the columns
  719. * this way we have a chance of entering hidden directories like ". ."
  720. */
  721. if (filename)
  722. {
  723. /*
  724. * filename = g_strdup (columns [idx++]);
  725. */
  726. t = g_strdup (p + column_ptr[idx]);
  727. *filename = t;
  728. }
  729. if (linkname)
  730. *linkname = NULL;
  731. }
  732. if (t)
  733. {
  734. int p2 = strlen (t);
  735. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  736. t[p2] = 0;
  737. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  738. t[p2] = 0;
  739. }
  740. g_free (p_copy);
  741. return TRUE;
  742. error:
  743. {
  744. static int errorcount = 0;
  745. if (++errorcount < 5)
  746. {
  747. message (D_ERROR, _("Cannot parse:"), "%s", (p_copy && *p_copy) ? p_copy : line);
  748. }
  749. else if (errorcount == 5)
  750. message (D_ERROR, MSG_ERROR, _("More parsing errors will be ignored."));
  751. }
  752. g_free (p_copy);
  753. return FALSE;
  754. }
  755. /* --------------------------------------------------------------------------------------------- */