Merge branch '2595_vfs_encode'
[midnight-commander.git] / lib / vfs / utilvfs.c
blobb7bfb2309ee2b76b78732b8c50924e8542dc78ac
1 /* Utilities for VFS modules.
3 Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
5 Copyright (C) 1995, 1996 Miguel de Icaza
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License
9 as published by the Free Software Foundation; either version 2 of
10 the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /**
22 * \file
23 * \brief Source: Utilities for VFS modules
24 * \author Miguel de Icaza
25 * \date 1995, 1996
28 #include <config.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
38 #include "lib/global.h"
39 #include "lib/unixcompat.h"
40 #include "lib/util.h" /* mc_mkstemps() */
41 #include "lib/widget.h" /* message() */
43 #include "vfs.h"
44 #include "utilvfs.h"
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 #ifndef TUNMLEN
51 #define TUNMLEN 256
52 #endif
53 #ifndef TGNMLEN
54 #define TGNMLEN 256
55 #endif
57 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
58 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
60 /* Parsing code is used by ftpfs, fish and extfs */
61 #define MAXCOLS 30
63 #define MC_HISTORY_VFS_PASSWORD "mc.vfs.password"
65 /*** file scope type declarations ****************************************************************/
67 /*** file scope variables ************************************************************************/
69 static char *columns[MAXCOLS]; /* Points to the string in column n */
70 static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */
72 /*** file scope functions ************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
75 static int
76 is_num (int idx)
78 char *column = columns[idx];
80 if (!column || column[0] < '0' || column[0] > '9')
81 return 0;
83 return 1;
86 /* --------------------------------------------------------------------------------------------- */
87 /* Return 1 for MM-DD-YY and MM-DD-YYYY */
89 static int
90 is_dos_date (const char *str)
92 int len;
94 if (!str)
95 return 0;
97 len = strlen (str);
98 if (len != 8 && len != 10)
99 return 0;
101 if (str[2] != str[5])
102 return 0;
104 if (!strchr ("\\-/", (int) str[2]))
105 return 0;
107 return 1;
110 /* --------------------------------------------------------------------------------------------- */
112 static int
113 is_week (const char *str, struct tm *tim)
115 static const char *week = "SunMonTueWedThuFriSat";
116 const char *pos;
118 if (!str)
119 return 0;
121 pos = strstr (week, str);
122 if (pos != NULL)
124 if (tim != NULL)
125 tim->tm_wday = (pos - week) / 3;
126 return 1;
128 return 0;
131 /* --------------------------------------------------------------------------------------------- */
133 static int
134 is_month (const char *str, struct tm *tim)
136 static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
137 const char *pos;
139 if (!str)
140 return 0;
142 pos = strstr (month, str);
143 if (pos != NULL)
145 if (tim != NULL)
146 tim->tm_mon = (pos - month) / 3;
147 return 1;
149 return 0;
152 /* --------------------------------------------------------------------------------------------- */
154 * Check for possible locale's abbreviated month name (Jan..Dec).
155 * Any 3 bytes long string without digit, control and punctuation characters.
156 * isalpha() is locale specific, so it cannot be used if current
157 * locale is "C" and ftp server use Cyrillic.
158 * NB: It is assumed there are no whitespaces in month.
160 static int
161 is_localized_month (const char *month)
163 int i = 0;
165 if (!month)
166 return 0;
168 while ((i < 3) && *month && !isdigit ((unsigned char) *month)
169 && !iscntrl ((unsigned char) *month) && !ispunct ((unsigned char) *month))
171 i++;
172 month++;
174 return ((i == 3) && (*month == 0));
177 /* --------------------------------------------------------------------------------------------- */
179 static int
180 is_time (const char *str, struct tm *tim)
182 const char *p, *p2;
184 if (str == NULL)
185 return 0;
187 p = strchr (str, ':');
188 p2 = strrchr (str, ':');
189 if (p != NULL && p2 != NULL)
191 if (p != p2)
193 if (sscanf (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min, &tim->tm_sec) != 3)
194 return 0;
196 else
198 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
199 return 0;
202 else
203 return 0;
205 return 1;
208 /* --------------------------------------------------------------------------------------------- */
210 static int
211 is_year (char *str, struct tm *tim)
213 long year;
215 if (!str)
216 return 0;
218 if (strchr (str, ':'))
219 return 0;
221 if (strlen (str) != 4)
222 return 0;
224 if (sscanf (str, "%ld", &year) != 1)
225 return 0;
227 if (year < 1900 || year > 3000)
228 return 0;
230 tim->tm_year = (int) (year - 1900);
232 return 1;
235 /* --------------------------------------------------------------------------------------------- */
236 /*** public functions ****************************************************************************/
237 /* --------------------------------------------------------------------------------------------- */
238 /** Get current username
240 * @return g_malloc()ed string with the name of the currently logged in
241 * user ("anonymous" if uid is not registered in the system)
244 char *
245 vfs_get_local_username (void)
247 struct passwd *p_i;
249 p_i = getpwuid (geteuid ());
251 return (p_i && p_i->pw_name) ? g_strdup (p_i->pw_name) : g_strdup ("anonymous"); /* Unknown UID, strange */
254 /* --------------------------------------------------------------------------------------------- */
256 * Look up a user or group name from a uid/gid, maintaining a cache.
257 * FIXME, for now it's a one-entry cache.
258 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
259 * This file should be modified for non-unix systems to do something
260 * reasonable.
263 /* --------------------------------------------------------------------------------------------- */
266 vfs_finduid (const char *uname)
268 static int saveuid = -993;
269 static char saveuname[TUNMLEN];
270 static int my_uid = -993;
272 struct passwd *pw;
274 if (uname[0] != saveuname[0] /* Quick test w/o proc call */
275 || 0 != strncmp (uname, saveuname, TUNMLEN))
277 g_strlcpy (saveuname, uname, TUNMLEN);
278 pw = getpwnam (uname);
279 if (pw)
281 saveuid = pw->pw_uid;
283 else
285 saveuid = myuid;
288 return saveuid;
291 /* --------------------------------------------------------------------------------------------- */
294 vfs_findgid (const char *gname)
296 static int savegid = -993;
297 static char savegname[TGNMLEN];
298 static int my_gid = -993;
300 struct group *gr;
302 if (gname[0] != savegname[0] /* Quick test w/o proc call */
303 || 0 != strncmp (gname, savegname, TUNMLEN))
305 g_strlcpy (savegname, gname, TUNMLEN);
306 gr = getgrnam (gname);
307 if (gr)
309 savegid = gr->gr_gid;
311 else
313 savegid = mygid;
316 return savegid;
319 /* --------------------------------------------------------------------------------------------- */
321 * Create a temporary file with a name resembling the original.
322 * This is needed e.g. for local copies requested by extfs.
323 * Some extfs scripts may look at the extension.
324 * We also protect stupid scripts agains dangerous names.
328 vfs_mkstemps (char **pname, const char *prefix, const char *param_basename)
330 const char *p;
331 char *suffix, *q;
332 int shift;
333 int fd;
335 /* Strip directories */
336 p = strrchr (param_basename, PATH_SEP);
337 if (!p)
338 p = param_basename;
339 else
340 p++;
342 /* Protection against very long names */
343 shift = strlen (p) - (MC_MAXPATHLEN - 16);
344 if (shift > 0)
345 p += shift;
347 suffix = g_malloc (MC_MAXPATHLEN);
349 /* Protection against unusual characters */
350 q = suffix;
351 while (*p && (*p != '#'))
353 if (strchr (".-_@", *p) || isalnum ((unsigned char) *p))
354 *q++ = *p;
355 p++;
357 *q = 0;
359 fd = mc_mkstemps (pname, prefix, suffix);
360 g_free (suffix);
361 return fd;
364 /* --------------------------------------------------------------------------------------------- */
365 /** Extract the hostname and username from the path
367 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
369 * ftp://sunsite.unc.edu/pub/linux
370 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
371 * ftp://tsx-11.mit.edu:8192/
372 * ftp://joe@foo.edu:11321/private
373 * ftp://joe:password@foo.se
375 * @param path is an input string to be parsed
376 * @param default_port is an input default port
377 * @param flags are parsing modifier flags (@see vfs_url_flags_t)
379 * @return g_malloc()ed url info.
380 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
381 * is not set, then the current login name is supplied.
382 * Return value is a g_malloc()ed structure with the pathname relative to the
383 * host.
386 vfs_path_element_t *
387 vfs_url_split (const char *path, int default_port, vfs_url_flags_t flags)
389 vfs_path_element_t *path_element;
391 char *pcopy;
392 const char *pend;
393 char *dir, *colon, *inner_colon, *at, *rest;
395 path_element = g_new0 (vfs_path_element_t, 1);
396 path_element->port = default_port;
398 pcopy = g_strdup (path);
399 pend = pcopy + strlen (pcopy);
400 dir = pcopy;
402 if ((flags & URL_NOSLASH) == 0)
404 /* locate path component */
405 while (*dir != PATH_SEP && *dir != '\0')
406 dir++;
407 if (*dir == '\0')
408 path_element->path = g_strdup (PATH_SEP_STR);
409 else
411 path_element->path = g_strdup (dir);
412 *dir = '\0';
416 /* search for any possible user */
417 at = strrchr (pcopy, '@');
419 /* We have a username */
420 if (at == NULL)
421 rest = pcopy;
422 else
424 *at = '\0';
425 inner_colon = strchr (pcopy, ':');
426 if (inner_colon != NULL)
428 *inner_colon = '\0';
429 inner_colon++;
430 path_element->password = g_strdup (inner_colon);
433 if (*pcopy != '\0')
434 path_element->user = g_strdup (pcopy);
436 if (pend == at + 1)
437 rest = at;
438 else
439 rest = at + 1;
442 if ((flags & URL_USE_ANONYMOUS) == 0)
443 path_element->user = vfs_get_local_username ();
445 /* Check if the host comes with a port spec, if so, chop it */
446 if (*rest != '[')
447 colon = strchr (rest, ':');
448 else
450 colon = strchr (++rest, ']');
451 if (colon != NULL)
453 colon[0] = '\0';
454 colon[1] = '\0';
455 colon++;
457 else
459 vfs_path_element_free (path_element);
460 return NULL;
464 if (colon != NULL)
466 *colon = '\0';
467 if (sscanf (colon + 1, "%d", &path_element->port) == 1)
469 if (path_element->port <= 0 || path_element->port >= 65536)
470 path_element->port = default_port;
472 else
473 while (*(++colon) != '\0')
475 switch (*colon)
477 case 'C':
478 path_element->port = 1;
479 break;
480 case 'r':
481 path_element->port = 2;
482 break;
487 path_element->host = g_strdup (rest);
489 return path_element;
492 /* --------------------------------------------------------------------------------------------- */
495 vfs_split_text (char *p)
497 char *original = p;
498 int numcols;
500 memset (columns, 0, sizeof (columns));
502 for (numcols = 0; *p && numcols < MAXCOLS; numcols++)
504 while (*p == ' ' || *p == '\r' || *p == '\n')
506 *p = 0;
507 p++;
509 columns[numcols] = p;
510 column_ptr[numcols] = p - original;
511 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
512 p++;
514 return numcols;
517 /* --------------------------------------------------------------------------------------------- */
519 gboolean
520 vfs_parse_filetype (const char *s, size_t * ret_skipped, mode_t * ret_type)
522 mode_t type;
524 switch (*s)
526 case 'd':
527 type = S_IFDIR;
528 break;
529 case 'b':
530 type = S_IFBLK;
531 break;
532 case 'c':
533 type = S_IFCHR;
534 break;
535 case 'l':
536 type = S_IFLNK;
537 break;
538 #ifdef S_IFSOCK
539 case 's':
540 type = S_IFSOCK;
541 break;
542 #else
543 case 's':
544 type = S_IFIFO;
545 break;
546 #endif
547 #ifdef S_IFDOOR /* Solaris door */
548 case 'D':
549 type = S_IFDOOR;
550 break;
551 #else
552 case 'D':
553 type = S_IFIFO;
554 break;
555 #endif
556 case 'p':
557 type = S_IFIFO;
558 break;
559 #ifdef S_IFNAM /* Special named files */
560 case 'n':
561 type = S_IFNAM;
562 break;
563 #else
564 case 'n':
565 type = S_IFREG;
566 break;
567 #endif
568 case 'm': /* Don't know what these are :-) */
569 case '-':
570 case '?':
571 type = S_IFREG;
572 break;
573 default:
574 return FALSE;
577 *ret_type = type;
578 *ret_skipped = 1;
579 return TRUE;
582 /* --------------------------------------------------------------------------------------------- */
584 gboolean
585 vfs_parse_fileperms (const char *s, size_t * ret_skipped, mode_t * ret_perms)
587 const char *p;
588 mode_t perms;
590 p = s;
591 perms = 0;
593 switch (*p++)
595 case '-':
596 break;
597 case 'r':
598 perms |= S_IRUSR;
599 break;
600 default:
601 return FALSE;
603 switch (*p++)
605 case '-':
606 break;
607 case 'w':
608 perms |= S_IWUSR;
609 break;
610 default:
611 return FALSE;
613 switch (*p++)
615 case '-':
616 break;
617 case 'S':
618 perms |= S_ISUID;
619 break;
620 case 's':
621 perms |= S_IXUSR | S_ISUID;
622 break;
623 case 'x':
624 perms |= S_IXUSR;
625 break;
626 default:
627 return FALSE;
629 switch (*p++)
631 case '-':
632 break;
633 case 'r':
634 perms |= S_IRGRP;
635 break;
636 default:
637 return FALSE;
639 switch (*p++)
641 case '-':
642 break;
643 case 'w':
644 perms |= S_IWGRP;
645 break;
646 default:
647 return FALSE;
649 switch (*p++)
651 case '-':
652 break;
653 case 'S':
654 perms |= S_ISGID;
655 break;
656 case 'l':
657 perms |= S_ISGID;
658 break; /* found on Solaris */
659 case 's':
660 perms |= S_IXGRP | S_ISGID;
661 break;
662 case 'x':
663 perms |= S_IXGRP;
664 break;
665 default:
666 return FALSE;
668 switch (*p++)
670 case '-':
671 break;
672 case 'r':
673 perms |= S_IROTH;
674 break;
675 default:
676 return FALSE;
678 switch (*p++)
680 case '-':
681 break;
682 case 'w':
683 perms |= S_IWOTH;
684 break;
685 default:
686 return FALSE;
688 switch (*p++)
690 case '-':
691 break;
692 case 'T':
693 perms |= S_ISVTX;
694 break;
695 case 't':
696 perms |= S_IXOTH | S_ISVTX;
697 break;
698 case 'x':
699 perms |= S_IXOTH;
700 break;
701 default:
702 return FALSE;
704 if (*p == '+')
705 { /* ACLs on Solaris, HP-UX and others */
706 p++;
709 *ret_skipped = p - s;
710 *ret_perms = perms;
711 return TRUE;
714 /* --------------------------------------------------------------------------------------------- */
716 gboolean
717 vfs_parse_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode)
719 const char *p;
720 mode_t type, perms;
721 size_t skipped;
723 p = s;
725 if (!vfs_parse_filetype (p, &skipped, &type))
726 return FALSE;
727 p += skipped;
729 if (!vfs_parse_fileperms (p, &skipped, &perms))
730 return FALSE;
731 p += skipped;
733 *ret_skipped = p - s;
734 *ret_mode = type | perms;
735 return TRUE;
738 /* --------------------------------------------------------------------------------------------- */
740 gboolean
741 vfs_parse_raw_filemode (const char *s, size_t * ret_skipped, mode_t * ret_mode)
743 const char *p;
744 mode_t remote_type = 0, local_type, perms = 0;
746 p = s;
748 /* isoctal */
749 while (*p >= '0' && *p <= '7')
751 perms *= 010;
752 perms += (*p - '0');
753 ++p;
756 if (*p++ != ' ')
757 return FALSE;
759 while (*p >= '0' && *p <= '7')
761 remote_type *= 010;
762 remote_type += (*p - '0');
763 ++p;
766 if (*p++ != ' ')
767 return FALSE;
769 /* generated with:
770 $ perl -e 'use Fcntl ":mode";
771 my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
772 foreach $t (@modes) { printf ("%o\n", $t); };'
773 TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
774 (see vfs_parse_filetype)
777 switch (remote_type)
779 case 020000:
780 local_type = S_IFCHR;
781 break;
782 case 040000:
783 local_type = S_IFDIR;
784 break;
785 case 060000:
786 local_type = S_IFBLK;
787 break;
788 case 0120000:
789 local_type = S_IFLNK;
790 break;
791 case 0100000:
792 default: /* don't know what is it */
793 local_type = S_IFREG;
794 break;
797 *ret_skipped = p - s;
798 *ret_mode = local_type | perms;
799 return TRUE;
802 /* --------------------------------------------------------------------------------------------- */
803 /** This function parses from idx in the columns[] array */
806 vfs_parse_filedate (int idx, time_t * t)
808 char *p;
809 struct tm tim;
810 int d[3];
811 int got_year = 0;
812 int l10n = 0; /* Locale's abbreviated month name */
813 time_t current_time;
814 struct tm *local_time;
816 /* Let's setup default time values */
817 current_time = time (NULL);
818 local_time = localtime (&current_time);
819 tim.tm_mday = local_time->tm_mday;
820 tim.tm_mon = local_time->tm_mon;
821 tim.tm_year = local_time->tm_year;
823 tim.tm_hour = 0;
824 tim.tm_min = 0;
825 tim.tm_sec = 0;
826 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
828 p = columns[idx++];
830 /* We eat weekday name in case of extfs */
831 if (is_week (p, &tim))
832 p = columns[idx++];
834 /* Month name */
835 if (is_month (p, &tim))
837 /* And we expect, it followed by day number */
838 if (is_num (idx))
839 tim.tm_mday = (int) atol (columns[idx++]);
840 else
841 return 0; /* No day */
844 else
846 /* We expect:
847 3 fields max or we'll see oddities with certain file names.
848 So both year and time is not allowed.
849 Mon DD hh:mm[:ss]
850 Mon DD YYYY
851 But in case of extfs we allow these date formats:
852 MM-DD-YY hh:mm[:ss]
853 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
854 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
856 /* Special case with MM-DD-YY or MM-DD-YYYY */
857 if (is_dos_date (p))
859 p[2] = p[5] = '-';
861 if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3)
863 /* Months are zero based */
864 if (d[0] > 0)
865 d[0]--;
867 if (d[2] > 1900)
869 d[2] -= 1900;
871 else
873 /* Y2K madness */
874 if (d[2] < 70)
875 d[2] += 100;
878 tim.tm_mon = d[0];
879 tim.tm_mday = d[1];
880 tim.tm_year = d[2];
881 got_year = 1;
883 else
884 return 0; /* sscanf failed */
886 else
888 /* Locale's abbreviated month name followed by day number */
889 if (is_localized_month (p) && (is_num (idx++)))
890 l10n = 1;
891 else
892 return 0; /* unsupported format */
896 /* Here we expect to find time or year */
897 if (is_num (idx) && (is_time (columns[idx], &tim) || (got_year = is_year (columns[idx], &tim))))
898 idx++;
899 else
900 return 0; /* Neither time nor date */
903 * If the date is less than 6 months in the past, it is shown without year
904 * other dates in the past or future are shown with year but without time
905 * This does not check for years before 1900 ... I don't know, how
906 * to represent them at all
908 if (!got_year && local_time->tm_mon < 6
909 && local_time->tm_mon < tim.tm_mon && tim.tm_mon - local_time->tm_mon >= 6)
911 tim.tm_year--;
913 *t = mktime (&tim);
914 if (l10n || (*t < 0))
915 *t = 0;
916 return idx;
919 /* --------------------------------------------------------------------------------------------- */
921 gboolean
922 vfs_parse_ls_lga (const char *p, struct stat * s, char **filename, char **linkname,
923 size_t * filename_pos)
925 int idx, idx2, num_cols;
926 int i;
927 char *p_copy = NULL;
928 char *t = NULL;
929 const char *line = p;
930 size_t skipped;
932 if (strncmp (p, "total", 5) == 0)
933 return FALSE;
935 if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
936 goto error;
937 p += skipped;
939 if (*p == ' ') /* Notwell 4 */
940 p++;
941 if (*p == '[')
943 if (strlen (p) <= 8 || p[8] != ']')
944 goto error;
945 /* Should parse here the Notwell permissions :) */
946 if (S_ISDIR (s->st_mode))
947 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH);
948 else
949 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
950 p += 9;
952 else
954 size_t lc_skipped;
955 mode_t perms;
957 if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
958 goto error;
959 p += lc_skipped;
960 s->st_mode |= perms;
963 p_copy = g_strdup (p);
964 num_cols = vfs_split_text (p_copy);
966 s->st_nlink = atol (columns[0]);
967 if (s->st_nlink <= 0)
968 goto error;
970 if (!is_num (1))
971 s->st_uid = vfs_finduid (columns[1]);
972 else
973 s->st_uid = (uid_t) atol (columns[1]);
975 /* Mhm, the ls -lg did not produce a group field */
976 for (idx = 3; idx <= 5; idx++)
977 if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
978 || is_dos_date (columns[idx]) || is_localized_month (columns[idx]))
979 break;
981 if (idx == 6 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
982 goto error;
984 /* We don't have gid */
985 if (idx == 3 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
986 idx2 = 2;
987 else
989 /* We have gid field */
990 if (is_num (2))
991 s->st_gid = (gid_t) atol (columns[2]);
992 else
993 s->st_gid = vfs_findgid (columns[2]);
994 idx2 = 3;
997 /* This is device */
998 if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))
1000 int maj, min;
1002 /* Corner case: there is no whitespace(s) between maj & min */
1003 if (!is_num (idx2) && idx2 == 2)
1005 if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &maj, &min) != 2)
1006 goto error;
1008 else
1010 if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
1011 goto error;
1013 if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
1014 goto error;
1016 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1017 s->st_rdev = makedev (maj, min);
1018 #endif
1019 s->st_size = 0;
1022 else
1024 /* Common file size */
1025 if (!is_num (idx2))
1026 goto error;
1028 #ifdef HAVE_ATOLL
1029 s->st_size = (off_t) atoll (columns[idx2]);
1030 #else
1031 s->st_size = (off_t) atof (columns[idx2]);
1032 #endif
1033 #ifdef HAVE_STRUCT_STAT_ST_RDEV
1034 s->st_rdev = 0;
1035 #endif
1038 idx = vfs_parse_filedate (idx, &s->st_mtime);
1039 if (!idx)
1040 goto error;
1041 /* Use resulting time value */
1042 s->st_atime = s->st_ctime = s->st_mtime;
1043 /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
1044 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
1045 s->st_blksize = 512;
1046 #endif
1047 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
1048 s->st_blocks = (s->st_size + 511) / 512;
1049 #endif
1051 if (filename_pos != NULL)
1053 if ((*filename_pos == 0) || (*filename_pos > (size_t) column_ptr[idx]))
1054 *filename_pos = column_ptr[idx];
1055 else
1056 column_ptr[idx] = *filename_pos;
1059 for (i = idx + 1, idx2 = 0; i < num_cols; i++)
1060 if (strcmp (columns[i], "->") == 0)
1062 idx2 = i;
1063 break;
1066 if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
1067 && idx2)
1070 if (filename)
1072 *filename = g_strndup (p + column_ptr[idx], column_ptr[idx2] - column_ptr[idx] - 1);
1074 if (linkname)
1076 t = g_strdup (p + column_ptr[idx2 + 1]);
1077 *linkname = t;
1080 else
1082 /* Extract the filename from the string copy, not from the columns
1083 * this way we have a chance of entering hidden directories like ". ."
1085 if (filename)
1088 * filename = g_strdup (columns [idx++]);
1091 t = g_strdup (p + column_ptr[idx]);
1092 *filename = t;
1094 if (linkname)
1095 *linkname = NULL;
1098 if (t)
1100 int p2 = strlen (t);
1101 if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
1102 t[p2] = 0;
1103 if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
1104 t[p2] = 0;
1107 g_free (p_copy);
1108 return TRUE;
1110 error:
1112 static int errorcount = 0;
1114 if (++errorcount < 5)
1116 message (D_ERROR, _("Cannot parse:"), "%s", (p_copy && *p_copy) ? p_copy : line);
1118 else if (errorcount == 5)
1119 message (D_ERROR, MSG_ERROR, _("More parsing errors will be ignored."));
1122 g_free (p_copy);
1123 return FALSE;
1126 /* --------------------------------------------------------------------------------------------- */
1128 void
1129 vfs_die (const char *m)
1131 message (D_ERROR, _("Internal error:"), "%s", m);
1132 exit (EXIT_FAILURE);
1135 /* --------------------------------------------------------------------------------------------- */
1137 char *
1138 vfs_get_password (const char *msg)
1140 return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD);
1143 /* --------------------------------------------------------------------------------------------- */