1 /* $NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $ */
4 * Copyright (c) 1995 John T. Kohl
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 static const char rcsid[] =
33 "$FreeBSD: src/sbin/fsdb/fsdbutil.c,v 1.9.2.2 2002/03/20 13:39:02 joerg Exp $";
36 #include <sys/param.h>
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ffs/fs.h>
47 #include <sys/ioctl.h>
52 static int charsperline __P((void));
53 static int printindir __P((ufs_daddr_t blk, int level, char *bufp));
54 static void printblocks __P((ino_t inum, struct dinode *dp));
64 for (p = line, i = 0; p != NULL && i < 8; i++) {
65 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
77 argcount(cmdp, argc, argv)
78 struct cmdtable *cmdp;
82 if (cmdp->minargc == cmdp->maxargc)
83 warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
85 warnx("command `%s' takes from %u to %u arguments",
86 cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
88 warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
93 printstat(cp, inum, dp)
104 switch (dp->di_mode & IFMT) {
109 puts("regular file");
112 printf("block special (%d,%d)",
113 major(dp->di_rdev), minor(dp->di_rdev));
116 printf("character special (%d,%d)",
117 major(dp->di_rdev), minor(dp->di_rdev));
120 fputs("symlink",stdout);
121 if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
123 printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
134 printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
137 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
141 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
145 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
148 if ((pw = getpwuid(dp->di_uid)))
149 printf("OWNER=%s ", pw->pw_name);
151 printf("OWNUID=%u ", dp->di_uid);
152 if ((grp = getgrgid(dp->di_gid)))
153 printf("GRP=%s ", grp->gr_name);
155 printf("GID=%u ", dp->di_gid);
157 printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
158 dp->di_blocks, dp->di_gen);
163 * Determine the number of characters in a
175 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
177 if (columns == 0 && (cp = getenv("COLUMNS")))
180 columns = 80; /* last resort */
186 * Recursively print a list of indirect blocks.
189 printindir(blk, level, bufp)
194 struct bufarea buf, *bp;
195 char tempbuf[32]; /* enough to print an ufs_daddr_t */
196 int i, j, cpl, charssofar;
200 /* for the final indirect level, don't use the cache */
202 bp->b_un.b_buf = bufp;
203 bp->b_prev = bp->b_next = bp;
206 getblk(bp, blk, sblock.fs_bsize);
208 bp = getdatablk(blk, sblock.fs_bsize);
210 cpl = charsperline();
211 for (i = charssofar = 0; i < NINDIR(&sblock); i++) {
212 blkno = bp->b_un.b_indir[i];
218 j = sprintf(tempbuf, "%d", blkno);
221 if (charssofar >= cpl - 2) {
226 fputs(tempbuf, stdout);
232 if (printindir(blkno, level - 1, bufp) == 0)
243 * Print the block pointers for one inode.
246 printblocks(inum, dp)
254 printf("Blocks for inode %d:\n", inum);
255 printf("Direct blocks:\n");
256 ndb = howmany(dp->di_size, sblock.fs_bsize);
257 for (i = 0; i < NDADDR; i++) {
258 if (dp->di_db[i] == 0) {
264 printf("%d", dp->di_db[i]);
265 if (--ndb == 0 && (offset = blkoff(&sblock, dp->di_size)) != 0) {
266 nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
267 printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
271 if (dp->di_ib[0] == 0)
274 bufp = malloc((unsigned int)sblock.fs_bsize);
276 errx(EEXIT, "cannot allocate indirect block buffer");
277 printf("Indirect blocks:\n");
278 for (i = 0; i < NIADDR; i++)
279 if (printindir(dp->di_ib[i], i, bufp) == 0)
289 warnx("no current inode\n");
299 warnx("no current inode\n");
302 if ((curinode->di_mode & IFMT) != IFDIR) {
303 warnx("inode %d not a directory", curinum);
310 printactive(doblocks)
315 switch (curinode->di_mode & IFMT) {
324 printblocks(curinum, curinode);
326 printstat("current inode", curinum, curinode);
329 printf("current inode %d: unallocated inode\n", curinum);
332 printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
333 curinum, curinode->di_mode & IFMT, curinode->di_mode);