utilvfs.c 21 KB

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