parse_ls_vga.c 21 KB

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