xref: /freebsd/sbin/fsdb/fsdbutil.c (revision 8e537d168674d6b65869f73c20813001af875738)
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 char rcsid[] = "$NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/param.h>
38 #include <sys/time.h>
39 #include <sys/mount.h>
40 #include <ctype.h>
41 #include <fcntl.h>
42 #include <grp.h>
43 #include <pwd.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ffs/fs.h>
51 
52 #include "fsdb.h"
53 #include "fsck.h"
54 
55 char **
56 crack(line, argc)
57 	char *line;
58 	int *argc;
59 {
60     static char *argv[8];
61     int i;
62     char *p, *val;
63     for (p = line, i = 0; p != NULL && i < 8; i++) {
64 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
65 	    /**/;
66 	if (val)
67 	    argv[i] = val;
68 	else
69 	    break;
70     }
71     *argc = i;
72     return argv;
73 }
74 
75 int
76 argcount(cmdp, argc, argv)
77 	struct cmdtable *cmdp;
78 	int argc;
79 	char *argv[];
80 {
81     if (cmdp->minargc == cmdp->maxargc)
82 	warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
83     else
84 	warnx("command `%s' takes from %u to %u arguments",
85 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
86 
87     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
88     return 1;
89 }
90 
91 void
92 printstat(cp, inum, dp)
93 	const char *cp;
94 	ino_t inum;
95 	struct dinode *dp;
96 {
97     struct group *grp;
98     struct passwd *pw;
99     char *p;
100 
101     printf("%s: ", cp);
102     switch (dp->di_mode & IFMT) {
103     case IFDIR:
104 	puts("directory");
105 	break;
106     case IFREG:
107 	puts("regular file");
108 	break;
109     case IFBLK:
110 	printf("block special (%d,%d)",
111 	       major(dp->di_rdev), minor(dp->di_rdev));
112 	break;
113     case IFCHR:
114 	printf("character special (%d,%d)",
115 	       major(dp->di_rdev), minor(dp->di_rdev));
116 	break;
117     case IFLNK:
118 	fputs("symlink",stdout);
119 	if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
120 	    dp->di_blocks == 0)
121 	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
122 	else
123 		putchar('\n');
124 	break;
125     case IFSOCK:
126 	puts("socket");
127 	break;
128     case IFIFO:
129 	puts("fifo");
130 	break;
131     }
132     printf("I=%lu MODE=%o SIZE=%qu", inum, dp->di_mode, dp->di_size);
133     p = ctime(&dp->di_mtime.tv_sec);
134     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
135 	   dp->di_mtime.tv_nsec);
136     p = ctime(&dp->di_ctime.tv_sec);
137     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
138 	   dp->di_ctime.tv_nsec);
139     p = ctime(&dp->di_atime.tv_sec);
140     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
141 	   dp->di_atime.tv_nsec);
142 
143     if (pw = getpwuid(dp->di_uid))
144 	printf("OWNER=%s ", pw->pw_name);
145     else
146 	printf("OWNUID=%u ", dp->di_uid);
147     if (grp = getgrgid(dp->di_gid))
148 	printf("GRP=%s ", grp->gr_name);
149     else
150 	printf("GID=%u ", dp->di_gid);
151 
152     printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
153 	   dp->di_blocks, dp->di_gen);
154 }
155 
156 int
157 checkactive()
158 {
159     if (!curinode) {
160 	warnx("no current inode\n");
161 	return 0;
162     }
163     return 1;
164 }
165 
166 int
167 checkactivedir()
168 {
169     if (!curinode) {
170 	warnx("no current inode\n");
171 	return 0;
172     }
173     if ((curinode->di_mode & IFMT) != IFDIR) {
174 	warnx("inode %d not a directory", curinum);
175 	return 0;
176     }
177     return 1;
178 }
179 
180 int
181 printactive()
182 {
183     if (!checkactive())
184 	return 1;
185     switch (curinode->di_mode & IFMT) {
186     case IFDIR:
187     case IFREG:
188     case IFBLK:
189     case IFCHR:
190     case IFLNK:
191     case IFSOCK:
192     case IFIFO:
193 	printstat("current inode", curinum, curinode);
194 	break;
195     case 0:
196 	printf("current inode %d: unallocated inode\n", curinum);
197 	break;
198     default:
199 	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
200 	       curinum, curinode->di_mode & IFMT, curinode->di_mode);
201 	break;
202     }
203     return 0;
204 }
205