overlay/020: make sure the system supports the required namespaces
[xfstests-dev.git] / src / lstat64.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2000-2002 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6  
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <sys/stat.h>
13 #include <sys/sysmacros.h>
14
15 long    timebuf;
16
17 void
18 timesince(long timesec)
19 {
20         long    d_since;        /* days */
21         long    h_since;        /* hours */
22         long    m_since;        /* minutes */
23         long    s_since;        /* seconds */
24
25         s_since = timebuf - timesec;
26         d_since = s_since / 86400l ;
27         s_since -= d_since * 86400l ;
28         h_since = s_since / 3600l ;
29         s_since -= h_since * 3600l ;
30         m_since = s_since / 60l ;
31         s_since -= m_since * 60l ;
32
33         printf("(%05ld.%02ld:%02ld:%02ld)\n",
34                         d_since, h_since, m_since, s_since);
35 }
36
37 void
38 usage(void)
39 {
40         fprintf(stderr, "Usage: lstat64 [-t] filename ...\n");
41         exit(1);
42 }
43
44 int
45 main(int argc, char **argv)
46 {
47         struct stat64   sbuf;
48         int             i, c;
49         int             terse_flag = 0;
50
51         while ((c = getopt(argc, argv, "t")) != EOF) {
52                 switch (c) {
53                         case 't':
54                                 terse_flag = 1;
55                                 break;
56
57                         case '?':
58                                 usage();
59                 }
60         }
61         if (optind == argc) {
62                 usage();
63         }
64
65         time(&timebuf);
66
67         for (i = optind; i < argc; i++) {
68                 char mode[] = "----------";
69
70                 if( lstat64(argv[i], &sbuf) < 0) {
71                         perror(argv[i]);
72                         continue;
73                 }
74
75                 if (terse_flag) {
76                         printf("%s %llu ", argv[i], (unsigned long long)sbuf.st_size);
77                 }
78                 else {
79                         printf("  File: \"%s\"\n", argv[i]);
80                         printf("  Size: %-10llu", (unsigned long long)sbuf.st_size);
81                 }
82
83                 if (sbuf.st_mode & S_IXOTH)
84                         mode[9] = 'x';
85                 if (sbuf.st_mode & S_IWOTH)
86                         mode[8] = 'w';
87                 if (sbuf.st_mode & S_IROTH)
88                         mode[7] = 'r';
89                 if (sbuf.st_mode & S_IXGRP)
90                         mode[6] = 'x';
91                 if (sbuf.st_mode & S_IWGRP)
92                         mode[5] = 'w';
93                 if (sbuf.st_mode & S_IRGRP)
94                         mode[4] = 'r';
95                 if (sbuf.st_mode & S_IXUSR)
96                         mode[3] = 'x';
97                 if (sbuf.st_mode & S_IWUSR)
98                         mode[2] = 'w';
99                 if (sbuf.st_mode & S_IRUSR)
100                         mode[1] = 'r';
101                 if (sbuf.st_mode & S_ISVTX)
102                         mode[9] = 't';
103                 if (sbuf.st_mode & S_ISGID)
104                         mode[6] = 's';
105                 if (sbuf.st_mode & S_ISUID)
106                         mode[3] = 's';
107
108                 if (!terse_flag)
109                         printf("   Filetype: ");
110                 switch (sbuf.st_mode & S_IFMT) {
111                 case S_IFSOCK:  
112                         if (!terse_flag)
113                                 puts("Socket");
114                         mode[0] = 's';
115                         break;
116                 case S_IFDIR:   
117                         if (!terse_flag)
118                                 puts("Directory");
119                         mode[0] = 'd';
120                         break;
121                 case S_IFCHR:   
122                         if (!terse_flag)
123                                 puts("Character Device");
124                         mode[0] = 'c';
125                         break;
126                 case S_IFBLK:   
127                         if (!terse_flag)
128                                 puts("Block Device");
129                         mode[0] = 'b';
130                         break;
131                 case S_IFREG:   
132                         if (!terse_flag)
133                                 puts("Regular File");
134                         mode[0] = '-';
135                         break;
136                 case S_IFLNK:   
137                         if (!terse_flag)
138                                 puts("Symbolic Link");
139                         mode[0] = 'l';
140                         break;
141                 case S_IFIFO:   
142                         if (!terse_flag)
143                                 puts("Fifo File");
144                         mode[0] = 'f';
145                         break;
146                 default:        
147                         if (!terse_flag)
148                                 puts("Unknown");
149                         mode[0] = '?';
150                 }
151
152                 if (terse_flag) {
153                         printf("%s %d,%d\n", mode, (int)sbuf.st_uid, (int)sbuf.st_gid);
154                         continue;
155                 }
156
157                 printf("  Mode: (%04o/%s)", (unsigned int)(sbuf.st_mode & 07777), mode);
158                 printf("         Uid: (%d)", (int)sbuf.st_uid);
159                 printf("  Gid: (%d)\n", (int)sbuf.st_gid);
160                 printf("Device: %2d,%-2d", major(sbuf.st_dev),
161                                 minor(sbuf.st_dev));
162                 printf("  Inode: %-9llu", (unsigned long long)sbuf.st_ino);
163                 printf(" Links: %-5ld", (long)sbuf.st_nlink);
164
165                 if ( ((sbuf.st_mode & S_IFMT) == S_IFCHR)
166                     || ((sbuf.st_mode & S_IFMT) == S_IFBLK) )
167                         printf("     Device type: %2d,%-2d\n",
168                                 major(sbuf.st_rdev), minor(sbuf.st_rdev));
169                 else
170                         printf("\n");
171
172                 printf("Access: %.24s",ctime(&sbuf.st_atime));
173                 timesince(sbuf.st_atime);
174                 printf("Modify: %.24s",ctime(&sbuf.st_mtime));
175                 timesince(sbuf.st_mtime);
176                 printf("Change: %.24s",ctime(&sbuf.st_ctime));
177                 timesince(sbuf.st_ctime);
178
179                 if (i+1 < argc)
180                         printf("\n");
181         }
182         exit(0);
183 }