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