fsdbutil.c revision 89791
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
31#ifndef lint
32static const char rcsid[] =
33  "$FreeBSD: head/sbin/fsdb/fsdbutil.c 89791 2002-01-25 18:31:57Z green $";
34#endif /* not lint */
35
36#include <sys/param.h>
37#include <ctype.h>
38#include <err.h>
39#include <grp.h>
40#include <pwd.h>
41#include <string.h>
42#include <time.h>
43
44#include <ufs/ufs/dinode.h>
45#include <ufs/ffs/fs.h>
46
47#include "fsdb.h"
48#include "fsck.h"
49
50char **
51crack(line, argc)
52	char *line;
53	int *argc;
54{
55    static char *argv[8];
56    int i;
57    char *p, *val;
58    for (p = line, i = 0; p != NULL && i < 8; i++) {
59	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
60	    /**/;
61	if (val)
62	    argv[i] = val;
63	else
64	    break;
65    }
66    *argc = i;
67    return argv;
68}
69
70char **
71recrack(line, argc, argc_max)
72	char *line;
73	int *argc;
74	int argc_max;
75{
76    static char *argv[8];
77    int i;
78    char *p, *val;
79    for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
80	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
81	    /**/;
82	if (val)
83	    argv[i] = val;
84	else
85	    break;
86    }
87    argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
88    argv[i][strcspn(argv[i], "\n")] = '\0';
89    *argc = i + 1;
90    printf("returning with argc %u\n", *argc);
91    return argv;
92}
93
94int
95argcount(cmdp, argc, argv)
96	struct cmdtable *cmdp;
97	int argc;
98	char *argv[];
99{
100    if (cmdp->minargc == cmdp->maxargc)
101	warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
102	    cmdp->minargc-1, argc);
103    else
104	warnx("command `%s' takes from %u to %u arguments",
105	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
106
107    warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
108    return 1;
109}
110
111void
112printstat(cp, inum, dp)
113	const char *cp;
114	ino_t inum;
115	struct dinode *dp;
116{
117    struct group *grp;
118    struct passwd *pw;
119    char *p;
120    time_t t;
121
122    printf("%s: ", cp);
123    switch (dp->di_mode & IFMT) {
124    case IFDIR:
125	puts("directory");
126	break;
127    case IFREG:
128	puts("regular file");
129	break;
130    case IFBLK:
131	printf("block special (%d,%d)",
132	       major(dp->di_rdev), minor(dp->di_rdev));
133	break;
134    case IFCHR:
135	printf("character special (%d,%d)",
136	       major(dp->di_rdev), minor(dp->di_rdev));
137	break;
138    case IFLNK:
139	fputs("symlink",stdout);
140	if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
141	    dp->di_blocks == 0)
142	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
143	else
144		putchar('\n');
145	break;
146    case IFSOCK:
147	puts("socket");
148	break;
149    case IFIFO:
150	puts("fifo");
151	break;
152    }
153    printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
154    t = dp->di_mtime;
155    p = ctime(&t);
156    printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
157	   dp->di_mtimensec);
158    t = dp->di_ctime;
159    p = ctime(&t);
160    printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
161	   dp->di_ctimensec);
162    t = dp->di_atime;
163    p = ctime(&t);
164    printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
165	   dp->di_atimensec);
166
167    if ((pw = getpwuid(dp->di_uid)))
168	printf("OWNER=%s ", pw->pw_name);
169    else
170	printf("OWNUID=%u ", dp->di_uid);
171    if ((grp = getgrgid(dp->di_gid)))
172	printf("GRP=%s ", grp->gr_name);
173    else
174	printf("GID=%u ", dp->di_gid);
175
176    printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
177	   dp->di_blocks, dp->di_gen);
178}
179
180int
181checkactive()
182{
183    if (!curinode) {
184	warnx("no current inode\n");
185	return 0;
186    }
187    return 1;
188}
189
190int
191checkactivedir()
192{
193    if (!curinode) {
194	warnx("no current inode\n");
195	return 0;
196    }
197    if ((curinode->di_mode & IFMT) != IFDIR) {
198	warnx("inode %d not a directory", curinum);
199	return 0;
200    }
201    return 1;
202}
203
204int
205printactive()
206{
207    if (!checkactive())
208	return 1;
209    switch (curinode->di_mode & IFMT) {
210    case IFDIR:
211    case IFREG:
212    case IFBLK:
213    case IFCHR:
214    case IFLNK:
215    case IFSOCK:
216    case IFIFO:
217	printstat("current inode", curinum, curinode);
218	break;
219    case 0:
220	printf("current inode %d: unallocated inode\n", curinum);
221	break;
222    default:
223	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
224	       curinum, curinode->di_mode & IFMT, curinode->di_mode);
225	break;
226    }
227    return 0;
228}
229