Minor optimization of MC viewer.
[kaloumi3.git] / vfs / utilvfs.c
blob79bec13c7aa62c56b67a5a2697892eedc90e58d7
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 "../src/global.h"
38 #include "../src/wtools.h" /* message() */
39 #include "../src/main.h" /* print_vfs_message */
40 #include "../src/unixcompat.h"
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 */
63 #ifdef USE_NETCODE
65 /** Extract the hostname and username from the path
67 * Format of the path is [user@]hostname:port/remote-dir, e.g.:
69 * ftp://sunsite.unc.edu/pub/linux
70 * ftp://miguel@sphinx.nuclecu.unam.mx/c/nc
71 * ftp://tsx-11.mit.edu:8192/
72 * ftp://joe@foo.edu:11321/private
73 * ftp://joe:password@foo.se
75 * @param path is an input string to be parsed
76 * @param host is an outptun g_malloc()ed hostname
77 * @param user is an outptut g_malloc()ed username
78 * (NULL if not specified)
79 * @param port is an outptut integer port number
80 * @param pass is an outptut g_malloc()ed password
81 * @param default_port is an input default port
82 * @param flags are parsing modifier flags (@see VFS_URL_FLAGS)
84 * @return g_malloc()ed host, user and pass if they are present.
85 * If the user is empty, e.g. ftp://@roxanne/private, and URL_USE_ANONYMOUS
86 * is not set, then the current login name is supplied.
87 * Return value is a g_malloc()ed string with the pathname relative to the
88 * host.
90 char *
91 vfs_split_url (const char *path, char **host, char **user, int *port,
92 char **pass, int default_port, enum VFS_URL_FLAGS flags)
94 char *dir, *colon, *inner_colon, *at, *rest;
95 char *retval;
96 char * const pcopy = g_strdup (path);
97 const char *pend = pcopy + strlen (pcopy);
99 if (pass)
100 *pass = NULL;
101 *port = default_port;
102 *user = NULL;
103 retval = NULL;
105 dir = pcopy;
106 if (!(flags & URL_NOSLASH)) {
107 /* locate path component */
108 while (*dir != PATH_SEP && *dir)
109 dir++;
110 if (*dir) {
111 retval = g_strdup (dir);
112 *dir = 0;
113 } else
114 retval = g_strdup (PATH_SEP_STR);
117 /* search for any possible user */
118 at = strrchr (pcopy, '@');
120 /* We have a username */
121 if (at) {
122 *at = 0;
123 inner_colon = strchr (pcopy, ':');
124 if (inner_colon) {
125 *inner_colon = 0;
126 inner_colon++;
127 if (pass)
128 *pass = g_strdup (inner_colon);
130 if (*pcopy != 0)
131 *user = g_strdup (pcopy);
133 if (pend == at + 1)
134 rest = at;
135 else
136 rest = at + 1;
137 } else
138 rest = pcopy;
140 if (!*user && !(flags & URL_USE_ANONYMOUS))
141 *user = vfs_get_local_username();
143 /* Check if the host comes with a port spec, if so, chop it */
144 if ('[' == *rest) {
145 colon = strchr (++rest, ']');
146 if (colon) {
147 colon[0] = '\0';
148 colon[1] = '\0';
149 colon++;
150 } else {
151 g_free (pcopy);
152 *host = NULL;
153 *port = 0;
154 return NULL;
156 } else
157 colon = strchr (rest, ':');
159 if (colon) {
160 *colon = 0;
161 if (sscanf (colon + 1, "%d", port) == 1) {
162 if (*port <= 0 || *port >= 65536)
163 *port = default_port;
164 } else {
165 while (*(++colon)) {
166 switch (*colon) {
167 case 'C':
168 *port = 1;
169 break;
170 case 'r':
171 *port = 2;
172 break;
177 if (host)
178 *host = g_strdup (rest);
180 g_free (pcopy);
181 return retval;
183 #endif /* USE_NETCODE */
186 * Look up a user or group name from a uid/gid, maintaining a cache.
187 * FIXME, for now it's a one-entry cache.
188 * FIXME2, the "-993" is to reduce the chance of a hit on the first lookup.
189 * This file should be modified for non-unix systems to do something
190 * reasonable.
193 #ifndef TUNMLEN
194 #define TUNMLEN 256
195 #endif
196 #ifndef TGNMLEN
197 #define TGNMLEN 256
198 #endif
200 #define myuid ( my_uid < 0? (my_uid = getuid()): my_uid )
201 #define mygid ( my_gid < 0? (my_gid = getgid()): my_gid )
204 vfs_finduid (const char *uname)
206 static int saveuid = -993;
207 static char saveuname[TUNMLEN];
208 static int my_uid = -993;
210 struct passwd *pw;
212 if (uname[0] != saveuname[0] /* Quick test w/o proc call */
213 ||0 != strncmp (uname, saveuname, TUNMLEN)) {
214 g_strlcpy (saveuname, uname, TUNMLEN);
215 pw = getpwnam (uname);
216 if (pw) {
217 saveuid = pw->pw_uid;
218 } else {
219 saveuid = myuid;
222 return saveuid;
226 vfs_findgid (const char *gname)
228 static int savegid = -993;
229 static char savegname[TGNMLEN];
230 static int my_gid = -993;
232 struct group *gr;
234 if (gname[0] != savegname[0] /* Quick test w/o proc call */
235 ||0 != strncmp (gname, savegname, TUNMLEN)) {
236 g_strlcpy (savegname, gname, TUNMLEN);
237 gr = getgrnam (gname);
238 if (gr) {
239 savegid = gr->gr_gid;
240 } else {
241 savegid = mygid;
244 return savegid;
248 * Create a temporary file with a name resembling the original.
249 * This is needed e.g. for local copies requested by extfs.
250 * Some extfs scripts may look at the extension.
251 * We also protect stupid scripts agains dangerous names.
254 vfs_mkstemps (char **pname, const char *prefix, const char *param_basename)
256 const char *p;
257 char *suffix, *q;
258 int shift;
259 int fd;
261 /* Strip directories */
262 p = strrchr (param_basename, PATH_SEP);
263 if (!p)
264 p = param_basename;
265 else
266 p++;
268 /* Protection against very long names */
269 shift = strlen (p) - (MC_MAXPATHLEN - 16);
270 if (shift > 0)
271 p += shift;
273 suffix = g_malloc (MC_MAXPATHLEN);
275 /* Protection against unusual characters */
276 q = suffix;
277 while (*p && (*p != '#')) {
278 if (strchr (".-_@", *p) || isalnum ((unsigned char) *p))
279 *q++ = *p;
280 p++;
282 *q = 0;
284 fd = mc_mkstemps (pname, prefix, suffix);
285 g_free (suffix);
286 return fd;
289 /* Parsing code is used by ftpfs, fish and extfs */
290 #define MAXCOLS 30
292 static char *columns[MAXCOLS]; /* Points to the string in column n */
293 static int column_ptr[MAXCOLS]; /* Index from 0 to the starting positions of the columns */
296 vfs_split_text (char *p)
298 char *original = p;
299 int numcols;
301 memset (columns, 0, sizeof (columns));
303 for (numcols = 0; *p && numcols < MAXCOLS; numcols++) {
304 while (*p == ' ' || *p == '\r' || *p == '\n') {
305 *p = 0;
306 p++;
308 columns[numcols] = p;
309 column_ptr[numcols] = p - original;
310 while (*p && *p != ' ' && *p != '\r' && *p != '\n')
311 p++;
313 return numcols;
316 static int
317 is_num (int idx)
319 char *column = columns[idx];
321 if (!column || column[0] < '0' || column[0] > '9')
322 return 0;
324 return 1;
327 /* Return 1 for MM-DD-YY and MM-DD-YYYY */
328 static int
329 is_dos_date (const char *str)
331 int len;
333 if (!str)
334 return 0;
336 len = strlen (str);
337 if (len != 8 && len != 10)
338 return 0;
340 if (str[2] != str[5])
341 return 0;
343 if (!strchr ("\\-/", (int) str[2]))
344 return 0;
346 return 1;
349 static int
350 is_week (const char *str, struct tm *tim)
352 static const char *week = "SunMonTueWedThuFriSat";
353 const char *pos;
355 if (!str)
356 return 0;
358 if ((pos = strstr (week, str)) != NULL) {
359 if (tim != NULL)
360 tim->tm_wday = (pos - week) / 3;
361 return 1;
363 return 0;
366 static int
367 is_month (const char *str, struct tm *tim)
369 static const char *month = "JanFebMarAprMayJunJulAugSepOctNovDec";
370 const char *pos;
372 if (!str)
373 return 0;
375 if ((pos = strstr (month, str)) != NULL) {
376 if (tim != NULL)
377 tim->tm_mon = (pos - month) / 3;
378 return 1;
380 return 0;
384 * Check for possible locale's abbreviated month name (Jan..Dec).
385 * Any 3 bytes long string without digit, control and punctuation characters.
386 * isalpha() is locale specific, so it cannot be used if current
387 * locale is "C" and ftp server use Cyrillic.
388 * NB: It is assumed there are no whitespaces in month.
390 static int
391 is_localized_month (const char *month)
393 int i = 0;
395 if (!month)
396 return 0;
398 while ((i < 3) && *month && !isdigit ((unsigned char) *month)
399 && !iscntrl ((unsigned char) *month)
400 && !ispunct ((unsigned char) *month)) {
401 i++;
402 month++;
404 return ((i == 3) && (*month == 0));
407 static int
408 is_time (const char *str, struct tm *tim)
410 const char *p, *p2;
412 if (!str)
413 return 0;
415 if ((p = strchr (str, ':')) && (p2 = strrchr (str, ':'))) {
416 if (p != p2) {
417 if (sscanf
418 (str, "%2d:%2d:%2d", &tim->tm_hour, &tim->tm_min,
419 &tim->tm_sec) != 3)
420 return 0;
421 } else {
422 if (sscanf (str, "%2d:%2d", &tim->tm_hour, &tim->tm_min) != 2)
423 return 0;
425 } else
426 return 0;
428 return 1;
431 static int
432 is_year (char *str, struct tm *tim)
434 long year;
436 if (!str)
437 return 0;
439 if (strchr (str, ':'))
440 return 0;
442 if (strlen (str) != 4)
443 return 0;
445 if (sscanf (str, "%ld", &year) != 1)
446 return 0;
448 if (year < 1900 || year > 3000)
449 return 0;
451 tim->tm_year = (int) (year - 1900);
453 return 1;
456 gboolean
457 vfs_parse_filetype (const char *s, size_t *ret_skipped, mode_t *ret_type)
459 mode_t type;
461 switch (*s) {
462 case 'd': type = S_IFDIR; break;
463 case 'b': type = S_IFBLK; break;
464 case 'c': type = S_IFCHR; break;
465 case 'l': type = S_IFLNK; break;
466 #ifdef S_IFSOCK
467 case 's': type = S_IFSOCK; break;
468 #else
469 case 's': type = S_IFIFO; break;
470 #endif
471 #ifdef S_IFDOOR /* Solaris door */
472 case 'D': type = S_IFDOOR; break;
473 #else
474 case 'D': type = S_IFIFO; break;
475 #endif
476 case 'p': type = S_IFIFO; break;
477 #ifdef S_IFNAM /* Special named files */
478 case 'n': type = S_IFNAM; break;
479 #else
480 case 'n': type = S_IFREG; break;
481 #endif
482 case 'm': /* Don't know what these are :-) */
483 case '-':
484 case '?': type = S_IFREG; break;
485 default: return FALSE;
488 *ret_type = type;
489 *ret_skipped = 1;
490 return TRUE;
493 gboolean
494 vfs_parse_fileperms (const char *s, size_t *ret_skipped, mode_t *ret_perms)
496 const char *p;
497 mode_t perms;
499 p = s;
500 perms = 0;
502 switch (*p++) {
503 case '-': break;
504 case 'r': perms |= S_IRUSR; break;
505 default: return FALSE;
507 switch (*p++) {
508 case '-': break;
509 case 'w': perms |= S_IWUSR; break;
510 default: return FALSE;
512 switch (*p++) {
513 case '-': break;
514 case 'S': perms |= S_ISUID; break;
515 case 's': perms |= S_IXUSR | S_ISUID; break;
516 case 'x': perms |= S_IXUSR; break;
517 default: return FALSE;
519 switch (*p++) {
520 case '-': break;
521 case 'r': perms |= S_IRGRP; break;
522 default: return FALSE;
524 switch (*p++) {
525 case '-': break;
526 case 'w': perms |= S_IWGRP; break;
527 default: return FALSE;
529 switch (*p++) {
530 case '-': break;
531 case 'S': perms |= S_ISGID; break;
532 case 'l': perms |= S_ISGID; break; /* found on Solaris */
533 case 's': perms |= S_IXGRP | S_ISGID; break;
534 case 'x': perms |= S_IXGRP; break;
535 default: return FALSE;
537 switch (*p++) {
538 case '-': break;
539 case 'r': perms |= S_IROTH; break;
540 default: return FALSE;
542 switch (*p++) {
543 case '-': break;
544 case 'w': perms |= S_IWOTH; break;
545 default: return FALSE;
547 switch (*p++) {
548 case '-': break;
549 case 'T': perms |= S_ISVTX; break;
550 case 't': perms |= S_IXOTH | S_ISVTX; break;
551 case 'x': perms |= S_IXOTH; break;
552 default: return FALSE;
554 if (*p == '+') { /* ACLs on Solaris, HP-UX and others */
555 p++;
558 *ret_skipped = p - s;
559 *ret_perms = perms;
560 return TRUE;
563 gboolean
564 vfs_parse_filemode (const char *s, size_t *ret_skipped,
565 mode_t *ret_mode)
567 const char *p;
568 mode_t type, perms;
569 size_t skipped;
571 p = s;
573 if (!vfs_parse_filetype (p, &skipped, &type))
574 return FALSE;
575 p += skipped;
577 if (!vfs_parse_fileperms (p, &skipped, &perms))
578 return FALSE;
579 p += skipped;
581 *ret_skipped = p - s;
582 *ret_mode = type | perms;
583 return TRUE;
586 gboolean
587 vfs_parse_raw_filemode (const char *s, size_t *ret_skipped,
588 mode_t *ret_mode)
590 const char *p;
591 mode_t remote_type = 0, local_type, perms = 0;
593 p = s;
595 /* isoctal */
596 while(*p >= '0' && *p <= '7')
598 perms *= 010;
599 perms += (*p - '0');
600 ++p;
603 if (*p++ != ' ')
604 return FALSE;
606 while(*p >= '0' && *p <= '7')
608 remote_type *= 010;
609 remote_type += (*p - '0');
610 ++p;
613 if (*p++ != ' ')
614 return FALSE;
616 /* generated with:
617 $ perl -e 'use Fcntl ":mode";
618 my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
619 foreach $t (@modes) { printf ("%o\n", $t); };'
620 TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
621 (see vfs_parse_filetype)
624 switch (remote_type)
626 case 020000:
627 local_type = S_IFCHR; break;
628 case 040000:
629 local_type = S_IFDIR; break;
630 case 060000:
631 local_type = S_IFBLK; break;
632 case 0120000:
633 local_type = S_IFLNK; break;
634 case 0100000:
635 default: /* don't know what is it */
636 local_type = S_IFREG; break;
639 *ret_skipped = p - s;
640 *ret_mode = local_type | perms;
641 return TRUE;
644 /* This function parses from idx in the columns[] array */
646 vfs_parse_filedate (int idx, time_t *t)
648 char *p;
649 struct tm tim;
650 int d[3];
651 int got_year = 0;
652 int l10n = 0; /* Locale's abbreviated month name */
653 time_t current_time;
654 struct tm *local_time;
656 /* Let's setup default time values */
657 current_time = time (NULL);
658 local_time = localtime (&current_time);
659 tim.tm_mday = local_time->tm_mday;
660 tim.tm_mon = local_time->tm_mon;
661 tim.tm_year = local_time->tm_year;
663 tim.tm_hour = 0;
664 tim.tm_min = 0;
665 tim.tm_sec = 0;
666 tim.tm_isdst = -1; /* Let mktime() try to guess correct dst offset */
668 p = columns[idx++];
670 /* We eat weekday name in case of extfs */
671 if (is_week (p, &tim))
672 p = columns[idx++];
674 /* Month name */
675 if (is_month (p, &tim)) {
676 /* And we expect, it followed by day number */
677 if (is_num (idx))
678 tim.tm_mday = (int) atol (columns[idx++]);
679 else
680 return 0; /* No day */
682 } else {
683 /* We expect:
684 3 fields max or we'll see oddities with certain file names.
685 So both year and time is not allowed.
686 Mon DD hh:mm[:ss]
687 Mon DD YYYY
688 But in case of extfs we allow these date formats:
689 MM-DD-YY hh:mm[:ss]
690 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
691 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
693 /* Special case with MM-DD-YY or MM-DD-YYYY */
694 if (is_dos_date (p)) {
695 p[2] = p[5] = '-';
697 if (sscanf (p, "%2d-%2d-%d", &d[0], &d[1], &d[2]) == 3) {
698 /* Months are zero based */
699 if (d[0] > 0)
700 d[0]--;
702 if (d[2] > 1900) {
703 d[2] -= 1900;
704 } else {
705 /* Y2K madness */
706 if (d[2] < 70)
707 d[2] += 100;
710 tim.tm_mon = d[0];
711 tim.tm_mday = d[1];
712 tim.tm_year = d[2];
713 got_year = 1;
714 } else
715 return 0; /* sscanf failed */
716 } else {
717 /* Locale's abbreviated month name followed by day number */
718 if (is_localized_month (p) && (is_num (idx++)))
719 l10n = 1;
720 else
721 return 0; /* unsupported format */
725 /* Here we expect to find time or year */
726 if (is_num (idx) && (is_time (columns[idx], &tim)
727 || (got_year = is_year (columns[idx], &tim))))
728 idx++;
729 else
730 return 0; /* Neither time nor date */
733 * If the date is less than 6 months in the past, it is shown without year
734 * other dates in the past or future are shown with year but without time
735 * This does not check for years before 1900 ... I don't know, how
736 * to represent them at all
738 if (!got_year && local_time->tm_mon < 6
739 && local_time->tm_mon < tim.tm_mon
740 && tim.tm_mon - local_time->tm_mon >= 6)
742 tim.tm_year--;
744 if (l10n || (*t = mktime (&tim)) < 0)
745 *t = 0;
746 return idx;
750 vfs_parse_ls_lga (const char *p, struct stat *s, char **filename,
751 char **linkname)
753 int idx, idx2, num_cols;
754 int i;
755 char *p_copy = NULL;
756 char *t = NULL;
757 const char *line = p;
758 size_t skipped;
760 if (strncmp (p, "total", 5) == 0)
761 return 0;
763 if (!vfs_parse_filetype (p, &skipped, &s->st_mode))
764 goto error;
765 p += skipped;
767 if (*p == ' ') /* Notwell 4 */
768 p++;
769 if (*p == '[') {
770 if (strlen (p) <= 8 || p[8] != ']')
771 goto error;
772 /* Should parse here the Notwell permissions :) */
773 if (S_ISDIR (s->st_mode))
774 s->st_mode |=
775 (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IXUSR | S_IXGRP
776 | S_IXOTH);
777 else
778 s->st_mode |= (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
779 p += 9;
780 } else {
781 size_t lc_skipped;
782 mode_t perms;
784 if (!vfs_parse_fileperms (p, &lc_skipped, &perms))
785 goto error;
786 p += lc_skipped;
787 s->st_mode |= perms;
790 p_copy = g_strdup (p);
791 num_cols = vfs_split_text (p_copy);
793 s->st_nlink = atol (columns[0]);
794 if (s->st_nlink <= 0)
795 goto error;
797 if (!is_num (1))
798 s->st_uid = vfs_finduid (columns[1]);
799 else
800 s->st_uid = (uid_t) atol (columns[1]);
802 /* Mhm, the ls -lg did not produce a group field */
803 for (idx = 3; idx <= 5; idx++)
804 if (is_month (columns[idx], NULL) || is_week (columns[idx], NULL)
805 || is_dos_date (columns[idx])
806 || is_localized_month (columns[idx]))
807 break;
809 if (idx == 6
810 || (idx == 5 && !S_ISCHR (s->st_mode) && !S_ISBLK (s->st_mode)))
811 goto error;
813 /* We don't have gid */
814 if (idx == 3
815 || (idx == 4 && (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode))))
816 idx2 = 2;
817 else {
818 /* We have gid field */
819 if (is_num (2))
820 s->st_gid = (gid_t) atol (columns[2]);
821 else
822 s->st_gid = vfs_findgid (columns[2]);
823 idx2 = 3;
826 /* This is device */
827 if (S_ISCHR (s->st_mode) || S_ISBLK (s->st_mode)) {
828 int maj, min;
830 /* Corner case: there is no whitespace(s) between maj & min */
831 if (!is_num (idx2) && idx2 == 2) {
832 if (!is_num (++idx2) || sscanf (columns[idx2], " %d,%d", &min, &min) != 2)
833 goto error;
834 } else {
835 if (!is_num (idx2) || sscanf (columns[idx2], " %d,", &maj) != 1)
836 goto error;
838 if (!is_num (++idx2) || sscanf (columns[idx2], " %d", &min) != 1)
839 goto error;
841 #ifdef HAVE_STRUCT_STAT_ST_RDEV
842 s->st_rdev = makedev (maj, min);
843 #endif
844 s->st_size = 0;
846 } else {
847 /* Common file size */
848 if (!is_num (idx2))
849 goto error;
851 #ifdef HAVE_ATOLL
852 s->st_size = (off_t) atoll (columns[idx2]);
853 #else
854 s->st_size = (off_t) atof (columns[idx2]);
855 #endif
856 #ifdef HAVE_STRUCT_STAT_ST_RDEV
857 s->st_rdev = 0;
858 #endif
861 idx = vfs_parse_filedate (idx, &s->st_mtime);
862 if (!idx)
863 goto error;
864 /* Use resulting time value */
865 s->st_atime = s->st_ctime = s->st_mtime;
866 /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
867 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
868 s->st_blksize = 512;
869 #endif
870 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
871 s->st_blocks = (s->st_size + 511) / 512;
872 #endif
874 for (i = idx + 1, idx2 = 0; i < num_cols; i++)
875 if (strcmp (columns[i], "->") == 0) {
876 idx2 = i;
877 break;
880 if (((S_ISLNK (s->st_mode) || (num_cols == idx + 3 && s->st_nlink > 1))) /* Maybe a hardlink? (in extfs) */
881 &&idx2) {
883 if (filename) {
884 *filename =
885 g_strndup (p + column_ptr[idx],
886 column_ptr[idx2] - column_ptr[idx] - 1);
888 if (linkname) {
889 t = g_strdup (p + column_ptr[idx2 + 1]);
890 *linkname = t;
892 } else {
893 /* Extract the filename from the string copy, not from the columns
894 * this way we have a chance of entering hidden directories like ". ."
896 if (filename) {
898 * filename = g_strdup (columns [idx++]);
901 t = g_strdup (p + column_ptr[idx]);
902 *filename = t;
904 if (linkname)
905 *linkname = NULL;
908 if (t) {
909 int p2 = strlen (t);
910 if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
911 t[p2] = 0;
912 if ((--p2 > 0) && (t[p2] == '\r' || t[p2] == '\n'))
913 t[p2] = 0;
916 g_free (p_copy);
917 return 1;
919 error:
921 static int errorcount = 0;
923 if (++errorcount < 5) {
924 message (D_ERROR, _("Cannot parse:"), "%s",
925 (p_copy && *p_copy) ? p_copy : line);
926 } else if (errorcount == 5)
927 message (D_ERROR, MSG_ERROR,
928 _("More parsing errors will be ignored."));
931 g_free (p_copy);
932 return 0;
935 void
936 vfs_die (const char *m)
938 message (D_ERROR, _("Internal error:"), "%s", m);
939 exit (1);
942 char *
943 vfs_get_password (const char *msg)
945 return input_dialog (msg, _("Password:"), MC_HISTORY_VFS_PASSWORD, INPUT_PASSWORD);