2 Routines for parsing output from the `ls' command.
4 Copyright (C) 1988, 1992, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 The Free Software Foundation, Inc.
8 Copyright (C) 1995, 1996 Miguel de Icaza
11 Miguel de Icaza, 1995, 1996
12 Slava Zanko <slavazanko@gmail.com>, 2011
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 * \brief Source: Utilities for VFS modules
33 * \author Miguel de Icaza
43 #include "lib/global.h"
44 #include "lib/widget.h" /* message() */
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 /* Parsing code is used by ftpfs, fish and extfs */
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 static char *columns
[MAXCOLS
]; /* Points to the string in column n */
60 static int column_ptr
[MAXCOLS
]; /* Index from 0 to the starting positions of the columns */
61 static size_t vfs_parce_ls_final_num_spaces
= 0;
63 /*** file scope functions ************************************************************************/
64 /* --------------------------------------------------------------------------------------------- */
69 char *column
= columns
[idx
];
71 if (!column
|| column
[0] < '0' || column
[0] > '9')
77 /* --------------------------------------------------------------------------------------------- */
78 /* Return 1 for MM-DD-YY and MM-DD-YYYY */
81 is_dos_date (const char *str
)
89 if (len
!= 8 && len
!= 10)
95 if (!strchr ("\\-/", (int) str
[2]))
101 /* --------------------------------------------------------------------------------------------- */
104 is_week (const char *str
, struct tm
*tim
)
106 static const char *week
= "SunMonTueWedThuFriSat";
112 pos
= strstr (week
, str
);
116 tim
->tm_wday
= (pos
- week
) / 3;
122 /* --------------------------------------------------------------------------------------------- */
125 is_month (const char *str
, struct tm
*tim
)
127 static const char *month
= "JanFebMarAprMayJunJulAugSepOctNovDec";
133 pos
= strstr (month
, str
);
137 tim
->tm_mon
= (pos
- month
) / 3;
143 /* --------------------------------------------------------------------------------------------- */
145 * Check for possible locale's abbreviated month name (Jan..Dec).
146 * Any 3 bytes long string without digit, control and punctuation characters.
147 * isalpha() is locale specific, so it cannot be used if current
148 * locale is "C" and ftp server use Cyrillic.
149 * NB: It is assumed there are no whitespaces in month.
152 is_localized_month (const char *month
)
159 while ((i
< 3) && *month
&& !isdigit ((unsigned char) *month
)
160 && !iscntrl ((unsigned char) *month
) && !ispunct ((unsigned char) *month
))
165 return ((i
== 3) && (*month
== 0));
168 /* --------------------------------------------------------------------------------------------- */
171 is_time (const char *str
, struct tm
*tim
)
178 p
= strchr (str
, ':');
179 p2
= strrchr (str
, ':');
180 if (p
!= NULL
&& p2
!= NULL
)
184 if (sscanf (str
, "%2d:%2d:%2d", &tim
->tm_hour
, &tim
->tm_min
, &tim
->tm_sec
) != 3)
189 if (sscanf (str
, "%2d:%2d", &tim
->tm_hour
, &tim
->tm_min
) != 2)
199 /* --------------------------------------------------------------------------------------------- */
202 is_year (char *str
, struct tm
*tim
)
209 if (strchr (str
, ':'))
212 if (strlen (str
) != 4)
215 if (sscanf (str
, "%ld", &year
) != 1)
218 if (year
< 1900 || year
> 3000)
221 tim
->tm_year
= (int) (year
- 1900);
226 /* --------------------------------------------------------------------------------------------- */
227 /*** public functions ****************************************************************************/
228 /* --------------------------------------------------------------------------------------------- */
231 vfs_parse_filetype (const char *s
, size_t * ret_skipped
, mode_t
* ret_type
)
258 #ifdef S_IFDOOR /* Solaris door */
270 #ifdef S_IFNAM /* Special named files */
279 case 'm': /* Don't know what these are :-) */
293 /* --------------------------------------------------------------------------------------------- */
296 vfs_parse_fileperms (const char *s
, size_t * ret_skipped
, mode_t
* ret_perms
)
332 perms
|= S_IXUSR
| S_ISUID
;
369 break; /* found on Solaris */
371 perms
|= S_IXGRP
| S_ISGID
;
407 perms
|= S_IXOTH
| S_ISVTX
;
416 { /* ACLs on Solaris, HP-UX and others */
420 *ret_skipped
= p
- s
;
425 /* --------------------------------------------------------------------------------------------- */
428 vfs_parse_filemode (const char *s
, size_t * ret_skipped
, mode_t
* ret_mode
)
436 if (!vfs_parse_filetype (p
, &skipped
, &type
))
440 if (!vfs_parse_fileperms (p
, &skipped
, &perms
))
444 *ret_skipped
= p
- s
;
445 *ret_mode
= type
| perms
;
449 /* --------------------------------------------------------------------------------------------- */
452 vfs_parse_raw_filemode (const char *s
, size_t * ret_skipped
, mode_t
* ret_mode
)
455 mode_t remote_type
= 0, local_type
, perms
= 0;
460 while (*p
>= '0' && *p
<= '7')
470 while (*p
>= '0' && *p
<= '7')
473 remote_type
+= (*p
- '0');
481 $ perl -e 'use Fcntl ":mode";
482 my @modes = (S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFREG);
483 foreach $t (@modes) { printf ("%o\n", $t); };'
484 TODO: S_IFDOOR, S_IFIFO, S_IFSOCK (if supported by os)
485 (see vfs_parse_filetype)
491 local_type
= S_IFCHR
;
494 local_type
= S_IFDIR
;
497 local_type
= S_IFBLK
;
500 local_type
= S_IFLNK
;
503 default: /* don't know what is it */
504 local_type
= S_IFREG
;
508 *ret_skipped
= p
- s
;
509 *ret_mode
= local_type
| perms
;
513 /* --------------------------------------------------------------------------------------------- */
514 /** This function parses from idx in the columns[] array */
517 vfs_parse_filedate (int idx
, time_t * t
)
523 int l10n
= 0; /* Locale's abbreviated month name */
525 struct tm
*local_time
;
527 /* Let's setup default time values */
528 current_time
= time (NULL
);
529 local_time
= localtime (¤t_time
);
530 tim
.tm_mday
= local_time
->tm_mday
;
531 tim
.tm_mon
= local_time
->tm_mon
;
532 tim
.tm_year
= local_time
->tm_year
;
537 tim
.tm_isdst
= -1; /* Let mktime() try to guess correct dst offset */
541 /* We eat weekday name in case of extfs */
542 if (is_week (p
, &tim
))
546 if (is_month (p
, &tim
))
548 /* And we expect, it followed by day number */
550 tim
.tm_mday
= (int) atol (columns
[idx
++]);
552 return 0; /* No day */
558 3 fields max or we'll see oddities with certain file names.
559 So both year and time is not allowed.
562 But in case of extfs we allow these date formats:
564 where Mon is Jan-Dec, DD, MM, YY two digit day, month, year,
565 YYYY four digit year, hh, mm, ss two digit hour, minute or second. */
567 /* Special case with MM-DD-YY or MM-DD-YYYY */
572 if (sscanf (p
, "%2d-%2d-%d", &d
[0], &d
[1], &d
[2]) == 3)
574 /* Months are zero based */
595 return 0; /* sscanf failed */
599 /* Locale's abbreviated month name followed by day number */
600 if (is_localized_month (p
) && (is_num (idx
++)))
603 return 0; /* unsupported format */
607 /* Here we expect to find time or year */
608 if (is_num (idx
) && (is_time (columns
[idx
], &tim
) || (got_year
= is_year (columns
[idx
], &tim
))))
611 return 0; /* Neither time nor date */
614 * If the date is less than 6 months in the past, it is shown without year
615 * other dates in the past or future are shown with year but without time
616 * This does not check for years before 1900 ... I don't know, how
617 * to represent them at all
619 if (!got_year
&& local_time
->tm_mon
< 6
620 && local_time
->tm_mon
< tim
.tm_mon
&& tim
.tm_mon
- local_time
->tm_mon
>= 6)
625 if (l10n
|| (*t
< 0))
630 /* --------------------------------------------------------------------------------------------- */
633 vfs_split_text (char *p
)
638 memset (columns
, 0, sizeof (columns
));
640 for (numcols
= 0; *p
&& numcols
< MAXCOLS
; numcols
++)
642 while (*p
== ' ' || *p
== '\r' || *p
== '\n')
647 columns
[numcols
] = p
;
648 column_ptr
[numcols
] = p
- original
;
649 while (*p
&& *p
!= ' ' && *p
!= '\r' && *p
!= '\n')
655 /* --------------------------------------------------------------------------------------------- */
658 vfs_parse_ls_lga_init (void)
660 vfs_parce_ls_final_num_spaces
= 0;
663 /* --------------------------------------------------------------------------------------------- */
666 vfs_parse_ls_lga_get_final_spaces (void)
668 return vfs_parce_ls_final_num_spaces
;
671 /* --------------------------------------------------------------------------------------------- */
674 vfs_parse_ls_lga (const char *p
, struct stat
* s
, char **filename
, char **linkname
,
677 int idx
, idx2
, num_cols
;
681 const char *line
= p
;
684 if (strncmp (p
, "total", 5) == 0)
687 if (!vfs_parse_filetype (p
, &skipped
, &s
->st_mode
))
691 if (*p
== ' ') /* Notwell 4 */
695 if (strlen (p
) <= 8 || p
[8] != ']')
697 /* Should parse here the Notwell permissions :) */
698 if (S_ISDIR (s
->st_mode
))
699 s
->st_mode
|= (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
| S_IXUSR
| S_IXGRP
| S_IXOTH
);
701 s
->st_mode
|= (S_IRUSR
| S_IRGRP
| S_IROTH
| S_IWUSR
);
709 if (!vfs_parse_fileperms (p
, &lc_skipped
, &perms
))
715 p_copy
= g_strdup (p
);
716 num_cols
= vfs_split_text (p_copy
);
718 s
->st_nlink
= atol (columns
[0]);
719 if (s
->st_nlink
<= 0)
723 s
->st_uid
= vfs_finduid (columns
[1]);
725 s
->st_uid
= (uid_t
) atol (columns
[1]);
727 /* Mhm, the ls -lg did not produce a group field */
728 for (idx
= 3; idx
<= 5; idx
++)
729 if (is_month (columns
[idx
], NULL
) || is_week (columns
[idx
], NULL
)
730 || is_dos_date (columns
[idx
]) || is_localized_month (columns
[idx
]))
733 if (idx
== 6 || (idx
== 5 && !S_ISCHR (s
->st_mode
) && !S_ISBLK (s
->st_mode
)))
736 /* We don't have gid */
737 if (idx
== 3 || (idx
== 4 && (S_ISCHR (s
->st_mode
) || S_ISBLK (s
->st_mode
))))
741 /* We have gid field */
743 s
->st_gid
= (gid_t
) atol (columns
[2]);
745 s
->st_gid
= vfs_findgid (columns
[2]);
750 if (S_ISCHR (s
->st_mode
) || S_ISBLK (s
->st_mode
))
754 /* Corner case: there is no whitespace(s) between maj & min */
755 if (!is_num (idx2
) && idx2
== 2)
757 if (!is_num (++idx2
) || sscanf (columns
[idx2
], " %d,%d", &maj
, &min
) != 2)
762 if (!is_num (idx2
) || sscanf (columns
[idx2
], " %d,", &maj
) != 1)
765 if (!is_num (++idx2
) || sscanf (columns
[idx2
], " %d", &min
) != 1)
768 #ifdef HAVE_STRUCT_STAT_ST_RDEV
769 s
->st_rdev
= makedev (maj
, min
);
776 /* Common file size */
781 s
->st_size
= (off_t
) atoll (columns
[idx2
]);
783 s
->st_size
= (off_t
) atof (columns
[idx2
]);
785 #ifdef HAVE_STRUCT_STAT_ST_RDEV
790 idx
= vfs_parse_filedate (idx
, &s
->st_mtime
);
793 /* Use resulting time value */
794 s
->st_atime
= s
->st_ctime
= s
->st_mtime
;
795 /* s->st_dev and s->st_ino must be initialized by vfs_s_new_inode () */
796 #ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
799 #ifdef HAVE_STRUCT_STAT_ST_BLOCKS
800 s
->st_blocks
= (s
->st_size
+ 511) / 512;
803 if (num_spaces
!= NULL
)
805 *num_spaces
= column_ptr
[idx
] - column_ptr
[idx
- 1] - strlen (columns
[idx
- 1]);
806 if (strcmp (columns
[idx
], "..") == 0)
807 vfs_parce_ls_final_num_spaces
= *num_spaces
;
810 for (i
= idx
+ 1, idx2
= 0; i
< num_cols
; i
++)
811 if (strcmp (columns
[i
], "->") == 0)
817 if (((S_ISLNK (s
->st_mode
) || (num_cols
== idx
+ 3 && s
->st_nlink
> 1))) /* Maybe a hardlink? (in extfs) */
823 *filename
= g_strndup (p
+ column_ptr
[idx
], column_ptr
[idx2
] - column_ptr
[idx
] - 1);
827 t
= g_strdup (p
+ column_ptr
[idx2
+ 1]);
833 /* Extract the filename from the string copy, not from the columns
834 * this way we have a chance of entering hidden directories like ". ."
839 * filename = g_strdup (columns [idx++]);
842 t
= g_strdup (p
+ column_ptr
[idx
]);
852 if ((--p2
> 0) && (t
[p2
] == '\r' || t
[p2
] == '\n'))
854 if ((--p2
> 0) && (t
[p2
] == '\r' || t
[p2
] == '\n'))
863 static int errorcount
= 0;
865 if (++errorcount
< 5)
867 message (D_ERROR
, _("Cannot parse:"), "%s", (p_copy
&& *p_copy
) ? p_copy
: line
);
869 else if (errorcount
== 5)
870 message (D_ERROR
, MSG_ERROR
, _("More parsing errors will be ignored."));
877 /* --------------------------------------------------------------------------------------------- */