File: stat.c

package info (click to toggle)
stat 2.2-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 68 kB
  • ctags: 30
  • sloc: ansic: 276; makefile: 51
file content (281 lines) | stat: -rw-r--r-- 7,110 bytes parent folder | download
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
#include <stdio.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <time.h>
#include <sys/vfs.h>

#include "fs.h"

/* stat the filesystem and print what we find */
void do_statfs(char *filename, int terse)
{
        struct statfs statfsbuf;

        if (statfs(filename, &statfsbuf) == -1) {
        	perror(filename);
       	        return;
        }

	if (terse != 0) {
		printf("%s %x %x %d %x %ld %ld %ld %d %ld %ld\n",
			filename,
			statfsbuf.f_fsid.__val[0],
			statfsbuf.f_fsid.__val[1],
			statfsbuf.f_namelen,
			statfsbuf.f_type,
			statfsbuf.f_blocks,
			statfsbuf.f_bfree,
			statfsbuf.f_bavail,
			statfsbuf.f_bsize,
			statfsbuf.f_files,
			statfsbuf.f_ffree);
			
		return;
	}
	
	printf("  File: \"%s\"\n", filename);
	printf("    ID: %-8x %-8x Namelen: %-7d Type: ", statfsbuf.f_fsid.__val[0], statfsbuf.f_fsid.__val[1], statfsbuf.f_namelen);

        switch (statfsbuf.f_type) {
        	case AFFS_SUPER_MAGIC:
        		printf("AFFS\n");
        		break;
        	case EXT_SUPER_MAGIC:
        		printf("EXT\n");
        		break;
        	case EXT2_OLD_SUPER_MAGIC:
        		printf("EXT2\n");
        		break;
        	case EXT2_SUPER_MAGIC:
        		printf("EXT2\n");
        		break;
		case HPFS_SUPER_MAGIC:
			printf("HPFS\n");
			break;
		case ISOFS_SUPER_MAGIC:
			printf("ISOFS\n");
			break;
		case MINIX_SUPER_MAGIC:
			printf("MINIX\n");
		case MINIX_SUPER_MAGIC2:
			printf("MINIX (30 char.)\n");
			break;
		case MINIX2_SUPER_MAGIC:
			printf("MINIX V2\n");
			break;
		case MINIX2_SUPER_MAGIC2:
			printf("MINIX V2 (30 char.)\n");
			break;
		case MSDOS_SUPER_MAGIC:
			printf("MSDOS\n");
			break;
		case NCP_SUPER_MAGIC:
			printf("NOVELL\n");
			break;
		case NFS_SUPER_MAGIC:
			printf("NFS\n");
			break;
		case PROC_SUPER_MAGIC:
			printf("PROC\n");
			break;
		case SMB_SUPER_MAGIC:
			printf("SMB\n");
			break;
		case XENIX_SUPER_MAGIC:
			printf("XENIX\n");
			break;
		case SYSV4_SUPER_MAGIC:
			printf("SYSV4\n");
			break;
		case SYSV2_SUPER_MAGIC:
			printf("SYSV2\n");
			break;
		case COH_SUPER_MAGIC:
			printf("COH\n");
			break;
		case UFS_MAGIC:
			printf("UFS\n");
			break;
		case _XIAFS_SUPER_MAGIC:
			printf("XIA\n");
			break;
		case NTFS_SUPER_MAGIC:
			printf("NTFS\n");
			break;
        	default:
        		printf("UNKNOWN (0x%x)\n", statfsbuf.f_type);
        }
	printf("Blocks: Total: %-10ld Free: %-10ld Available: %-10ld Size: %d\n", statfsbuf.f_blocks, statfsbuf.f_bfree, statfsbuf.f_bavail, statfsbuf.f_bsize);
	printf("Inodes: Total: %-10ld Free: %-10ld\n", statfsbuf.f_files, statfsbuf.f_ffree);
}
                                                                                                                                                         
/* stat the file and print what we find */
void do_stat(char *filename, int link, int terse)
{
    struct stat statbuf;
    int i = (link == 1) ? stat(filename, &statbuf) : lstat(filename, &statbuf);
    char access[10];
    struct passwd *pw_ent;
    struct group *gw_ent;
    char linkname[256];

    if (i == -1) {
	perror(filename);
	return;
    }
    
    if (terse != 0) {
		printf("%s %u %u %x %d %d %x %d %d %x %x %d %d %d\n",
			filename,
			(unsigned int)statbuf.st_size,
			(unsigned int)statbuf.st_blocks,
			statbuf.st_mode,
			statbuf.st_uid,
			statbuf.st_gid,
			(int)statbuf.st_dev,
			(int)statbuf.st_ino,
			(int)statbuf.st_nlink,
			major(statbuf.st_rdev),
			minor(statbuf.st_rdev),
			(int)statbuf.st_atime,
			(int)statbuf.st_mtime,
			(int)statbuf.st_ctime);
		return;
	}

    if ((statbuf.st_mode & S_IFMT) == S_IFLNK) {
	if ((i = readlink(filename, linkname, 256)) == -1) {
	    perror(filename);
	    return;
	}

	linkname[(i >= 256) ? 255 : i] = '\0';
	printf("  File: \"%s\" -> \"%s\"\n", filename, linkname);
    } else
	printf("  File: \"%s\"\n", filename);

    printf("  Size: %-10u Blocks: %-10u", (unsigned int)statbuf.st_size, (unsigned int)statbuf.st_blocks);

    switch (statbuf.st_mode & S_IFMT) {
    case S_IFDIR:
	(void) printf("Directory\n");
	break;
    case S_IFCHR:
	(void) printf("Character Device\n");
	break;
    case S_IFBLK:
	(void) printf("Block Device\n");
	break;
    case S_IFREG:
	(void) printf("Regular File\n");
	break;
    case S_IFLNK:
	(void) printf("Symbolic Link\n");
	break;
    case S_IFSOCK:
	(void) printf("Socket\n");
	break;
    case S_IFIFO:
	(void) printf("Fifo File\n");
	break;
    default:
	(void) printf("Unknown\n");
    }

    access[9] = (statbuf.st_mode & S_IXOTH) ? ((statbuf.st_mode & S_ISVTX) ? 't' : 'x') : ((statbuf.st_mode & S_ISVTX) ? 'T' : '-');
    access[8] = (statbuf.st_mode & S_IWOTH) ? 'w' : '-';
    access[7] = (statbuf.st_mode & S_IROTH) ? 'r' : '-';
    access[6] = (statbuf.st_mode & S_IXGRP) ? ((statbuf.st_mode & S_ISGID) ? 's' : 'x') : ((statbuf.st_mode & S_ISGID) ? 'S' : '-');
    access[5] = (statbuf.st_mode & S_IWGRP) ? 'w' : '-';
    access[4] = (statbuf.st_mode & S_IRGRP) ? 'r' : '-';
    access[3] = (statbuf.st_mode & S_IXUSR) ? ((statbuf.st_mode & S_ISUID) ? 's' : 'x') :  ((statbuf.st_mode & S_ISUID) ? 'S' : '-');
    access[2] = (statbuf.st_mode & S_IWUSR) ? 'w' : '-';
    access[1] = (statbuf.st_mode & S_IRUSR) ? 'r' : '-';

    switch (statbuf.st_mode & S_IFMT) {
    case S_IFDIR:
	access[0] = 'd';
	break;
    case S_IFCHR:
	access[0] = 'c';
	break;
    case S_IFBLK:
	access[0] = 'b';
	break;
    case S_IFREG:
	access[0] = '-';
	break;
    case S_IFLNK:
	access[0] = 'l';
	break;
    case S_IFSOCK:
	access[0] = 's';
	break;
    case S_IFIFO:
	access[0] = 'p';
	break;
    default:
	access[0] = '?';
    }
    (void) printf("Access: (%04o/%10.10s)", statbuf.st_mode & 07777, access);

    setpwent();
    setgrent();
    pw_ent = getpwuid(statbuf.st_uid);
    gw_ent = getgrgid(statbuf.st_gid);
    printf("         Uid: (%5d/%8s)  Gid: (%5d/%8s)\n", statbuf.st_uid,
    		(pw_ent != 0L) ? pw_ent->pw_name : "UNKNOWN", statbuf.st_gid,
    		(gw_ent != 0L) ? gw_ent->gr_name : "UNKNOWN");

    printf("Device: %-10x Inode: %-10d Links: %-5d", (int)statbuf.st_dev,
    	 (int)statbuf.st_ino, (int)statbuf.st_nlink);

    i = statbuf.st_mode & S_IFMT;
    if (i == S_IFCHR || i == S_IFBLK)
	printf(" Device type: %x,%x\n", major(statbuf.st_rdev), minor(statbuf.st_rdev));
    else
	(void) printf("\n");

    printf("Access: %s", ctime(&statbuf.st_atime));
    printf("Modify: %s", ctime(&statbuf.st_mtime));
    printf("Change: %s\n", ctime(&statbuf.st_ctime));
}

void usage(char *progname)
{
    fprintf(stderr, "Usage: %s [-l] [-f] file1 [file2 ...]\n", progname);
    exit(1);
}

int main(int argc, char *argv[])
{
    int c, i, link = 0, fs = 0, terse = 0;

    while ((c = getopt(argc, argv, "lfvt")) != EOF) {
	switch (c) {
	case 'l':
	    link = 1;
	    break;
	case 'f':
	    fs = 1;
	    break;
	case 't':
	    terse = 1;
	    break;
	case 'v':
	    printf("stat version: 2.2\n");
	default:
	    usage(argv[0]);
	}
    }
    if (argc == 1)
	usage(argv[0]);

    for (i = optind; i < argc; i++)
    	(fs == 0) ? do_stat(argv[i], link, terse) : do_statfs(argv[i], terse);

    return (0);
}