4 #ifdef HAVE_SYS_SYSMACROS_H
5 # include <sys/sysmacros.h>
19 #include "xreadlink.h"
21 #define PROGRAM_NAME "stat"
23 #define AUTHORS "Michael Meskes"
25 static struct option const long_options[] = {
26 {"link", no_argument, 0, 'l'},
27 {"filesystem", no_argument, 0, 'f'},
28 {"terse", no_argument, 0, 't'},
29 {GETOPT_HELP_OPTION_DECL},
30 {GETOPT_VERSION_OPTION_DECL},
34 static char *program_name;
36 /* stat the filesystem and print what we find */
38 do_statfs (char const *filename, int terse)
40 struct statfs statfsbuf;
43 if (statfs (filename, &statfsbuf) == -1)
45 error (0, errno, "%s", quotearg_colon (filename));
51 #ifdef __USE_FILE_OFFSET64
52 # define STATFSFMT "%s %x %x %lu %lx %lld %lld %lld %ld %lld %lld\n"
54 # define STATFSFMT "%s %x %x %d %x %ld %ld %ld %d %ld %ld\n"
58 statfsbuf.f_fsid.__val[0],
59 statfsbuf.f_fsid.__val[1],
65 statfsbuf.f_bsize, statfsbuf.f_files, statfsbuf.f_ffree);
70 printf (" File: \"%s\"\n", filename);
71 #ifdef __USE_FILE_OFFSET64
72 printf (" ID: %-8x %-8x Namelen: %-7ld Type: ",
73 statfsbuf.f_fsid.__val[0],
74 statfsbuf.f_fsid.__val[1], statfsbuf.f_namelen);
76 printf (" ID: %-8x %-8x Namelen: %-7d Type: ",
77 statfsbuf.f_fsid.__val[0],
78 statfsbuf.f_fsid.__val[1], statfsbuf.f_namelen);
81 name = fs_name (statfsbuf.f_type);
83 printf ("%s\n", name);
85 printf ("UNKNOWN (0x%lx)\n", (long) statfsbuf.f_type);
87 #ifdef __USE_FILE_OFFSET64
89 ("Blocks: Total: %-10lld Free: %-10lld Available: %-10lld Size:%ld \n",
90 statfsbuf.f_blocks, statfsbuf.f_bfree, statfsbuf.f_bavail,
92 printf ("Inodes: Total: %-10lld Free: %-10lld\n", statfsbuf.f_files,
95 printf ("Blocks: Total: %-10ld Free: %-10ld Available: %-10ld Size:%d\n",
97 statfsbuf.f_bfree, statfsbuf.f_bavail, statfsbuf.f_bsize);
98 printf ("Inodes: Total: %-10ld Free: %-10ld\n",
99 statfsbuf.f_files, statfsbuf.f_ffree);
103 /* stat the file and print what we find */
105 do_stat (char const *filename, int follow_links, int terse)
108 int err = (follow_links
109 ? stat (filename, &statbuf)
110 : lstat (filename, &statbuf));
112 struct passwd *pw_ent;
113 struct group *gw_ent;
117 error (0, errno, "%s", quotearg_colon (filename));
123 printf ("%s %u %u %x %d %d %x %d %d %x %x %d %d %d %d\n",
125 (unsigned int) statbuf.st_size,
126 (unsigned int) statbuf.st_blocks,
130 (int) statbuf.st_dev,
131 (int) statbuf.st_ino,
132 (int) statbuf.st_nlink,
133 major (statbuf.st_rdev),
134 minor (statbuf.st_rdev),
135 (int) statbuf.st_atime,
136 (int) statbuf.st_mtime,
137 (int) statbuf.st_ctime, (int) statbuf.st_blksize);
141 if (S_ISLNK (statbuf.st_mode))
143 char *linkname = xreadlink (filename);
145 printf (" File: \"%s\" -> \"%s\"\n", filename, linkname);
148 if (linkname == NULL)
150 error (0, errno, _("cannot read symbolic link %s"),
151 quotearg_colon (filename));
157 printf (" File: \"%s\"\n", filename);
159 printf (" Size: %-10u\tBlocks: %-10u IO Block: %-6d ",
160 (unsigned int) statbuf.st_size,
161 (unsigned int) statbuf.st_blocks, (int) statbuf.st_blksize);
163 switch (statbuf.st_mode & S_IFMT)
166 printf ("Directory\n");
169 printf ("Character Device\n");
172 printf ("Block Device\n");
175 printf ("Regular File\n");
178 printf ("Symbolic Link\n");
184 printf ("Fifo File\n");
187 printf ("Unknown\n");
190 printf ("Device: %xh/%dd\tInode: %-10d Links: %-5d",
191 (int) statbuf.st_dev,
192 (int) statbuf.st_dev, (int) statbuf.st_ino, (int) statbuf.st_nlink);
194 if (S_ISBLK (statbuf.st_mode) || S_ISCHR (statbuf.st_mode))
195 printf (" Device type: %x,%x\n",
196 major (statbuf.st_rdev), minor (statbuf.st_rdev));
200 mode_string (statbuf.st_mode, modebuf);
202 printf ("Access: (%04o/%10.10s)", statbuf.st_mode & 07777, modebuf);
206 pw_ent = getpwuid (statbuf.st_uid);
207 gw_ent = getgrgid (statbuf.st_gid);
208 printf (" Uid: (%5d/%8s) Gid: (%5d/%8s)\n",
210 (pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN",
211 statbuf.st_gid, (gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");
213 printf ("Access: %s", ctime (&statbuf.st_atime));
214 printf ("Modify: %s", ctime (&statbuf.st_mtime));
215 printf ("Change: %s\n", ctime (&statbuf.st_ctime));
222 fprintf (stderr, _("Try %s --help' for more information.\n"),
226 printf (_("Usage: %s [OPTION] FILE...\n"), program_name);
228 Display file or filesystem status.\n\
230 -l, --link follow links\n\
231 -f, --filesystem display filesystem status instead of file status\n\
232 -t, --terse print the information in terse form\n\
234 fputs (HELP_OPTION_DESCRIPTION, stdout);
235 fputs (VERSION_OPTION_DESCRIPTION, stdout);
236 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
242 main (int argc, char **argv)
246 int follow_links = 0;
250 program_name = argv[0];
251 setlocale (LC_ALL, "");
252 bindtextdomain (PACKAGE, LOCALEDIR);
253 textdomain (PACKAGE);
255 atexit (close_stdout);
256 while ((c = getopt_long (argc, argv, "lft", long_options, NULL)) != -1)
269 case_GETOPT_HELP_CHAR;
271 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
274 usage (EXIT_FAILURE);
280 error (0, 0, _("too few arguments"));
281 usage (EXIT_FAILURE);
284 for (i = optind; i < argc; i++)
287 do_stat (argv[i], follow_links, terse);
289 do_statfs (argv[i], terse);