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