xref: /freebsd/sbin/fsdb/fsdbutil.c (revision bc7512cc58af2e8bbe5bbf5ca0059b1daa1da897)
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 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <grp.h>
42 #include <pwd.h>
43 #include <stdint.h>
44 #include <string.h>
45 #include <time.h>
46 #include <timeconv.h>
47 
48 #include <ufs/ufs/dinode.h>
49 #include <ufs/ffs/fs.h>
50 
51 #include <sys/ioctl.h>
52 
53 #include "fsdb.h"
54 #include "fsck.h"
55 
56 void prtblknos(struct fs *fs, union dinode *dp);
57 
58 char **
59 crack(char *line, int *argc)
60 {
61     static char *argv[8];
62     int i;
63     char *p, *val;
64     for (p = line, i = 0; p != NULL && i < 8; i++) {
65 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
66 	    /**/;
67 	if (val)
68 	    argv[i] = val;
69 	else
70 	    break;
71     }
72     *argc = i;
73     return argv;
74 }
75 
76 char **
77 recrack(char *line, int *argc, int argc_max)
78 {
79     static char *argv[8];
80     int i;
81     char *p, *val;
82     for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) {
83 	while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
84 	    /**/;
85 	if (val)
86 	    argv[i] = val;
87 	else
88 	    break;
89     }
90     argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1;
91     argv[i][strcspn(argv[i], "\n")] = '\0';
92     *argc = i + 1;
93     return argv;
94 }
95 
96 int
97 argcount(struct cmdtable *cmdp, int argc, char *argv[])
98 {
99     if (cmdp->minargc == cmdp->maxargc)
100 	warnx("command `%s' takes %u arguments, got %u", cmdp->cmd,
101 	    cmdp->minargc-1, argc-1);
102     else
103 	warnx("command `%s' takes from %u to %u arguments",
104 	      cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
105 
106     warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
107     return 1;
108 }
109 
110 void
111 printstat(const char *cp, ino_t inum, union dinode *dp)
112 {
113     struct group *grp;
114     struct passwd *pw;
115     ufs2_daddr_t blocks;
116     int64_t gen;
117     char *p;
118     time_t t;
119 
120     printf("%s: ", cp);
121     switch (DIP(dp, di_mode) & IFMT) {
122     case IFDIR:
123 	puts("directory");
124 	break;
125     case IFREG:
126 	puts("regular file");
127 	break;
128     case IFBLK:
129 	printf("block special (%#jx)", (uintmax_t)DIP(dp, di_rdev));
130 	break;
131     case IFCHR:
132 	printf("character special (%#jx)", DIP(dp, di_rdev));
133 	break;
134     case IFLNK:
135 	fputs("symlink",stdout);
136 	if (DIP(dp, di_size) > 0 &&
137 	    DIP(dp, di_size) < sblock.fs_maxsymlinklen &&
138 	    DIP(dp, di_blocks) == 0) {
139 	    printf(" to `%.*s'\n", (int) DIP(dp, di_size),
140 		DIP(dp, di_shortlink));
141 	} else {
142 	    putchar('\n');
143 	}
144 	break;
145     case IFSOCK:
146 	puts("socket");
147 	break;
148     case IFIFO:
149 	puts("fifo");
150 	break;
151     }
152     printf("I=%ju MODE=%o SIZE=%ju", (uintmax_t)inum, DIP(dp, di_mode),
153 	(uintmax_t)DIP(dp, di_size));
154     if (sblock.fs_magic != FS_UFS1_MAGIC) {
155 	t = _time64_to_time(dp->dp2.di_birthtime);
156 	p = ctime(&t);
157 	printf("\n\tBTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
158 	   dp->dp2.di_birthnsec);
159     }
160     if (sblock.fs_magic == FS_UFS1_MAGIC)
161 	t = _time32_to_time(dp->dp1.di_mtime);
162     else
163 	t = _time64_to_time(dp->dp2.di_mtime);
164     p = ctime(&t);
165     printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
166 	   DIP(dp, di_mtimensec));
167     if (sblock.fs_magic == FS_UFS1_MAGIC)
168 	t = _time32_to_time(dp->dp1.di_ctime);
169     else
170 	t = _time64_to_time(dp->dp2.di_ctime);
171     p = ctime(&t);
172     printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
173 	   DIP(dp, di_ctimensec));
174     if (sblock.fs_magic == FS_UFS1_MAGIC)
175 	t = _time32_to_time(dp->dp1.di_atime);
176     else
177 	t = _time64_to_time(dp->dp2.di_atime);
178     p = ctime(&t);
179     printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
180 	   DIP(dp, di_atimensec));
181 
182     if ((pw = getpwuid(DIP(dp, di_uid))))
183 	printf("OWNER=%s ", pw->pw_name);
184     else
185 	printf("OWNUID=%u ", DIP(dp, di_uid));
186     if ((grp = getgrgid(DIP(dp, di_gid))))
187 	printf("GRP=%s ", grp->gr_name);
188     else
189 	printf("GID=%u ", DIP(dp, di_gid));
190 
191     blocks = DIP(dp, di_blocks);
192     gen = DIP(dp, di_gen);
193     printf("LINKCNT=%d FLAGS=%#x BLKCNT=%jx GEN=%jx\n", DIP(dp, di_nlink),
194 	DIP(dp, di_flags), (intmax_t)blocks, (intmax_t)gen);
195 }
196 
197 
198 int
199 checkactive(void)
200 {
201     if (!curinode) {
202 	warnx("no current inode\n");
203 	return 0;
204     }
205     return 1;
206 }
207 
208 int
209 checkactivedir(void)
210 {
211     if (!curinode) {
212 	warnx("no current inode\n");
213 	return 0;
214     }
215     if ((DIP(curinode, di_mode) & IFMT) != IFDIR) {
216 	warnx("inode %ju not a directory", (uintmax_t)curinum);
217 	return 0;
218     }
219     return 1;
220 }
221 
222 int
223 printactive(int doblocks)
224 {
225     if (!checkactive())
226 	return 1;
227     switch (DIP(curinode, di_mode) & IFMT) {
228     case IFDIR:
229     case IFREG:
230     case IFBLK:
231     case IFCHR:
232     case IFLNK:
233     case IFSOCK:
234     case IFIFO:
235 	if (doblocks)
236 	    prtblknos(&sblock, curinode);
237 	else
238 	    printstat("current inode", curinum, curinode);
239 	break;
240     case 0:
241 	printf("current inode %ju: unallocated inode\n", (uintmax_t)curinum);
242 	break;
243     default:
244 	printf("current inode %ju: screwy itype 0%o (mode 0%o)?\n",
245 	    (uintmax_t)curinum, DIP(curinode, di_mode) & IFMT,
246 	    DIP(curinode, di_mode));
247 	break;
248     }
249     return 0;
250 }
251