1 /* stat.c -- display file or filesystem status
2 Copyright (C) 2001, 2002, 2003 Free Software Foundation.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 Written by Michael Meskes. */
23 #include <sys/types.h>
24 #ifdef HAVE_SYS_SYSMACROS_H
25 # include <sys/sysmacros.h>
31 #if HAVE_SYS_STATVFS_H
32 # include <sys/statvfs.h>
38 /* NetBSD 1.5.2 needs these, for the declaration of struct statfs. */
39 #if !HAVE_SYS_STATVFS_H && !HAVE_SYS_VFS_H
40 # if HAVE_SYS_MOUNT_H && HAVE_SYS_PARAM_H
41 # include <sys/param.h>
42 # include <sys/mount.h>
51 #include "file-type.h"
57 #include "xreadlink.h"
59 #define NAMEMAX_FORMAT PRIuMAX
61 #if HAVE_STRUCT_STATVFS_F_BASETYPE
62 # define STRUCT_STATVFS struct statvfs
63 # define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATVFS
64 # if HAVE_STRUCT_STATVFS_F_NAMEMAX
65 # define SB_F_NAMEMAX(S) ((uintmax_t) ((S)->f_namemax))
68 # define STRUCT_STATVFS struct statfs
69 # define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATFS
70 # if HAVE_STRUCT_STATFS_F_NAMELEN
71 # define SB_F_NAMEMAX(S) ((uintmax_t) ((S)->f_namelen))
76 /* NetBSD 1.5.2 has neither f_namemax nor f_namelen. */
77 # define SB_F_NAMEMAX(S) "*"
78 # undef NAMEMAX_FORMAT
79 # define NAMEMAX_FORMAT "s"
82 #define PROGRAM_NAME "stat"
84 #define AUTHORS "Michael Meskes"
86 static struct option
const long_options
[] = {
87 {"link", no_argument
, 0, 'l'}, /* deprecated. FIXME: remove in 2003 */
88 {"dereference", no_argument
, 0, 'L'},
89 {"format", required_argument
, 0, 'c'},
90 {"filesystem", no_argument
, 0, 'f'},
91 {"terse", no_argument
, 0, 't'},
92 {GETOPT_HELP_OPTION_DECL
},
93 {GETOPT_VERSION_OPTION_DECL
},
97 /* Nonzero means we should exit with EXIT_FAILURE upon completion. */
102 /* Return the type of the specified file system.
103 Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
104 Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
105 Still others have neither and have to get by with f_type (Linux). */
107 human_fstype (STRUCT_STATVFS
const *statfsbuf
)
109 #if HAVE_STRUCT_STATVFS_F_BASETYPE
110 return statfsbuf
->f_basetype
;
112 # if HAVE_STRUCT_STATFS_F_FSTYPENAME
113 return statfsbuf
->f_fstypename
;
117 switch (statfsbuf
->f_type
)
119 # if defined __linux__
126 case S_MAGIC_EXT2_OLD
:
138 case S_MAGIC_ISOFS_WIN
:
141 case S_MAGIC_ISOFS_R_WIN
:
147 case S_MAGIC_MINIX_30
:
148 type
= "minix (30 char.)";
150 case S_MAGIC_MINIX_V2
:
153 case S_MAGIC_MINIX_V2_30
:
154 type
= "minix v2 (30 char.)";
198 case S_MAGIC_REISERFS
:
296 return (char *) type
;
299 static char buf
[sizeof "UNKNOWN (0x%x)" - 2
300 + 2 * sizeof (statfsbuf
->f_type
)];
301 sprintf (buf
, "UNKNOWN (0x%x)", statfsbuf
->f_type
);
309 human_access (struct stat
const *statbuf
)
311 static char modebuf
[11];
312 mode_string (statbuf
->st_mode
, modebuf
);
318 human_time (time_t const *t
)
321 struct tm
*tm
= localtime (t
);
325 return (char *) _("*** invalid date/time ***");
327 nstrftime (str
, sizeof str
, "%Y-%m-%d %H:%M:%S.%N %z", tm
, 0, 0);
331 /* print statfs info */
333 print_statfs (char *pformat
, char m
, char const *filename
,
336 STRUCT_STATVFS
const *statfsbuf
= data
;
341 strcat (pformat
, "s");
342 printf (pformat
, filename
);
346 #if HAVE_STRUCT_STATXFS_F_FSID___VAL
347 strcat (pformat
, "x %-8x");
348 printf (pformat
, statfsbuf
->f_fsid
.__val
[0], /* u_long */
349 statfsbuf
->f_fsid
.__val
[1]);
351 strcat (pformat
, "Lx");
352 printf (pformat
, statfsbuf
->f_fsid
);
357 strcat (pformat
, NAMEMAX_FORMAT
);
358 printf (pformat
, SB_F_NAMEMAX (statfsbuf
));
361 #if HAVE_STRUCT_STATXFS_F_TYPE
362 strcat (pformat
, "lx");
363 printf (pformat
, (long int) (statfsbuf
->f_type
)); /* no equiv. */
369 strcat (pformat
, "s");
370 printf (pformat
, human_fstype (statfsbuf
));
373 strcat (pformat
, PRIdMAX
);
374 printf (pformat
, (intmax_t) (statfsbuf
->f_blocks
));
377 strcat (pformat
, PRIdMAX
);
378 printf (pformat
, (intmax_t) (statfsbuf
->f_bfree
));
381 strcat (pformat
, PRIdMAX
);
382 printf (pformat
, (intmax_t) (statfsbuf
->f_bavail
));
385 strcat (pformat
, "ld");
386 printf (pformat
, (long int) (statfsbuf
->f_bsize
));
389 strcat (pformat
, PRIdMAX
);
390 printf (pformat
, (intmax_t) (statfsbuf
->f_files
));
393 strcat (pformat
, PRIdMAX
);
394 printf (pformat
, (intmax_t) (statfsbuf
->f_ffree
));
398 strcat (pformat
, "c");
404 /* print stat info */
406 print_stat (char *pformat
, char m
, char const *filename
, void const *data
)
408 struct stat
*statbuf
= (struct stat
*) data
;
409 struct passwd
*pw_ent
;
410 struct group
*gw_ent
;
415 strcat (pformat
, "s");
416 printf (pformat
, filename
);
419 strcat (pformat
, "s");
420 if (S_ISLNK (statbuf
->st_mode
))
422 char *linkname
= xreadlink (filename
);
423 if (linkname
== NULL
)
425 error (0, errno
, _("cannot read symbolic link %s"),
429 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
430 printf (pformat
, quote (filename
));
432 printf (pformat
, quote (linkname
));
436 printf (pformat
, quote (filename
));
440 strcat (pformat
, "d");
441 printf (pformat
, (int) statbuf
->st_dev
);
444 strcat (pformat
, "x");
445 printf (pformat
, (int) statbuf
->st_dev
);
448 strcat (pformat
, "d");
449 printf (pformat
, (int) statbuf
->st_ino
);
452 strcat (pformat
, "o");
453 printf (pformat
, statbuf
->st_mode
& 07777);
456 strcat (pformat
, "s");
457 printf (pformat
, human_access (statbuf
));
460 strcat (pformat
, "x");
461 printf (pformat
, statbuf
->st_mode
);
464 strcat (pformat
, "s");
465 printf (pformat
, file_type (statbuf
));
468 strcat (pformat
, "d");
469 printf (pformat
, (int) statbuf
->st_nlink
);
472 strcat (pformat
, "d");
473 printf (pformat
, statbuf
->st_uid
);
476 strcat (pformat
, "s");
478 pw_ent
= getpwuid (statbuf
->st_uid
);
479 printf (pformat
, (pw_ent
!= 0L) ? pw_ent
->pw_name
: "UNKNOWN");
482 strcat (pformat
, "d");
483 printf (pformat
, statbuf
->st_gid
);
486 strcat (pformat
, "s");
488 gw_ent
= getgrgid (statbuf
->st_gid
);
489 printf (pformat
, (gw_ent
!= 0L) ? gw_ent
->gr_name
: "UNKNOWN");
492 strcat (pformat
, "x");
493 printf (pformat
, major (statbuf
->st_rdev
));
496 strcat (pformat
, "x");
497 printf (pformat
, minor (statbuf
->st_rdev
));
500 strcat (pformat
, PRIuMAX
);
501 printf (pformat
, (uintmax_t) (statbuf
->st_size
));
504 strcat (pformat
, "u");
505 printf (pformat
, (unsigned int) ST_NBLOCKS (*statbuf
));
508 strcat (pformat
, "d");
509 printf (pformat
, (int) statbuf
->st_blksize
);
512 strcat (pformat
, "s");
513 printf (pformat
, human_time (&(statbuf
->st_atime
)));
516 strcat (pformat
, "d");
517 printf (pformat
, (int) statbuf
->st_atime
);
520 strcat (pformat
, "s");
521 printf (pformat
, human_time (&(statbuf
->st_mtime
)));
524 strcat (pformat
, "d");
525 printf (pformat
, (int) statbuf
->st_mtime
);
528 strcat (pformat
, "s");
529 printf (pformat
, human_time (&(statbuf
->st_ctime
)));
532 strcat (pformat
, "d");
533 printf (pformat
, (int) statbuf
->st_ctime
);
536 strcat (pformat
, "c");
543 print_it (char const *masterformat
, char const *filename
,
544 void (*print_func
) (char *, char, char const *, void const *),
549 /* create a working copy of the format string */
550 char *format
= xstrdup (masterformat
);
552 char *dest
= xmalloc (strlen (format
) + 1);
558 char *p
= strchr (b
, '%');
565 len
= strspn (p
, "#-+.I 0123456789");
567 memcpy (dest
+ 1, p
, len
);
578 print_func (dest
, *p
, filename
, data
);
591 fputc ('\n', stdout
);
594 /* stat the filesystem and print what we find */
596 do_statfs (char const *filename
, int terse
, char const *format
)
598 STRUCT_STATVFS statfsbuf
;
599 int i
= statfs (filename
, &statfsbuf
);
603 error (0, errno
, _("cannot read file system information for %s"),
611 ? "%n %i %l %t %b %f %a %s %c %d"
613 " ID: %-8i Namelen: %-7l Type: %T\n"
614 "Blocks: Total: %-10b Free: %-10f Available: %-10a Size: %s\n"
615 "Inodes: Total: %-10c Free: %-10d");
618 print_it (format
, filename
, print_statfs
, &statfsbuf
);
621 /* stat the file and print what we find */
623 do_stat (char const *filename
, int follow_links
, int terse
,
627 int i
= ((follow_links
== 1)
628 ? stat (filename
, &statbuf
)
629 : lstat (filename
, &statbuf
));
633 error (0, errno
, _("cannot stat %s"), quote (filename
));
641 format
= "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
645 /* tmp hack to match orignal output until conditional implemented */
646 i
= statbuf
.st_mode
& S_IFMT
;
647 if (i
== S_IFCHR
|| i
== S_IFBLK
)
651 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
652 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
653 " Device type: %t,%T\n"
654 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
655 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
661 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
662 "Device: %Dh/%dd\tInode: %-10i Links: %-5h\n"
663 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
664 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
668 print_it (format
, filename
, print_stat
, &statbuf
);
675 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
679 printf (_("Usage: %s [OPTION] FILE...\n"), program_name
);
681 Display file or filesystem status.\n\
683 -f, --filesystem display filesystem status instead of file status\n\
684 -c --format=FORMAT use the specified FORMAT instead of the default\n\
685 -L, --dereference follow links\n\
686 -t, --terse print the information in terse form\n\
688 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
689 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
692 The valid format sequences for files (without --filesystem):\n\
694 %A Access rights in human readable form\n\
695 %a Access rights in octal\n\
696 %b Number of blocks allocated\n\
699 %D Device number in hex\n\
700 %d Device number in decimal\n\
702 %f raw mode in hex\n\
703 %G Group name of owner\n\
704 %g Group ID of owner\n\
707 %h Number of hard links\n\
709 %N Quoted File name with dereference if symbolic link\n\
712 %s Total size, in bytes\n\
713 %T Minor device type in hex\n\
714 %t Major device type in hex\n\
717 %U User name of owner\n\
718 %u User ID of owner\n\
719 %X Time of last access as seconds since Epoch\n\
720 %x Time of last access\n\
721 %Y Time of last modification as seconds since Epoch\n\
722 %y Time of last modification\n\
723 %Z Time of last change as seconds since Epoch\n\
724 %z Time of last change\n\
729 Valid format sequences for file systems:\n\
731 %a Free blocks available to non-superuser\n\
732 %b Total data blocks in file system\n\
733 %c Total file nodes in file system\n\
734 %d Free file nodes in file system\n\
735 %f Free blocks in file system\n\
738 %i File System id in hex\n\
739 %l Maximum length of filenames\n\
741 %s Optimal transfer block size\n\
742 %T Type in human readable form\n\
745 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
751 main (int argc
, char *argv
[])
755 int follow_links
= 0;
760 program_name
= argv
[0];
761 setlocale (LC_ALL
, "");
762 bindtextdomain (PACKAGE
, LOCALEDIR
);
763 textdomain (PACKAGE
);
765 atexit (close_stdout
);
767 while ((c
= getopt_long (argc
, argv
, "c:fLlt", long_options
, NULL
)) != -1)
774 case 'l': /* deprecated */
785 case_GETOPT_HELP_CHAR
;
787 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
790 usage (EXIT_FAILURE
);
796 error (0, 0, _("too few arguments"));
797 usage (EXIT_FAILURE
);
800 for (i
= optind
; i
< argc
; i
++)
803 do_stat (argv
[i
], follow_links
, terse
, format
);
805 do_statfs (argv
[i
], terse
, format
);
808 exit (G_fail
? EXIT_FAILURE
: EXIT_SUCCESS
);