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[] =
36 #include <sys/param.h>
46 #include <ufs/ufs/dinode.h>
47 #include <ufs/ffs/fs.h>
49 #include <sys/ioctl.h>
54 static int charsperline(void);
55 static void printindir(ufs2_daddr_t blk, int level, char *bufp);
56 static void printblocks(ino_t inum, union dinode *dp);
59 crack(char *line, int *argc)
64 for (p = line, i = 0; p != NULL && i < 8; i++) {
65 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
77 recrack(char *line, int *argc, int argc_max)
82 for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
83 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
90 argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
91 argv[i][strcspn(argv[i], "\n")] = '\0';
97 argcount(struct cmdtable *cmdp, int argc, char *argv[])
99 if (cmdp->minargc == cmdp->maxargc)
100 warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
101 cmdp->minargc-1, argc-1);
103 warnx("command `%s' takes from %u to %u arguments",
104 cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
106 warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
111 printstat(const char *cp, ino_t inum, union dinode *dp)
121 switch (DIP(dp, di_mode) & IFMT) {
126 puts("regular file");
129 printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
132 printf("character special (%#jx)", DIP(dp, di_rdev));
135 fputs("symlink",stdout);
136 if (DIP(dp, di_size) > 0 &&
137 DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
138 DIP(dp, di_blocks) == 0) {
139 if (sblock.fs_magic == FS_UFS1_MAGIC)
140 p = (caddr_t)dp->dp1.di_db;
142 p = (caddr_t)dp->dp2.di_db;
143 printf(" to `%.*s'\n", (int) DIP(dp, di_size), p);
155 printf("I=%ju MODE=%o SIZE=%ju", (uintmax_t)inum, DIP(dp, di_mode),
156 (uintmax_t)DIP(dp, di_size));
157 if (sblock.fs_magic != FS_UFS1_MAGIC) {
158 t = _time64_to_time(dp->dp2.di_birthtime);
160 printf("\n\tBTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
161 dp->dp2.di_birthnsec);
163 if (sblock.fs_magic == FS_UFS1_MAGIC)
164 t = _time32_to_time(dp->dp1.di_mtime);
166 t = _time64_to_time(dp->dp2.di_mtime);
168 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
169 DIP(dp, di_mtimensec));
170 if (sblock.fs_magic == FS_UFS1_MAGIC)
171 t = _time32_to_time(dp->dp1.di_ctime);
173 t = _time64_to_time(dp->dp2.di_ctime);
175 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
176 DIP(dp, di_ctimensec));
177 if (sblock.fs_magic == FS_UFS1_MAGIC)
178 t = _time32_to_time(dp->dp1.di_atime);
180 t = _time64_to_time(dp->dp2.di_atime);
182 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
183 DIP(dp, di_atimensec));
185 if ((pw = getpwuid(DIP(dp, di_uid))))
186 printf("OWNER=%s ", pw->pw_name);
188 printf("OWNUID=%u ", DIP(dp, di_uid));
189 if ((grp = getgrgid(DIP(dp, di_gid))))
190 printf("GRP=%s ", grp->gr_name);
192 printf("GID=%u ", DIP(dp, di_gid));
194 blocks = DIP(dp, di_blocks);
195 gen = DIP(dp, di_gen);
196 printf("LINKCNT=%d FLAGS=%#x BLKCNT=%jx GEN=%jx\n", DIP(dp, di_nlink),
197 DIP(dp, di_flags), (intmax_t)blocks, (intmax_t)gen);
202 * Determine the number of characters in a
214 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
216 if (columns == 0 && (cp = getenv("COLUMNS")))
219 columns = 80; /* last resort */
225 * Recursively print a list of indirect blocks.
228 printindir(ufs2_daddr_t blk, int level, char *bufp)
230 struct bufarea buf, *bp;
231 char tempbuf[32]; /* enough to print an ufs2_daddr_t */
232 int i, j, cpl, charssofar;
237 printf("%jd (%d) =>\n", (intmax_t)blk, level);
239 /* for the final indirect level, don't use the cache */
241 bp->b_un.b_buf = bufp;
242 initbarea(bp, BT_UNKNOWN);
244 getblk(bp, blk, sblock.fs_bsize);
246 bp = getdatablk(blk, sblock.fs_bsize, BT_UNKNOWN);
248 cpl = charsperline();
249 for (i = charssofar = 0; i < NINDIR(&sblock); i++) {
250 if (sblock.fs_magic == FS_UFS1_MAGIC)
251 blkno = bp->b_un.b_indir1[i];
253 blkno = bp->b_un.b_indir2[i];
256 j = sprintf(tempbuf, "%jd", (intmax_t)blkno);
259 if (charssofar >= cpl - 2) {
264 fputs(tempbuf, stdout);
270 printindir(blkno, level - 1, bufp);
282 * Print the block pointers for one inode.
285 printblocks(ino_t inum, union dinode *dp)
292 printf("Blocks for inode %ju:\n", (uintmax_t)inum);
293 printf("Direct blocks:\n");
294 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
295 for (i = 0; i < NDADDR && i < ndb; i++) {
298 blkno = DIP(dp, di_db[i]);
299 printf("%jd", (intmax_t)blkno);
302 offset = blkoff(&sblock, DIP(dp, di_size));
304 nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
305 printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
312 bufp = malloc((unsigned int)sblock.fs_bsize);
314 errx(EEXIT, "cannot allocate indirect block buffer");
315 printf("Indirect blocks:\n");
316 for (i = 0; i < NIADDR; i++)
317 printindir(DIP(dp, di_ib[i]), i, bufp);
326 warnx("no current inode\n");
336 warnx("no current inode\n");
339 if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
340 warnx("inode %ju not a directory", (uintmax_t)curinum);
347 printactive(int doblocks)
351 switch (DIP(curinode, di_mode) & IFMT) {
360 printblocks(curinum, curinode);
362 printstat("current inode", curinum, curinode);
365 printf("current inode %ju: unallocated inode\n", (uintmax_t)curinum);
368 printf("current inode %ju: screwy itype 0%o (mode 0%o)?\n",
369 (uintmax_t)curinum, DIP(curinode, di_mode) & IFMT,
370 DIP(curinode, di_mode));