xref: /openbsd-src/sbin/fsdb/fsdbutil.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: fsdbutil.c,v 1.14 2009/10/27 23:59:33 deraadt Exp $	*/
2 /*	$NetBSD: fsdbutil.c,v 1.5 1996/09/28 19:30:37 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1996 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by John T. Kohl.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <sys/param.h>
36 #include <sys/time.h>
37 #include <sys/mount.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <fcntl.h>
41 #include <grp.h>
42 #include <pwd.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50 
51 #include "fsdb.h"
52 #include "fsck.h"
53 
54 char **
55 crack(char *line, int *argc)
56 {
57 	static char *argv[8];
58 	int i;
59 	char *p, *val;
60 
61 	for (p = line, i = 0; p != NULL && i < 8; i++) {
62 		while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
63 			/**/;
64 		if (val)
65 			argv[i] = val;
66 		else
67 			break;
68 	}
69 	*argc = i;
70 	return argv;
71 }
72 
73 int
74 argcount(struct cmdtable *cmdp, int argc, 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 	warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
82 	return 1;
83 }
84 
85 void
86 printstat(const char *cp, ino_t inum, union dinode *dp)
87 {
88 	struct group *grp;
89 	struct passwd *pw;
90 	time_t t;
91 	char *p;
92 
93 	printf("%s: ", cp);
94 	switch (DIP(dp, di_mode) & IFMT) {
95 	case IFDIR:
96 		puts("directory");
97 		break;
98 	case IFREG:
99 		puts("regular file");
100 		break;
101 	case IFBLK:
102 		printf("block special (%d,%d)",
103 		    (int)major(DIP(dp, di_rdev)), (int)minor(DIP(dp, di_rdev)));
104 		break;
105 	case IFCHR:
106 		printf("character special (%d,%d)",
107 		    (int)major(DIP(dp, di_rdev)), (int)minor(DIP(dp, di_rdev)));
108 		break;
109 	case IFLNK:
110 		fputs("symlink",stdout);
111 		if (DIP(dp, di_size) > 0 &&
112 		    DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
113 		    DIP(dp, di_blocks) == 0) {
114 			char *p = sblock.fs_magic == FS_UFS1_MAGIC ?
115 			    (char *)dp->dp1.di_shortlink :
116 			    (char *)dp->dp2.di_shortlink;
117 			printf(" to `%.*s'\n", (int)DIP(dp, di_size), p);
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 
129 	printf("I=%u MODE=%o SIZE=%llu", inum, DIP(dp, di_mode), DIP(dp, di_size));
130 	t = DIP(dp, di_mtime);
131 	p = ctime(&t);
132 	printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
133 	    DIP(dp, di_mtimensec));
134 	t = DIP(dp, di_ctime);
135 	p = ctime(&t);
136 	printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
137 	    DIP(dp, di_ctimensec));
138 	t = DIP(dp, di_atime);
139 	p = ctime(&t);
140 	printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
141 	    DIP(dp, di_atimensec));
142 
143 	if ((pw = getpwuid(DIP(dp, di_uid))))
144 		printf("OWNER=%s ", pw->pw_name);
145 	else
146 		printf("OWNUID=%u ", DIP(dp, di_uid));
147 	if ((grp = getgrgid(DIP(dp, di_gid))))
148 		printf("GRP=%s ", grp->gr_name);
149 	else
150 		printf("GID=%u ", DIP(dp, di_gid));
151 
152 	printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", DIP(dp, di_nlink),
153 	    DIP(dp, di_flags), (unsigned)DIP(dp, di_blocks), DIP(dp, di_gen));
154 }
155 
156 int
157 checkactive(void)
158 {
159 	if (!curinode) {
160 		warnx("no current inode");
161 		return 0;
162 	}
163 	return 1;
164 }
165 
166 int
167 checkactivedir(void)
168 {
169 	if (!curinode) {
170 		warnx("no current inode");
171 		return 0;
172 	}
173 	if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
174 		warnx("inode %d not a directory", curinum);
175 		return 0;
176 	}
177 	return 1;
178 }
179 
180 int
181 printactive(void)
182 {
183 	if (!checkactive())
184 		return 1;
185 	switch (DIP(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, DIP(curinode, di_mode) & IFMT,
201 		    DIP(curinode, di_mode));
202 		break;
203 	}
204 	return 0;
205 }
206