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