utilvfs.c 20 KB

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