Sync usage with man page.
[netbsd-mini2440.git] / sbin / fsdb / fsdbutil.c
blobe2bdace4fccd34e808641808397605a29fe712eb
1 /* $NetBSD: fsdbutil.c,v 1.21 2008/12/29 20:02:30 mlelstv Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by John T. Kohl.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __RCSID("$NetBSD: fsdbutil.c,v 1.21 2008/12/29 20:02:30 mlelstv Exp $");
35 #endif /* not lint */
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/param.h>
40 #include <sys/time.h>
41 #include <sys/mount.h>
42 #include <fcntl.h>
43 #include <grp.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49 #include <unistd.h>
50 #include <err.h>
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ffs/fs.h>
55 #include "fsdb.h"
56 #include "fsck.h"
58 char **
59 crack(char *line, int *argc)
61 static char *argv[8];
62 int i;
63 char *p, *val;
64 for (p = line, i = 0; p != NULL && i < 8; i++) {
65 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
66 /**/ ;
67 if (val)
68 argv[i] = val;
69 else
70 break;
72 *argc = i;
73 return argv;
76 int
77 argcount(struct cmdtable *cmdp, int argc, char *argv[])
79 if (cmdp->minargc == cmdp->maxargc)
80 warnx("command `%s' takes %u arguments", cmdp->cmd,
81 cmdp->minargc - 1);
82 else
83 warnx("command `%s' takes from %u to %u arguments",
84 cmdp->cmd, cmdp->minargc - 1, cmdp->maxargc - 1);
86 warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
87 return 1;
90 void
91 printstat(const char *cp, ino_t inum, union dinode *dp)
93 struct group *grp;
94 struct passwd *pw;
95 time_t t;
96 char *p;
97 uint64_t size, blocks;
98 uint16_t mode;
99 uint32_t rdev;
100 uint32_t uid, gid;
102 size = iswap64(DIP(dp, size));
103 blocks = is_ufs2 ? iswap64(DIP(dp, blocks)) : iswap32(DIP(dp, blocks));
104 mode = iswap16(DIP(dp, mode));
105 rdev = iswap32(DIP(dp, rdev));
107 printf("%s: ", cp);
108 switch (mode & IFMT) {
109 case IFDIR:
110 puts("directory");
111 break;
112 case IFREG:
113 puts("regular file");
114 break;
115 case IFBLK:
116 printf("block special (%llu,%llu)",
117 (unsigned long long)major(rdev),
118 (unsigned long long)minor(rdev));
119 break;
120 case IFCHR:
121 printf("character special (%llu,%llu)",
122 (unsigned long long)major(rdev),
123 (unsigned long long)minor(rdev));
124 break;
125 case IFLNK:
126 fputs("symlink", stdout);
127 if (size > 0 && size < (uint64_t)sblock->fs_maxsymlinklen &&
128 DIP(dp, blocks) == 0) {
129 p = is_ufs2 ? (char *)dp->dp2.di_db :
130 (char *)dp->dp1.di_db;
131 printf(" to `%.*s'\n", (int)size, p);
132 } else
133 putchar('\n');
134 break;
135 case IFSOCK:
136 puts("socket");
137 break;
138 case IFIFO:
139 puts("fifo");
140 break;
142 printf("I=%llu MODE=%o SIZE=%llu", (unsigned long long)inum, mode,
143 (unsigned long long)size);
144 t = is_ufs2 ? iswap64(dp->dp2.di_mtime) : iswap32(dp->dp1.di_mtime);
145 p = ctime(&t);
146 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
147 iswap32(DIP(dp, mtimensec)));
148 t = is_ufs2 ? iswap64(dp->dp2.di_ctime) : iswap32(dp->dp1.di_ctime);
149 p = ctime(&t);
150 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
151 iswap32(DIP(dp, ctimensec)));
152 t = is_ufs2 ? iswap64(dp->dp2.di_atime) : iswap32(dp->dp1.di_atime);
153 p = ctime(&t);
154 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
155 iswap32(DIP(dp,atimensec)));
157 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT)
158 uid = iswap16(dp->dp1.di_ouid);
159 else
160 uid = iswap32(DIP(dp, uid));
161 if ((pw = getpwuid(uid)) != NULL)
162 printf("OWNER=%s ", pw->pw_name);
163 else
164 printf("OWNUID=%u ", uid);
165 if (!is_ufs2 && sblock->fs_old_inodefmt < FS_44INODEFMT)
166 gid = iswap16(dp->dp1.di_ogid);
167 else
168 gid = iswap32(DIP(dp, gid));
169 if ((grp = getgrgid(gid)) != NULL)
170 printf("GRP=%s ", grp->gr_name);
171 else
172 printf("GID=%u ", gid);
174 printf("LINKCNT=%hd FLAGS=0x%x BLKCNT=0x%llx GEN=0x%x\n",
175 iswap16(DIP(dp, nlink)),
176 iswap32(DIP(dp, flags)), (unsigned long long)blocks,
177 iswap32(DIP(dp, gen)));
181 checkactive(void)
183 if (!curinode) {
184 warnx("no current inode");
185 return 0;
187 return 1;
191 checkactivedir(void)
193 if (!curinode) {
194 warnx("no current inode");
195 return 0;
197 if ((iswap16(DIP(curinode, mode)) & IFMT) != IFDIR) {
198 warnx("inode %llu not a directory",
199 (unsigned long long)curinum);
200 return 0;
202 return 1;
206 printactive(void)
208 uint16_t mode;
210 if (!checkactive())
211 return 1;
212 mode = iswap16(DIP(curinode, mode));
213 switch (mode & IFMT) {
214 case IFDIR:
215 case IFREG:
216 case IFBLK:
217 case IFCHR:
218 case IFLNK:
219 case IFSOCK:
220 case IFIFO:
221 printstat("current inode", curinum, curinode);
222 break;
223 case 0:
224 printf("current inode %llu: unallocated inode\n",
225 (unsigned long long)curinum);
226 break;
227 default:
228 printf("current inode %llu: screwy itype 0%o (mode 0%o)?\n",
229 (unsigned long long)curinum, mode & IFMT, mode);
230 break;
232 return 0;