utilvfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829
  1. /* Utilities for VFS modules.
  2. Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2006, 2007 Free Software Foundation, Inc.
  4. Copyright (C) 1995, 1996 Miguel de Icaza
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public License
  7. as published by the Free Software Foundation; either version 2 of
  8. the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  16. /**
  17. * \file
  18. * \brief Source: Utilities for VFS modules
  19. * \author Miguel de Icaza
  20. * \date 1995, 1996
  21. */
  22. #include <config.h>
  23. #include <ctype.h>
  24. #include <pwd.h>
  25. #include <grp.h>
  26. #include <stdlib.h>
  27. #include "lib/global.h"
  28. #include "lib/unixcompat.h"
  29. #include "src/wtools.h" /* message() */
  30. #include "src/main.h" /* print_vfs_message */
  31. #include "src/history.h"
  32. #include "vfs.h"
  33. #include "utilvfs.h"
  34. /** Get current username
  35. *
  36. * @return g_malloc()ed string with the name of the currently logged in
  37. * user ("anonymous" if uid is not registered in the system)
  38. */
  39. char *
  40. vfs_get_local_username(void)
  41. {
  42. struct passwd * p_i;
  43. p_i = getpwuid (geteuid ());
  44. return (p_i && p_i->pw_name)
  45. ? g_strdup (p_i->pw_name)
  46. : g_strdup ("anonymous"); /* Unknown UID, strange */
  47. }
  48. /*
  49. * Look up a user or group name from a uid/gid, maintaining a cache.
  50. * FIXME, for now it's a one-entry cache.
  51. * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
  52. * This file should be modified for non-unix systems to do something
  53. * reasonable.
  54. */
  55. #ifndef TUNMLEN
  56. #define TUNMLEN 256
  57. #endif
  58. #ifndef TGNMLEN
  59. #define TGNMLEN 256
  60. #endif
  61. #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
  62. #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
  63. int
  64. vfs_finduid (const char *uname)
  65. {
  66. static int saveuid = -993;
  67. static char saveuname[TUNMLEN];
  68. static int my_uid = -993;
  69. struct passwd *pw;
  70. if (uname[0] != saveuname[0] /* Quick test w/o proc call */
  71. ||0 != strncmp (uname, saveuname, TUNMLEN)) {
  72. g_strlcpy (saveuname, uname, TUNMLEN);
  73. pw = getpwnam (uname);
  74. if (pw) {
  75. saveuid = pw->pw_uid;
  76. } else {
  77. saveuid = myuid;
  78. }
  79. }
  80. return saveuid;
  81. }
  82. int
  83. vfs_findgid (const char *gname)
  84. {
  85. static int savegid = -993;
  86. static char savegname[TGNMLEN];
  87. static int my_gid = -993;
  88. struct group *gr;
  89. if (gname[0] != savegname[0] /* Quick test w/o proc call */
  90. ||0 != strncmp (gname, savegname, TUNMLEN)) {
  91. g_strlcpy (savegname, gname, TUNMLEN);
  92. gr = getgrnam (gname);
  93. if (gr) {
  94. savegid = gr->gr_gid;
  95. } else {
  96. savegid = mygid;
  97. }
  98. }
  99. return savegid;
  100. }
  101. /*
  102. * Create a temporary file with a name resembling the original.
  103. * This is needed e.g. for local copies requested by extfs.
  104. * Some extfs scripts may look at the extension.
  105. * We also protect stupid scripts agains dangerous names.
  106. */
  107. int
  108. vfs_mkstemps (char **pname, const char *prefix, const char *param_basename)
  109. {
  110. const char *p;
  111. char *suffix, *q;
  112. int shift;
  113. int fd;
  114. /* Strip directories */
  115. p = strrchr (param_basename, PATH_SEP);
  116. if (!p)
  117. p = param_basename;
  118. else
  119. p++;
  120. /* Protection against very long names */
  121. shift = strlen (p) - (MC_MAXPATHLEN - 16);
  122. if (shift > 0)
  123. p += shift;
  124. suffix = g_malloc (MC_MAXPATHLEN);
  125. /* Protection against unusual characters */
  126. q = suffix;
  127. while (*p && (*p != '#')) {
  128. if (strchr (".-_@", *p) || isalnum ((unsigned char) *p))
  129. *q++ = *p;
  130. p++;
  131. }
  132. *q = 0;
  133. fd = mc_mkstemps (pname, prefix, suffix);
  134. g_free (suffix);
  135. return fd;
  136. }
  137. /* Parsing code is used by ftpfs, fish and extfs */
  138. #define MAXCOLS 30
  139. static char *columns[MAXCOLS]; /* Points to the string in column n */
  140. static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */
  141. int
  142. vfs_split_text (char *p)
  143. {
  144. char *original = p;
  145. int numcols;
  146. memset (columns, 0, sizeof (columns));
  147. for (numcols = 0; *p && numcols < MAXCOLS; numcols++) {
  148. while (*p == ' ' || *p == '\r' || *p == '\n') {
  149. *p = 0;
  150. p++;
  151. }
  152. columns[numcols] = p;
  153. column_ptr[numcols] = p - original;
  154. while (*p && *p != ' ' && *p != '\r' && *p != '\n')
  155. p++;
  156. }
  157. return numcols;
  158. }
  159. static int
  160. is_num (int idx)
  161. {
  162. char *column = columns[idx];
  163. if (!column || column[0] < '0' || column[0] > '9')
  164. return 0;
  165. return 1;
  166. }
  167. /* Return 1 for MM-DD-YY and MM-DD-YYYY */
  168. static int
  169. is_dos_date (const char *str)
  170. {
  171. int len;
  172. if (!str)
  173. return 0;
  174. len = strlen (str);
  175. if (len != 8 && len != 10)
  176. return 0;
  177. if (str[2] != str[5])
  178. return 0;
  179. if (!strchr ("\\-/", (int) str[2]))
  180. return 0;
  181. return 1;
  182. }
  183. static int
  184. is_week (const char *str, struct tm *tim)
  185. {
  186. static const char *week = "SunMonTueWedThuFriSat";
  187. const char *pos;
  188. if (!str)
  189. return 0;
  190. pos = strstr (week, str);
  191. if (pos != NULL) {
  192. if (tim != NULL)
  193. tim->tm_wday = (pos - week) / 3;
  194. return 1;
  195. }
  196. return 0;
  197. }
  198. static int
  199. is_month (const char *str, struct tm *tim)
  200. {
  201. static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
  202. const char *pos;
  203. if (!str)
  204. return 0;
  205. pos = strstr (month, str);
  206. if (pos != NULL) {
  207. if (tim != NULL)
  208. tim->tm_mon = (pos - month) / 3;
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. /*
  214. * Check for possible locale's abbreviated month name (Jan..Dec).
  215. * Any 3 bytes long string without digit, control and punctuation characters.
  216. * isalpha() is locale specific, so it cannot be used if current
  217. * locale is "C" and ftp server use Cyrillic.
  218. * NB: It is assumed there are no whitespaces in month.
  219. */
  220. static int
  221. is_localized_month (const char *month)
  222. {
  223. int i = 0;
  224. if (!month)
  225. return 0;
  226. while ((i < 3) && *month && !isdigit ((unsigned char) *month)
  227. && !iscntrl ((unsigned char) *month)
  228. && !ispunct ((unsigned char) *month)) {
  229. i++;
  230. month++;
  231. }
  232. return ((i == 3) && (*month == 0));
  233. }
  234. static int
  235. is_time (const char *str, struct tm *tim)
  236. {
  237. const char *p, *p2;
  238. if (str == NULL)
  239. return 0;
  240. p = strchr (str, ':');
  241. p2 = strrchr (str, ':');
  242. if (p != NULL && p2 != NULL) {
  243. if (p != p2) {
  244. if (sscanf
  245. (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min,
  246. &tim->tm_sec) != 3)
  247. return 0;
  248. } else {
  249. if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
  250. return 0;
  251. }
  252. } else
  253. return 0;
  254. return 1;
  255. }
  256. static int
  257. is_year (char *str, struct tm *tim)
  258. {
  259. long year;
  260. if (!str)
  261. return 0;
  262. if (strchr (str, ':'))
  263. return 0;
  264. if (strlen (str) != 4)
  265. return 0;
  266. if (sscanf (str, "%ld", &year) != 1)
  267. return 0;
  268. if (year < 1900 || year > 3000)
  269. return 0;
  270. tim->tm_year = (int) (year - 1900);
  271. return 1;
  272. }
  273. gboolean
  274. vfs_parse_filetype (const char *s, size_t *ret_skipped, mode_t *ret_type)
  275. {
  276. mode_t type;
  277. switch (*s) {
  278. case 'd': type = S_IFDIR; break;
  279. case 'b': type = S_IFBLK; break;
  280. case 'c': type = S_IFCHR; break;
  281. case 'l': type = S_IFLNK; break;
  282. #ifdef S_IFSOCK
  283. case 's': type = S_IFSOCK; break;
  284. #else
  285. case 's': type = S_IFIFO; break;
  286. #endif
  287. #ifdef S_IFDOOR /* Solaris door */
  288. case 'D': type = S_IFDOOR; break;
  289. #else
  290. case 'D': type = S_IFIFO; break;
  291. #endif
  292. case 'p': type = S_IFIFO; break;
  293. #ifdef S_IFNAM /* Special named files */
  294. case 'n': type = S_IFNAM; break;
  295. #else
  296. case 'n': type = S_IFREG; break;
  297. #endif
  298. case 'm': /* Don't know what these are :-) */
  299. case '-':
  300. case '?': type = S_IFREG; break;
  301. default: return FALSE;
  302. }
  303. *ret_type = type;
  304. *ret_skipped = 1;
  305. return TRUE;
  306. }
  307. gboolean
  308. vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms)
  309. {
  310. const char *p;
  311. mode_t perms;
  312. p = s;
  313. perms = 0;
  314. switch (*p++) {
  315. case '-': break;
  316. case 'r': perms |= S_IRUSR; break;
  317. default: return FALSE;
  318. }
  319. switch (*p++) {
  320. case '-': break;
  321. case 'w': perms |= S_IWUSR; break;
  322. default: return FALSE;
  323. }
  324. switch (*p++) {
  325. case '-': break;
  326. case 'S': perms |= S_ISUID; break;
  327. case 's': perms |= S_IXUSR | S_ISUID; break;
  328. case 'x': perms |= S_IXUSR; break;
  329. default: return FALSE;
  330. }
  331. switch (*p++) {
  332. case '-': break;
  333. case 'r': perms |= S_IRGRP; break;
  334. default: return FALSE;
  335. }
  336. switch (*p++) {
  337. case '-': break;
  338. case 'w': perms |= S_IWGRP; break;
  339. default: return FALSE;
  340. }
  341. switch (*p++) {
  342. case '-': break;
  343. case 'S': perms |= S_ISGID; break;
  344. case 'l': perms |= S_ISGID; break; /* found on Solaris */
  345. case 's': perms |= S_IXGRP | S_ISGID; break;
  346. case 'x': perms |= S_IXGRP; break;
  347. default: return FALSE;
  348. }
  349. switch (*p++) {
  350. case '-': break;
  351. case 'r': perms |= S_IROTH; break;
  352. default: return FALSE;
  353. }
  354. switch (*p++) {
  355. case '-': break;
  356. case 'w': perms |= S_IWOTH; break;
  357. default: return FALSE;
  358. }
  359. switch (*p++) {
  360. case '-': break;
  361. case 'T': perms |= S_ISVTX; break;
  362. case 't': perms |= S_IXOTH | S_ISVTX; break;
  363. case 'x': perms |= S_IXOTH; break;
  364. default: return FALSE;
  365. }
  366. if (*p == '+') { /* ACLs on Solaris, HP-UX and others */
  367. p++;
  368. }
  369. *ret_skipped = p - s;
  370. *ret_perms = perms;
  371. return TRUE;
  372. }
  373. gboolean
  374. vfs_parse_filemode (const char *s, size_t *ret_skipped,
  375. mode_t *ret_mode)
  376. {
  377. const char *p;
  378. mode_t type, perms;
  379. size_t skipped;
  380. p = s;
  381. if (!vfs_parse_filetype (p, &skipped, &type))
  382. return FALSE;
  383. p += skipped;
  384. if (!vfs_parse_fileperms (p, &skipped, &perms))
  385. return FALSE;
  386. p += skipped;
  387. *ret_skipped = p - s;
  388. *ret_mode = type | perms;
  389. return TRUE;
  390. }
  391. gboolean
  392. vfs_parse_raw_filemode (const char *s, size_t *ret_skipped,
  393. mode_t *ret_mode)
  394. {
  395. const char *p;
  396. mode_t remote_type = 0, local_type, perms = 0;
  397. p = s;
  398. /* isoctal */
  399. while(*p >= '0' && *p <= '7')
  400. {
  401. perms *= 010;
  402. perms += (*p - '0');
  403. ++p;
  404. }
  405. if (*p++ != ' ')
  406. return FALSE;
  407. while(*p >= '0' && *p <= '7')
  408. {
  409. remote_type *= 010;
  410. remote_type += (*p - '0');
  411. ++p;
  412. }
  413. if (*p++ != ' ')
  414. return FALSE;
  415. /* generated with:
  416. $ perl -e 'use Fcntl ":mode";
  417. my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
  418. foreach $t (@modes) { printf ("%o\n", $t); };'
  419. TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
  420. (see vfs_parse_filetype)
  421. */
  422. switch (remote_type)
  423. {
  424. case 020000:
  425. local_type = S_IFCHR; break;
  426. case 040000:
  427. local_type = S_IFDIR; break;
  428. case 060000:
  429. local_type = S_IFBLK; break;
  430. case 0120000:
  431. local_type = S_IFLNK; break;
  432. case 0100000:
  433. default: /* don't know what is it */
  434. local_type = S_IFREG; break;
  435. }
  436. *ret_skipped = p - s;
  437. *ret_mode = local_type | perms;
  438. return TRUE;
  439. }
  440. /* This function parses from idx in the columns[] array */
  441. int
  442. vfs_parse_filedate (int idx, time_t *t)
  443. {
  444. char *p;
  445. struct tm tim;
  446. int d[3];
  447. int got_year = 0;
  448. int l10n = 0; /* Locale's abbreviated month name */
  449. time_t current_time;
  450. struct tm *local_time;
  451. /* Let's setup default time values */
  452. current_time = time (NULL);
  453. local_time = localtime (&current_time);
  454. tim.tm_mday = local_time->tm_mday;
  455. tim.tm_mon = local_time->tm_mon;
  456. tim.tm_year = local_time->tm_year;
  457. tim.tm_hour = 0;
  458. tim.tm_min = 0;
  459. tim.tm_sec = 0;
  460. tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
  461. p = columns[idx++];
  462. /* We eat weekday name in case of extfs */
  463. if (is_week (p, &tim))
  464. p = columns[idx++];
  465. /* Month name */
  466. if (is_month (p, &tim)) {
  467. /* And we expect, it followed by day number */
  468. if (is_num (idx))
  469. tim.tm_mday = (int) atol (columns[idx++]);
  470. else
  471. return 0; /* No day */
  472. } else {
  473. /* We expect:
  474. 3 fields max or we'll see oddities with certain file names.
  475. So both year and time is not allowed.
  476. Mon DD hh:mm[:ss]
  477. Mon DD YYYY
  478. But in case of extfs we allow these date formats:
  479. MM-DD-YY hh:mm[:ss]
  480. where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
  481. YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
  482. /* Special case with MM-DD-YY or MM-DD-YYYY */
  483. if (is_dos_date (p)) {
  484. p[2] = p[5] = '-';
  485. if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) {
  486. /* Months are zero based */
  487. if (d[0] > 0)
  488. d[0]--;
  489. if (d[2] > 1900) {
  490. d[2] -= 1900;
  491. } else {
  492. /* Y2K madness */
  493. if (d[2] < 70)
  494. d[2] += 100;
  495. }
  496. tim.tm_mon = d[0];
  497. tim.tm_mday = d[1];
  498. tim.tm_year = d[2];
  499. got_year = 1;
  500. } else
  501. return 0; /* sscanf failed */
  502. } else {
  503. /* Locale's abbreviated month name followed by day number */
  504. if (is_localized_month (p) && (is_num (idx++)))
  505. l10n = 1;
  506. else
  507. return 0; /* unsupported format */
  508. }
  509. }
  510. /* Here we expect to find time or year */
  511. if (is_num (idx) && (is_time (columns[idx], &tim)
  512. || (got_year = is_year (columns[idx], &tim))))
  513. idx++;
  514. else
  515. return 0; /* Neither time nor date */
  516. /*
  517. * If the date is less than 6 months in the past, it is shown without year
  518. * other dates in the past or future are shown with year but without time
  519. * This does not check for years before 1900 ... I don't know, how
  520. * to represent them at all
  521. */
  522. if (!got_year && local_time->tm_mon < 6
  523. && local_time->tm_mon < tim.tm_mon
  524. && tim.tm_mon - local_time->tm_mon >= 6)
  525. tim.tm_year--;
  526. *t = mktime (&tim);
  527. if (l10n || (*t < 0))
  528. *t = 0;
  529. return idx;
  530. }
  531. int
  532. vfs_parse_ls_lga (const char *p, struct stat *s, char **filename,
  533. char **linkname)
  534. {
  535. int idx, idx2, num_cols;
  536. int i;
  537. char *p_copy = NULL;
  538. char *t = NULL;
  539. const char *line = p;
  540. size_t skipped;
  541. if (strncmp (p, "total", 5) == 0)
  542. return 0;
  543. if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
  544. goto error;
  545. p += skipped;
  546. if (*p == ' ') /* Notwell 4 */
  547. p++;
  548. if (*p == '[') {
  549. if (strlen (p) <= 8 || p[8] != ']')
  550. goto error;
  551. /* Should parse here the Notwell permissions :) */
  552. if (S_ISDIR (s->st_mode))
  553. s->st_mode |=
  554. (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP
  555. | S_IXOTH);
  556. else
  557. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  558. p += 9;
  559. } else {
  560. size_t lc_skipped;
  561. mode_t perms;
  562. if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
  563. goto error;
  564. p += lc_skipped;
  565. s->st_mode |= perms;
  566. }
  567. p_copy = g_strdup (p);
  568. num_cols = vfs_split_text (p_copy);
  569. s->st_nlink = atol (columns[0]);
  570. if (s->st_nlink <= 0)
  571. goto error;
  572. if (!is_num (1))
  573. s->st_uid = vfs_finduid (columns[1]);
  574. else
  575. s->st_uid = (uid_t) atol (columns[1]);
  576. /* Mhm, the ls -lg did not produce a group field */
  577. for (idx = 3; idx <= 5; idx++)
  578. if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
  579. || is_dos_date (columns[idx])
  580. || is_localized_month (columns[idx]))
  581. break;
  582. if (idx == 6
  583. || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
  584. goto error;
  585. /* We don't have gid */
  586. if (idx == 3
  587. || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
  588. idx2 = 2;
  589. else {
  590. /* We have gid field */
  591. if (is_num (2))
  592. s->st_gid = (gid_t) atol (columns[2]);
  593. else
  594. s->st_gid = vfs_findgid (columns[2]);
  595. idx2 = 3;
  596. }
  597. /* This is device */
  598. if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) {
  599. int maj, min;
  600. /* Corner case: there is no whitespace(s) between maj & min */
  601. if (!is_num (idx2) && idx2 == 2) {
  602. if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &min, &min) != 2)
  603. goto error;
  604. } else {
  605. if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
  606. goto error;
  607. if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
  608. goto error;
  609. }
  610. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  611. s->st_rdev = makedev (maj, min);
  612. #endif
  613. s->st_size = 0;
  614. } else {
  615. /* Common file size */
  616. if (!is_num (idx2))
  617. goto error;
  618. #ifdef HAVE_ATOLL
  619. s->st_size = (off_t) atoll (columns[idx2]);
  620. #else
  621. s->st_size = (off_t) atof (columns[idx2]);
  622. #endif
  623. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  624. s->st_rdev = 0;
  625. #endif
  626. }
  627. idx = vfs_parse_filedate (idx, &s->st_mtime);
  628. if (!idx)
  629. goto error;
  630. /* Use resulting time value */
  631. s->st_atime = s->st_ctime = s->st_mtime;
  632. /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
  633. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  634. s->st_blksize = 512;
  635. #endif
  636. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  637. s->st_blocks = (s->st_size + 511) / 512;
  638. #endif
  639. for (i = idx + 1, idx2 = 0; i < num_cols; i++)
  640. if (strcmp (columns[i], "->") == 0) {
  641. idx2 = i;
  642. break;
  643. }
  644. if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
  645. &&idx2) {
  646. if (filename) {
  647. *filename =
  648. g_strndup (p + column_ptr[idx],
  649. column_ptr[idx2] - column_ptr[idx] - 1);
  650. }
  651. if (linkname) {
  652. t = g_strdup (p + column_ptr[idx2 + 1]);
  653. *linkname = t;
  654. }
  655. } else {
  656. /* Extract the filename from the string copy, not from the columns
  657. * this way we have a chance of entering hidden directories like ". ."
  658. */
  659. if (filename) {
  660. /*
  661. * filename = g_strdup (columns [idx++]);
  662. */
  663. t = g_strdup (p + column_ptr[idx]);
  664. *filename = t;
  665. }
  666. if (linkname)
  667. *linkname = NULL;
  668. }
  669. if (t) {
  670. int p2 = strlen (t);
  671. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  672. t[p2] = 0;
  673. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  674. t[p2] = 0;
  675. }
  676. g_free (p_copy);
  677. return 1;
  678. error:
  679. {
  680. static int errorcount = 0;
  681. if (++errorcount < 5) {
  682. message (D_ERROR, _("Cannot parse:"), "%s",
  683. (p_copy && *p_copy) ? p_copy : line);
  684. } else if (errorcount == 5)
  685. message (D_ERROR, MSG_ERROR,
  686. _("More parsing errors will be ignored."));
  687. }
  688. g_free (p_copy);
  689. return 0;
  690. }
  691. void
  692. vfs_die (const char *m)
  693. {
  694. message (D_ERROR, _("Internal error:"), "%s", m);
  695. exit (EXIT_FAILURE);
  696. }
  697. char *
  698. vfs_get_password (const char *msg)
  699. {
  700. return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD);
  701. }