stat,tail: improve support for efivarfs, exofs, f2fs and ubifs
[coreutils.git] / src / stat.c
blob8ba958a02f07e18cd44032cefe73b437ec41cbc0
1 /* stat.c -- display file or file system status
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
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 3 of the License, or
7 (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
17 Written by Michael Meskes. */
19 #include <config.h>
21 /* Keep this conditional in sync with the similar conditional in
22 ../m4/stat-prog.m4. */
23 #if ((STAT_STATVFS || STAT_STATVFS64) \
24 && (HAVE_STRUCT_STATVFS_F_BASETYPE || HAVE_STRUCT_STATVFS_F_FSTYPENAME \
25 || (! HAVE_STRUCT_STATFS_F_FSTYPENAME && HAVE_STRUCT_STATVFS_F_TYPE)))
26 # define USE_STATVFS 1
27 #else
28 # define USE_STATVFS 0
29 #endif
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdalign.h>
34 #include <sys/types.h>
35 #include <pwd.h>
36 #include <grp.h>
37 #if USE_STATVFS
38 # include <sys/statvfs.h>
39 #elif HAVE_SYS_VFS_H
40 # include <sys/vfs.h>
41 #elif HAVE_SYS_MOUNT_H && HAVE_SYS_PARAM_H
42 /* NOTE: freebsd5.0 needs sys/param.h and sys/mount.h for statfs.
43 It does have statvfs.h, but shouldn't use it, since it doesn't
44 HAVE_STRUCT_STATVFS_F_BASETYPE. So find a clean way to fix it. */
45 /* NetBSD 1.5.2 needs these, for the declaration of struct statfs. */
46 # include <sys/param.h>
47 # include <sys/mount.h>
48 # if HAVE_NFS_NFS_CLNT_H && HAVE_NFS_VFS_H
49 /* Ultrix 4.4 needs these for the declaration of struct statfs. */
50 # include <netinet/in.h>
51 # include <nfs/nfs_clnt.h>
52 # include <nfs/vfs.h>
53 # endif
54 #elif HAVE_OS_H /* BeOS */
55 # include <fs_info.h>
56 #endif
57 #include <selinux/selinux.h>
59 #include "system.h"
61 #include "areadlink.h"
62 #include "error.h"
63 #include "file-type.h"
64 #include "filemode.h"
65 #include "fs.h"
66 #include "getopt.h"
67 #include "mountlist.h"
68 #include "quote.h"
69 #include "quotearg.h"
70 #include "stat-size.h"
71 #include "stat-time.h"
72 #include "strftime.h"
73 #include "find-mount-point.h"
74 #include "xvasprintf.h"
76 #if USE_STATVFS
77 # define STRUCT_STATVFS struct statvfs
78 # define STRUCT_STATXFS_F_FSID_IS_INTEGER STRUCT_STATVFS_F_FSID_IS_INTEGER
79 # define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATVFS_F_TYPE
80 # if HAVE_STRUCT_STATVFS_F_NAMEMAX
81 # define SB_F_NAMEMAX(S) ((S)->f_namemax)
82 # endif
83 # if ! STAT_STATVFS && STAT_STATVFS64
84 # define STATFS statvfs64
85 # else
86 # define STATFS statvfs
87 # endif
88 # define STATFS_FRSIZE(S) ((S)->f_frsize)
89 #else
90 # define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATFS_F_TYPE
91 # if HAVE_STRUCT_STATFS_F_NAMELEN
92 # define SB_F_NAMEMAX(S) ((S)->f_namelen)
93 # endif
94 # define STATFS statfs
95 # if HAVE_OS_H /* BeOS */
96 /* BeOS has a statvfs function, but it does not return sensible values
97 for f_files, f_ffree and f_favail, and lacks f_type, f_basetype and
98 f_fstypename. Use 'struct fs_info' instead. */
99 static int ATTRIBUTE_WARN_UNUSED_RESULT
100 statfs (char const *filename, struct fs_info *buf)
102 dev_t device = dev_for_path (filename);
103 if (device < 0)
105 errno = (device == B_ENTRY_NOT_FOUND ? ENOENT
106 : device == B_BAD_VALUE ? EINVAL
107 : device == B_NAME_TOO_LONG ? ENAMETOOLONG
108 : device == B_NO_MEMORY ? ENOMEM
109 : device == B_FILE_ERROR ? EIO
110 : 0);
111 return -1;
113 /* If successful, buf->dev will be == device. */
114 return fs_stat_dev (device, buf);
116 # define f_fsid dev
117 # define f_blocks total_blocks
118 # define f_bfree free_blocks
119 # define f_bavail free_blocks
120 # define f_bsize io_size
121 # define f_files total_nodes
122 # define f_ffree free_nodes
123 # define STRUCT_STATVFS struct fs_info
124 # define STRUCT_STATXFS_F_FSID_IS_INTEGER true
125 # define STATFS_FRSIZE(S) ((S)->block_size)
126 # else
127 # define STRUCT_STATVFS struct statfs
128 # define STRUCT_STATXFS_F_FSID_IS_INTEGER STRUCT_STATFS_F_FSID_IS_INTEGER
129 # if HAVE_STRUCT_STATFS_F_FRSIZE
130 # define STATFS_FRSIZE(S) ((S)->f_frsize)
131 # else
132 # define STATFS_FRSIZE(S) 0
133 # endif
134 # endif
135 #endif
137 #ifdef SB_F_NAMEMAX
138 # define OUT_NAMEMAX out_uint
139 #else
140 /* NetBSD 1.5.2 has neither f_namemax nor f_namelen. */
141 # define SB_F_NAMEMAX(S) "*"
142 # define OUT_NAMEMAX out_string
143 #endif
145 #if HAVE_STRUCT_STATVFS_F_BASETYPE
146 # define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME f_basetype
147 #else
148 # if HAVE_STRUCT_STATVFS_F_FSTYPENAME || HAVE_STRUCT_STATFS_F_FSTYPENAME
149 # define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME f_fstypename
150 # elif HAVE_OS_H /* BeOS */
151 # define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME fsh_name
152 # endif
153 #endif
155 /* FIXME: these are used by printf.c, too */
156 #define isodigit(c) ('0' <= (c) && (c) <= '7')
157 #define octtobin(c) ((c) - '0')
158 #define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
159 (c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
161 static char const digits[] = "0123456789";
163 /* Flags that are portable for use in printf, for at least one
164 conversion specifier; make_format removes unportable flags as
165 needed for particular specifiers. The glibc 2.2 extension "I" is
166 listed here; it is removed by make_format because it has undefined
167 behavior elsewhere and because it is incompatible with
168 out_epoch_sec. */
169 static char const printf_flags[] = "'-+ #0I";
171 #define PROGRAM_NAME "stat"
173 #define AUTHORS proper_name ("Michael Meskes")
175 enum
177 PRINTF_OPTION = CHAR_MAX + 1
180 static struct option const long_options[] =
182 {"context", no_argument, 0, 'Z'},
183 {"dereference", no_argument, NULL, 'L'},
184 {"file-system", no_argument, NULL, 'f'},
185 {"format", required_argument, NULL, 'c'},
186 {"printf", required_argument, NULL, PRINTF_OPTION},
187 {"terse", no_argument, NULL, 't'},
188 {GETOPT_HELP_OPTION_DECL},
189 {GETOPT_VERSION_OPTION_DECL},
190 {NULL, 0, NULL, 0}
193 /* Whether to follow symbolic links; True for --dereference (-L). */
194 static bool follow_links;
196 /* Whether to interpret backslash-escape sequences.
197 True for --printf=FMT, not for --format=FMT (-c). */
198 static bool interpret_backslash_escapes;
200 /* The trailing delimiter string:
201 "" for --printf=FMT, "\n" for --format=FMT (-c). */
202 static char const *trailing_delim = "";
204 /* The representation of the decimal point in the current locale. */
205 static char const *decimal_point;
206 static size_t decimal_point_len;
208 /* Return the type of the specified file system.
209 Some systems have statfvs.f_basetype[FSTYPSZ] (AIX, HP-UX, and Solaris).
210 Others have statvfs.f_fstypename[_VFS_NAMELEN] (NetBSD 3.0).
211 Others have statfs.f_fstypename[MFSNAMELEN] (NetBSD 1.5.2).
212 Still others have neither and have to get by with f_type (GNU/Linux).
213 But f_type may only exist in statfs (Cygwin). */
214 static char const * ATTRIBUTE_WARN_UNUSED_RESULT
215 human_fstype (STRUCT_STATVFS const *statfsbuf)
217 #ifdef STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME
218 return statfsbuf->STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME;
219 #else
220 switch (statfsbuf->f_type)
222 # if defined __linux__
224 /* Compare with what's in libc:
225 f=/a/libc/sysdeps/unix/sysv/linux/linux_fsinfo.h
226 sed -n '/ADFS_SUPER_MAGIC/,/SYSFS_MAGIC/p' $f \
227 | perl -n -e '/#define (.*?)_(?:SUPER_)MAGIC\s+0x(\S+)/' \
228 -e 'and print "case S_MAGIC_$1: /\* 0x" . uc($2) . " *\/\n"' \
229 | sort > sym_libc
230 perl -ne '/^\s+(case S_MAGIC_.*?): \/\* 0x(\S+) \*\//' \
231 -e 'and do { $v=uc$2; print "$1: /\* 0x$v *\/\n"}' stat.c \
232 | sort > sym_stat
233 diff -u sym_stat sym_libc
236 /* Also compare with the list in "man 2 statfs" using the
237 fs-magic-compare make target. */
239 /* IMPORTANT NOTE: Each of the following 'case S_MAGIC_...:'
240 statements must be followed by a hexadecimal constant in
241 a comment. The S_MAGIC_... name and constant are automatically
242 combined to produce the #define directives in fs.h. */
244 case S_MAGIC_ADFS: /* 0xADF5 local */
245 return "adfs";
246 case S_MAGIC_AFFS: /* 0xADFF local */
247 return "affs";
248 case S_MAGIC_AFS: /* 0x5346414F remote */
249 return "afs";
250 case S_MAGIC_ANON_INODE_FS: /* 0x09041934 local */
251 return "anon-inode FS";
252 case S_MAGIC_AUFS: /* 0x61756673 remote */
253 /* FIXME: change syntax or add an optional attribute like "inotify:no".
254 The above is labeled as "remote" so that tail always uses polling,
255 but this isn't really a remote file system type. */
256 return "aufs";
257 case S_MAGIC_AUTOFS: /* 0x0187 local */
258 return "autofs";
259 case S_MAGIC_BEFS: /* 0x42465331 local */
260 return "befs";
261 case S_MAGIC_BDEVFS: /* 0x62646576 local */
262 return "bdevfs";
263 case S_MAGIC_BFS: /* 0x1BADFACE local */
264 return "bfs";
265 case S_MAGIC_BINFMTFS: /* 0x42494E4D local */
266 return "binfmt_misc";
267 case S_MAGIC_BTRFS: /* 0x9123683E local */
268 return "btrfs";
269 case S_MAGIC_CEPH: /* 0x00C36400 remote */
270 return "ceph";
271 case S_MAGIC_CGROUP: /* 0x0027E0EB local */
272 return "cgroupfs";
273 case S_MAGIC_CIFS: /* 0xFF534D42 remote */
274 return "cifs";
275 case S_MAGIC_CODA: /* 0x73757245 remote */
276 return "coda";
277 case S_MAGIC_COH: /* 0x012FF7B7 local */
278 return "coh";
279 case S_MAGIC_CRAMFS: /* 0x28CD3D45 local */
280 return "cramfs";
281 case S_MAGIC_CRAMFS_WEND: /* 0x453DCD28 local */
282 return "cramfs-wend";
283 case S_MAGIC_DEBUGFS: /* 0x64626720 local */
284 return "debugfs";
285 case S_MAGIC_DEVFS: /* 0x1373 local */
286 return "devfs";
287 case S_MAGIC_DEVPTS: /* 0x1CD1 local */
288 return "devpts";
289 case S_MAGIC_ECRYPTFS: /* 0xF15F local */
290 return "ecryptfs";
291 case S_MAGIC_EFIVARFS: /* 0xDE5E81E4 local */
292 return "efivarfs";
293 case S_MAGIC_EFS: /* 0x00414A53 local */
294 return "efs";
295 case S_MAGIC_EXOFS: /* 0x5DF5 local */
296 return "exofs";
297 case S_MAGIC_EXT: /* 0x137D local */
298 return "ext";
299 case S_MAGIC_EXT2: /* 0xEF53 local */
300 return "ext2/ext3";
301 case S_MAGIC_EXT2_OLD: /* 0xEF51 local */
302 return "ext2";
303 case S_MAGIC_F2FS: /* 0xF2F52010 local */
304 return "f2fs";
305 case S_MAGIC_FAT: /* 0x4006 local */
306 return "fat";
307 case S_MAGIC_FHGFS: /* 0x19830326 remote */
308 return "fhgfs";
309 case S_MAGIC_FUSEBLK: /* 0x65735546 remote */
310 return "fuseblk";
311 case S_MAGIC_FUSECTL: /* 0x65735543 remote */
312 return "fusectl";
313 case S_MAGIC_FUTEXFS: /* 0x0BAD1DEA local */
314 return "futexfs";
315 case S_MAGIC_GFS: /* 0x1161970 remote */
316 return "gfs/gfs2";
317 case S_MAGIC_GPFS: /* 0x47504653 remote */
318 return "gpfs";
319 case S_MAGIC_HFS: /* 0x4244 local */
320 return "hfs";
321 case S_MAGIC_HPFS: /* 0xF995E849 local */
322 return "hpfs";
323 case S_MAGIC_HUGETLBFS: /* 0x958458F6 local */
324 return "hugetlbfs";
325 case S_MAGIC_MTD_INODE_FS: /* 0x11307854 local */
326 return "inodefs";
327 case S_MAGIC_INOTIFYFS: /* 0x2BAD1DEA local */
328 return "inotifyfs";
329 case S_MAGIC_ISOFS: /* 0x9660 local */
330 return "isofs";
331 case S_MAGIC_ISOFS_R_WIN: /* 0x4004 local */
332 return "isofs";
333 case S_MAGIC_ISOFS_WIN: /* 0x4000 local */
334 return "isofs";
335 case S_MAGIC_JFFS: /* 0x07C0 local */
336 return "jffs";
337 case S_MAGIC_JFFS2: /* 0x72B6 local */
338 return "jffs2";
339 case S_MAGIC_JFS: /* 0x3153464A local */
340 return "jfs";
341 case S_MAGIC_KAFS: /* 0x6B414653 remote */
342 return "k-afs";
343 case S_MAGIC_LUSTRE: /* 0x0BD00BD0 remote */
344 return "lustre";
345 case S_MAGIC_MINIX: /* 0x137F local */
346 return "minix";
347 case S_MAGIC_MINIX_30: /* 0x138F local */
348 return "minix (30 char.)";
349 case S_MAGIC_MINIX_V2: /* 0x2468 local */
350 return "minix v2";
351 case S_MAGIC_MINIX_V2_30: /* 0x2478 local */
352 return "minix v2 (30 char.)";
353 case S_MAGIC_MINIX_V3: /* 0x4D5A local */
354 return "minix3";
355 case S_MAGIC_MQUEUE: /* 0x19800202 local */
356 return "mqueue";
357 case S_MAGIC_MSDOS: /* 0x4D44 local */
358 return "msdos";
359 case S_MAGIC_NCP: /* 0x564C remote */
360 return "novell";
361 case S_MAGIC_NFS: /* 0x6969 remote */
362 return "nfs";
363 case S_MAGIC_NFSD: /* 0x6E667364 remote */
364 return "nfsd";
365 case S_MAGIC_NILFS: /* 0x3434 local */
366 return "nilfs";
367 case S_MAGIC_NTFS: /* 0x5346544E local */
368 return "ntfs";
369 case S_MAGIC_OPENPROM: /* 0x9FA1 local */
370 return "openprom";
371 case S_MAGIC_OCFS2: /* 0x7461636f remote */
372 return "ocfs2";
373 case S_MAGIC_PANFS: /* 0xAAD7AAEA remote */
374 return "panfs";
375 case S_MAGIC_PIPEFS: /* 0x50495045 remote */
376 /* FIXME: change syntax or add an optional attribute like "inotify:no".
377 The above is labeled as "remote" so that tail always uses polling,
378 but this isn't really a remote file system type. */
379 return "pipefs";
380 case S_MAGIC_PROC: /* 0x9FA0 local */
381 return "proc";
382 case S_MAGIC_PSTOREFS: /* 0x6165676C local */
383 return "pstorefs";
384 case S_MAGIC_QNX4: /* 0x002F local */
385 return "qnx4";
386 case S_MAGIC_QNX6: /* 0x68191122 local */
387 return "qnx6";
388 case S_MAGIC_RAMFS: /* 0x858458F6 local */
389 return "ramfs";
390 case S_MAGIC_REISERFS: /* 0x52654973 local */
391 return "reiserfs";
392 case S_MAGIC_ROMFS: /* 0x7275 local */
393 return "romfs";
394 case S_MAGIC_RPC_PIPEFS: /* 0x67596969 local */
395 return "rpc_pipefs";
396 case S_MAGIC_SECURITYFS: /* 0x73636673 local */
397 return "securityfs";
398 case S_MAGIC_SELINUX: /* 0xF97CFF8C local */
399 return "selinux";
400 case S_MAGIC_SMB: /* 0x517B remote */
401 return "smb";
402 case S_MAGIC_SOCKFS: /* 0x534F434B local */
403 return "sockfs";
404 case S_MAGIC_SQUASHFS: /* 0x73717368 local */
405 return "squashfs";
406 case S_MAGIC_SYSFS: /* 0x62656572 local */
407 return "sysfs";
408 case S_MAGIC_SYSV2: /* 0x012FF7B6 local */
409 return "sysv2";
410 case S_MAGIC_SYSV4: /* 0x012FF7B5 local */
411 return "sysv4";
412 case S_MAGIC_TMPFS: /* 0x01021994 local */
413 return "tmpfs";
414 case S_MAGIC_UBIFS: /* 0x24051905 local */
415 return "ubifs";
416 case S_MAGIC_UDF: /* 0x15013346 local */
417 return "udf";
418 case S_MAGIC_UFS: /* 0x00011954 local */
419 return "ufs";
420 case S_MAGIC_UFS_BYTESWAPPED: /* 0x54190100 local */
421 return "ufs";
422 case S_MAGIC_USBDEVFS: /* 0x9FA2 local */
423 return "usbdevfs";
424 case S_MAGIC_V9FS: /* 0x01021997 local */
425 return "v9fs";
426 case S_MAGIC_VMHGFS: /* 0xBACBACBC remote */
427 return "vmhgfs";
428 case S_MAGIC_VXFS: /* 0xA501FCF5 local */
429 return "vxfs";
430 case S_MAGIC_VZFS: /* 0x565A4653 local */
431 return "vzfs";
432 case S_MAGIC_XENFS: /* 0xABBA1974 local */
433 return "xenfs";
434 case S_MAGIC_XENIX: /* 0x012FF7B4 local */
435 return "xenix";
436 case S_MAGIC_XFS: /* 0x58465342 local */
437 return "xfs";
438 case S_MAGIC_XIAFS: /* 0x012FD16D local */
439 return "xia";
440 case S_MAGIC_ZFS: /* 0x2FC12FC1 local */
441 return "zfs";
443 # elif __GNU__
444 case FSTYPE_UFS:
445 return "ufs";
446 case FSTYPE_NFS:
447 return "nfs";
448 case FSTYPE_GFS:
449 return "gfs";
450 case FSTYPE_LFS:
451 return "lfs";
452 case FSTYPE_SYSV:
453 return "sysv";
454 case FSTYPE_FTP:
455 return "ftp";
456 case FSTYPE_TAR:
457 return "tar";
458 case FSTYPE_AR:
459 return "ar";
460 case FSTYPE_CPIO:
461 return "cpio";
462 case FSTYPE_MSLOSS:
463 return "msloss";
464 case FSTYPE_CPM:
465 return "cpm";
466 case FSTYPE_HFS:
467 return "hfs";
468 case FSTYPE_DTFS:
469 return "dtfs";
470 case FSTYPE_GRFS:
471 return "grfs";
472 case FSTYPE_TERM:
473 return "term";
474 case FSTYPE_DEV:
475 return "dev";
476 case FSTYPE_PROC:
477 return "proc";
478 case FSTYPE_IFSOCK:
479 return "ifsock";
480 case FSTYPE_AFS:
481 return "afs";
482 case FSTYPE_DFS:
483 return "dfs";
484 case FSTYPE_PROC9:
485 return "proc9";
486 case FSTYPE_SOCKET:
487 return "socket";
488 case FSTYPE_MISC:
489 return "misc";
490 case FSTYPE_EXT2FS:
491 return "ext2/ext3";
492 case FSTYPE_HTTP:
493 return "http";
494 case FSTYPE_MEMFS:
495 return "memfs";
496 case FSTYPE_ISO9660:
497 return "iso9660";
498 # endif
499 default:
501 unsigned long int type = statfsbuf->f_type;
502 static char buf[sizeof "UNKNOWN (0x%lx)" - 3
503 + (sizeof type * CHAR_BIT + 3) / 4];
504 sprintf (buf, "UNKNOWN (0x%lx)", type);
505 return buf;
508 #endif
511 static char * ATTRIBUTE_WARN_UNUSED_RESULT
512 human_access (struct stat const *statbuf)
514 static char modebuf[12];
515 filemodestring (statbuf, modebuf);
516 modebuf[10] = 0;
517 return modebuf;
520 static char * ATTRIBUTE_WARN_UNUSED_RESULT
521 human_time (struct timespec t)
523 static char str[MAX (INT_BUFSIZE_BOUND (intmax_t),
524 (INT_STRLEN_BOUND (int) /* YYYY */
525 + 1 /* because YYYY might equal INT_MAX + 1900 */
526 + sizeof "-MM-DD HH:MM:SS.NNNNNNNNN +ZZZZ"))];
527 struct tm const *tm = localtime (&t.tv_sec);
528 if (tm == NULL)
529 return timetostr (t.tv_sec, str);
530 nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", tm, 0, t.tv_nsec);
531 return str;
534 /* PFORMAT points to a '%' followed by a prefix of a format, all of
535 size PREFIX_LEN. The flags allowed for this format are
536 ALLOWED_FLAGS; remove other printf flags from the prefix, then
537 append SUFFIX. */
538 static void
539 make_format (char *pformat, size_t prefix_len, char const *allowed_flags,
540 char const *suffix)
542 char *dst = pformat + 1;
543 char const *src;
544 char const *srclim = pformat + prefix_len;
545 for (src = dst; src < srclim && strchr (printf_flags, *src); src++)
546 if (strchr (allowed_flags, *src))
547 *dst++ = *src;
548 while (src < srclim)
549 *dst++ = *src++;
550 strcpy (dst, suffix);
553 static void
554 out_string (char *pformat, size_t prefix_len, char const *arg)
556 make_format (pformat, prefix_len, "-", "s");
557 printf (pformat, arg);
559 static int
560 out_int (char *pformat, size_t prefix_len, intmax_t arg)
562 make_format (pformat, prefix_len, "'-+ 0", PRIdMAX);
563 return printf (pformat, arg);
565 static int
566 out_uint (char *pformat, size_t prefix_len, uintmax_t arg)
568 make_format (pformat, prefix_len, "'-0", PRIuMAX);
569 return printf (pformat, arg);
571 static void
572 out_uint_o (char *pformat, size_t prefix_len, uintmax_t arg)
574 make_format (pformat, prefix_len, "-#0", PRIoMAX);
575 printf (pformat, arg);
577 static void
578 out_uint_x (char *pformat, size_t prefix_len, uintmax_t arg)
580 make_format (pformat, prefix_len, "-#0", PRIxMAX);
581 printf (pformat, arg);
583 static int
584 out_minus_zero (char *pformat, size_t prefix_len)
586 make_format (pformat, prefix_len, "'-+ 0", ".0f");
587 return printf (pformat, -0.25);
590 /* Output the number of seconds since the Epoch, using a format that
591 acts like printf's %f format. */
592 static void
593 out_epoch_sec (char *pformat, size_t prefix_len,
594 struct stat const *statbuf _GL_UNUSED,
595 struct timespec arg)
597 char *dot = memchr (pformat, '.', prefix_len);
598 size_t sec_prefix_len = prefix_len;
599 int width = 0;
600 int precision = 0;
601 bool frac_left_adjust = false;
603 if (dot)
605 sec_prefix_len = dot - pformat;
606 pformat[prefix_len] = '\0';
608 if (ISDIGIT (dot[1]))
610 long int lprec = strtol (dot + 1, NULL, 10);
611 precision = (lprec <= INT_MAX ? lprec : INT_MAX);
613 else
615 precision = 9;
618 if (precision && ISDIGIT (dot[-1]))
620 /* If a nontrivial width is given, subtract the width of the
621 decimal point and PRECISION digits that will be output
622 later. */
623 char *p = dot;
624 *dot = '\0';
627 --p;
628 while (ISDIGIT (p[-1]));
630 long int lwidth = strtol (p, NULL, 10);
631 width = (lwidth <= INT_MAX ? lwidth : INT_MAX);
632 if (1 < width)
634 p += (*p == '0');
635 sec_prefix_len = p - pformat;
636 int w_d = (decimal_point_len < width
637 ? width - decimal_point_len
638 : 0);
639 if (1 < w_d)
641 int w = w_d - precision;
642 if (1 < w)
644 char *dst = pformat;
645 for (char const *src = dst; src < p; src++)
647 if (*src == '-')
648 frac_left_adjust = true;
649 else
650 *dst++ = *src;
652 sec_prefix_len =
653 (dst - pformat
654 + (frac_left_adjust ? 0 : sprintf (dst, "%d", w)));
661 int divisor = 1;
662 for (int i = precision; i < 9; i++)
663 divisor *= 10;
664 int frac_sec = arg.tv_nsec / divisor;
665 int int_len;
667 if (TYPE_SIGNED (time_t))
669 bool minus_zero = false;
670 if (arg.tv_sec < 0 && arg.tv_nsec != 0)
672 int frac_sec_modulus = 1000000000 / divisor;
673 frac_sec = (frac_sec_modulus - frac_sec
674 - (arg.tv_nsec % divisor != 0));
675 arg.tv_sec += (frac_sec != 0);
676 minus_zero = (arg.tv_sec == 0);
678 int_len = (minus_zero
679 ? out_minus_zero (pformat, sec_prefix_len)
680 : out_int (pformat, sec_prefix_len, arg.tv_sec));
682 else
683 int_len = out_uint (pformat, sec_prefix_len, arg.tv_sec);
685 if (precision)
687 int prec = (precision < 9 ? precision : 9);
688 int trailing_prec = precision - prec;
689 int ilen = (int_len < 0 ? 0 : int_len);
690 int trailing_width = (ilen < width && decimal_point_len < width - ilen
691 ? width - ilen - decimal_point_len - prec
692 : 0);
693 printf ("%s%.*d%-*.*d", decimal_point, prec, frac_sec,
694 trailing_width, trailing_prec, 0);
698 /* Print the context information of FILENAME, and return true iff the
699 context could not be obtained. */
700 static bool ATTRIBUTE_WARN_UNUSED_RESULT
701 out_file_context (char *pformat, size_t prefix_len, char const *filename)
703 char *scontext;
704 bool fail = false;
706 if ((follow_links
707 ? getfilecon (filename, &scontext)
708 : lgetfilecon (filename, &scontext)) < 0)
710 error (0, errno, _("failed to get security context of %s"),
711 quote (filename));
712 scontext = NULL;
713 fail = true;
715 strcpy (pformat + prefix_len, "s");
716 printf (pformat, (scontext ? scontext : "?"));
717 if (scontext)
718 freecon (scontext);
719 return fail;
722 /* Print statfs info. Return zero upon success, nonzero upon failure. */
723 static bool ATTRIBUTE_WARN_UNUSED_RESULT
724 print_statfs (char *pformat, size_t prefix_len, unsigned int m,
725 char const *filename,
726 void const *data)
728 STRUCT_STATVFS const *statfsbuf = data;
729 bool fail = false;
731 switch (m)
733 case 'n':
734 out_string (pformat, prefix_len, filename);
735 break;
737 case 'i':
739 #if STRUCT_STATXFS_F_FSID_IS_INTEGER
740 uintmax_t fsid = statfsbuf->f_fsid;
741 #else
742 typedef unsigned int fsid_word;
743 verify (alignof (STRUCT_STATVFS) % alignof (fsid_word) == 0);
744 verify (offsetof (STRUCT_STATVFS, f_fsid) % alignof (fsid_word) == 0);
745 verify (sizeof statfsbuf->f_fsid % alignof (fsid_word) == 0);
746 fsid_word const *p = (fsid_word *) &statfsbuf->f_fsid;
748 /* Assume a little-endian word order, as that is compatible
749 with glibc's statvfs implementation. */
750 uintmax_t fsid = 0;
751 int words = sizeof statfsbuf->f_fsid / sizeof *p;
752 int i;
753 for (i = 0; i < words && i * sizeof *p < sizeof fsid; i++)
755 uintmax_t u = p[words - 1 - i];
756 fsid |= u << (i * CHAR_BIT * sizeof *p);
758 #endif
759 out_uint_x (pformat, prefix_len, fsid);
761 break;
763 case 'l':
764 OUT_NAMEMAX (pformat, prefix_len, SB_F_NAMEMAX (statfsbuf));
765 break;
766 case 't':
767 #if HAVE_STRUCT_STATXFS_F_TYPE
768 out_uint_x (pformat, prefix_len, statfsbuf->f_type);
769 #else
770 fputc ('?', stdout);
771 #endif
772 break;
773 case 'T':
774 out_string (pformat, prefix_len, human_fstype (statfsbuf));
775 break;
776 case 'b':
777 out_int (pformat, prefix_len, statfsbuf->f_blocks);
778 break;
779 case 'f':
780 out_int (pformat, prefix_len, statfsbuf->f_bfree);
781 break;
782 case 'a':
783 out_int (pformat, prefix_len, statfsbuf->f_bavail);
784 break;
785 case 's':
786 out_uint (pformat, prefix_len, statfsbuf->f_bsize);
787 break;
788 case 'S':
790 uintmax_t frsize = STATFS_FRSIZE (statfsbuf);
791 if (! frsize)
792 frsize = statfsbuf->f_bsize;
793 out_uint (pformat, prefix_len, frsize);
795 break;
796 case 'c':
797 out_uint (pformat, prefix_len, statfsbuf->f_files);
798 break;
799 case 'd':
800 out_int (pformat, prefix_len, statfsbuf->f_ffree);
801 break;
802 default:
803 fputc ('?', stdout);
804 break;
806 return fail;
809 /* Return any bind mounted source for a path.
810 The caller should not free the returned buffer.
811 Return NULL if no bind mount found. */
812 static char const * ATTRIBUTE_WARN_UNUSED_RESULT
813 find_bind_mount (char const * name)
815 char const * bind_mount = NULL;
817 static struct mount_entry *mount_list;
818 static bool tried_mount_list = false;
819 if (!tried_mount_list) /* attempt/warn once per process. */
821 if (!(mount_list = read_file_system_list (false)))
822 error (0, errno, "%s", _("cannot read table of mounted file systems"));
823 tried_mount_list = true;
826 struct mount_entry *me;
827 for (me = mount_list; me; me = me->me_next)
829 if (me->me_dummy && me->me_devname[0] == '/'
830 && STREQ (me->me_mountdir, name))
832 struct stat name_stats;
833 struct stat dev_stats;
835 if (stat (name, &name_stats) == 0
836 && stat (me->me_devname, &dev_stats) == 0
837 && SAME_INODE (name_stats, dev_stats))
839 bind_mount = me->me_devname;
840 break;
845 return bind_mount;
848 /* Print mount point. Return zero upon success, nonzero upon failure. */
849 static bool ATTRIBUTE_WARN_UNUSED_RESULT
850 out_mount_point (char const *filename, char *pformat, size_t prefix_len,
851 const struct stat *statp)
854 char const *np = "?", *bp = NULL;
855 char *mp = NULL;
856 bool fail = true;
858 /* Look for bind mounts first. Note we output the immediate alias,
859 rather than further resolving to a base device mount point. */
860 if (follow_links || !S_ISLNK (statp->st_mode))
862 char *resolved = canonicalize_file_name (filename);
863 if (!resolved)
865 error (0, errno, _("failed to canonicalize %s"), quote (filename));
866 goto print_mount_point;
868 bp = find_bind_mount (resolved);
869 free (resolved);
870 if (bp)
872 fail = false;
873 goto print_mount_point;
877 /* If there is no direct bind mount, then navigate
878 back up the tree looking for a device change.
879 Note we don't detect if any of the directory components
880 are bind mounted to the same device, but that's OK
881 since we've not directly queried them. */
882 if ((mp = find_mount_point (filename, statp)))
884 /* This dir might be bind mounted to another device,
885 so we resolve the bound source in that case also. */
886 bp = find_bind_mount (mp);
887 fail = false;
890 print_mount_point:
892 out_string (pformat, prefix_len, bp ? bp : mp ? mp : np);
893 free (mp);
894 return fail;
897 /* Map a TS with negative TS.tv_nsec to {0,0}. */
898 static inline struct timespec
899 neg_to_zero (struct timespec ts)
901 if (0 <= ts.tv_nsec)
902 return ts;
903 struct timespec z = {0, 0};
904 return z;
907 /* Print stat info. Return zero upon success, nonzero upon failure. */
908 static bool
909 print_stat (char *pformat, size_t prefix_len, unsigned int m,
910 char const *filename, void const *data)
912 struct stat *statbuf = (struct stat *) data;
913 struct passwd *pw_ent;
914 struct group *gw_ent;
915 bool fail = false;
917 switch (m)
919 case 'n':
920 out_string (pformat, prefix_len, filename);
921 break;
922 case 'N':
923 out_string (pformat, prefix_len, quote (filename));
924 if (S_ISLNK (statbuf->st_mode))
926 char *linkname = areadlink_with_size (filename, statbuf->st_size);
927 if (linkname == NULL)
929 error (0, errno, _("cannot read symbolic link %s"),
930 quote (filename));
931 return true;
933 printf (" -> ");
934 out_string (pformat, prefix_len, quote (linkname));
935 free (linkname);
937 break;
938 case 'd':
939 out_uint (pformat, prefix_len, statbuf->st_dev);
940 break;
941 case 'D':
942 out_uint_x (pformat, prefix_len, statbuf->st_dev);
943 break;
944 case 'i':
945 out_uint (pformat, prefix_len, statbuf->st_ino);
946 break;
947 case 'a':
948 out_uint_o (pformat, prefix_len, statbuf->st_mode & CHMOD_MODE_BITS);
949 break;
950 case 'A':
951 out_string (pformat, prefix_len, human_access (statbuf));
952 break;
953 case 'f':
954 out_uint_x (pformat, prefix_len, statbuf->st_mode);
955 break;
956 case 'F':
957 out_string (pformat, prefix_len, file_type (statbuf));
958 break;
959 case 'h':
960 out_uint (pformat, prefix_len, statbuf->st_nlink);
961 break;
962 case 'u':
963 out_uint (pformat, prefix_len, statbuf->st_uid);
964 break;
965 case 'U':
966 setpwent ();
967 pw_ent = getpwuid (statbuf->st_uid);
968 out_string (pformat, prefix_len,
969 pw_ent ? pw_ent->pw_name : "UNKNOWN");
970 break;
971 case 'g':
972 out_uint (pformat, prefix_len, statbuf->st_gid);
973 break;
974 case 'G':
975 setgrent ();
976 gw_ent = getgrgid (statbuf->st_gid);
977 out_string (pformat, prefix_len,
978 gw_ent ? gw_ent->gr_name : "UNKNOWN");
979 break;
980 case 't':
981 out_uint_x (pformat, prefix_len, major (statbuf->st_rdev));
982 break;
983 case 'm':
984 fail |= out_mount_point (filename, pformat, prefix_len, statbuf);
985 break;
986 case 'T':
987 out_uint_x (pformat, prefix_len, minor (statbuf->st_rdev));
988 break;
989 case 's':
990 out_int (pformat, prefix_len, statbuf->st_size);
991 break;
992 case 'B':
993 out_uint (pformat, prefix_len, ST_NBLOCKSIZE);
994 break;
995 case 'b':
996 out_uint (pformat, prefix_len, ST_NBLOCKS (*statbuf));
997 break;
998 case 'o':
999 out_uint (pformat, prefix_len, ST_BLKSIZE (*statbuf));
1000 break;
1001 case 'w':
1003 struct timespec t = get_stat_birthtime (statbuf);
1004 if (t.tv_nsec < 0)
1005 out_string (pformat, prefix_len, "-");
1006 else
1007 out_string (pformat, prefix_len, human_time (t));
1009 break;
1010 case 'W':
1011 out_epoch_sec (pformat, prefix_len, statbuf,
1012 neg_to_zero (get_stat_birthtime (statbuf)));
1013 break;
1014 case 'x':
1015 out_string (pformat, prefix_len, human_time (get_stat_atime (statbuf)));
1016 break;
1017 case 'X':
1018 out_epoch_sec (pformat, prefix_len, statbuf, get_stat_atime (statbuf));
1019 break;
1020 case 'y':
1021 out_string (pformat, prefix_len, human_time (get_stat_mtime (statbuf)));
1022 break;
1023 case 'Y':
1024 out_epoch_sec (pformat, prefix_len, statbuf, get_stat_mtime (statbuf));
1025 break;
1026 case 'z':
1027 out_string (pformat, prefix_len, human_time (get_stat_ctime (statbuf)));
1028 break;
1029 case 'Z':
1030 out_epoch_sec (pformat, prefix_len, statbuf, get_stat_ctime (statbuf));
1031 break;
1032 case 'C':
1033 fail |= out_file_context (pformat, prefix_len, filename);
1034 break;
1035 default:
1036 fputc ('?', stdout);
1037 break;
1039 return fail;
1042 /* Output a single-character \ escape. */
1044 static void
1045 print_esc_char (char c)
1047 switch (c)
1049 case 'a': /* Alert. */
1050 c ='\a';
1051 break;
1052 case 'b': /* Backspace. */
1053 c ='\b';
1054 break;
1055 case 'e': /* Escape. */
1056 c ='\x1B';
1057 break;
1058 case 'f': /* Form feed. */
1059 c ='\f';
1060 break;
1061 case 'n': /* New line. */
1062 c ='\n';
1063 break;
1064 case 'r': /* Carriage return. */
1065 c ='\r';
1066 break;
1067 case 't': /* Horizontal tab. */
1068 c ='\t';
1069 break;
1070 case 'v': /* Vertical tab. */
1071 c ='\v';
1072 break;
1073 case '"':
1074 case '\\':
1075 break;
1076 default:
1077 error (0, 0, _("warning: unrecognized escape '\\%c'"), c);
1078 break;
1080 putchar (c);
1083 /* Print the information specified by the format string, FORMAT,
1084 calling PRINT_FUNC for each %-directive encountered.
1085 Return zero upon success, nonzero upon failure. */
1086 static bool ATTRIBUTE_WARN_UNUSED_RESULT
1087 print_it (char const *format, char const *filename,
1088 bool (*print_func) (char *, size_t, unsigned int,
1089 char const *, void const *),
1090 void const *data)
1092 bool fail = false;
1094 /* Add 2 to accommodate our conversion of the stat '%s' format string
1095 to the longer printf '%llu' one. */
1096 enum
1098 MAX_ADDITIONAL_BYTES =
1099 (MAX (sizeof PRIdMAX,
1100 MAX (sizeof PRIoMAX, MAX (sizeof PRIuMAX, sizeof PRIxMAX)))
1101 - 1)
1103 size_t n_alloc = strlen (format) + MAX_ADDITIONAL_BYTES + 1;
1104 char *dest = xmalloc (n_alloc);
1105 char const *b;
1106 for (b = format; *b; b++)
1108 switch (*b)
1110 case '%':
1112 size_t len = strspn (b + 1, printf_flags);
1113 char const *fmt_char = b + len + 1;
1114 fmt_char += strspn (fmt_char, digits);
1115 if (*fmt_char == '.')
1116 fmt_char += 1 + strspn (fmt_char + 1, digits);
1117 len = fmt_char - (b + 1);
1118 unsigned int fmt_code = *fmt_char;
1119 memcpy (dest, b, len + 1);
1121 b = fmt_char;
1122 switch (fmt_code)
1124 case '\0':
1125 --b;
1126 /* fall through */
1127 case '%':
1128 if (0 < len)
1130 dest[len + 1] = *fmt_char;
1131 dest[len + 2] = '\0';
1132 error (EXIT_FAILURE, 0, _("%s: invalid directive"),
1133 quotearg_colon (dest));
1135 putchar ('%');
1136 break;
1137 default:
1138 fail |= print_func (dest, len + 1, fmt_code, filename, data);
1139 break;
1141 break;
1144 case '\\':
1145 if ( ! interpret_backslash_escapes)
1147 putchar ('\\');
1148 break;
1150 ++b;
1151 if (isodigit (*b))
1153 int esc_value = octtobin (*b);
1154 int esc_length = 1; /* number of octal digits */
1155 for (++b; esc_length < 3 && isodigit (*b);
1156 ++esc_length, ++b)
1158 esc_value = esc_value * 8 + octtobin (*b);
1160 putchar (esc_value);
1161 --b;
1163 else if (*b == 'x' && isxdigit (to_uchar (b[1])))
1165 int esc_value = hextobin (b[1]); /* Value of \xhh escape. */
1166 /* A hexadecimal \xhh escape sequence must have
1167 1 or 2 hex. digits. */
1168 ++b;
1169 if (isxdigit (to_uchar (b[1])))
1171 ++b;
1172 esc_value = esc_value * 16 + hextobin (*b);
1174 putchar (esc_value);
1176 else if (*b == '\0')
1178 error (0, 0, _("warning: backslash at end of format"));
1179 putchar ('\\');
1180 /* Arrange to exit the loop. */
1181 --b;
1183 else
1185 print_esc_char (*b);
1187 break;
1189 default:
1190 putchar (*b);
1191 break;
1194 free (dest);
1196 fputs (trailing_delim, stdout);
1198 return fail;
1201 /* Stat the file system and print what we find. */
1202 static bool ATTRIBUTE_WARN_UNUSED_RESULT
1203 do_statfs (char const *filename, char const *format)
1205 STRUCT_STATVFS statfsbuf;
1207 if (STREQ (filename, "-"))
1209 error (0, 0, _("using %s to denote standard input does not work"
1210 " in file system mode"), quote (filename));
1211 return false;
1214 if (STATFS (filename, &statfsbuf) != 0)
1216 error (0, errno, _("cannot read file system information for %s"),
1217 quote (filename));
1218 return false;
1221 bool fail = print_it (format, filename, print_statfs, &statfsbuf);
1222 return ! fail;
1225 /* stat the file and print what we find */
1226 static bool ATTRIBUTE_WARN_UNUSED_RESULT
1227 do_stat (char const *filename, char const *format,
1228 char const *format2)
1230 struct stat statbuf;
1232 if (STREQ (filename, "-"))
1234 if (fstat (STDIN_FILENO, &statbuf) != 0)
1236 error (0, errno, _("cannot stat standard input"));
1237 return false;
1240 /* We can't use the shorter
1241 (follow_links?stat:lstat) (filename, &statbug)
1242 since stat might be a function-like macro. */
1243 else if ((follow_links
1244 ? stat (filename, &statbuf)
1245 : lstat (filename, &statbuf)) != 0)
1247 error (0, errno, _("cannot stat %s"), quote (filename));
1248 return false;
1251 if (S_ISBLK (statbuf.st_mode) || S_ISCHR (statbuf.st_mode))
1252 format = format2;
1254 bool fail = print_it (format, filename, print_stat, &statbuf);
1255 return ! fail;
1258 /* Return an allocated format string in static storage that
1259 corresponds to whether FS and TERSE options were declared. */
1260 static char *
1261 default_format (bool fs, bool terse, bool device)
1263 char *format;
1264 if (fs)
1266 if (terse)
1267 format = xstrdup ("%n %i %l %t %s %S %b %f %a %c %d\n");
1268 else
1270 /* TRANSLATORS: This string uses format specifiers from
1271 'stat --help' with --file-system, and NOT from printf. */
1272 format = xstrdup (_(" File: \"%n\"\n"
1273 " ID: %-8i Namelen: %-7l Type: %T\n"
1274 "Block size: %-10s Fundamental block size: %S\n"
1275 "Blocks: Total: %-10b Free: %-10f Available: %a\n"
1276 "Inodes: Total: %-10c Free: %d\n"));
1279 else /* ! fs */
1281 if (terse)
1283 if (0 < is_selinux_enabled ())
1284 format = xstrdup ("%n %s %b %f %u %g %D %i %h %t %T"
1285 " %X %Y %Z %W %o %C\n");
1286 else
1287 format = xstrdup ("%n %s %b %f %u %g %D %i %h %t %T"
1288 " %X %Y %Z %W %o\n");
1290 else
1292 char *temp;
1293 /* TRANSLATORS: This string uses format specifiers from
1294 'stat --help' without --file-system, and NOT from printf. */
1295 format = xstrdup (_("\
1296 File: %N\n\
1297 Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n\
1298 "));
1300 temp = format;
1301 if (device)
1303 /* TRANSLATORS: This string uses format specifiers from
1304 'stat --help' without --file-system, and NOT from printf. */
1305 format = xasprintf ("%s%s", format, _("\
1306 " "Device: %Dh/%dd\tInode: %-10i Links: %-5h Device type: %t,%T\n\
1307 "));
1309 else
1311 /* TRANSLATORS: This string uses format specifiers from
1312 'stat --help' without --file-system, and NOT from printf. */
1313 format = xasprintf ("%s%s", format, _("\
1314 " "Device: %Dh/%dd\tInode: %-10i Links: %h\n\
1315 "));
1317 free (temp);
1319 temp = format;
1320 /* TRANSLATORS: This string uses format specifiers from
1321 'stat --help' without --file-system, and NOT from printf. */
1322 format = xasprintf ("%s%s", format, _("\
1323 " "Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n\
1324 "));
1325 free (temp);
1327 if (0 < is_selinux_enabled ())
1329 temp = format;
1330 /* TRANSLATORS: This string uses format specifiers from
1331 'stat --help' without --file-system, and NOT from printf. */
1332 format = xasprintf ("%s%s", format, _("Context: %C\n"));
1333 free (temp);
1336 temp = format;
1337 /* TRANSLATORS: This string uses format specifiers from
1338 'stat --help' without --file-system, and NOT from printf. */
1339 format = xasprintf ("%s%s", format,
1340 _("Access: %x\n"
1341 "Modify: %y\n"
1342 "Change: %z\n"
1343 " Birth: %w\n"));
1344 free (temp);
1347 return format;
1350 void
1351 usage (int status)
1353 if (status != EXIT_SUCCESS)
1354 emit_try_help ();
1355 else
1357 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
1358 fputs (_("\
1359 Display file or file system status.\n\
1360 "), stdout);
1362 emit_mandatory_arg_note ();
1364 fputs (_("\
1365 -L, --dereference follow links\n\
1366 -f, --file-system display file system status instead of file status\n\
1367 "), stdout);
1368 fputs (_("\
1369 -c --format=FORMAT use the specified FORMAT instead of the default;\n\
1370 output a newline after each use of FORMAT\n\
1371 --printf=FORMAT like --format, but interpret backslash escapes,\n\
1372 and do not output a mandatory trailing newline.\n\
1373 If you want a newline, include \\n in FORMAT\n\
1374 -t, --terse print the information in terse form\n\
1375 "), stdout);
1376 fputs (HELP_OPTION_DESCRIPTION, stdout);
1377 fputs (VERSION_OPTION_DESCRIPTION, stdout);
1379 fputs (_("\n\
1380 The valid format sequences for files (without --file-system):\n\
1382 %a access rights in octal\n\
1383 %A access rights in human readable form\n\
1384 %b number of blocks allocated (see %B)\n\
1385 %B the size in bytes of each block reported by %b\n\
1386 %C SELinux security context string\n\
1387 "), stdout);
1388 fputs (_("\
1389 %d device number in decimal\n\
1390 %D device number in hex\n\
1391 %f raw mode in hex\n\
1392 %F file type\n\
1393 %g group ID of owner\n\
1394 %G group name of owner\n\
1395 "), stdout);
1396 fputs (_("\
1397 %h number of hard links\n\
1398 %i inode number\n\
1399 %m mount point\n\
1400 %n file name\n\
1401 %N quoted file name with dereference if symbolic link\n\
1402 %o optimal I/O transfer size hint\n\
1403 %s total size, in bytes\n\
1404 %t major device type in hex, for character/block device special files\n\
1405 %T minor device type in hex, for character/block device special files\n\
1406 "), stdout);
1407 fputs (_("\
1408 %u user ID of owner\n\
1409 %U user name of owner\n\
1410 %w time of file birth, human-readable; - if unknown\n\
1411 %W time of file birth, seconds since Epoch; 0 if unknown\n\
1412 %x time of last access, human-readable\n\
1413 %X time of last access, seconds since Epoch\n\
1414 %y time of last modification, human-readable\n\
1415 %Y time of last modification, seconds since Epoch\n\
1416 %z time of last change, human-readable\n\
1417 %Z time of last change, seconds since Epoch\n\
1419 "), stdout);
1421 fputs (_("\
1422 Valid format sequences for file systems:\n\
1424 %a free blocks available to non-superuser\n\
1425 %b total data blocks in file system\n\
1426 %c total file nodes in file system\n\
1427 %d free file nodes in file system\n\
1428 %f free blocks in file system\n\
1429 "), stdout);
1430 fputs (_("\
1431 %i file system ID in hex\n\
1432 %l maximum length of filenames\n\
1433 %n file name\n\
1434 %s block size (for faster transfers)\n\
1435 %S fundamental block size (for block counts)\n\
1436 %t file system type in hex\n\
1437 %T file system type in human readable form\n\
1438 "), stdout);
1439 printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
1440 emit_ancillary_info ();
1442 exit (status);
1446 main (int argc, char *argv[])
1448 int c;
1449 int i;
1450 bool fs = false;
1451 bool terse = false;
1452 char *format = NULL;
1453 char *format2;
1454 bool ok = true;
1456 initialize_main (&argc, &argv);
1457 set_program_name (argv[0]);
1458 setlocale (LC_ALL, "");
1459 bindtextdomain (PACKAGE, LOCALEDIR);
1460 textdomain (PACKAGE);
1462 struct lconv const *locale = localeconv ();
1463 decimal_point = (locale->decimal_point[0] ? locale->decimal_point : ".");
1464 decimal_point_len = strlen (decimal_point);
1466 atexit (close_stdout);
1468 while ((c = getopt_long (argc, argv, "c:fLt", long_options, NULL)) != -1)
1470 switch (c)
1472 case PRINTF_OPTION:
1473 format = optarg;
1474 interpret_backslash_escapes = true;
1475 trailing_delim = "";
1476 break;
1478 case 'c':
1479 format = optarg;
1480 interpret_backslash_escapes = false;
1481 trailing_delim = "\n";
1482 break;
1484 case 'L':
1485 follow_links = true;
1486 break;
1488 case 'f':
1489 fs = true;
1490 break;
1492 case 't':
1493 terse = true;
1494 break;
1496 case_GETOPT_HELP_CHAR;
1498 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
1500 default:
1501 usage (EXIT_FAILURE);
1505 if (argc == optind)
1507 error (0, 0, _("missing operand"));
1508 usage (EXIT_FAILURE);
1511 if (format)
1512 format2 = format;
1513 else
1515 format = default_format (fs, terse, false);
1516 format2 = default_format (fs, terse, true);
1519 for (i = optind; i < argc; i++)
1520 ok &= (fs
1521 ? do_statfs (argv[i], format)
1522 : do_stat (argv[i], format, format2));
1524 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);