1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
|
/* stat.c -- display file or file system status
Copyright (C) 2001-2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Written by Michael Meskes. */
#include <config.h>
/* Keep this conditional in sync with the similar conditional in
../m4/stat-prog.m4. */
#if (STAT_STATVFS \
&& (HAVE_STRUCT_STATVFS_F_BASETYPE || HAVE_STRUCT_STATVFS_F_FSTYPENAME \
|| (! HAVE_STRUCT_STATFS_F_FSTYPENAME && HAVE_STRUCT_STATVFS_F_TYPE)))
# define USE_STATVFS 1
#else
# define USE_STATVFS 0
#endif
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#if USE_STATVFS
# include <sys/statvfs.h>
#elif HAVE_SYS_VFS_H
# include <sys/vfs.h>
#elif HAVE_SYS_MOUNT_H && HAVE_SYS_PARAM_H
/* NOTE: freebsd5.0 needs sys/param.h and sys/mount.h for statfs.
It does have statvfs.h, but shouldn't use it, since it doesn't
HAVE_STRUCT_STATVFS_F_BASETYPE. So find a clean way to fix it. */
/* NetBSD 1.5.2 needs these, for the declaration of struct statfs. */
# include <sys/param.h>
# include <sys/mount.h>
# if HAVE_NFS_NFS_CLNT_H && HAVE_NFS_VFS_H
/* Ultrix 4.4 needs these for the declaration of struct statfs. */
# include <netinet/in.h>
# include <nfs/nfs_clnt.h>
# include <nfs/vfs.h>
# endif
#elif HAVE_OS_H /* BeOS */
# include <fs_info.h>
#endif
#include <selinux/selinux.h>
#include "system.h"
#include "alignof.h"
#include "areadlink.h"
#include "error.h"
#include "file-type.h"
#include "filemode.h"
#include "fs.h"
#include "getopt.h"
#include "mountlist.h"
#include "quote.h"
#include "quotearg.h"
#include "stat-time.h"
#include "strftime.h"
#include "find-mount-point.h"
#if USE_STATVFS
# define STRUCT_STATVFS struct statvfs
# define STRUCT_STATXFS_F_FSID_IS_INTEGER STRUCT_STATVFS_F_FSID_IS_INTEGER
# define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATVFS_F_TYPE
# if HAVE_STRUCT_STATVFS_F_NAMEMAX
# define SB_F_NAMEMAX(S) ((S)->f_namemax)
# endif
# define STATFS statvfs
# define STATFS_FRSIZE(S) ((S)->f_frsize)
#else
# define HAVE_STRUCT_STATXFS_F_TYPE HAVE_STRUCT_STATFS_F_TYPE
# if HAVE_STRUCT_STATFS_F_NAMELEN
# define SB_F_NAMEMAX(S) ((S)->f_namelen)
# endif
# define STATFS statfs
# if HAVE_OS_H /* BeOS */
/* BeOS has a statvfs function, but it does not return sensible values
for f_files, f_ffree and f_favail, and lacks f_type, f_basetype and
f_fstypename. Use 'struct fs_info' instead. */
static int ATTRIBUTE_WARN_UNUSED_RESULT
statfs (char const *filename, struct fs_info *buf)
{
dev_t device = dev_for_path (filename);
if (device < 0)
{
errno = (device == B_ENTRY_NOT_FOUND ? ENOENT
: device == B_BAD_VALUE ? EINVAL
: device == B_NAME_TOO_LONG ? ENAMETOOLONG
: device == B_NO_MEMORY ? ENOMEM
: device == B_FILE_ERROR ? EIO
: 0);
return -1;
}
/* If successful, buf->dev will be == device. */
return fs_stat_dev (device, buf);
}
# define f_fsid dev
# define f_blocks total_blocks
# define f_bfree free_blocks
# define f_bavail free_blocks
# define f_bsize io_size
# define f_files total_nodes
# define f_ffree free_nodes
# define STRUCT_STATVFS struct fs_info
# define STRUCT_STATXFS_F_FSID_IS_INTEGER true
# define STATFS_FRSIZE(S) ((S)->block_size)
# else
# define STRUCT_STATVFS struct statfs
# define STRUCT_STATXFS_F_FSID_IS_INTEGER STRUCT_STATFS_F_FSID_IS_INTEGER
# define STATFS_FRSIZE(S) 0
# endif
#endif
#ifdef SB_F_NAMEMAX
# define OUT_NAMEMAX out_uint
#else
/* NetBSD 1.5.2 has neither f_namemax nor f_namelen. */
# define SB_F_NAMEMAX(S) "*"
# define OUT_NAMEMAX out_string
#endif
#if HAVE_STRUCT_STATVFS_F_BASETYPE
# define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME f_basetype
#else
# if HAVE_STRUCT_STATVFS_F_FSTYPENAME || HAVE_STRUCT_STATFS_F_FSTYPENAME
# define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME f_fstypename
# elif HAVE_OS_H /* BeOS */
# define STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME fsh_name
# endif
#endif
/* FIXME: these are used by printf.c, too */
#define isodigit(c) ('0' <= (c) && (c) <= '7')
#define octtobin(c) ((c) - '0')
#define hextobin(c) ((c) >= 'a' && (c) <= 'f' ? (c) - 'a' + 10 : \
(c) >= 'A' && (c) <= 'F' ? (c) - 'A' + 10 : (c) - '0')
#define PROGRAM_NAME "stat"
#define AUTHORS proper_name ("Michael Meskes")
enum
{
PRINTF_OPTION = CHAR_MAX + 1
};
static struct option const long_options[] =
{
{"context", no_argument, 0, 'Z'},
{"dereference", no_argument, NULL, 'L'},
{"file-system", no_argument, NULL, 'f'},
{"format", required_argument, NULL, 'c'},
{"printf", required_argument, NULL, PRINTF_OPTION},
{"terse", no_argument, NULL, 't'},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
};
/* Whether to follow symbolic links; True for --dereference (-L). */
static bool follow_links;
/* Whether to interpret backslash-escape sequences.
True for --printf=FMT, not for --format=FMT (-c). */
static bool interpret_backslash_escapes;
/* The trailing delimiter string:
"" for --printf=FMT, "\n" for --format=FMT (-c). */
static char const *trailing_delim = "";
/* Return the type of the specified file system.
Some systems have statfvs.f_basetype[FSTYPSZ] (AIX, HP-UX, and Solaris).
Others have statvfs.f_fstypename[_VFS_NAMELEN] (NetBSD 3.0).
Others have statfs.f_fstypename[MFSNAMELEN] (NetBSD 1.5.2).
Still others have neither and have to get by with f_type (GNU/Linux).
But f_type may only exist in statfs (Cygwin). */
static char const * ATTRIBUTE_WARN_UNUSED_RESULT
human_fstype (STRUCT_STATVFS const *statfsbuf)
{
#ifdef STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME
return statfsbuf->STATXFS_FILE_SYSTEM_TYPE_MEMBER_NAME;
#else
switch (statfsbuf->f_type)
{
# if defined __linux__
/* Compare with what's in libc:
f=/a/libc/sysdeps/unix/sysv/linux/linux_fsinfo.h
sed -n '/ADFS_SUPER_MAGIC/,/SYSFS_MAGIC/p' $f \
| perl -n -e '/#define (.*?)_(?:SUPER_)MAGIC\s+0x(\S+)/' \
-e 'and print "case S_MAGIC_$1: /\* 0x" . uc($2) . " *\/\n"' \
| sort > sym_libc
perl -ne '/^\s+(case S_MAGIC_.*?): \/\* 0x(\S+) \*\//' \
-e 'and do { $v=uc$2; print "$1: /\* 0x$v *\/\n"}' stat.c \
| sort > sym_stat
diff -u sym_stat sym_libc
*/
/* Also compare with the list in "man 2 statfs" using the
fs-magic-compare make target. */
/* IMPORTANT NOTE: Each of the following `case S_MAGIC_...:'
statements must be followed by a hexadecimal constant in
a comment. The S_MAGIC_... name and constant are automatically
combined to produce the #define directives in fs.h. */
case S_MAGIC_ADFS: /* 0xADF5 */
return "adfs";
case S_MAGIC_AFFS: /* 0xADFF */
return "affs";
case S_MAGIC_AFS: /* 0x5346414F */
return "afs";
case S_MAGIC_ANON_INODE_FS: /* 0x09041934 */
return "anon-inode FS";
case S_MAGIC_AUTOFS: /* 0x0187 */
return "autofs";
case S_MAGIC_BEFS: /* 0x42465331 */
return "befs";
case S_MAGIC_BFS: /* 0x1BADFACE */
return "bfs";
case S_MAGIC_BINFMT_MISC: /* 0x42494E4D */
return "binfmt_misc";
case S_MAGIC_BTRFS: /* 0x9123683E */
return "btrfs";
case S_MAGIC_CGROUP: /* 0x0027E0EB */
return "cgroupfs";
case S_MAGIC_CIFS: /* 0xFF534D42 */
return "cifs";
case S_MAGIC_CODA: /* 0x73757245 */
return "coda";
case S_MAGIC_COH: /* 0x012FF7B7 */
return "coh";
case S_MAGIC_CRAMFS: /* 0x28CD3D45 */
return "cramfs";
case S_MAGIC_CRAMFS_WEND: /* 0x453DCD28 */
return "cramfs-wend";
case S_MAGIC_DEBUGFS: /* 0x64626720 */
return "debugfs";
case S_MAGIC_DEVFS: /* 0x1373 */
return "devfs";
case S_MAGIC_DEVPTS: /* 0x1CD1 */
return "devpts";
case S_MAGIC_EFS: /* 0x00414A53 */
return "efs";
case S_MAGIC_EXT: /* 0x137D */
return "ext";
case S_MAGIC_EXT2: /* 0xEF53 */
return "ext2/ext3";
case S_MAGIC_EXT2_OLD: /* 0xEF51 */
return "ext2";
case S_MAGIC_FAT: /* 0x4006 */
return "fat";
case S_MAGIC_FUSEBLK: /* 0x65735546 */
return "fuseblk";
case S_MAGIC_FUSECTL: /* 0x65735543 */
return "fusectl";
case S_MAGIC_FUTEXFS: /* 0x0BAD1DEA */
return "futexfs";
case S_MAGIC_GFS: /* 0x1161970 */
return "gfs/gfs2";
case S_MAGIC_HFS: /* 0x4244 */
return "hfs";
case S_MAGIC_HPFS: /* 0xF995E849 */
return "hpfs";
case S_MAGIC_HUGETLBFS: /* 0x958458F6 */
return "hugetlbfs";
case S_MAGIC_INOTIFYFS: /* 0x2BAD1DEA */
return "inotifyfs";
case S_MAGIC_ISOFS: /* 0x9660 */
return "isofs";
case S_MAGIC_ISOFS_R_WIN: /* 0x4004 */
return "isofs";
case S_MAGIC_ISOFS_WIN: /* 0x4000 */
return "isofs";
case S_MAGIC_JFFS: /* 0x07C0 */
return "jffs";
case S_MAGIC_JFFS2: /* 0x72B6 */
return "jffs2";
case S_MAGIC_JFS: /* 0x3153464A */
return "jfs";
case S_MAGIC_KAFS: /* 0x6B414653 */
return "k-afs";
case S_MAGIC_LUSTRE: /* 0x0BD00BD0 */
return "lustre";
case S_MAGIC_MINIX: /* 0x137F */
return "minix";
case S_MAGIC_MINIX_30: /* 0x138F */
return "minix (30 char.)";
case S_MAGIC_MINIX_V2: /* 0x2468 */
return "minix v2";
case S_MAGIC_MINIX_V2_30: /* 0x2478 */
return "minix v2 (30 char.)";
case S_MAGIC_MINIX_V3: /* 0x4D5A */
return "minix3";
case S_MAGIC_MSDOS: /* 0x4D44 */
return "msdos";
case S_MAGIC_NCP: /* 0x564C */
return "novell";
case S_MAGIC_NFS: /* 0x6969 */
return "nfs";
case S_MAGIC_NFSD: /* 0x6E667364 */
return "nfsd";
case S_MAGIC_NILFS: /* 0x3434 */
return "nilfs";
case S_MAGIC_NTFS: /* 0x5346544E */
return "ntfs";
case S_MAGIC_OPENPROM: /* 0x9FA1 */
return "openprom";
case S_MAGIC_OCFS2: /* 0x7461636f */
return "ocfs2";
case S_MAGIC_PROC: /* 0x9FA0 */
return "proc";
case S_MAGIC_QNX4: /* 0x002F */
return "qnx4";
case S_MAGIC_RAMFS: /* 0x858458F6 */
return "ramfs";
case S_MAGIC_REISERFS: /* 0x52654973 */
return "reiserfs";
case S_MAGIC_ROMFS: /* 0x7275 */
return "romfs";
case S_MAGIC_RPC_PIPEFS: /* 0x67596969 */
return "rpc_pipefs";
case S_MAGIC_SECURITYFS: /* 0x73636673 */
return "securityfs";
case S_MAGIC_SELINUX: /* 0xF97CFF8C */
return "selinux";
case S_MAGIC_SMB: /* 0x517B */
return "smb";
case S_MAGIC_SOCKFS: /* 0x534F434B */
return "sockfs";
case S_MAGIC_SQUASHFS: /* 0x73717368 */
return "squashfs";
case S_MAGIC_SYSFS: /* 0x62656572 */
return "sysfs";
case S_MAGIC_SYSV2: /* 0x012FF7B6 */
return "sysv2";
case S_MAGIC_SYSV4: /* 0x012FF7B5 */
return "sysv4";
case S_MAGIC_TMPFS: /* 0x01021994 */
return "tmpfs";
case S_MAGIC_UDF: /* 0x15013346 */
return "udf";
case S_MAGIC_UFS: /* 0x00011954 */
return "ufs";
case S_MAGIC_UFS_BYTESWAPPED: /* 0x54190100 */
return "ufs";
case S_MAGIC_USBDEVFS: /* 0x9FA2 */
return "usbdevfs";
case S_MAGIC_VXFS: /* 0xA501FCF5 */
return "vxfs";
case S_MAGIC_XENFS: /* 0xABBA1974 */
return "xenfs";
case S_MAGIC_XENIX: /* 0x012FF7B4 */
return "xenix";
case S_MAGIC_XFS: /* 0x58465342 */
return "xfs";
case S_MAGIC_XIAFS: /* 0x012FD16D */
return "xia";
# elif __GNU__
case FSTYPE_UFS:
return "ufs";
case FSTYPE_NFS:
return "nfs";
case FSTYPE_GFS:
return "gfs";
case FSTYPE_LFS:
return "lfs";
case FSTYPE_SYSV:
return "sysv";
case FSTYPE_FTP:
return "ftp";
case FSTYPE_TAR:
return "tar";
case FSTYPE_AR:
return "ar";
case FSTYPE_CPIO:
return "cpio";
case FSTYPE_MSLOSS:
return "msloss";
case FSTYPE_CPM:
return "cpm";
case FSTYPE_HFS:
return "hfs";
case FSTYPE_DTFS:
return "dtfs";
case FSTYPE_GRFS:
return "grfs";
case FSTYPE_TERM:
return "term";
case FSTYPE_DEV:
return "dev";
case FSTYPE_PROC:
return "proc";
case FSTYPE_IFSOCK:
return "ifsock";
case FSTYPE_AFS:
return "afs";
case FSTYPE_DFS:
return "dfs";
case FSTYPE_PROC9:
return "proc9";
case FSTYPE_SOCKET:
return "socket";
case FSTYPE_MISC:
return "misc";
case FSTYPE_EXT2FS:
return "ext2/ext3";
case FSTYPE_HTTP:
return "http";
case FSTYPE_MEMFS:
return "memfs";
case FSTYPE_ISO9660:
return "iso9660";
# endif
default:
{
unsigned long int type = statfsbuf->f_type;
static char buf[sizeof "UNKNOWN (0x%lx)" - 3
+ (sizeof type * CHAR_BIT + 3) / 4];
sprintf (buf, "UNKNOWN (0x%lx)", type);
return buf;
}
}
#endif
}
static char * ATTRIBUTE_WARN_UNUSED_RESULT
human_access (struct stat const *statbuf)
{
static char modebuf[12];
filemodestring (statbuf, modebuf);
modebuf[10] = 0;
return modebuf;
}
static char * ATTRIBUTE_WARN_UNUSED_RESULT
human_time (struct timespec t)
{
static char str[MAX (INT_BUFSIZE_BOUND (intmax_t),
(INT_STRLEN_BOUND (int) /* YYYY */
+ 1 /* because YYYY might equal INT_MAX + 1900 */
+ sizeof "-MM-DD HH:MM:SS.NNNNNNNNN +ZZZZ"))];
struct tm const *tm = localtime (&t.tv_sec);
if (tm == NULL)
return timetostr (t.tv_sec, str);
nstrftime (str, sizeof str, "%Y-%m-%d %H:%M:%S.%N %z", tm, 0, t.tv_nsec);
return str;
}
static void
out_string (char *pformat, size_t prefix_len, char const *arg)
{
strcpy (pformat + prefix_len, "s");
printf (pformat, arg);
}
static void
out_int (char *pformat, size_t prefix_len, intmax_t arg)
{
strcpy (pformat + prefix_len, PRIdMAX);
printf (pformat, arg);
}
static void
out_uint (char *pformat, size_t prefix_len, uintmax_t arg)
{
strcpy (pformat + prefix_len, PRIuMAX);
printf (pformat, arg);
}
static void
out_uint_o (char *pformat, size_t prefix_len, uintmax_t arg)
{
strcpy (pformat + prefix_len, PRIoMAX);
printf (pformat, arg);
}
static void
out_uint_x (char *pformat, size_t prefix_len, uintmax_t arg)
{
strcpy (pformat + prefix_len, PRIxMAX);
printf (pformat, arg);
}
/* Very specialized function (modifies FORMAT), just so as to avoid
duplicating this code between both print_statfs and print_stat.
Return zero upon success, nonzero upon failure. */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
out_file_context (char const *filename, char *pformat, size_t prefix_len)
{
char *scontext;
bool fail = false;
if ((follow_links
? getfilecon (filename, &scontext)
: lgetfilecon (filename, &scontext)) < 0)
{
error (0, errno, _("failed to get security context of %s"),
quote (filename));
scontext = NULL;
fail = true;
}
strcpy (pformat + prefix_len, "s");
printf (pformat, (scontext ? scontext : "?"));
if (scontext)
freecon (scontext);
return fail;
}
/* Print statfs info. Return zero upon success, nonzero upon failure. */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
print_statfs (char *pformat, size_t prefix_len, char m, char const *filename,
void const *data)
{
STRUCT_STATVFS const *statfsbuf = data;
bool fail = false;
switch (m)
{
case 'n':
out_string (pformat, prefix_len, filename);
break;
case 'i':
{
#if STRUCT_STATXFS_F_FSID_IS_INTEGER
uintmax_t fsid = statfsbuf->f_fsid;
#else
typedef unsigned int fsid_word;
verify (alignof (STRUCT_STATVFS) % alignof (fsid_word) == 0);
verify (offsetof (STRUCT_STATVFS, f_fsid) % alignof (fsid_word) == 0);
verify (sizeof statfsbuf->f_fsid % alignof (fsid_word) == 0);
fsid_word const *p = (fsid_word *) &statfsbuf->f_fsid;
/* Assume a little-endian word order, as that is compatible
with glibc's statvfs implementation. */
uintmax_t fsid = 0;
int words = sizeof statfsbuf->f_fsid / sizeof *p;
int i;
for (i = 0; i < words && i * sizeof *p < sizeof fsid; i++)
{
uintmax_t u = p[words - 1 - i];
fsid |= u << (i * CHAR_BIT * sizeof *p);
}
#endif
out_uint_x (pformat, prefix_len, fsid);
}
break;
case 'l':
OUT_NAMEMAX (pformat, prefix_len, SB_F_NAMEMAX (statfsbuf));
break;
case 't':
#if HAVE_STRUCT_STATXFS_F_TYPE
out_uint_x (pformat, prefix_len, statfsbuf->f_type);
#else
fputc ('?', stdout);
#endif
break;
case 'T':
out_string (pformat, prefix_len, human_fstype (statfsbuf));
break;
case 'b':
out_int (pformat, prefix_len, statfsbuf->f_blocks);
break;
case 'f':
out_int (pformat, prefix_len, statfsbuf->f_bfree);
break;
case 'a':
out_int (pformat, prefix_len, statfsbuf->f_bavail);
break;
case 's':
out_uint (pformat, prefix_len, statfsbuf->f_bsize);
break;
case 'S':
{
uintmax_t frsize = STATFS_FRSIZE (statfsbuf);
if (! frsize)
frsize = statfsbuf->f_bsize;
out_uint (pformat, prefix_len, frsize);
}
break;
case 'c':
out_uint (pformat, prefix_len, statfsbuf->f_files);
break;
case 'd':
out_int (pformat, prefix_len, statfsbuf->f_ffree);
break;
case 'C':
fail |= out_file_context (filename, pformat, prefix_len);
break;
default:
fputc ('?', stdout);
break;
}
return fail;
}
/* Return any bind mounted source for a path.
The caller should not free the returned buffer.
Return NULL if no bind mount found. */
static char const * ATTRIBUTE_WARN_UNUSED_RESULT
find_bind_mount (char const * name)
{
char const * bind_mount = NULL;
static struct mount_entry *mount_list;
static bool tried_mount_list = false;
if (!tried_mount_list) /* attempt/warn once per process. */
{
if (!(mount_list = read_file_system_list (false)))
error (0, errno, "%s", _("cannot read table of mounted file systems"));
tried_mount_list = true;
}
struct mount_entry *me;
for (me = mount_list; me; me = me->me_next)
{
if (me->me_dummy && me->me_devname[0] == '/'
&& STREQ (me->me_mountdir, name))
{
struct stat name_stats;
struct stat dev_stats;
if (stat (name, &name_stats) == 0
&& stat (me->me_devname, &dev_stats) == 0
&& SAME_INODE (name_stats, dev_stats))
{
bind_mount = me->me_devname;
break;
}
}
}
return bind_mount;
}
/* Print mount point. Return zero upon success, nonzero upon failure. */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
out_mount_point (char const *filename, char *pformat, size_t prefix_len,
const struct stat *statp)
{
char const *np = "?", *bp = NULL;
char *mp = NULL;
bool fail = true;
/* Look for bind mounts first. Note we output the immediate alias,
rather than further resolving to a base device mount point. */
if (follow_links || !S_ISLNK (statp->st_mode))
{
char *resolved = canonicalize_file_name (filename);
if (!resolved)
{
error (0, errno, _("failed to canonicalize %s"), quote (filename));
goto print_mount_point;
}
bp = find_bind_mount (resolved);
free (resolved);
if (bp)
{
fail = false;
goto print_mount_point;
}
}
/* If there is no direct bind mount, then navigate
back up the tree looking for a device change.
Note we don't detect if any of the directory components
are bind mounted to the same device, but that's OK
since we've not directly queried them. */
if ((mp = find_mount_point (filename, statp)))
{
/* This dir might be bind mounted to another device,
so we resolve the bound source in that case also. */
bp = find_bind_mount (mp);
fail = false;
}
print_mount_point:
out_string (pformat, prefix_len, bp ? bp : mp ? mp : np);
free (mp);
return fail;
}
/* Print stat info. Return zero upon success, nonzero upon failure. */
static bool
print_stat (char *pformat, size_t prefix_len, char m,
char const *filename, void const *data)
{
struct stat *statbuf = (struct stat *) data;
struct passwd *pw_ent;
struct group *gw_ent;
bool fail = false;
switch (m)
{
case 'n':
out_string (pformat, prefix_len, filename);
break;
case 'N':
out_string (pformat, prefix_len, quote (filename));
if (S_ISLNK (statbuf->st_mode))
{
char *linkname = areadlink_with_size (filename, statbuf->st_size);
if (linkname == NULL)
{
error (0, errno, _("cannot read symbolic link %s"),
quote (filename));
return true;
}
printf (" -> ");
out_string (pformat, prefix_len, quote (linkname));
free (linkname);
}
break;
case 'd':
out_uint (pformat, prefix_len, statbuf->st_dev);
break;
case 'D':
out_uint_x (pformat, prefix_len, statbuf->st_dev);
break;
case 'i':
out_uint (pformat, prefix_len, statbuf->st_ino);
break;
case 'a':
out_uint_o (pformat, prefix_len, statbuf->st_mode & CHMOD_MODE_BITS);
break;
case 'A':
out_string (pformat, prefix_len, human_access (statbuf));
break;
case 'f':
out_uint_x (pformat, prefix_len, statbuf->st_mode);
break;
case 'F':
out_string (pformat, prefix_len, file_type (statbuf));
break;
case 'h':
out_uint (pformat, prefix_len, statbuf->st_nlink);
break;
case 'u':
out_uint (pformat, prefix_len, statbuf->st_uid);
break;
case 'U':
setpwent ();
pw_ent = getpwuid (statbuf->st_uid);
out_string (pformat, prefix_len,
pw_ent ? pw_ent->pw_name : "UNKNOWN");
break;
case 'g':
out_uint (pformat, prefix_len, statbuf->st_gid);
break;
case 'G':
setgrent ();
gw_ent = getgrgid (statbuf->st_gid);
out_string (pformat, prefix_len,
gw_ent ? gw_ent->gr_name : "UNKNOWN");
break;
case 't':
out_uint_x (pformat, prefix_len, major (statbuf->st_rdev));
break;
case 'm':
fail |= out_mount_point (filename, pformat, prefix_len, statbuf);
break;
case 'T':
out_uint_x (pformat, prefix_len, minor (statbuf->st_rdev));
break;
case 's':
out_uint (pformat, prefix_len, statbuf->st_size);
break;
case 'B':
out_uint (pformat, prefix_len, ST_NBLOCKSIZE);
break;
case 'b':
out_uint (pformat, prefix_len, ST_NBLOCKS (*statbuf));
break;
case 'o':
out_uint (pformat, prefix_len, statbuf->st_blksize);
break;
case 'w':
{
struct timespec t = get_stat_birthtime (statbuf);
if (t.tv_nsec < 0)
out_string (pformat, prefix_len, "-");
else
out_string (pformat, prefix_len, human_time (t));
}
break;
case 'W':
{
struct timespec t = get_stat_birthtime (statbuf);
if (t.tv_nsec < 0)
out_string (pformat, prefix_len, "-");
else if (TYPE_SIGNED (time_t))
out_int (pformat, prefix_len, t.tv_sec);
else
out_uint (pformat, prefix_len, t.tv_sec);
}
break;
case 'x':
out_string (pformat, prefix_len, human_time (get_stat_atime (statbuf)));
break;
case 'X':
if (TYPE_SIGNED (time_t))
out_int (pformat, prefix_len, statbuf->st_atime);
else
out_uint (pformat, prefix_len, statbuf->st_atime);
break;
case 'y':
out_string (pformat, prefix_len, human_time (get_stat_mtime (statbuf)));
break;
case 'Y':
if (TYPE_SIGNED (time_t))
out_int (pformat, prefix_len, statbuf->st_mtime);
else
out_uint (pformat, prefix_len, statbuf->st_mtime);
break;
case 'z':
out_string (pformat, prefix_len, human_time (get_stat_ctime (statbuf)));
break;
case 'Z':
if (TYPE_SIGNED (time_t))
out_int (pformat, prefix_len, statbuf->st_ctime);
else
out_uint (pformat, prefix_len, statbuf->st_ctime);
break;
case 'C':
fail |= out_file_context (filename, pformat, prefix_len);
break;
default:
fputc ('?', stdout);
break;
}
return fail;
}
/* Output a single-character \ escape. */
static void
print_esc_char (char c)
{
switch (c)
{
case 'a': /* Alert. */
c ='\a';
break;
case 'b': /* Backspace. */
c ='\b';
break;
case 'e': /* Escape. */
c ='\x1B';
break;
case 'f': /* Form feed. */
c ='\f';
break;
case 'n': /* New line. */
c ='\n';
break;
case 'r': /* Carriage return. */
c ='\r';
break;
case 't': /* Horizontal tab. */
c ='\t';
break;
case 'v': /* Vertical tab. */
c ='\v';
break;
case '"':
case '\\':
break;
default:
error (0, 0, _("warning: unrecognized escape `\\%c'"), c);
break;
}
putchar (c);
}
/* Print the information specified by the format string, FORMAT,
calling PRINT_FUNC for each %-directive encountered.
Return zero upon success, nonzero upon failure. */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
print_it (char const *format, char const *filename,
bool (*print_func) (char *, size_t, char, char const *, void const *),
void const *data)
{
bool fail = false;
/* Add 2 to accommodate our conversion of the stat `%s' format string
to the longer printf `%llu' one. */
enum
{
MAX_ADDITIONAL_BYTES =
(MAX (sizeof PRIdMAX,
MAX (sizeof PRIoMAX, MAX (sizeof PRIuMAX, sizeof PRIxMAX)))
- 1)
};
size_t n_alloc = strlen (format) + MAX_ADDITIONAL_BYTES + 1;
char *dest = xmalloc (n_alloc);
char const *b;
for (b = format; *b; b++)
{
switch (*b)
{
case '%':
{
size_t len = strspn (b + 1, "#-+.I 0123456789");
char const *fmt_char = b + len + 1;
memcpy (dest, b, len + 1);
b = fmt_char;
switch (*fmt_char)
{
case '\0':
--b;
/* fall through */
case '%':
if (0 < len)
{
dest[len + 1] = *fmt_char;
dest[len + 2] = '\0';
error (EXIT_FAILURE, 0, _("%s: invalid directive"),
quotearg_colon (dest));
}
putchar ('%');
break;
default:
fail |= print_func (dest, len + 1, *fmt_char, filename, data);
break;
}
break;
}
case '\\':
if ( ! interpret_backslash_escapes)
{
putchar ('\\');
break;
}
++b;
if (isodigit (*b))
{
int esc_value = octtobin (*b);
int esc_length = 1; /* number of octal digits */
for (++b; esc_length < 3 && isodigit (*b);
++esc_length, ++b)
{
esc_value = esc_value * 8 + octtobin (*b);
}
putchar (esc_value);
--b;
}
else if (*b == 'x' && isxdigit (to_uchar (b[1])))
{
int esc_value = hextobin (b[1]); /* Value of \xhh escape. */
/* A hexadecimal \xhh escape sequence must have
1 or 2 hex. digits. */
++b;
if (isxdigit (to_uchar (b[1])))
{
++b;
esc_value = esc_value * 16 + hextobin (*b);
}
putchar (esc_value);
}
else if (*b == '\0')
{
error (0, 0, _("warning: backslash at end of format"));
putchar ('\\');
/* Arrange to exit the loop. */
--b;
}
else
{
print_esc_char (*b);
}
break;
default:
putchar (*b);
break;
}
}
free (dest);
fputs (trailing_delim, stdout);
return fail;
}
/* Stat the file system and print what we find. */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
do_statfs (char const *filename, bool terse, char const *format)
{
STRUCT_STATVFS statfsbuf;
if (STREQ (filename, "-"))
{
error (0, 0, _("using %s to denote standard input does not work"
" in file system mode"), quote (filename));
return false;
}
if (STATFS (filename, &statfsbuf) != 0)
{
error (0, errno, _("cannot read file system information for %s"),
quote (filename));
return false;
}
if (format == NULL)
{
format = (terse
? "%n %i %l %t %s %S %b %f %a %c %d\n"
: " File: \"%n\"\n"
" ID: %-8i Namelen: %-7l Type: %T\n"
"Block size: %-10s Fundamental block size: %S\n"
"Blocks: Total: %-10b Free: %-10f Available: %a\n"
"Inodes: Total: %-10c Free: %d\n");
}
bool fail = print_it (format, filename, print_statfs, &statfsbuf);
return ! fail;
}
/* stat the file and print what we find */
static bool ATTRIBUTE_WARN_UNUSED_RESULT
do_stat (char const *filename, bool terse, char const *format)
{
struct stat statbuf;
if (STREQ (filename, "-"))
{
if (fstat (STDIN_FILENO, &statbuf) != 0)
{
error (0, errno, _("cannot stat standard input"));
return false;
}
}
/* We can't use the shorter
(follow_links?stat:lstat) (filename, &statbug)
since stat might be a function-like macro. */
else if ((follow_links
? stat (filename, &statbuf)
: lstat (filename, &statbuf)) != 0)
{
error (0, errno, _("cannot stat %s"), quote (filename));
return false;
}
if (format == NULL)
{
if (terse)
{
format = "%n %s %b %f %u %g %D %i %h %t %T %X %Y %Z %W %o\n";
}
else
{
/* Temporary hack to match original output until conditional
implemented. */
if (S_ISBLK (statbuf.st_mode) || S_ISCHR (statbuf.st_mode))
{
format =
" File: %N\n"
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
"Device: %Dh/%dd\tInode: %-10i Links: %-5h"
" Device type: %t,%T\n"
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
"Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: %w\n";
}
else
{
format =
" File: %N\n"
" Size: %-10s\tBlocks: %-10b IO Block: %-6o %F\n"
"Device: %Dh/%dd\tInode: %-10i Links: %h\n"
"Access: (%04a/%10.10A) Uid: (%5u/%8U) Gid: (%5g/%8G)\n"
"Access: %x\n" "Modify: %y\n" "Change: %z\n" " Birth: %w\n";
}
}
}
bool fail = print_it (format, filename, print_stat, &statbuf);
return ! fail;
}
void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
else
{
printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
fputs (_("\
Display file or file system status.\n\
\n\
-L, --dereference follow links\n\
-f, --file-system display file system status instead of file status\n\
"), stdout);
fputs (_("\
-c --format=FORMAT use the specified FORMAT instead of the default;\n\
output a newline after each use of FORMAT\n\
--printf=FORMAT like --format, but interpret backslash escapes,\n\
and do not output a mandatory trailing newline.\n\
If you want a newline, include \\n in FORMAT\n\
-t, --terse print the information in terse form\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
fputs (_("\n\
The valid format sequences for files (without --file-system):\n\
\n\
%a Access rights in octal\n\
%A Access rights in human readable form\n\
%b Number of blocks allocated (see %B)\n\
%B The size in bytes of each block reported by %b\n\
%C SELinux security context string\n\
"), stdout);
fputs (_("\
%d Device number in decimal\n\
%D Device number in hex\n\
%f Raw mode in hex\n\
%F File type\n\
%g Group ID of owner\n\
%G Group name of owner\n\
"), stdout);
fputs (_("\
%h Number of hard links\n\
%i Inode number\n\
%m Mount point\n\
%n File name\n\
%N Quoted file name with dereference if symbolic link\n\
%o I/O block size\n\
%s Total size, in bytes\n\
%t Major device type in hex\n\
%T Minor device type in hex\n\
"), stdout);
fputs (_("\
%u User ID of owner\n\
%U User name of owner\n\
%w Time of file birth, or - if unknown\n\
%W Time of file birth as seconds since Epoch, or - if unknown\n\
%x Time of last access\n\
%X Time of last access as seconds since Epoch\n\
%y Time of last modification\n\
%Y Time of last modification as seconds since Epoch\n\
%z Time of last change\n\
%Z Time of last change as seconds since Epoch\n\
\n\
"), stdout);
fputs (_("\
Valid format sequences for file systems:\n\
\n\
%a Free blocks available to non-superuser\n\
%b Total data blocks in file system\n\
%c Total file nodes in file system\n\
%d Free file nodes in file system\n\
%f Free blocks in file system\n\
%C SELinux security context string\n\
"), stdout);
fputs (_("\
%i File System ID in hex\n\
%l Maximum length of filenames\n\
%n File name\n\
%s Block size (for faster transfers)\n\
%S Fundamental block size (for block counts)\n\
%t Type in hex\n\
%T Type in human readable form\n\
"), stdout);
printf (USAGE_BUILTIN_WARNING, PROGRAM_NAME);
emit_ancillary_info ();
}
exit (status);
}
int
main (int argc, char *argv[])
{
int c;
int i;
bool fs = false;
bool terse = false;
char *format = NULL;
bool ok = true;
initialize_main (&argc, &argv);
set_program_name (argv[0]);
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
atexit (close_stdout);
while ((c = getopt_long (argc, argv, "c:fLt", long_options, NULL)) != -1)
{
switch (c)
{
case PRINTF_OPTION:
format = optarg;
interpret_backslash_escapes = true;
trailing_delim = "";
break;
case 'c':
format = optarg;
interpret_backslash_escapes = false;
trailing_delim = "\n";
break;
case 'L':
follow_links = true;
break;
case 'f':
fs = true;
break;
case 't':
terse = true;
break;
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
default:
usage (EXIT_FAILURE);
}
}
if (argc == optind)
{
error (0, 0, _("missing operand"));
usage (EXIT_FAILURE);
}
for (i = optind; i < argc; i++)
ok &= (fs
? do_statfs (argv[i], terse, format)
: do_stat (argv[i], terse, format));
exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
}
|