fsdbutil.c revision 37237
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	"$Id: fsdbutil.c,v 1.7 1998/06/15 07:12:20 charnier Exp $";
34#endif /* not lint */
35
36#include <sys/types.h>
37#include <sys/param.h>
38#include <ctype.h>
39#include <err.h>
40#include <grp.h>
41#include <pwd.h>
42#include <string.h>
43#include <time.h>
44
45#include <ufs/ufs/dinode.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
70int
71argcount(cmdp, argc, argv)
72	struct cmdtable *cmdp;
73	int argc;
74	char *argv[];
75{
76    if (cmdp->minargc == cmdp->maxargc)
77	warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
78    else
79	warnx("command `%s' takes from %u to %u arguments",
80	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
81
82    warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
83    return 1;
84}
85
86void
87printstat(cp, inum, dp)
88	const char *cp;
89	ino_t inum;
90	struct dinode *dp;
91{
92    struct group *grp;
93    struct passwd *pw;
94    char *p;
95    time_t t;
96
97    printf("%s: ", cp);
98    switch (dp->di_mode & IFMT) {
99    case IFDIR:
100	puts("directory");
101	break;
102    case IFREG:
103	puts("regular file");
104	break;
105    case IFBLK:
106	printf("block special (%d,%d)",
107	       major(dp->di_rdev), minor(dp->di_rdev));
108	break;
109    case IFCHR:
110	printf("character special (%d,%d)",
111	       major(dp->di_rdev), minor(dp->di_rdev));
112	break;
113    case IFLNK:
114	fputs("symlink",stdout);
115	if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
116	    dp->di_blocks == 0)
117	    printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
118	else
119		putchar('\n');
120	break;
121    case IFSOCK:
122	puts("socket");
123	break;
124    case IFIFO:
125	puts("fifo");
126	break;
127    }
128    printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
129    t = dp->di_mtime;
130    p = ctime(&t);
131    printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
132	   dp->di_mtimensec);
133    t = dp->di_ctime;
134    p = ctime(&t);
135    printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
136	   dp->di_ctimensec);
137    t = dp->di_atime;
138    p = ctime(&t);
139    printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
140	   dp->di_atimensec);
141
142    if ((pw = getpwuid(dp->di_uid)))
143	printf("OWNER=%s ", pw->pw_name);
144    else
145	printf("OWNUID=%u ", dp->di_uid);
146    if ((grp = getgrgid(dp->di_gid)))
147	printf("GRP=%s ", grp->gr_name);
148    else
149	printf("GID=%u ", dp->di_gid);
150
151    printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
152	   dp->di_blocks, dp->di_gen);
153}
154
155int
156checkactive()
157{
158    if (!curinode) {
159	warnx("no current inode\n");
160	return 0;
161    }
162    return 1;
163}
164
165int
166checkactivedir()
167{
168    if (!curinode) {
169	warnx("no current inode\n");
170	return 0;
171    }
172    if ((curinode->di_mode & IFMT) != IFDIR) {
173	warnx("inode %d not a directory", curinum);
174	return 0;
175    }
176    return 1;
177}
178
179int
180printactive()
181{
182    if (!checkactive())
183	return 1;
184    switch (curinode->di_mode & IFMT) {
185    case IFDIR:
186    case IFREG:
187    case IFBLK:
188    case IFCHR:
189    case IFLNK:
190    case IFSOCK:
191    case IFIFO:
192	printstat("current inode", curinum, curinode);
193	break;
194    case 0:
195	printf("current inode %d: unallocated inode\n", curinum);
196	break;
197    default:
198	printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
199	       curinum, curinode->di_mode & IFMT, curinode->di_mode);
200	break;
201    }
202    return 0;
203}
204