utilvfs.c 20 KB

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