allow mkfs arguments to be passed from the env for testing multiple
[xfstests-dev.git] / src / lstat64.c
1 /*
2  * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
3  * 
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  * 
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * 
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  * 
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  * 
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  * 
26  * http://www.sgi.com 
27  * 
28  * For further information regarding this notice, see: 
29  * 
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32  
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37 #include <sys/stat.h>
38 #include <sys/sysmacros.h>
39
40 long    timebuf;
41
42 void
43 timesince(long timesec)
44 {
45         long    d_since;        /* days */
46         long    h_since;        /* hours */
47         long    m_since;        /* minutes */
48         long    s_since;        /* seconds */
49
50         s_since = timebuf - timesec;
51         d_since = s_since / 86400l ;
52         s_since -= d_since * 86400l ;
53         h_since = s_since / 3600l ;
54         s_since -= h_since * 3600l ;
55         m_since = s_since / 60l ;
56         s_since -= m_since * 60l ;
57
58         printf("(%05ld.%02ld:%02ld:%02ld)\n",
59                         d_since, h_since, m_since, s_since);
60 }
61
62 int
63 main(int argc, char **argv)
64 {
65         struct stat64   sbuf;
66         char            mode[10];
67         int             i;
68
69         time(&timebuf);
70
71         for (i = 1; i < argc; i++) {
72
73                 if( lstat64(argv[i], &sbuf) < 0) {
74                         perror(argv[i]);
75                         continue;
76                 }
77
78                 printf("  File: \"%s\"\n", argv[i]);
79                 printf("  Size: %-10llu", sbuf.st_size);
80
81                 strcpy(mode,"----------");
82                 if (sbuf.st_mode & (S_IEXEC>>6))
83                         mode[9] = 'x';
84                 if (sbuf.st_mode & (S_IWRITE>>6))
85                         mode[8] = 'w';
86                 if (sbuf.st_mode & (S_IREAD>>6))
87                         mode[7] = 'r';
88                 if (sbuf.st_mode & (S_IEXEC>>3))
89                         mode[6] = 'x';
90                 if (sbuf.st_mode & (S_IWRITE>>3))
91                         mode[5] = 'w';
92                 if (sbuf.st_mode & (S_IREAD>>3))
93                         mode[4] = 'r';
94                 if (sbuf.st_mode & S_IEXEC)
95                         mode[3] = 'x';
96                 if (sbuf.st_mode & S_IWRITE)
97                         mode[2] = 'w';
98                 if (sbuf.st_mode & S_IREAD)
99                         mode[1] = 'r';
100                 if (sbuf.st_mode & S_ISVTX)
101                         mode[9] = 't';
102                 if (sbuf.st_mode & S_ISGID)
103                         mode[6] = 's';
104                 if (sbuf.st_mode & S_ISUID)
105                         mode[3] = 's';
106
107                 printf("   Filetype: ");
108                 switch (sbuf.st_mode & S_IFMT) {
109                 case S_IFSOCK:  
110                         puts("Socket");
111                         mode[0] = 's';
112                         break;
113                 case S_IFDIR:   
114                         puts("Directory");
115                         mode[0] = 'd';
116                         break;
117                 case S_IFCHR:   
118                         puts("Character Device");
119                         mode[0] = 'c';
120                         break;
121                 case S_IFBLK:   
122                         puts("Block Device");
123                         mode[0] = 'b';
124                         break;
125                 case S_IFREG:   
126                         puts("Regular File");
127                         mode[0] = '-';
128                         break;
129                 case S_IFLNK:   
130                         puts("Symbolic Link");
131                         mode[0] = 'l';
132                         break;
133                 case S_IFIFO:   
134                         puts("Fifo File");
135                         mode[0] = 'f';
136                         break;
137                 default:        
138                         puts("Unknown");
139                         mode[0] = '?';
140                 }
141
142                 printf("  Mode: (%04o/%s)", sbuf.st_mode & 07777, mode);
143                 printf("         Uid: (%d)", sbuf.st_uid);
144                 printf("  Gid: (%d)\n", sbuf.st_gid);
145                 printf("Device: %2d,%-2d", major(sbuf.st_dev),
146                                 minor(sbuf.st_dev));
147                 printf("  Inode: %-10llu", (unsigned long long)sbuf.st_ino);
148                 printf("Links: %-5d", sbuf.st_nlink);
149
150                 if ( ((sbuf.st_mode & S_IFMT) == S_IFCHR)
151                     || ((sbuf.st_mode & S_IFMT) == S_IFBLK) )
152                         printf("     Device type: %2d,%-2d\n",
153                                 major(sbuf.st_rdev), minor(sbuf.st_rdev));
154                 else
155                         printf("\n");
156
157                 printf("Access: %.24s",ctime(&sbuf.st_atime));
158                 timesince(sbuf.st_atime);
159                 printf("Modify: %.24s",ctime(&sbuf.st_mtime));
160                 timesince(sbuf.st_mtime);
161                 printf("Change: %.24s",ctime(&sbuf.st_ctime));
162                 timesince(sbuf.st_ctime);
163
164                 if (i+1 < argc)
165                         printf("\n");
166         }
167         if (i == 1) {
168                 fprintf(stderr, "Usage: lstat64 filename...\n");
169                 exit(1);
170         }
171         exit(0);
172 }