parse_ls_vga.c 21 KB

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