utilvfs.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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 "../src/global.h"
  28. #include "../src/wtools.h" /* message() */
  29. #include "../src/main.h" /* print_vfs_message */
  30. #include "../src/unixcompat.h"
  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. if ((pos = strstr (week, str)) != NULL) {
  191. if (tim != NULL)
  192. tim->tm_wday = (pos - week) / 3;
  193. return 1;
  194. }
  195. return 0;
  196. }
  197. static int
  198. is_month (const char *str, struct tm *tim)
  199. {
  200. static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
  201. const char *pos;
  202. if (!str)
  203. return 0;
  204. if ((pos = strstr (month, str)) != NULL) {
  205. if (tim != NULL)
  206. tim->tm_mon = (pos - month) / 3;
  207. return 1;
  208. }
  209. return 0;
  210. }
  211. /*
  212. * Check for possible locale's abbreviated month name (Jan..Dec).
  213. * Any 3 bytes long string without digit, control and punctuation characters.
  214. * isalpha() is locale specific, so it cannot be used if current
  215. * locale is "C" and ftp server use Cyrillic.
  216. * NB: It is assumed there are no whitespaces in month.
  217. */
  218. static int
  219. is_localized_month (const char *month)
  220. {
  221. int i = 0;
  222. if (!month)
  223. return 0;
  224. while ((i < 3) && *month && !isdigit ((unsigned char) *month)
  225. && !iscntrl ((unsigned char) *month)
  226. && !ispunct ((unsigned char) *month)) {
  227. i++;
  228. month++;
  229. }
  230. return ((i == 3) && (*month == 0));
  231. }
  232. static int
  233. is_time (const char *str, struct tm *tim)
  234. {
  235. const char *p, *p2;
  236. if (!str)
  237. return 0;
  238. if ((p = strchr (str, ':')) && (p2 = strrchr (str, ':'))) {
  239. if (p != p2) {
  240. if (sscanf
  241. (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min,
  242. &tim->tm_sec) != 3)
  243. return 0;
  244. } else {
  245. if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
  246. return 0;
  247. }
  248. } else
  249. return 0;
  250. return 1;
  251. }
  252. static int
  253. is_year (char *str, struct tm *tim)
  254. {
  255. long year;
  256. if (!str)
  257. return 0;
  258. if (strchr (str, ':'))
  259. return 0;
  260. if (strlen (str) != 4)
  261. return 0;
  262. if (sscanf (str, "%ld", &year) != 1)
  263. return 0;
  264. if (year < 1900 || year > 3000)
  265. return 0;
  266. tim->tm_year = (int) (year - 1900);
  267. return 1;
  268. }
  269. gboolean
  270. vfs_parse_filetype (const char *s, size_t *ret_skipped, mode_t *ret_type)
  271. {
  272. mode_t type;
  273. switch (*s) {
  274. case 'd': type = S_IFDIR; break;
  275. case 'b': type = S_IFBLK; break;
  276. case 'c': type = S_IFCHR; break;
  277. case 'l': type = S_IFLNK; break;
  278. #ifdef S_IFSOCK
  279. case 's': type = S_IFSOCK; break;
  280. #else
  281. case 's': type = S_IFIFO; break;
  282. #endif
  283. #ifdef S_IFDOOR /* Solaris door */
  284. case 'D': type = S_IFDOOR; break;
  285. #else
  286. case 'D': type = S_IFIFO; break;
  287. #endif
  288. case 'p': type = S_IFIFO; break;
  289. #ifdef S_IFNAM /* Special named files */
  290. case 'n': type = S_IFNAM; break;
  291. #else
  292. case 'n': type = S_IFREG; break;
  293. #endif
  294. case 'm': /* Don't know what these are :-) */
  295. case '-':
  296. case '?': type = S_IFREG; break;
  297. default: return FALSE;
  298. }
  299. *ret_type = type;
  300. *ret_skipped = 1;
  301. return TRUE;
  302. }
  303. gboolean
  304. vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms)
  305. {
  306. const char *p;
  307. mode_t perms;
  308. p = s;
  309. perms = 0;
  310. switch (*p++) {
  311. case '-': break;
  312. case 'r': perms |= S_IRUSR; break;
  313. default: return FALSE;
  314. }
  315. switch (*p++) {
  316. case '-': break;
  317. case 'w': perms |= S_IWUSR; break;
  318. default: return FALSE;
  319. }
  320. switch (*p++) {
  321. case '-': break;
  322. case 'S': perms |= S_ISUID; break;
  323. case 's': perms |= S_IXUSR | S_ISUID; break;
  324. case 'x': perms |= S_IXUSR; break;
  325. default: return FALSE;
  326. }
  327. switch (*p++) {
  328. case '-': break;
  329. case 'r': perms |= S_IRGRP; break;
  330. default: return FALSE;
  331. }
  332. switch (*p++) {
  333. case '-': break;
  334. case 'w': perms |= S_IWGRP; break;
  335. default: return FALSE;
  336. }
  337. switch (*p++) {
  338. case '-': break;
  339. case 'S': perms |= S_ISGID; break;
  340. case 'l': perms |= S_ISGID; break; /* found on Solaris */
  341. case 's': perms |= S_IXGRP | S_ISGID; break;
  342. case 'x': perms |= S_IXGRP; break;
  343. default: return FALSE;
  344. }
  345. switch (*p++) {
  346. case '-': break;
  347. case 'r': perms |= S_IROTH; break;
  348. default: return FALSE;
  349. }
  350. switch (*p++) {
  351. case '-': break;
  352. case 'w': perms |= S_IWOTH; break;
  353. default: return FALSE;
  354. }
  355. switch (*p++) {
  356. case '-': break;
  357. case 'T': perms |= S_ISVTX; break;
  358. case 't': perms |= S_IXOTH | S_ISVTX; break;
  359. case 'x': perms |= S_IXOTH; break;
  360. default: return FALSE;
  361. }
  362. if (*p == '+') { /* ACLs on Solaris, HP-UX and others */
  363. p++;
  364. }
  365. *ret_skipped = p - s;
  366. *ret_perms = perms;
  367. return TRUE;
  368. }
  369. gboolean
  370. vfs_parse_filemode (const char *s, size_t *ret_skipped,
  371. mode_t *ret_mode)
  372. {
  373. const char *p;
  374. mode_t type, perms;
  375. size_t skipped;
  376. p = s;
  377. if (!vfs_parse_filetype (p, &skipped, &type))
  378. return FALSE;
  379. p += skipped;
  380. if (!vfs_parse_fileperms (p, &skipped, &perms))
  381. return FALSE;
  382. p += skipped;
  383. *ret_skipped = p - s;
  384. *ret_mode = type | perms;
  385. return TRUE;
  386. }
  387. gboolean
  388. vfs_parse_raw_filemode (const char *s, size_t *ret_skipped,
  389. mode_t *ret_mode)
  390. {
  391. const char *p;
  392. mode_t remote_type = 0, local_type, perms = 0;
  393. p = s;
  394. /* isoctal */
  395. while(*p >= '0' && *p <= '7')
  396. {
  397. perms *= 010;
  398. perms += (*p - '0');
  399. ++p;
  400. }
  401. if (*p++ != ' ')
  402. return FALSE;
  403. while(*p >= '0' && *p <= '7')
  404. {
  405. remote_type *= 010;
  406. remote_type += (*p - '0');
  407. ++p;
  408. }
  409. if (*p++ != ' ')
  410. return FALSE;
  411. /* generated with:
  412. $ perl -e 'use Fcntl ":mode";
  413. my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
  414. foreach $t (@modes) { printf ("%o\n", $t); };'
  415. TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
  416. (see vfs_parse_filetype)
  417. */
  418. switch (remote_type)
  419. {
  420. case 020000:
  421. local_type = S_IFCHR; break;
  422. case 040000:
  423. local_type = S_IFDIR; break;
  424. case 060000:
  425. local_type = S_IFBLK; break;
  426. case 0120000:
  427. local_type = S_IFLNK; break;
  428. case 0100000:
  429. default: /* don't know what is it */
  430. local_type = S_IFREG; break;
  431. }
  432. *ret_skipped = p - s;
  433. *ret_mode = local_type | perms;
  434. return TRUE;
  435. }
  436. /* This function parses from idx in the columns[] array */
  437. int
  438. vfs_parse_filedate (int idx, time_t *t)
  439. {
  440. char *p;
  441. struct tm tim;
  442. int d[3];
  443. int got_year = 0;
  444. int l10n = 0; /* Locale's abbreviated month name */
  445. time_t current_time;
  446. struct tm *local_time;
  447. /* Let's setup default time values */
  448. current_time = time (NULL);
  449. local_time = localtime (&current_time);
  450. tim.tm_mday = local_time->tm_mday;
  451. tim.tm_mon = local_time->tm_mon;
  452. tim.tm_year = local_time->tm_year;
  453. tim.tm_hour = 0;
  454. tim.tm_min = 0;
  455. tim.tm_sec = 0;
  456. tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
  457. p = columns[idx++];
  458. /* We eat weekday name in case of extfs */
  459. if (is_week (p, &tim))
  460. p = columns[idx++];
  461. /* Month name */
  462. if (is_month (p, &tim)) {
  463. /* And we expect, it followed by day number */
  464. if (is_num (idx))
  465. tim.tm_mday = (int) atol (columns[idx++]);
  466. else
  467. return 0; /* No day */
  468. } else {
  469. /* We expect:
  470. 3 fields max or we'll see oddities with certain file names.
  471. So both year and time is not allowed.
  472. Mon DD hh:mm[:ss]
  473. Mon DD YYYY
  474. But in case of extfs we allow these date formats:
  475. MM-DD-YY hh:mm[:ss]
  476. where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
  477. YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
  478. /* Special case with MM-DD-YY or MM-DD-YYYY */
  479. if (is_dos_date (p)) {
  480. p[2] = p[5] = '-';
  481. if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) {
  482. /* Months are zero based */
  483. if (d[0] > 0)
  484. d[0]--;
  485. if (d[2] > 1900) {
  486. d[2] -= 1900;
  487. } else {
  488. /* Y2K madness */
  489. if (d[2] < 70)
  490. d[2] += 100;
  491. }
  492. tim.tm_mon = d[0];
  493. tim.tm_mday = d[1];
  494. tim.tm_year = d[2];
  495. got_year = 1;
  496. } else
  497. return 0; /* sscanf failed */
  498. } else {
  499. /* Locale's abbreviated month name followed by day number */
  500. if (is_localized_month (p) && (is_num (idx++)))
  501. l10n = 1;
  502. else
  503. return 0; /* unsupported format */
  504. }
  505. }
  506. /* Here we expect to find time or year */
  507. if (is_num (idx) && (is_time (columns[idx], &tim)
  508. || (got_year = is_year (columns[idx], &tim))))
  509. idx++;
  510. else
  511. return 0; /* Neither time nor date */
  512. /*
  513. * If the date is less than 6 months in the past, it is shown without year
  514. * other dates in the past or future are shown with year but without time
  515. * This does not check for years before 1900 ... I don't know, how
  516. * to represent them at all
  517. */
  518. if (!got_year && local_time->tm_mon < 6
  519. && local_time->tm_mon < tim.tm_mon
  520. && tim.tm_mon - local_time->tm_mon >= 6)
  521. tim.tm_year--;
  522. if (l10n || (*t = mktime (&tim)) < 0)
  523. *t = 0;
  524. return idx;
  525. }
  526. int
  527. vfs_parse_ls_lga (const char *p, struct stat *s, char **filename,
  528. char **linkname)
  529. {
  530. int idx, idx2, num_cols;
  531. int i;
  532. char *p_copy = NULL;
  533. char *t = NULL;
  534. const char *line = p;
  535. size_t skipped;
  536. if (strncmp (p, "total", 5) == 0)
  537. return 0;
  538. if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
  539. goto error;
  540. p += skipped;
  541. if (*p == ' ') /* Notwell 4 */
  542. p++;
  543. if (*p == '[') {
  544. if (strlen (p) <= 8 || p[8] != ']')
  545. goto error;
  546. /* Should parse here the Notwell permissions :) */
  547. if (S_ISDIR (s->st_mode))
  548. s->st_mode |=
  549. (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP
  550. | S_IXOTH);
  551. else
  552. s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  553. p += 9;
  554. } else {
  555. size_t lc_skipped;
  556. mode_t perms;
  557. if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
  558. goto error;
  559. p += lc_skipped;
  560. s->st_mode |= perms;
  561. }
  562. p_copy = g_strdup (p);
  563. num_cols = vfs_split_text (p_copy);
  564. s->st_nlink = atol (columns[0]);
  565. if (s->st_nlink <= 0)
  566. goto error;
  567. if (!is_num (1))
  568. s->st_uid = vfs_finduid (columns[1]);
  569. else
  570. s->st_uid = (uid_t) atol (columns[1]);
  571. /* Mhm, the ls -lg did not produce a group field */
  572. for (idx = 3; idx <= 5; idx++)
  573. if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
  574. || is_dos_date (columns[idx])
  575. || is_localized_month (columns[idx]))
  576. break;
  577. if (idx == 6
  578. || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
  579. goto error;
  580. /* We don't have gid */
  581. if (idx == 3
  582. || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
  583. idx2 = 2;
  584. else {
  585. /* We have gid field */
  586. if (is_num (2))
  587. s->st_gid = (gid_t) atol (columns[2]);
  588. else
  589. s->st_gid = vfs_findgid (columns[2]);
  590. idx2 = 3;
  591. }
  592. /* This is device */
  593. if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) {
  594. int maj, min;
  595. /* Corner case: there is no whitespace(s) between maj & min */
  596. if (!is_num (idx2) && idx2 == 2) {
  597. if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &min, &min) != 2)
  598. goto error;
  599. } else {
  600. if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
  601. goto error;
  602. if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
  603. goto error;
  604. }
  605. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  606. s->st_rdev = makedev (maj, min);
  607. #endif
  608. s->st_size = 0;
  609. } else {
  610. /* Common file size */
  611. if (!is_num (idx2))
  612. goto error;
  613. #ifdef HAVE_ATOLL
  614. s->st_size = (off_t) atoll (columns[idx2]);
  615. #else
  616. s->st_size = (off_t) atof (columns[idx2]);
  617. #endif
  618. #ifdef HAVE_STRUCT_STAT_ST_RDEV
  619. s->st_rdev = 0;
  620. #endif
  621. }
  622. idx = vfs_parse_filedate (idx, &s->st_mtime);
  623. if (!idx)
  624. goto error;
  625. /* Use resulting time value */
  626. s->st_atime = s->st_ctime = s->st_mtime;
  627. /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
  628. #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
  629. s->st_blksize = 512;
  630. #endif
  631. #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
  632. s->st_blocks = (s->st_size + 511) / 512;
  633. #endif
  634. for (i = idx + 1, idx2 = 0; i < num_cols; i++)
  635. if (strcmp (columns[i], "->") == 0) {
  636. idx2 = i;
  637. break;
  638. }
  639. if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
  640. &&idx2) {
  641. if (filename) {
  642. *filename =
  643. g_strndup (p + column_ptr[idx],
  644. column_ptr[idx2] - column_ptr[idx] - 1);
  645. }
  646. if (linkname) {
  647. t = g_strdup (p + column_ptr[idx2 + 1]);
  648. *linkname = t;
  649. }
  650. } else {
  651. /* Extract the filename from the string copy, not from the columns
  652. * this way we have a chance of entering hidden directories like ". ."
  653. */
  654. if (filename) {
  655. /*
  656. * filename = g_strdup (columns [idx++]);
  657. */
  658. t = g_strdup (p + column_ptr[idx]);
  659. *filename = t;
  660. }
  661. if (linkname)
  662. *linkname = NULL;
  663. }
  664. if (t) {
  665. int p2 = strlen (t);
  666. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  667. t[p2] = 0;
  668. if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
  669. t[p2] = 0;
  670. }
  671. g_free (p_copy);
  672. return 1;
  673. error:
  674. {
  675. static int errorcount = 0;
  676. if (++errorcount < 5) {
  677. message (D_ERROR, _("Cannot parse:"), "%s",
  678. (p_copy && *p_copy) ? p_copy : line);
  679. } else if (errorcount == 5)
  680. message (D_ERROR, MSG_ERROR,
  681. _("More parsing errors will be ignored."));
  682. }
  683. g_free (p_copy);
  684. return 0;
  685. }
  686. void
  687. vfs_die (const char *m)
  688. {
  689. message (D_ERROR, _("Internal error:"), "%s", m);
  690. exit (1);
  691. }
  692. char *
  693. vfs_get_password (const char *msg)
  694. {
  695. return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD);
  696. }