Sweep-fix comparing pointers with 0 (and assigning 0 to pointers).
[dragonfly.git] / sbin / fsdb / fsdbutil.c
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  * $FreeBSD: src/sbin/fsdb/fsdbutil.c,v 1.9.2.2 2002/03/20 13:39:02 joerg Exp $
31  */
32
33 #include <sys/param.h>
34 #include <ctype.h>
35 #include <err.h>
36 #include <grp.h>
37 #include <pwd.h>
38 #include <string.h>
39 #include <time.h>
40
41 #include <vfs/ufs/dinode.h>
42 #include <vfs/ufs/fs.h>
43
44 #include <sys/ioctl.h>
45
46 #include "fsdb.h"
47 #include "fsck.h"
48
49 static int charsperline(void);
50 static int printindir(ufs_daddr_t blk, int level, char *bufp);
51 static void printblocks(ino_t inum, struct ufs1_dinode *dp);
52
53 char **
54 crack(char *line, int *argc)
55 {
56     static char *argv[8];
57     int i;
58     char *p, *val;
59     for (p = line, i = 0; p != NULL && i < 8; i++) {
60         while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
61             /**/;
62         if (val)
63             argv[i] = val;
64         else
65             break;
66     }
67     *argc = i;
68     return argv;
69 }
70
71 int
72 argcount(struct cmdtable *cmdp, int argc, char **argv)
73 {
74     if (cmdp->minargc == cmdp->maxargc)
75         warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
76     else
77         warnx("command `%s' takes from %u to %u arguments",
78               cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
79             
80     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
81     return 1;
82 }
83
84 void
85 printstat(const char *cp, ino_t inum, struct ufs1_dinode *dp)
86 {
87     struct group *grp;
88     struct passwd *pw;
89     char *p;
90     time_t t;
91
92     printf("%s: ", cp);
93     switch (dp->di_mode & IFMT) {
94     case IFDIR:
95         puts("directory");
96         break;
97     case IFREG:
98         puts("regular file");
99         break;
100     case IFBLK:
101         printf("block special (%d,%d)",
102                major(dp->di_rdev), minor(dp->di_rdev));
103         break;
104     case IFCHR:
105         printf("character special (%d,%d)",
106                major(dp->di_rdev), minor(dp->di_rdev));
107         break;
108     case IFLNK:
109         fputs("symlink",stdout);
110         if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
111             dp->di_blocks == 0)
112             printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
113         else
114                 putchar('\n');
115         break;
116     case IFSOCK:
117         puts("socket");
118         break;
119     case IFIFO:
120         puts("fifo");
121         break;
122     }
123     printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
124     t = dp->di_mtime;
125     p = ctime(&t);
126     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
127            dp->di_mtimensec);
128     t = dp->di_ctime;
129     p = ctime(&t);
130     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
131            dp->di_ctimensec);
132     t = dp->di_atime;
133     p = ctime(&t);
134     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
135            dp->di_atimensec);
136
137     if ((pw = getpwuid(dp->di_uid)))
138         printf("OWNER=%s ", pw->pw_name);
139     else
140         printf("OWNUID=%u ", dp->di_uid);
141     if ((grp = getgrgid(dp->di_gid)))
142         printf("GRP=%s ", grp->gr_name);
143     else
144         printf("GID=%u ", dp->di_gid);
145
146     printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
147            dp->di_blocks, dp->di_gen);
148 }
149
150
151 /*
152  * Determine the number of characters in a
153  * single line.
154  */
155
156 static int
157 charsperline(void)
158 {
159         int columns;
160         char *cp;
161         struct winsize ws;
162
163         columns = 0;
164         if (ioctl(0, TIOCGWINSZ, &ws) != -1)
165                 columns = ws.ws_col;
166         if (columns == 0 && (cp = getenv("COLUMNS")))
167                 columns = atoi(cp);
168         if (columns == 0)
169                 columns = 80;   /* last resort */
170         return (columns);
171 }
172
173
174 /*
175  * Recursively print a list of indirect blocks.
176  */
177 static int
178 printindir(ufs_daddr_t blk, int level, char *bufp)
179 {
180     struct bufarea buf, *bp;
181     char tempbuf[32];           /* enough to print an ufs_daddr_t */
182     int i, j, cpl, charssofar;
183     ufs_daddr_t blkno;
184
185     if (level == 0) {
186         /* for the final indirect level, don't use the cache */
187         bp = &buf;
188         bp->b_un.b_buf = bufp;
189         bp->b_prev = bp->b_next = bp;
190         initbarea(bp);
191
192         getblk(bp, blk, sblock.fs_bsize);
193     } else
194         bp = getdatablk(blk, sblock.fs_bsize);
195
196     cpl = charsperline();
197     for (i = charssofar = 0; i < NINDIR(&sblock); i++) {
198         blkno = bp->b_un.b_indir[i];
199         if (blkno == 0) {
200             if (level == 0)
201                 putchar('\n');
202             return 0;
203         }
204         j = sprintf(tempbuf, "%d", blkno);
205         if (level == 0) {
206             charssofar += j;
207             if (charssofar >= cpl - 2) {
208                 putchar('\n');
209                 charssofar = j;
210             }
211         }
212         fputs(tempbuf, stdout);
213         if (level == 0) {
214             printf(", ");
215             charssofar += 2;
216         } else {
217             printf(" =>\n");
218             if (printindir(blkno, level - 1, bufp) == 0)
219                 return 0;
220         }
221     }
222     if (level == 0)
223         putchar('\n');
224     return 1;
225 }
226
227
228 /*
229  * Print the block pointers for one inode.
230  */
231 static void
232 printblocks(ino_t inum, struct ufs1_dinode *dp)
233 {
234     char *bufp;
235     int i, nfrags;
236     long ndb, offset;
237
238     printf("Blocks for inode %d:\n", inum);
239     printf("Direct blocks:\n");
240     ndb = howmany(dp->di_size, sblock.fs_bsize);
241     for (i = 0; i < NDADDR; i++) {
242         if (dp->di_db[i] == 0) {
243             putchar('\n');
244             return;
245         }
246         if (i > 0)
247             printf(", ");
248         printf("%d", dp->di_db[i]);
249         if (--ndb == 0 && (offset = blkoff(&sblock, dp->di_size)) != 0) {
250             nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
251             printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
252         }
253     }
254     putchar('\n');
255     if (dp->di_ib[0] == 0)
256         return;
257
258     bufp = malloc((unsigned int)sblock.fs_bsize);
259     if (bufp == NULL)
260         errx(EEXIT, "cannot allocate indirect block buffer");
261     printf("Indirect blocks:\n");
262     for (i = 0; i < NIADDR; i++)
263         if (printindir(dp->di_ib[i], i, bufp) == 0)
264             break;
265     free(bufp);
266 }
267
268
269 int
270 checkactive(void)
271 {
272     if (!curinode) {
273         warnx("no current inode\n");
274         return 0;
275     }
276     return 1;
277 }
278
279 int
280 checkactivedir(void)
281 {
282     if (!curinode) {
283         warnx("no current inode\n");
284         return 0;
285     }
286     if ((curinode->di_mode & IFMT) != IFDIR) {
287         warnx("inode %d not a directory", curinum);
288         return 0;
289     }
290     return 1;
291 }
292
293 int
294 printactive(int doblocks)
295 {
296     if (!checkactive())
297         return 1;
298     switch (curinode->di_mode & IFMT) {
299     case IFDIR:
300     case IFREG:
301     case IFBLK:
302     case IFCHR:
303     case IFLNK:
304     case IFSOCK:
305     case IFIFO:
306         if (doblocks)
307             printblocks(curinum, curinode);
308         else
309             printstat("current inode", curinum, curinode);
310         break;
311     case 0:
312         printf("current inode %d: unallocated inode\n", curinum);
313         break;
314     default:
315         printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
316                curinum, curinode->di_mode & IFMT, curinode->di_mode);
317         break;
318     }
319     return 0;
320 }