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