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