xref: /freebsd/sbin/fsdb/fsdbutil.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1 /*	$NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  *  Copyright (c) 1995 John T. Kohl
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. The name of the author may not be used to endorse or promote products
18  *     derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <ctype.h>
35 #include <err.h>
36 #include <grp.h>
37 #include <pwd.h>
38 #include <stdint.h>
39 #include <string.h>
40 #include <time.h>
41 #include <timeconv.h>
42 
43 #include <ufs/ufs/dinode.h>
44 #include <ufs/ffs/fs.h>
45 
46 #include <sys/ioctl.h>
47 
48 #include "fsdb.h"
49 #include "fsck.h"
50 
51 void prtblknos(struct fs *fs, union dinode *dp);
52 
53 char **
54 crack(char *line, 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 
71 char **
72 recrack(char *line, int *argc, int argc_max)
73 {
74     static char *argv[8];
75     int i;
76     char *p, *val;
77     for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
78 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
79 	    /**/;
80 	if (val)
81 	    argv[i] = val;
82 	else
83 	    break;
84     }
85     argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
86     argv[i][strcspn(argv[i], "\n")] = '\0';
87     *argc = i + 1;
88     return argv;
89 }
90 
91 int
92 argcount(struct cmdtable *cmdp, int argc, char *argv[])
93 {
94     if (cmdp->minargc == cmdp->maxargc)
95 	warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
96 	    cmdp->minargc-1, argc-1);
97     else
98 	warnx("command `%s' takes from %u to %u arguments",
99 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
100 
101     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
102     return 1;
103 }
104 
105 void
106 printstat(const char *cp, ino_t inum, union dinode *dp)
107 {
108     struct group *grp;
109     struct passwd *pw;
110     ufs2_daddr_t blocks;
111     int64_t gen;
112     char *p;
113     time_t t;
114 
115     printf("%s: ", cp);
116     switch (DIP(dp, di_mode) & IFMT) {
117     case IFDIR:
118 	puts("directory");
119 	break;
120     case IFREG:
121 	puts("regular file");
122 	break;
123     case IFBLK:
124 	printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
125 	break;
126     case IFCHR:
127 	printf("character special (%#jx)", DIP(dp, di_rdev));
128 	break;
129     case IFLNK:
130 	fputs("symlink",stdout);
131 	if (DIP(dp, di_size) > 0 &&
132 	    DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
133 	    DIP(dp, di_blocks) == 0) {
134 	    printf(" to `%.*s'\n", (int) DIP(dp, di_size),
135 		DIP(dp, di_shortlink));
136 	} else {
137 	    putchar('\n');
138 	}
139 	break;
140     case IFSOCK:
141 	puts("socket");
142 	break;
143     case IFIFO:
144 	puts("fifo");
145 	break;
146     }
147     printf("I=%ju MODE=%o SIZE=%ju", (uintmax_t)inum, DIP(dp, di_mode),
148 	(uintmax_t)DIP(dp, di_size));
149     if (sblock.fs_magic != FS_UFS1_MAGIC) {
150 	t = _time64_to_time(dp->dp2.di_birthtime);
151 	p = ctime(&t);
152 	printf("\n\tBTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
153 	   dp->dp2.di_birthnsec);
154     }
155     if (sblock.fs_magic == FS_UFS1_MAGIC)
156 	t = _time32_to_time(dp->dp1.di_mtime);
157     else
158 	t = _time64_to_time(dp->dp2.di_mtime);
159     p = ctime(&t);
160     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
161 	   DIP(dp, di_mtimensec));
162     if (sblock.fs_magic == FS_UFS1_MAGIC)
163 	t = _time32_to_time(dp->dp1.di_ctime);
164     else
165 	t = _time64_to_time(dp->dp2.di_ctime);
166     p = ctime(&t);
167     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
168 	   DIP(dp, di_ctimensec));
169     if (sblock.fs_magic == FS_UFS1_MAGIC)
170 	t = _time32_to_time(dp->dp1.di_atime);
171     else
172 	t = _time64_to_time(dp->dp2.di_atime);
173     p = ctime(&t);
174     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
175 	   DIP(dp, di_atimensec));
176 
177     if ((pw = getpwuid(DIP(dp, di_uid))))
178 	printf("OWNER=%s ", pw->pw_name);
179     else
180 	printf("OWNUID=%u ", DIP(dp, di_uid));
181     if ((grp = getgrgid(DIP(dp, di_gid))))
182 	printf("GRP=%s ", grp->gr_name);
183     else
184 	printf("GID=%u ", DIP(dp, di_gid));
185 
186     blocks = DIP(dp, di_blocks);
187     gen = DIP(dp, di_gen);
188     printf("LINKCNT=%d FLAGS=%#x BLKCNT=%jx GEN=%jx\n", DIP(dp, di_nlink),
189 	DIP(dp, di_flags), (intmax_t)blocks, (intmax_t)gen);
190 }
191 
192 
193 int
194 checkactive(void)
195 {
196     if (!curinode) {
197 	warnx("no current inode\n");
198 	return 0;
199     }
200     return 1;
201 }
202 
203 int
204 checkactivedir(void)
205 {
206     if (!curinode) {
207 	warnx("no current inode\n");
208 	return 0;
209     }
210     if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
211 	warnx("inode %ju not a directory", (uintmax_t)curinum);
212 	return 0;
213     }
214     return 1;
215 }
216 
217 int
218 printactive(int doblocks)
219 {
220     if (!checkactive())
221 	return 1;
222     switch (DIP(curinode, di_mode) & IFMT) {
223     case IFDIR:
224     case IFREG:
225     case IFBLK:
226     case IFCHR:
227     case IFLNK:
228     case IFSOCK:
229     case IFIFO:
230 	if (doblocks)
231 	    prtblknos(&sblock, curinode);
232 	else
233 	    printstat("current inode", curinum, curinode);
234 	break;
235     case 0:
236 	printf("current inode %ju: unallocated inode\n", (uintmax_t)curinum);
237 	break;
238     default:
239 	printf("current inode %ju: screwy itype 0%o (mode 0%o)?\n",
240 	    (uintmax_t)curinum, DIP(curinode, di_mode) & IFMT,
241 	    DIP(curinode, di_mode));
242 	break;
243     }
244     return 0;
245 }
246