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>
26 #if HAVE_SYS_STATVFS_H && HAVE_STRUCT_STATVFS_F_BASETYPE
27 # include <sys/statvfs.h>
30 #elif HAVE_SYS_MOUNT_H && HAVE_SYS_PARAM_H
31 /* NOTE: freebsd5.0 needs sys/param.h and sys/mount.h for statfs.
32 It does have statvfs.h, but shouldn't use it, since it doesn't
33 HAVE_STRUCT_STATVFS_F_BASETYPE. So find a clean way to fix it. */
34 /* NetBSD 1.5.2 needs these, for the declaration of struct statfs. */
35 # include <sys/param.h>
36 # include <sys/mount.h>
37 # if HAVE_NETINET_IN_H && HAVE_NFS_NFS_CLNT_H && HAVE_NFS_VFS_H
38 /* Ultrix 4.4 needs these for the declaration of struct statfs. */
39 # include <netinet/in.h>
40 # include <nfs/nfs_clnt.h>
49 #include "file-type.h"
55 #include "xreadlink.h"
57 #define NAMEMAX_FORMAT PRIuMAX
59 #if HAVE_STRUCT_STATVFS_F_BASETYPE
60 # define STRUCT_STATVFS struct statvfs
61 # define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATVFS
62 # if HAVE_STRUCT_STATVFS_F_NAMEMAX
63 # define SB_F_NAMEMAX(S) ((uintmax_t) ((S)->f_namemax))
66 # define STRUCT_STATVFS struct statfs
67 # define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATFS
68 # if HAVE_STRUCT_STATFS_F_NAMELEN
69 # define SB_F_NAMEMAX(S) ((uintmax_t) ((S)->f_namelen))
74 /* NetBSD 1.5.2 has neither f_namemax nor f_namelen. */
75 # define SB_F_NAMEMAX(S) "*"
76 # undef NAMEMAX_FORMAT
77 # define NAMEMAX_FORMAT "s"
80 #if HAVE_STRUCT_STATVFS_F_BASETYPE
81 # define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME f_basetype
83 # if HAVE_STRUCT_STATFS_F_FSTYPENAME
84 # define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME f_fstypename
88 #define PROGRAM_NAME "stat"
90 #define WRITTEN_BY _("Written by Michael Meskes.")
92 static struct option const long_options[] = {
93 {"link", no_argument, 0, 'l'}, /* deprecated. FIXME: remove in 2003 */
94 {"dereference", no_argument, 0, 'L'},
95 {"format", required_argument, 0, 'c'},
96 {"filesystem", no_argument, 0, 'f'},
97 {"terse", no_argument, 0, 't'},
98 {GETOPT_HELP_OPTION_DECL},
99 {GETOPT_VERSION_OPTION_DECL},
103 /* Nonzero means we should exit with EXIT_FAILURE upon completion. */
108 /* Return the type of the specified file system.
109 Some systems have statfvs.f_basetype[FSTYPSZ]. (AIX, HP-UX, and Solaris)
110 Others have statfs.f_fstypename[MFSNAMELEN]. (NetBSD 1.5.2)
111 Still others have neither and have to get by with f_type (Linux). */
113 human_fstype (STRUCT_STATVFS const *statfsbuf)
115 #ifdef STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME
116 /* Cast away the `const' attribute. */
117 return (char *) statfsbuf->STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME;
120 switch (statfsbuf->f_type)
122 # if defined __linux__
124 /* IMPORTANT NOTE: Each of the following `case S_MAGIC_...:'
125 statements must be followed by a hexadecimal constant in
126 a comment. The S_MAGIC_... name and constant are automatically
127 combined to produce the #define directives in fs.h. */
129 case S_MAGIC_AFFS: /* 0xADFF */
132 case S_MAGIC_DEVPTS: /* 0x1CD1 */
135 case S_MAGIC_EXT: /* 0x137D */
138 case S_MAGIC_EXT2_OLD: /* 0xEF51 */
141 case S_MAGIC_EXT2: /* 0xEF53 */
144 case S_MAGIC_HPFS: /* 0xF995E849 */
147 case S_MAGIC_ISOFS: /* 0x9660 */
150 case S_MAGIC_ISOFS_WIN: /* 0x4000 */
153 case S_MAGIC_ISOFS_R_WIN: /* 0x4004 */
156 case S_MAGIC_MINIX: /* 0x137F */
159 case S_MAGIC_MINIX_30: /* 0x138F */
160 type = "minix (30 char.)";
162 case S_MAGIC_MINIX_V2: /* 0x2468 */
165 case S_MAGIC_MINIX_V2_30: /* 0x2478 */
166 type = "minix v2 (30 char.)";
168 case S_MAGIC_MSDOS: /* 0x4d44 */
171 case S_MAGIC_FAT: /* 0x4006 */
174 case S_MAGIC_NCP: /* 0x564c */
177 case S_MAGIC_NFS: /* 0x6969 */
180 case S_MAGIC_PROC: /* 0x9fa0 */
183 case S_MAGIC_SMB: /* 0x517B */
186 case S_MAGIC_XENIX: /* 0x012FF7B4 */
189 case S_MAGIC_SYSV4: /* 0x012FF7B5 */
192 case S_MAGIC_SYSV2: /* 0x012FF7B6 */
195 case S_MAGIC_COH: /* 0x012FF7B7 */
198 case S_MAGIC_UFS: /* 0x00011954 */
201 case S_MAGIC_XIAFS: /* 0x012FD16D */
204 case S_MAGIC_NTFS: /* 0x5346544e */
207 case S_MAGIC_TMPFS: /* 0x1021994 */
210 case S_MAGIC_REISERFS: /* 0x52654973 */
213 case S_MAGIC_CRAMFS: /* 0x28cd3d45 */
216 case S_MAGIC_ROMFS: /* 0x7275 */
308 return (char *) type;
311 static char buf[sizeof "UNKNOWN (0x%x)" - 2
312 + 2 * sizeof (statfsbuf->f_type)];
313 sprintf (buf, "UNKNOWN (0x%x)", statfsbuf->f_type);
320 human_access (struct stat const *statbuf)
322 static char modebuf[11];
323 mode_string (statbuf->st_mode, modebuf);
329 human_time (time_t const *t)
332 struct tm *tm = localtime (t);
336 return (char *) _("*** invalid date/time ***");
338 nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", tm, 0, 0);
342 /* print statfs info */
344 print_statfs (char *pformat, char m, char const *filename,
347 STRUCT_STATVFS const *statfsbuf = data;
352 strcat (pformat, "s");
353 printf (pformat, filename);
357 #if HAVE_STRUCT_STATXFS_F_FSID___VAL
358 strcat (pformat, "x %-8x");
359 printf (pformat, statfsbuf->f_fsid.__val[0], /* u_long */
360 statfsbuf->f_fsid.__val[1]);
362 strcat (pformat, "Lx");
363 printf (pformat, statfsbuf->f_fsid);
368 strcat (pformat, NAMEMAX_FORMAT);
369 printf (pformat, SB_F_NAMEMAX (statfsbuf));
372 #if HAVE_STRUCT_STATXFS_F_TYPE
373 strcat (pformat, "lx");
374 printf (pformat, (long int) (statfsbuf->f_type)); /* no equiv. */
380 strcat (pformat, "s");
381 printf (pformat, human_fstype (statfsbuf));
384 strcat (pformat, PRIdMAX);
385 printf (pformat, (intmax_t) (statfsbuf->f_blocks));
388 strcat (pformat, PRIdMAX);
389 printf (pformat, (intmax_t) (statfsbuf->f_bfree));
392 strcat (pformat, PRIdMAX);
393 printf (pformat, (intmax_t) (statfsbuf->f_bavail));
396 strcat (pformat, "ld");
397 printf (pformat, (long int) (statfsbuf->f_bsize));
400 strcat (pformat, PRIdMAX);
401 printf (pformat, (intmax_t) (statfsbuf->f_files));
404 strcat (pformat, PRIdMAX);
405 printf (pformat, (intmax_t) (statfsbuf->f_ffree));
409 strcat (pformat, "c");
415 /* print stat info */
417 print_stat (char *pformat, char m, char const *filename, void const *data)
419 struct stat *statbuf = (struct stat *) data;
420 struct passwd *pw_ent;
421 struct group *gw_ent;
426 strcat (pformat, "s");
427 printf (pformat, filename);
430 strcat (pformat, "s");
431 if (S_ISLNK (statbuf->st_mode))
433 char *linkname = xreadlink (filename);
434 if (linkname == NULL)
436 error (0, errno, _("cannot read symbolic link %s"),
440 /*printf("\"%s\" -> \"%s\"", filename, linkname); */
441 printf (pformat, quote (filename));
443 printf (pformat, quote (linkname));
447 printf (pformat, quote (filename));
451 strcat (pformat, "d");
452 printf (pformat, (int) statbuf->st_dev);
455 strcat (pformat, "x");
456 printf (pformat, (int) statbuf->st_dev);
459 strcat (pformat, "d");
460 printf (pformat, (int) statbuf->st_ino);
463 strcat (pformat, "o");
464 printf (pformat, statbuf->st_mode & 07777);
467 strcat (pformat, "s");
468 printf (pformat, human_access (statbuf));
471 strcat (pformat, "x");
472 printf (pformat, statbuf->st_mode);
475 strcat (pformat, "s");
476 printf (pformat, file_type (statbuf));
479 strcat (pformat, "d");
480 printf (pformat, (int) statbuf->st_nlink);
483 strcat (pformat, "d");
484 printf (pformat, statbuf->st_uid);
487 strcat (pformat, "s");
489 pw_ent = getpwuid (statbuf->st_uid);
490 printf (pformat, (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN");
493 strcat (pformat, "d");
494 printf (pformat, statbuf->st_gid);
497 strcat (pformat, "s");
499 gw_ent = getgrgid (statbuf->st_gid);
500 printf (pformat, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
503 strcat (pformat, "x");
504 printf (pformat, major (statbuf->st_rdev));
507 strcat (pformat, "x");
508 printf (pformat, minor (statbuf->st_rdev));
511 strcat (pformat, PRIuMAX);
512 printf (pformat, (uintmax_t) (statbuf->st_size));
515 strcat (pformat, "u");
516 printf (pformat, (unsigned int) ST_NBLOCKSIZE);
519 strcat (pformat, "u");
520 printf (pformat, (unsigned int) ST_NBLOCKS (*statbuf));
523 strcat (pformat, "d");
524 printf (pformat, (int) statbuf->st_blksize);
527 strcat (pformat, "s");
528 printf (pformat, human_time (&(statbuf->st_atime)));
531 strcat (pformat, "d");
532 printf (pformat, (int) statbuf->st_atime);
535 strcat (pformat, "s");
536 printf (pformat, human_time (&(statbuf->st_mtime)));
539 strcat (pformat, "d");
540 printf (pformat, (int) statbuf->st_mtime);
543 strcat (pformat, "s");
544 printf (pformat, human_time (&(statbuf->st_ctime)));
547 strcat (pformat, "d");
548 printf (pformat, (int) statbuf->st_ctime);
551 strcat (pformat, "c");
558 print_it (char const *masterformat, char const *filename,
559 void (*print_func) (char *, char, char const *, void const *),
564 /* create a working copy of the format string */
565 char *format = xstrdup (masterformat);
567 char *dest = xmalloc (strlen (format) + 1);
572 char *p = strchr (b, '%');
579 len = strspn (p, "#-+.I 0123456789");
581 memcpy (dest + 1, p, len);
595 print_func (dest, *p, filename, data);
607 fputc ('\n', stdout);
610 /* stat the filesystem and print what we find */
612 do_statfs (char const *filename, int terse, char const *format)
614 STRUCT_STATVFS statfsbuf;
615 int i = statfs (filename, &statfsbuf);
619 error (0, errno, _("cannot read file system information for %s"),
627 ? "%n %i %l %t %b %f %a %s %c %d"
629 " ID: %-8i Namelen: %-7l Type: %T\n"
630 "Blocks: Total: %-10b Free: %-10f Available: %-10a Size: %s\n"
631 "Inodes: Total: %-10c Free: %-10d");
634 print_it (format, filename, print_statfs, &statfsbuf);
637 /* stat the file and print what we find */
639 do_stat (char const *filename, int follow_links, int terse,
643 int i = ((follow_links == 1)
644 ? stat (filename, &statbuf)
645 : lstat (filename, &statbuf));
649 error (0, errno, _("cannot stat %s"), quote (filename));
657 format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %o";
661 /* tmp hack to match orignal output until conditional implemented */
662 i = statbuf.st_mode & S_IFMT;
663 if (i == S_IFCHR || i == S_IFBLK)
667 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
668 "Device: %Dh/%dd\tInode: %-10i Links: %-5h"
669 " Device type: %t,%T\n"
670 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
671 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
677 " Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
678 "Device: %Dh/%dd\tInode: %-10i Links: %h\n"
679 "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
680 "Access: %x\n" "Modify: %y\n" "Change: %z\n";
684 print_it (format, filename, print_stat, &statbuf);
691 fprintf (stderr, _("Try `%s --help' for more information.\n"),
695 printf (_("Usage: %s [OPTION] FILE...\n"), program_name);
697 Display file or filesystem status.\n\
699 -f, --filesystem display filesystem status instead of file status\n\
700 -c --format=FORMAT use the specified FORMAT instead of the default\n\
701 -L, --dereference follow links\n\
702 -t, --terse print the information in terse form\n\
704 fputs (HELP_OPTION_DESCRIPTION, stdout);
705 fputs (VERSION_OPTION_DESCRIPTION, stdout);
708 The valid format sequences for files (without --filesystem):\n\
710 %A Access rights in human readable form\n\
711 %a Access rights in octal\n\
712 %B The size in bytes of each block reported by `%b'\n\
713 %b Number of blocks allocated (see %B)\n\
716 %D Device number in hex\n\
717 %d Device number in decimal\n\
719 %f Raw mode in hex\n\
720 %G Group name of owner\n\
721 %g Group ID of owner\n\
724 %h Number of hard links\n\
726 %N Quoted File name with dereference if symbolic link\n\
729 %s Total size, in bytes\n\
730 %T Minor device type in hex\n\
731 %t Major device type in hex\n\
734 %U User name of owner\n\
735 %u User ID of owner\n\
736 %X Time of last access as seconds since Epoch\n\
737 %x Time of last access\n\
738 %Y Time of last modification as seconds since Epoch\n\
739 %y Time of last modification\n\
740 %Z Time of last change as seconds since Epoch\n\
741 %z Time of last change\n\
746 Valid format sequences for file systems:\n\
748 %a Free blocks available to non-superuser\n\
749 %b Total data blocks in file system\n\
750 %c Total file nodes in file system\n\
751 %d Free file nodes in file system\n\
752 %f Free blocks in file system\n\
755 %i File System id in hex\n\
756 %l Maximum length of filenames\n\
758 %s Optimal transfer block size\n\
759 %T Type in human readable form\n\
762 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
768 main (int argc, char *argv[])
772 int follow_links = 0;
777 initialize_main (&argc, &argv);
778 program_name = argv[0];
779 setlocale (LC_ALL, "");
780 bindtextdomain (PACKAGE, LOCALEDIR);
781 textdomain (PACKAGE);
783 atexit (close_stdout);
785 while ((c = getopt_long (argc, argv, "c:fLlt", long_options, NULL)) != -1)
793 case 'l': /* deprecated */
794 error (0, 0, _("Warning: `-l' is deprecated; use `-L' instead"));
808 case_GETOPT_HELP_CHAR;
810 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, WRITTEN_BY);
813 usage (EXIT_FAILURE);
819 error (0, 0, _("too few arguments"));
820 usage (EXIT_FAILURE);
823 for (i = optind; i < argc; i++)
826 do_stat (argv[i], follow_links, terse, format);
828 do_statfs (argv[i], terse, format);
831 exit (G_fail ? EXIT_FAILURE : EXIT_SUCCESS);