fix problems with includes now that our previous linux/acl.h has gone away.
[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 <time.h>
35 #include <sys/stat.h>
36 #include <sys/sysmacros.h>
37
38 long    timebuf;
39
40 void
41 timesince(long timesec)
42 {
43         long    d_since;        /* days */
44         long    h_since;        /* hours */
45         long    m_since;        /* minutes */
46         long    s_since;        /* seconds */
47
48         s_since = timebuf - timesec;
49         d_since = s_since / 86400l ;
50         s_since -= d_since * 86400l ;
51         h_since = s_since / 3600l ;
52         s_since -= h_since * 3600l ;
53         m_since = s_since / 60l ;
54         s_since -= m_since * 60l ;
55
56         printf("(%05ld.%02ld:%02ld:%02ld)\n",
57                         d_since, h_since, m_since, s_since);
58 }
59
60 int
61 main(int argc, char **argv)
62 {
63         struct stat64   sbuf;
64         char            mode[10];
65         int             i;
66
67         time(&timebuf);
68
69         for (i = 1; i < argc; i++) {
70
71                 if( lstat64(argv[i], &sbuf) < 0) {
72                         perror(argv[i]);
73                         continue;
74                 }
75
76                 printf("  File: \"%s\"\n", argv[i]);
77                 printf("  Size: %-10llu", sbuf.st_size);
78
79                 strcpy(mode,"----------");
80                 if (sbuf.st_mode & (S_IEXEC>>6))
81                         mode[9] = 'x';
82                 if (sbuf.st_mode & (S_IWRITE>>6))
83                         mode[8] = 'w';
84                 if (sbuf.st_mode & (S_IREAD>>6))
85                         mode[7] = 'r';
86                 if (sbuf.st_mode & (S_IEXEC>>3))
87                         mode[6] = 'x';
88                 if (sbuf.st_mode & (S_IWRITE>>3))
89                         mode[5] = 'w';
90                 if (sbuf.st_mode & (S_IREAD>>3))
91                         mode[4] = 'r';
92                 if (sbuf.st_mode & S_IEXEC)
93                         mode[3] = 'x';
94                 if (sbuf.st_mode & S_IWRITE)
95                         mode[2] = 'w';
96                 if (sbuf.st_mode & S_IREAD)
97                         mode[1] = 'r';
98                 if (sbuf.st_mode & S_ISVTX)
99                         mode[9] = 't';
100                 if (sbuf.st_mode & S_ISGID)
101                         mode[6] = 's';
102                 if (sbuf.st_mode & S_ISUID)
103                         mode[3] = 's';
104
105                 printf("   Filetype: ");
106                 switch (sbuf.st_mode & S_IFMT) {
107                 case S_IFSOCK:  
108                         puts("Socket");
109                         mode[0] = 's';
110                         break;
111                 case S_IFDIR:   
112                         puts("Directory");
113                         mode[0] = 'd';
114                         break;
115                 case S_IFCHR:   
116                         puts("Character Device");
117                         mode[0] = 'c';
118                         break;
119                 case S_IFBLK:   
120                         puts("Block Device");
121                         mode[0] = 'b';
122                         break;
123                 case S_IFREG:   
124                         puts("Regular File");
125                         mode[0] = '-';
126                         break;
127                 case S_IFLNK:   
128                         puts("Symbolic Link");
129                         mode[0] = 'l';
130                         break;
131                 case S_IFIFO:   
132                         puts("Fifo File");
133                         mode[0] = 'f';
134                         break;
135                 default:        
136                         puts("Unknown");
137                         mode[0] = '?';
138                 }
139
140                 printf("  Mode: (%04o/%s)", sbuf.st_mode & 07777, mode);
141                 printf("         Uid: (%d)", sbuf.st_uid);
142                 printf("  Gid: (%d)\n", sbuf.st_gid);
143                 printf("Device: %2d,%-2d", major(sbuf.st_dev),
144                                 minor(sbuf.st_dev));
145                 printf("  Inode: %-10llu", (unsigned long long)sbuf.st_ino);
146                 printf("Links: %-5d", sbuf.st_nlink);
147
148                 if ( ((sbuf.st_mode & S_IFMT) == S_IFCHR)
149                     || ((sbuf.st_mode & S_IFMT) == S_IFBLK) )
150                         printf("     Device type: %2d,%-2d\n",
151                                 major(sbuf.st_rdev), minor(sbuf.st_rdev));
152                 else
153                         printf("\n");
154
155                 printf("Access: %.24s",ctime(&sbuf.st_atime));
156                 timesince(sbuf.st_atime);
157                 printf("Modify: %.24s",ctime(&sbuf.st_mtime));
158                 timesince(sbuf.st_mtime);
159                 printf("Change: %.24s",ctime(&sbuf.st_ctime));
160                 timesince(sbuf.st_ctime);
161
162                 if (i+1 < argc)
163                         printf("\n");
164         }
165         if (i == 1) {
166                 fprintf(stderr, "Usage: lstat64 filename...\n");
167                 exit(1);
168         }
169         exit(0);
170 }