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