Merge branch '2121_dir_symlink'
[kaloumi3.git] / lib / vfs / mc-vfs / utilvfs.c
blob1928a45d85f4043ad1d9202e52a061117347c0b0
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>
32 #include <pwd.h>
33 #include <grp.h>
34 #include <stdlib.h>
36 #include "lib/global.h"
37 #include "lib/unixcompat.h"
39 #include "src/wtools.h" /* message() */
40 #include "src/main.h" /* print_vfs_message */
41 #include "src/history.h"
43 #include "vfs.h"
44 #include "utilvfs.h"
46 /** Get current username
48 * @return g_malloc()ed string with the name of the currently logged in
49 * user ("anonymous" if uid is not registered in the system)
51 char *
52 vfs_get_local_username(void)
54 struct passwd * p_i;
56 p_i = getpwuid (geteuid ());
58 return (p_i && p_i->pw_name)
59 ? g_strdup (p_i->pw_name)
60 : g_strdup ("anonymous"); /* Unknown UID, strange */
64 * Look up a user or group name from a uid/gid, maintaining a cache.
65 * FIXME, for now it's a one-entry cache.
66 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
67 * This file should be modified for non-unix systems to do something
68 * reasonable.
71 #ifndef TUNMLEN
72 #define TUNMLEN 256
73 #endif
74 #ifndef TGNMLEN
75 #define TGNMLEN 256
76 #endif
78 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
79 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
81 int
82 vfs_finduid (const char *uname)
84 static int saveuid = -993;
85 static char saveuname[TUNMLEN];
86 static int my_uid = -993;
88 struct passwd *pw;
90 if (uname[0] != saveuname[0] /* Quick test w/o proc call */
91 ||0 != strncmp (uname, saveuname, TUNMLEN)) {
92 g_strlcpy (saveuname, uname, TUNMLEN);
93 pw = getpwnam (uname);
94 if (pw) {
95 saveuid = pw->pw_uid;
96 } else {
97 saveuid = myuid;
100 return saveuid;
104 vfs_findgid (const char *gname)
106 static int savegid = -993;
107 static char savegname[TGNMLEN];
108 static int my_gid = -993;
110 struct group *gr;
112 if (gname[0] != savegname[0] /* Quick test w/o proc call */
113 ||0 != strncmp (gname, savegname, TUNMLEN)) {
114 g_strlcpy (savegname, gname, TUNMLEN);
115 gr = getgrnam (gname);
116 if (gr) {
117 savegid = gr->gr_gid;
118 } else {
119 savegid = mygid;
122 return savegid;
126 * Create a temporary file with a name resembling the original.
127 * This is needed e.g. for local copies requested by extfs.
128 * Some extfs scripts may look at the extension.
129 * We also protect stupid scripts agains dangerous names.
132 vfs_mkstemps (char **pname, const char *prefix, const char *param_basename)
134 const char *p;
135 char *suffix, *q;
136 int shift;
137 int fd;
139 /* Strip directories */
140 p = strrchr (param_basename, PATH_SEP);
141 if (!p)
142 p = param_basename;
143 else
144 p++;
146 /* Protection against very long names */
147 shift = strlen (p) - (MC_MAXPATHLEN - 16);
148 if (shift > 0)
149 p += shift;
151 suffix = g_malloc (MC_MAXPATHLEN);
153 /* Protection against unusual characters */
154 q = suffix;
155 while (*p && (*p != '#')) {
156 if (strchr (".-_@", *p) || isalnum ((unsigned char) *p))
157 *q++ = *p;
158 p++;
160 *q = 0;
162 fd = mc_mkstemps (pname, prefix, suffix);
163 g_free (suffix);
164 return fd;
167 /* Parsing code is used by ftpfs, fish and extfs */
168 #define MAXCOLS 30
170 static char *columns[MAXCOLS]; /* Points to the string in column n */
171 static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */
174 vfs_split_text (char *p)
176 char *original = p;
177 int numcols;
179 memset (columns, 0, sizeof (columns));
181 for (numcols = 0; *p && numcols < MAXCOLS; numcols++) {
182 while (*p == ' ' || *p == '\r' || *p == '\n') {
183 *p = 0;
184 p++;
186 columns[numcols] = p;
187 column_ptr[numcols] = p - original;
188 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
189 p++;
191 return numcols;
194 static int
195 is_num (int idx)
197 char *column = columns[idx];
199 if (!column || column[0] < '0' || column[0] > '9')
200 return 0;
202 return 1;
205 /* Return 1 for MM-DD-YY and MM-DD-YYYY */
206 static int
207 is_dos_date (const char *str)
209 int len;
211 if (!str)
212 return 0;
214 len = strlen (str);
215 if (len != 8 && len != 10)
216 return 0;
218 if (str[2] != str[5])
219 return 0;
221 if (!strchr ("\\-/", (int) str[2]))
222 return 0;
224 return 1;
227 static int
228 is_week (const char *str, struct tm *tim)
230 static const char *week = "SunMonTueWedThuFriSat";
231 const char *pos;
233 if (!str)
234 return 0;
236 if ((pos = strstr (week, str)) != NULL) {
237 if (tim != NULL)
238 tim->tm_wday = (pos - week) / 3;
239 return 1;
241 return 0;
244 static int
245 is_month (const char *str, struct tm *tim)
247 static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
248 const char *pos;
250 if (!str)
251 return 0;
253 if ((pos = strstr (month, str)) != NULL) {
254 if (tim != NULL)
255 tim->tm_mon = (pos - month) / 3;
256 return 1;
258 return 0;
262 * Check for possible locale's abbreviated month name (Jan..Dec).
263 * Any 3 bytes long string without digit, control and punctuation characters.
264 * isalpha() is locale specific, so it cannot be used if current
265 * locale is "C" and ftp server use Cyrillic.
266 * NB: It is assumed there are no whitespaces in month.
268 static int
269 is_localized_month (const char *month)
271 int i = 0;
273 if (!month)
274 return 0;
276 while ((i < 3) && *month && !isdigit ((unsigned char) *month)
277 && !iscntrl ((unsigned char) *month)
278 && !ispunct ((unsigned char) *month)) {
279 i++;
280 month++;
282 return ((i == 3) && (*month == 0));
285 static int
286 is_time (const char *str, struct tm *tim)
288 const char *p, *p2;
290 if (!str)
291 return 0;
293 if ((p = strchr (str, ':')) && (p2 = strrchr (str, ':'))) {
294 if (p != p2) {
295 if (sscanf
296 (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min,
297 &tim->tm_sec) != 3)
298 return 0;
299 } else {
300 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
301 return 0;
303 } else
304 return 0;
306 return 1;
309 static int
310 is_year (char *str, struct tm *tim)
312 long year;
314 if (!str)
315 return 0;
317 if (strchr (str, ':'))
318 return 0;
320 if (strlen (str) != 4)
321 return 0;
323 if (sscanf (str, "%ld", &year) != 1)
324 return 0;
326 if (year < 1900 || year > 3000)
327 return 0;
329 tim->tm_year = (int) (year - 1900);
331 return 1;
334 gboolean
335 vfs_parse_filetype (const char *s, size_t *ret_skipped, mode_t *ret_type)
337 mode_t type;
339 switch (*s) {
340 case 'd': type = S_IFDIR; break;
341 case 'b': type = S_IFBLK; break;
342 case 'c': type = S_IFCHR; break;
343 case 'l': type = S_IFLNK; break;
344 #ifdef S_IFSOCK
345 case 's': type = S_IFSOCK; break;
346 #else
347 case 's': type = S_IFIFO; break;
348 #endif
349 #ifdef S_IFDOOR /* Solaris door */
350 case 'D': type = S_IFDOOR; break;
351 #else
352 case 'D': type = S_IFIFO; break;
353 #endif
354 case 'p': type = S_IFIFO; break;
355 #ifdef S_IFNAM /* Special named files */
356 case 'n': type = S_IFNAM; break;
357 #else
358 case 'n': type = S_IFREG; break;
359 #endif
360 case 'm': /* Don't know what these are :-) */
361 case '-':
362 case '?': type = S_IFREG; break;
363 default: return FALSE;
366 *ret_type = type;
367 *ret_skipped = 1;
368 return TRUE;
371 gboolean
372 vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms)
374 const char *p;
375 mode_t perms;
377 p = s;
378 perms = 0;
380 switch (*p++) {
381 case '-': break;
382 case 'r': perms |= S_IRUSR; break;
383 default: return FALSE;
385 switch (*p++) {
386 case '-': break;
387 case 'w': perms |= S_IWUSR; break;
388 default: return FALSE;
390 switch (*p++) {
391 case '-': break;
392 case 'S': perms |= S_ISUID; break;
393 case 's': perms |= S_IXUSR | S_ISUID; break;
394 case 'x': perms |= S_IXUSR; break;
395 default: return FALSE;
397 switch (*p++) {
398 case '-': break;
399 case 'r': perms |= S_IRGRP; break;
400 default: return FALSE;
402 switch (*p++) {
403 case '-': break;
404 case 'w': perms |= S_IWGRP; break;
405 default: return FALSE;
407 switch (*p++) {
408 case '-': break;
409 case 'S': perms |= S_ISGID; break;
410 case 'l': perms |= S_ISGID; break; /* found on Solaris */
411 case 's': perms |= S_IXGRP | S_ISGID; break;
412 case 'x': perms |= S_IXGRP; break;
413 default: return FALSE;
415 switch (*p++) {
416 case '-': break;
417 case 'r': perms |= S_IROTH; break;
418 default: return FALSE;
420 switch (*p++) {
421 case '-': break;
422 case 'w': perms |= S_IWOTH; break;
423 default: return FALSE;
425 switch (*p++) {
426 case '-': break;
427 case 'T': perms |= S_ISVTX; break;
428 case 't': perms |= S_IXOTH | S_ISVTX; break;
429 case 'x': perms |= S_IXOTH; break;
430 default: return FALSE;
432 if (*p == '+') { /* ACLs on Solaris, HP-UX and others */
433 p++;
436 *ret_skipped = p - s;
437 *ret_perms = perms;
438 return TRUE;
441 gboolean
442 vfs_parse_filemode (const char *s, size_t *ret_skipped,
443 mode_t *ret_mode)
445 const char *p;
446 mode_t type, perms;
447 size_t skipped;
449 p = s;
451 if (!vfs_parse_filetype (p, &skipped, &type))
452 return FALSE;
453 p += skipped;
455 if (!vfs_parse_fileperms (p, &skipped, &perms))
456 return FALSE;
457 p += skipped;
459 *ret_skipped = p - s;
460 *ret_mode = type | perms;
461 return TRUE;
464 gboolean
465 vfs_parse_raw_filemode (const char *s, size_t *ret_skipped,
466 mode_t *ret_mode)
468 const char *p;
469 mode_t remote_type = 0, local_type, perms = 0;
471 p = s;
473 /* isoctal */
474 while(*p >= '0' && *p <= '7')
476 perms *= 010;
477 perms += (*p - '0');
478 ++p;
481 if (*p++ != ' ')
482 return FALSE;
484 while(*p >= '0' && *p <= '7')
486 remote_type *= 010;
487 remote_type += (*p - '0');
488 ++p;
491 if (*p++ != ' ')
492 return FALSE;
494 /* generated with:
495 $ perl -e 'use Fcntl ":mode";
496 my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
497 foreach $t (@modes) { printf ("%o\n", $t); };'
498 TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
499 (see vfs_parse_filetype)
502 switch (remote_type)
504 case 020000:
505 local_type = S_IFCHR; break;
506 case 040000:
507 local_type = S_IFDIR; break;
508 case 060000:
509 local_type = S_IFBLK; break;
510 case 0120000:
511 local_type = S_IFLNK; break;
512 case 0100000:
513 default: /* don't know what is it */
514 local_type = S_IFREG; break;
517 *ret_skipped = p - s;
518 *ret_mode = local_type | perms;
519 return TRUE;
522 /* This function parses from idx in the columns[] array */
524 vfs_parse_filedate (int idx, time_t *t)
526 char *p;
527 struct tm tim;
528 int d[3];
529 int got_year = 0;
530 int l10n = 0; /* Locale's abbreviated month name */
531 time_t current_time;
532 struct tm *local_time;
534 /* Let's setup default time values */
535 current_time = time (NULL);
536 local_time = localtime (&current_time);
537 tim.tm_mday = local_time->tm_mday;
538 tim.tm_mon = local_time->tm_mon;
539 tim.tm_year = local_time->tm_year;
541 tim.tm_hour = 0;
542 tim.tm_min = 0;
543 tim.tm_sec = 0;
544 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
546 p = columns[idx++];
548 /* We eat weekday name in case of extfs */
549 if (is_week (p, &tim))
550 p = columns[idx++];
552 /* Month name */
553 if (is_month (p, &tim)) {
554 /* And we expect, it followed by day number */
555 if (is_num (idx))
556 tim.tm_mday = (int) atol (columns[idx++]);
557 else
558 return 0; /* No day */
560 } else {
561 /* We expect:
562 3 fields max or we'll see oddities with certain file names.
563 So both year and time is not allowed.
564 Mon DD hh:mm[:ss]
565 Mon DD YYYY
566 But in case of extfs we allow these date formats:
567 MM-DD-YY hh:mm[:ss]
568 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
569 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
571 /* Special case with MM-DD-YY or MM-DD-YYYY */
572 if (is_dos_date (p)) {
573 p[2] = p[5] = '-';
575 if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) {
576 /* Months are zero based */
577 if (d[0] > 0)
578 d[0]--;
580 if (d[2] > 1900) {
581 d[2] -= 1900;
582 } else {
583 /* Y2K madness */
584 if (d[2] < 70)
585 d[2] += 100;
588 tim.tm_mon = d[0];
589 tim.tm_mday = d[1];
590 tim.tm_year = d[2];
591 got_year = 1;
592 } else
593 return 0; /* sscanf failed */
594 } else {
595 /* Locale's abbreviated month name followed by day number */
596 if (is_localized_month (p) && (is_num (idx++)))
597 l10n = 1;
598 else
599 return 0; /* unsupported format */
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 */
611 * If the date is less than 6 months in the past, it is shown without year
612 * other dates in the past or future are shown with year but without time
613 * This does not check for years before 1900 ... I don't know, how
614 * to represent them at all
616 if (!got_year && local_time->tm_mon < 6
617 && local_time->tm_mon < tim.tm_mon
618 && tim.tm_mon - local_time->tm_mon >= 6)
620 tim.tm_year--;
622 if (l10n || (*t = mktime (&tim)) < 0)
623 *t = 0;
624 return idx;
628 vfs_parse_ls_lga (const char *p, struct stat *s, char **filename,
629 char **linkname)
631 int idx, idx2, num_cols;
632 int i;
633 char *p_copy = NULL;
634 char *t = NULL;
635 const char *line = p;
636 size_t skipped;
638 if (strncmp (p, "total", 5) == 0)
639 return 0;
641 if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
642 goto error;
643 p += skipped;
645 if (*p == ' ') /* Notwell 4 */
646 p++;
647 if (*p == '[') {
648 if (strlen (p) <= 8 || p[8] != ']')
649 goto error;
650 /* Should parse here the Notwell permissions :) */
651 if (S_ISDIR (s->st_mode))
652 s->st_mode |=
653 (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP
654 | S_IXOTH);
655 else
656 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
657 p += 9;
658 } else {
659 size_t lc_skipped;
660 mode_t perms;
662 if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
663 goto error;
664 p += lc_skipped;
665 s->st_mode |= perms;
668 p_copy = g_strdup (p);
669 num_cols = vfs_split_text (p_copy);
671 s->st_nlink = atol (columns[0]);
672 if (s->st_nlink <= 0)
673 goto error;
675 if (!is_num (1))
676 s->st_uid = vfs_finduid (columns[1]);
677 else
678 s->st_uid = (uid_t) atol (columns[1]);
680 /* Mhm, the ls -lg did not produce a group field */
681 for (idx = 3; idx <= 5; idx++)
682 if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
683 || is_dos_date (columns[idx])
684 || is_localized_month (columns[idx]))
685 break;
687 if (idx == 6
688 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
689 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;
704 /* This is device */
705 if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) {
706 int maj, min;
708 /* Corner case: there is no whitespace(s) between maj & min */
709 if (!is_num (idx2) && idx2 == 2) {
710 if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &min, &min) != 2)
711 goto error;
712 } else {
713 if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
714 goto error;
716 if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
717 goto error;
719 #ifdef HAVE_STRUCT_STAT_ST_RDEV
720 s->st_rdev = makedev (maj, min);
721 #endif
722 s->st_size = 0;
724 } else {
725 /* Common file size */
726 if (!is_num (idx2))
727 goto error;
729 #ifdef HAVE_ATOLL
730 s->st_size = (off_t) atoll (columns[idx2]);
731 #else
732 s->st_size = (off_t) atof (columns[idx2]);
733 #endif
734 #ifdef HAVE_STRUCT_STAT_ST_RDEV
735 s->st_rdev = 0;
736 #endif
739 idx = vfs_parse_filedate (idx, &s->st_mtime);
740 if (!idx)
741 goto error;
742 /* Use resulting time value */
743 s->st_atime = s->st_ctime = s->st_mtime;
744 /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
745 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
746 s->st_blksize = 512;
747 #endif
748 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
749 s->st_blocks = (s->st_size + 511) / 512;
750 #endif
752 for (i = idx + 1, idx2 = 0; i < num_cols; i++)
753 if (strcmp (columns[i], "->") == 0) {
754 idx2 = i;
755 break;
758 if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
759 &&idx2) {
761 if (filename) {
762 *filename =
763 g_strndup (p + column_ptr[idx],
764 column_ptr[idx2] - column_ptr[idx] - 1);
766 if (linkname) {
767 t = g_strdup (p + column_ptr[idx2 + 1]);
768 *linkname = t;
770 } else {
771 /* Extract the filename from the string copy, not from the columns
772 * this way we have a chance of entering hidden directories like ". ."
774 if (filename) {
776 * filename = g_strdup (columns [idx++]);
779 t = g_strdup (p + column_ptr[idx]);
780 *filename = t;
782 if (linkname)
783 *linkname = NULL;
786 if (t) {
787 int p2 = strlen (t);
788 if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
789 t[p2] = 0;
790 if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
791 t[p2] = 0;
794 g_free (p_copy);
795 return 1;
797 error:
799 static int errorcount = 0;
801 if (++errorcount < 5) {
802 message (D_ERROR, _("Cannot parse:"), "%s",
803 (p_copy && *p_copy) ? p_copy : line);
804 } else if (errorcount == 5)
805 message (D_ERROR, MSG_ERROR,
806 _("More parsing errors will be ignored."));
809 g_free (p_copy);
810 return 0;
813 void
814 vfs_die (const char *m)
816 message (D_ERROR, _("Internal error:"), "%s", m);
817 exit (1);
820 char *
821 vfs_get_password (const char *msg)
823 return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD);