viced-vol-ansification-20030303
[openafs.git] / src / vol / gi.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 RCSID("$Header$");
14
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdio.h>
18
19 int statflag;
20
21 #include "AFS_component_version_number.c"
22
23 int main(int argc, char **argv)
24 {
25         int error=0;
26         struct stat status;
27         int dev, fd, inode;
28
29 #if defined(AFS_NT40_ENV) || defined(AFS_NAMEI_ENV)
30         fprintf(stderr, "gi not supported on NT or NAMEI systems.\n");
31         exit(1);
32 #else
33
34         argc--; argv++;
35         while (argc && **argv == '-') {
36                 if (strcmp(*argv, "-stat") == 0)
37                         statflag = 1;
38                 else {
39                         error=1;
40                         break;
41                 }
42                 argc--; argv++;
43         }
44         if (error || argc!=2) {
45                 fprintf(stderr, "Usage: gi [-stat] partition inodenumber\n");
46                 exit(1);
47         }
48         if (stat(*argv, &status) != 0) {
49                 fprintf(stderr, "gi: cannot stat %s [should be mounted partition name]\n", *argv);
50                 exit(1);
51         }
52         dev = status.st_dev;
53         inode = atoi(*++argv);
54         fd = iopen(dev, inode, 0);
55         if (fd < 0) {
56                 Perror("Unable to open inode %d", inode);
57                 exit(1);
58         }
59         if (statflag) {
60                 if (fstat(fd, &status) != 0) {
61                         Perror("Unable to fstat the inode!");                   
62                         exit(1);
63                 }
64                 printf("Inode status: dev=%d, ino=%d, mode=%o, nlink=%d, uid=%d, gid=%d, size=%d, mtime=%d, blocks=%d\n", status.st_dev, status.st_ino, status.st_mode, status.st_nlink, status.st_uid, status.st_gid, status.st_size, status.st_mtime);
65         }
66         else {
67                 /* Send the inode to standard out */
68                 char buf[4096]; int n;
69                 while ((n = read(fd, buf, sizeof(buf))) >0)
70                         write(1, buf, n);
71         }
72         exit(0);
73 #endif /* AFS_NT40_ENV || AFS_NAMEI_ENV */
74 }
75
76 void Perror(char *err, int a1, int a2, int a3)
77 {
78     char msg[200];
79     sprintf(msg, err, a1, a2, a3);
80     perror(msg);
81 }
82