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. */
23 * \brief Source: Utilities for VFS modules
24 * \author Miguel de Icaza
36 #include "../src/global.h"
37 #include "../src/tty.h" /* enable/disable interrupt key */
38 #include "../src/wtools.h" /* message() */
39 #include "../src/main.h" /* print_vfs_message */
42 #include "../src/unixcompat.h"
43 #include "../src/history.h"
45 /* Extract the hostname and username from the path */
46 /* path is in the form: [user@]hostname:port/remote-dir, e.g.:
48 * ftp://sunsite.unc.edu/pub/linux
49 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
50 * ftp://tsx-11.mit.edu:8192/
51 * ftp://joe@foo.edu:11321/private
52 * ftp://joe:password@foo.se
54 * Returns g_malloc()ed host, user and pass they are present.
55 * If the user is empty, e.g. ftp://@roxanne/private, and URL_ALLOW_ANON
56 * is not set, then the current login name is supplied.
58 * Return value is a g_malloc()ed string with the pathname relative to the
64 vfs_split_url (const char *path
, char **host
, char **user
, int *port
,
65 char **pass
, int default_port
, int flags
)
67 struct passwd
*passwd_info
;
68 char *dir
, *colon
, *inner_colon
, *at
, *rest
;
70 char * const pcopy
= g_strdup (path
);
71 const char *pend
= pcopy
+ strlen (pcopy
);
80 if (!(flags
& URL_NOSLASH
)) {
81 /* locate path component */
82 while (*dir
!= PATH_SEP
&& *dir
)
85 retval
= g_strdup (dir
);
88 retval
= g_strdup (PATH_SEP_STR
);
91 /* search for any possible user */
92 at
= strrchr (pcopy
, '@');
94 /* We have a username */
97 inner_colon
= strchr (pcopy
, ':');
102 *pass
= g_strdup (inner_colon
);
105 *user
= g_strdup (pcopy
);
114 if (!*user
&& !(flags
& URL_ALLOW_ANON
)) {
115 passwd_info
= getpwuid (geteuid ());
116 if (passwd_info
&& passwd_info
->pw_name
)
117 *user
= g_strdup (passwd_info
->pw_name
);
119 /* This is very unlikely to happen */
120 *user
= g_strdup ("anonymous");
125 /* Check if the host comes with a port spec, if so, chop it */
126 colon
= strchr (rest
, ':');
129 if (sscanf (colon
+ 1, "%d", port
) == 1) {
130 if (*port
<= 0 || *port
>= 65536)
131 *port
= default_port
;
146 *host
= g_strdup (rest
);
151 #endif /* USE_NETCODE */
154 * Look up a user or group name from a uid/gid, maintaining a cache.
155 * FIXME, for now it's a one-entry cache.
156 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
157 * This file should be modified for non-unix systems to do something
168 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
169 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
172 vfs_finduid (const char *uname
)
174 static int saveuid
= -993;
175 static char saveuname
[TUNMLEN
];
176 static int my_uid
= -993;
180 if (uname
[0] != saveuname
[0] /* Quick test w/o proc call */
181 ||0 != strncmp (uname
, saveuname
, TUNMLEN
)) {
182 g_strlcpy (saveuname
, uname
, TUNMLEN
);
183 pw
= getpwnam (uname
);
185 saveuid
= pw
->pw_uid
;
194 vfs_findgid (const char *gname
)
196 static int savegid
= -993;
197 static char savegname
[TGNMLEN
];
198 static int my_gid
= -993;
202 if (gname
[0] != savegname
[0] /* Quick test w/o proc call */
203 ||0 != strncmp (gname
, savegname
, TUNMLEN
)) {
204 g_strlcpy (savegname
, gname
, TUNMLEN
);
205 gr
= getgrnam (gname
);
207 savegid
= gr
->gr_gid
;
216 * Create a temporary file with a name resembling the original.
217 * This is needed e.g. for local copies requested by extfs.
218 * Some extfs scripts may look at the extension.
219 * We also protect stupid scripts agains dangerous names.
222 vfs_mkstemps (char **pname
, const char *prefix
, const char *basename
)
229 /* Strip directories */
230 p
= strrchr (basename
, PATH_SEP
);
236 /* Protection against very long names */
237 shift
= strlen (p
) - (MC_MAXPATHLEN
- 16);
241 suffix
= g_malloc (MC_MAXPATHLEN
);
243 /* Protection against unusual characters */
245 while (*p
&& (*p
!= '#')) {
246 if (strchr (".-_@", *p
) || isalnum ((unsigned char) *p
))
252 fd
= mc_mkstemps (pname
, prefix
, suffix
);
257 /* Parsing code is used by ftpfs, fish and extfs */
260 static char *columns
[MAXCOLS
]; /* Points to the string in column n */
261 static int column_ptr
[MAXCOLS
]; /* Index from 0 to the starting positions of the columns */
264 vfs_split_text (char *p
)
269 memset (columns
, 0, sizeof (columns
));
271 for (numcols
= 0; *p
&& numcols
< MAXCOLS
; numcols
++) {
272 while (*p
== ' ' || *p
== '\r' || *p
== '\n') {
276 columns
[numcols
] = p
;
277 column_ptr
[numcols
] = p
- original
;
278 while (*p
&& *p
!= ' ' && *p
!= '\r' && *p
!= '\n')
287 char *column
= columns
[idx
];
289 if (!column
|| column
[0] < '0' || column
[0] > '9')
295 /* Return 1 for MM-DD-YY and MM-DD-YYYY */
297 is_dos_date (const char *str
)
305 if (len
!= 8 && len
!= 10)
308 if (str
[2] != str
[5])
311 if (!strchr ("\\-/", (int) str
[2]))
318 is_week (const char *str
, struct tm
*tim
)
320 static const char *week
= "SunMonTueWedThuFriSat";
326 if ((pos
= strstr (week
, str
)) != NULL
) {
328 tim
->tm_wday
= (pos
- week
) / 3;
335 is_month (const char *str
, struct tm
*tim
)
337 static const char *month
= "JanFebMarAprMayJunJulAugSepOctNovDec";
343 if ((pos
= strstr (month
, str
)) != NULL
) {
345 tim
->tm_mon
= (pos
- month
) / 3;
352 * Check for possible locale's abbreviated month name (Jan..Dec).
353 * Any 3 bytes long string without digit, control and punctuation characters.
354 * isalpha() is locale specific, so it cannot be used if current
355 * locale is "C" and ftp server use Cyrillic.
356 * NB: It is assumed there are no whitespaces in month.
359 is_localized_month (const char *month
)
366 while ((i
< 3) && *month
&& !isdigit ((unsigned char) *month
)
367 && !iscntrl ((unsigned char) *month
)
368 && !ispunct ((unsigned char) *month
)) {
372 return ((i
== 3) && (*month
== 0));
376 is_time (const char *str
, struct tm
*tim
)
383 if ((p
= strchr (str
, ':')) && (p2
= strrchr (str
, ':'))) {
386 (str
, "%2d:%2d:%2d", &tim
->tm_hour
, &tim
->tm_min
,
390 if (sscanf (str
, "%2d:%2d", &tim
->tm_hour
, &tim
->tm_min
) != 2)
400 is_year (char *str
, struct tm
*tim
)
407 if (strchr (str
, ':'))
410 if (strlen (str
) != 4)
413 if (sscanf (str
, "%ld", &year
) != 1)
416 if (year
< 1900 || year
> 3000)
419 tim
->tm_year
= (int) (year
- 1900);
425 vfs_parse_filetype (const char *s
, size_t *ret_skipped
, mode_t
*ret_type
)
430 case 'd': type
= S_IFDIR
; break;
431 case 'b': type
= S_IFBLK
; break;
432 case 'c': type
= S_IFCHR
; break;
433 case 'l': type
= S_IFLNK
; break;
435 case 's': type
= S_IFSOCK
; break;
437 case 's': type
= S_IFIFO
; break;
439 #ifdef S_IFDOOR /* Solaris door */
440 case 'D': type
= S_IFDOOR
; break;
442 case 'D': type
= S_IFIFO
; break;
444 case 'p': type
= S_IFIFO
; break;
445 #ifdef S_IFNAM /* Special named files */
446 case 'n': type
= S_IFNAM
; break;
448 case 'n': type
= S_IFREG
; break;
450 case 'm': /* Don't know what these are :-) */
452 case '?': type
= S_IFREG
; break;
453 default: return FALSE
;
462 vfs_parse_fileperms (const char *s
, size_t *ret_skipped
, mode_t
*ret_perms
)
472 case 'r': perms
|= S_IRUSR
; break;
473 default: return FALSE
;
477 case 'w': perms
|= S_IWUSR
; break;
478 default: return FALSE
;
482 case 'S': perms
|= S_ISUID
; break;
483 case 's': perms
|= S_IXUSR
| S_ISUID
; break;
484 case 'x': perms
|= S_IXUSR
; break;
485 default: return FALSE
;
489 case 'r': perms
|= S_IRGRP
; break;
490 default: return FALSE
;
494 case 'w': perms
|= S_IWGRP
; break;
495 default: return FALSE
;
499 case 'S': perms
|= S_ISGID
; break;
500 case 'l': perms
|= S_ISGID
; break; /* found on Solaris */
501 case 's': perms
|= S_IXGRP
| S_ISGID
; break;
502 case 'x': perms
|= S_IXGRP
; break;
503 default: return FALSE
;
507 case 'r': perms
|= S_IROTH
; break;
508 default: return FALSE
;
512 case 'w': perms
|= S_IWOTH
; break;
513 default: return FALSE
;
517 case 'T': perms
|= S_ISVTX
; break;
518 case 't': perms
|= S_IXOTH
| S_ISVTX
; break;
519 case 'x': perms
|= S_IXOTH
; break;
520 default: return FALSE
;
522 if (*p
== '+') { /* ACLs on Solaris, HP-UX and others */
526 *ret_skipped
= p
- s
;
532 vfs_parse_filemode (const char *s
, size_t *ret_skipped
,
541 if (!vfs_parse_filetype (p
, &skipped
, &type
))
545 if (!vfs_parse_fileperms (p
, &skipped
, &perms
))
549 *ret_skipped
= p
- s
;
550 *ret_mode
= type
| perms
;
555 vfs_parse_raw_filemode (const char *s
, size_t *ret_skipped
,
559 mode_t remote_type
= 0, local_type
, perms
= 0;
564 while(*p
>= '0' && *p
<= '7')
574 while(*p
>= '0' && *p
<= '7')
577 remote_type
+= (*p
- '0');
585 $ perl -e 'use Fcntl ":mode";
586 my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
587 foreach $t (@modes) { printf ("%o\n", $t); };'
588 TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
589 (see vfs_parse_filetype)
595 local_type
= S_IFCHR
; break;
597 local_type
= S_IFDIR
; break;
599 local_type
= S_IFBLK
; break;
601 local_type
= S_IFLNK
; break;
603 default: /* don't know what is it */
604 local_type
= S_IFREG
; break;
607 *ret_skipped
= p
- s
;
608 *ret_mode
= local_type
| perms
;
612 /* This function parses from idx in the columns[] array */
614 vfs_parse_filedate (int idx
, time_t *t
)
620 int l10n
= 0; /* Locale's abbreviated month name */
622 struct tm
*local_time
;
624 /* Let's setup default time values */
625 current_time
= time (NULL
);
626 local_time
= localtime (¤t_time
);
627 tim
.tm_mday
= local_time
->tm_mday
;
628 tim
.tm_mon
= local_time
->tm_mon
;
629 tim
.tm_year
= local_time
->tm_year
;
634 tim
.tm_isdst
= -1; /* Let mktime() try to guess correct dst offset */
638 /* We eat weekday name in case of extfs */
639 if (is_week (p
, &tim
))
643 if (is_month (p
, &tim
)) {
644 /* And we expect, it followed by day number */
646 tim
.tm_mday
= (int) atol (columns
[idx
++]);
648 return 0; /* No day */
652 3 fields max or we'll see oddities with certain file names.
653 So both year and time is not allowed.
656 But in case of extfs we allow these date formats:
658 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
659 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
661 /* Special case with MM-DD-YY or MM-DD-YYYY */
662 if (is_dos_date (p
)) {
665 if (sscanf (p
, "%2d-%2d-%d", &d
[0], &d
[1], &d
[2]) == 3) {
666 /* Months are zero based */
683 return 0; /* sscanf failed */
685 /* Locale's abbreviated month name followed by day number */
686 if (is_localized_month (p
) && (is_num (idx
++)))
689 return 0; /* unsupported format */
693 /* Here we expect to find time or year */
694 if (is_num (idx
) && (is_time (columns
[idx
], &tim
)
695 || (got_year
= is_year (columns
[idx
], &tim
))))
698 return 0; /* Neither time nor date */
701 * If the date is less than 6 months in the past, it is shown without year
702 * other dates in the past or future are shown with year but without time
703 * This does not check for years before 1900 ... I don't know, how
704 * to represent them at all
706 if (!got_year
&& local_time
->tm_mon
< 6
707 && local_time
->tm_mon
< tim
.tm_mon
708 && tim
.tm_mon
- local_time
->tm_mon
>= 6)
712 if (l10n
|| (*t
= mktime (&tim
)) < 0)
718 vfs_parse_ls_lga (const char *p
, struct stat
*s
, char **filename
,
721 int idx
, idx2
, num_cols
;
725 const char *line
= p
;
728 if (strncmp (p
, "total", 5) == 0)
731 if (!vfs_parse_filetype (p
, &skipped
, &s
->st_mode
))
735 if (*p
== ' ') /* Notwell 4 */
738 if (strlen (p
) <= 8 || p
[8] != ']')
740 /* Should parse here the Notwell permissions :) */
741 if (S_ISDIR (s
->st_mode
))
743 (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IXUSR
| S_IXGRP
746 s
->st_mode
|= (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
);
752 if (!vfs_parse_fileperms (p
, &skipped
, &perms
))
758 p_copy
= g_strdup (p
);
759 num_cols
= vfs_split_text (p_copy
);
761 s
->st_nlink
= atol (columns
[0]);
762 if (s
->st_nlink
<= 0)
766 s
->st_uid
= vfs_finduid (columns
[1]);
768 s
->st_uid
= (uid_t
) atol (columns
[1]);
770 /* Mhm, the ls -lg did not produce a group field */
771 for (idx
= 3; idx
<= 5; idx
++)
772 if (is_month (columns
[idx
], NULL
) || is_week (columns
[idx
], NULL
)
773 || is_dos_date (columns
[idx
])
774 || is_localized_month (columns
[idx
]))
778 || (idx
== 5 && !S_ISCHR (s
->st_mode
) && !S_ISBLK (s
->st_mode
)))
781 /* We don't have gid */
783 || (idx
== 4 && (S_ISCHR (s
->st_mode
) || S_ISBLK (s
->st_mode
))))
786 /* We have gid field */
788 s
->st_gid
= (gid_t
) atol (columns
[2]);
790 s
->st_gid
= vfs_findgid (columns
[2]);
795 if (S_ISCHR (s
->st_mode
) || S_ISBLK (s
->st_mode
)) {
798 /* Corner case: there is no whitespace(s) between maj & min */
799 if (!is_num (idx2
) && idx2
== 2) {
800 if (!is_num (++idx2
) || sscanf (columns
[idx2
], " %d,%d", &min
, &min
) != 2)
803 if (!is_num (idx2
) || sscanf (columns
[idx2
], " %d,", &maj
) != 1)
806 if (!is_num (++idx2
) || sscanf (columns
[idx2
], " %d", &min
) != 1)
809 #ifdef HAVE_STRUCT_STAT_ST_RDEV
810 s
->st_rdev
= makedev (maj
, min
);
815 /* Common file size */
820 s
->st_size
= (off_t
) atoll (columns
[idx2
]);
822 s
->st_size
= (off_t
) atof (columns
[idx2
]);
824 #ifdef HAVE_STRUCT_STAT_ST_RDEV
829 idx
= vfs_parse_filedate (idx
, &s
->st_mtime
);
832 /* Use resulting time value */
833 s
->st_atime
= s
->st_ctime
= s
->st_mtime
;
834 /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
835 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
838 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
839 s
->st_blocks
= (s
->st_size
+ 511) / 512;
842 for (i
= idx
+ 1, idx2
= 0; i
< num_cols
; i
++)
843 if (strcmp (columns
[i
], "->") == 0) {
848 if (((S_ISLNK (s
->st_mode
) || (num_cols
== idx
+ 3 && s
->st_nlink
> 1))) /* Maybe a hardlink? (in extfs) */
853 g_strndup (p
+ column_ptr
[idx
],
854 column_ptr
[idx2
] - column_ptr
[idx
] - 1);
857 t
= g_strdup (p
+ column_ptr
[idx2
+ 1]);
861 /* Extract the filename from the string copy, not from the columns
862 * this way we have a chance of entering hidden directories like ". ."
866 * filename = g_strdup (columns [idx++]);
869 t
= g_strdup (p
+ column_ptr
[idx
]);
878 if ((--p
> 0) && (t
[p
] == '\r' || t
[p
] == '\n'))
880 if ((--p
> 0) && (t
[p
] == '\r' || t
[p
] == '\n'))
889 static int errorcount
= 0;
891 if (++errorcount
< 5) {
892 message (D_ERROR
, _("Cannot parse:"), "%s",
893 (p_copy
&& *p_copy
) ? p_copy
: line
);
894 } else if (errorcount
== 5)
895 message (D_ERROR
, MSG_ERROR
,
896 _("More parsing errors will be ignored."));
904 vfs_die (const char *m
)
906 message (D_ERROR
, _("Internal error:"), "%s", m
);
911 vfs_get_password (const char *msg
)
913 return input_dialog (msg
, _("Password:"), MC_HISTORY_VFS_PASSWORD
, INPUT_PASSWORD
);