xref: /freebsd/sbin/fsdb/fsdbutil.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /*	$NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $	*/
2 
3 /*
4  *  Copyright (c) 1995 John T. Kohl
5  *  All rights reserved.
6  *
7  *  Redistribution and use in source and binary forms, with or without
8  *  modification, are permitted provided that the following conditions
9  *  are met:
10  *  1. Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  *  2. Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *  3. The name of the author may not be used to endorse or promote products
16  *     derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef lint
32 static const char rcsid[] =
33   "$FreeBSD$";
34 #endif /* not lint */
35 
36 #include <sys/types.h>
37 #include <ctype.h>
38 #include <err.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <string.h>
42 #include <time.h>
43 
44 #include <ufs/ufs/dinode.h>
45 
46 #include "fsdb.h"
47 #include "fsck.h"
48 
49 char **
50 crack(line, argc)
51 	char *line;
52 	int *argc;
53 {
54     static char *argv[8];
55     int i;
56     char *p, *val;
57     for (p = line, i = 0; p != NULL && i < 8; i++) {
58 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
59 	    /**/;
60 	if (val)
61 	    argv[i] = val;
62 	else
63 	    break;
64     }
65     *argc = i;
66     return argv;
67 }
68 
69 int
70 argcount(cmdp, argc, argv)
71 	struct cmdtable *cmdp;
72 	int argc;
73 	char *argv[];
74 {
75     if (cmdp->minargc == cmdp->maxargc)
76 	warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
77     else
78 	warnx("command `%s' takes from %u to %u arguments",
79 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
80 
81     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
82     return 1;
83 }
84 
85 void
86 printstat(cp, inum, dp)
87 	const char *cp;
88 	ino_t inum;
89 	struct dinode *dp;
90 {
91     struct group *grp;
92     struct passwd *pw;
93     char *p;
94     time_t t;
95 
96     printf("%s: ", cp);
97     switch (dp->di_mode & IFMT) {
98     case IFDIR:
99 	puts("directory");
100 	break;
101     case IFREG:
102 	puts("regular file");
103 	break;
104     case IFBLK:
105 	printf("block special (%d,%d)",
106 	       major(dp->di_rdev), minor(dp->di_rdev));
107 	break;
108     case IFCHR:
109 	printf("character special (%d,%d)",
110 	       major(dp->di_rdev), minor(dp->di_rdev));
111 	break;
112     case IFLNK:
113 	fputs("symlink",stdout);
114 	if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
115 	    dp->di_blocks == 0)
116 	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
117 	else
118 		putchar('\n');
119 	break;
120     case IFSOCK:
121 	puts("socket");
122 	break;
123     case IFIFO:
124 	puts("fifo");
125 	break;
126     }
127     printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
128     t = dp->di_mtime;
129     p = ctime(&t);
130     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
131 	   dp->di_mtimensec);
132     t = dp->di_ctime;
133     p = ctime(&t);
134     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
135 	   dp->di_ctimensec);
136     t = dp->di_atime;
137     p = ctime(&t);
138     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
139 	   dp->di_atimensec);
140 
141     if ((pw = getpwuid(dp->di_uid)))
142 	printf("OWNER=%s ", pw->pw_name);
143     else
144 	printf("OWNUID=%u ", dp->di_uid);
145     if ((grp = getgrgid(dp->di_gid)))
146 	printf("GRP=%s ", grp->gr_name);
147     else
148 	printf("GID=%u ", dp->di_gid);
149 
150     printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
151 	   dp->di_blocks, dp->di_gen);
152 }
153 
154 int
155 checkactive()
156 {
157     if (!curinode) {
158 	warnx("no current inode\n");
159 	return 0;
160     }
161     return 1;
162 }
163 
164 int
165 checkactivedir()
166 {
167     if (!curinode) {
168 	warnx("no current inode\n");
169 	return 0;
170     }
171     if ((curinode->di_mode & IFMT) != IFDIR) {
172 	warnx("inode %d not a directory", curinum);
173 	return 0;
174     }
175     return 1;
176 }
177 
178 int
179 printactive()
180 {
181     if (!checkactive())
182 	return 1;
183     switch (curinode->di_mode & IFMT) {
184     case IFDIR:
185     case IFREG:
186     case IFBLK:
187     case IFCHR:
188     case IFLNK:
189     case IFSOCK:
190     case IFIFO:
191 	printstat("current inode", curinum, curinode);
192 	break;
193     case 0:
194 	printf("current inode %d: unallocated inode\n", curinum);
195 	break;
196     default:
197 	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
198 	       curinum, curinode->di_mode & IFMT, curinode->di_mode);
199 	break;
200     }
201     return 0;
202 }
203