fsdbutil.c revision 89792
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 89792 2002-01-25 18:33:40Z 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    return argv;
91}
92
93int
94argcount(cmdp, argc, argv)
95	struct cmdtable *cmdp;
96	int argc;
97	char *argv[];
98{
99    if (cmdp->minargc == cmdp->maxargc)
100	warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
101	    cmdp->minargc-1, argc);
102    else
103	warnx("command `%s' takes from %u to %u arguments",
104	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
105
106    warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
107    return 1;
108}
109
110void
111printstat(cp, inum, dp)
112	const char *cp;
113	ino_t inum;
114	struct dinode *dp;
115{
116    struct group *grp;
117    struct passwd *pw;
118    char *p;
119    time_t t;
120
121    printf("%s: ", cp);
122    switch (dp->di_mode & IFMT) {
123    case IFDIR:
124	puts("directory");
125	break;
126    case IFREG:
127	puts("regular file");
128	break;
129    case IFBLK:
130	printf("block special (%d,%d)",
131	       major(dp->di_rdev), minor(dp->di_rdev));
132	break;
133    case IFCHR:
134	printf("character special (%d,%d)",
135	       major(dp->di_rdev), minor(dp->di_rdev));
136	break;
137    case IFLNK:
138	fputs("symlink",stdout);
139	if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
140	    dp->di_blocks == 0)
141	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
142	else
143		putchar('\n');
144	break;
145    case IFSOCK:
146	puts("socket");
147	break;
148    case IFIFO:
149	puts("fifo");
150	break;
151    }
152    printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
153    t = dp->di_mtime;
154    p = ctime(&t);
155    printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
156	   dp->di_mtimensec);
157    t = dp->di_ctime;
158    p = ctime(&t);
159    printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
160	   dp->di_ctimensec);
161    t = dp->di_atime;
162    p = ctime(&t);
163    printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
164	   dp->di_atimensec);
165
166    if ((pw = getpwuid(dp->di_uid)))
167	printf("OWNER=%s ", pw->pw_name);
168    else
169	printf("OWNUID=%u ", dp->di_uid);
170    if ((grp = getgrgid(dp->di_gid)))
171	printf("GRP=%s ", grp->gr_name);
172    else
173	printf("GID=%u ", dp->di_gid);
174
175    printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
176	   dp->di_blocks, dp->di_gen);
177}
178
179int
180checkactive()
181{
182    if (!curinode) {
183	warnx("no current inode\n");
184	return 0;
185    }
186    return 1;
187}
188
189int
190checkactivedir()
191{
192    if (!curinode) {
193	warnx("no current inode\n");
194	return 0;
195    }
196    if ((curinode->di_mode & IFMT) != IFDIR) {
197	warnx("inode %d not a directory", curinum);
198	return 0;
199    }
200    return 1;
201}
202
203int
204printactive()
205{
206    if (!checkactive())
207	return 1;
208    switch (curinode->di_mode & IFMT) {
209    case IFDIR:
210    case IFREG:
211    case IFBLK:
212    case IFCHR:
213    case IFLNK:
214    case IFSOCK:
215    case IFIFO:
216	printstat("current inode", curinum, curinode);
217	break;
218    case 0:
219	printf("current inode %d: unallocated inode\n", curinum);
220	break;
221    default:
222	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
223	       curinum, curinode->di_mode & IFMT, curinode->di_mode);
224	break;
225    }
226    return 0;
227}
228