xref: /freebsd/sbin/fsdb/fsdb.c (revision c4f6a2a9e1b1879b618c436ab4f56ff75c73a0f5)
1 /*	$NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 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
32 static const char rcsid[] =
33   "$FreeBSD$";
34 #endif /* not lint */
35 
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <ctype.h>
39 #include <err.h>
40 #include <grp.h>
41 #include <histedit.h>
42 #include <pwd.h>
43 #include <string.h>
44 
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
48 
49 #include "fsdb.h"
50 #include "fsck.h"
51 
52 static void usage(void) __dead2;
53 int cmdloop(void);
54 
55 static void
56 usage(void)
57 {
58 	fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
59 	exit(1);
60 }
61 
62 int returntosingle;
63 char nflag;
64 
65 /*
66  * We suck in lots of fsck code, and just pick & choose the stuff we want.
67  *
68  * fsreadfd is set up to read from the file system, fswritefd to write to
69  * the file system.
70  */
71 int
72 main(int argc, char *argv[])
73 {
74 	int ch, rval;
75 	char *fsys = NULL;
76 
77 	while (-1 != (ch = getopt(argc, argv, "fdr"))) {
78 		switch (ch) {
79 		case 'f':
80 			/* The -f option is left for historical
81 			 * reasons and has no meaning.
82 			 */
83 			break;
84 		case 'd':
85 			debug++;
86 			break;
87 		case 'r':
88 			nflag++; /* "no" in fsck, readonly for us */
89 			break;
90 		default:
91 			usage();
92 		}
93 	}
94 	argc -= optind;
95 	argv += optind;
96 	if (argc != 1)
97 		usage();
98 	else
99 		fsys = argv[0];
100 
101 	sblock_init();
102 	if (!setup(fsys))
103 		errx(1, "cannot set up file system `%s'", fsys);
104 	printf("%s file system `%s'\nLast Mounted on %s\n",
105 	       nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
106 	rval = cmdloop();
107 	if (!nflag) {
108 		sblock.fs_clean = 0;	/* mark it dirty */
109 		sbdirty();
110 		ckfini(0);
111 		printf("*** FILE SYSTEM MARKED DIRTY\n");
112 		printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
113 		printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
114 	}
115 	exit(rval);
116 }
117 
118 #define CMDFUNC(func) int func(int argc, char *argv[])
119 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
120 
121 CMDFUNC(helpfn);
122 CMDFUNC(focus);				/* focus on inode */
123 CMDFUNC(active);			/* print active inode */
124 CMDFUNC(blocks);			/* print blocks for active inode */
125 CMDFUNC(focusname);			/* focus by name */
126 CMDFUNC(zapi);				/* clear inode */
127 CMDFUNC(uplink);			/* incr link */
128 CMDFUNC(downlink);			/* decr link */
129 CMDFUNC(linkcount);			/* set link count */
130 CMDFUNC(quit);				/* quit */
131 CMDFUNC(ls);				/* list directory */
132 CMDFUNC(rm);				/* remove name */
133 CMDFUNC(ln);				/* add name */
134 CMDFUNC(newtype);			/* change type */
135 CMDFUNC(chmode);			/* change mode */
136 CMDFUNC(chlen);				/* change length */
137 CMDFUNC(chaflags);			/* change flags */
138 CMDFUNC(chgen);				/* change generation */
139 CMDFUNC(chowner);			/* change owner */
140 CMDFUNC(chgroup);			/* Change group */
141 CMDFUNC(back);				/* pop back to last ino */
142 CMDFUNC(chmtime);			/* Change mtime */
143 CMDFUNC(chctime);			/* Change ctime */
144 CMDFUNC(chatime);			/* Change atime */
145 CMDFUNC(chinum);			/* Change inode # of dirent */
146 CMDFUNC(chname);			/* Change dirname of dirent */
147 
148 struct cmdtable cmds[] = {
149 	{ "help", "Print out help", 1, 1, FL_RO, helpfn },
150 	{ "?", "Print out help", 1, 1, FL_RO, helpfn },
151 	{ "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
152 	{ "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
153 	{ "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
154 	{ "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
155 	{ "back", "Go to previous active inode", 1, 1, FL_RO, back },
156 	{ "active", "Print active inode", 1, 1, FL_RO, active },
157 	{ "print", "Print active inode", 1, 1, FL_RO, active },
158 	{ "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
159 	{ "uplink", "Increment link count", 1, 1, FL_WR, uplink },
160 	{ "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
161 	{ "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
162 	{ "ls", "List current inode as directory", 1, 1, FL_RO, ls },
163 	{ "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
164 	{ "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
165 	{ "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln },
166 	{ "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
167 	{ "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname },
168 	{ "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
169 	{ "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
170 	{ "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
171 	{ "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
172 	{ "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
173 	{ "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
174 	{ "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
175 	{ "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
176 	{ "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
177 	{ "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
178 	{ "quit", "Exit", 1, 1, FL_RO, quit },
179 	{ "q", "Exit", 1, 1, FL_RO, quit },
180 	{ "exit", "Exit", 1, 1, FL_RO, quit },
181 	{ NULL, 0, 0, 0 },
182 };
183 
184 int
185 helpfn(int argc, char *argv[])
186 {
187     struct cmdtable *cmdtp;
188 
189     printf("Commands are:\n%-10s %5s %5s   %s\n",
190 	   "command", "min argc", "max argc", "what");
191 
192     for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
193 	printf("%-10s %5u %5u   %s\n",
194 	       cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
195     return 0;
196 }
197 
198 char *
199 prompt(EditLine *el)
200 {
201     static char pstring[64];
202     snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
203     return pstring;
204 }
205 
206 
207 int
208 cmdloop(void)
209 {
210     char *line;
211     const char *elline;
212     int cmd_argc, rval = 0, known;
213 #define scratch known
214     char **cmd_argv;
215     struct cmdtable *cmdp;
216     History *hist;
217     EditLine *elptr;
218     HistEvent he;
219 
220     curinode = ginode(ROOTINO);
221     curinum = ROOTINO;
222     printactive(0);
223 
224     hist = history_init();
225     history(hist, &he, H_EVENT, 100);	/* 100 elt history buffer */
226 
227     elptr = el_init("fsdb", stdin, stdout, stderr);
228     el_set(elptr, EL_EDITOR, "emacs");
229     el_set(elptr, EL_PROMPT, prompt);
230     el_set(elptr, EL_HIST, history, hist);
231     el_source(elptr, NULL);
232 
233     while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
234 	if (debug)
235 	    printf("command `%s'\n", elline);
236 
237 	history(hist, &he, H_ENTER, elline);
238 
239 	line = strdup(elline);
240 	cmd_argv = crack(line, &cmd_argc);
241 	/*
242 	 * el_parse returns -1 to signal that it's not been handled
243 	 * internally.
244 	 */
245 	if (el_parse(elptr, cmd_argc, cmd_argv) != -1)
246 	    continue;
247 	if (cmd_argc) {
248 	    known = 0;
249 	    for (cmdp = cmds; cmdp->cmd; cmdp++) {
250 		if (!strcmp(cmdp->cmd, cmd_argv[0])) {
251 		    if ((cmdp->flags & FL_WR) == FL_WR && nflag)
252 			warnx("`%s' requires write access", cmd_argv[0]),
253 			    rval = 1;
254 		    else if (cmd_argc >= cmdp->minargc &&
255 			cmd_argc <= cmdp->maxargc)
256 			rval = (*cmdp->handler)(cmd_argc, cmd_argv);
257 		    else if (cmd_argc >= cmdp->minargc) {
258 			strcpy(line, elline);
259 			cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
260 			rval = (*cmdp->handler)(cmd_argc, cmd_argv);
261 		    } else
262 			rval = argcount(cmdp, cmd_argc, cmd_argv);
263 		    known = 1;
264 		    break;
265 		}
266 	    }
267 	    if (!known)
268 		warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
269 	} else
270 	    rval = 0;
271 	free(line);
272 	if (rval < 0)
273 	    /* user typed "quit" */
274 	    return 0;
275 	if (rval)
276 	    warnx("rval was %d", rval);
277     }
278     el_end(elptr);
279     history_end(hist);
280     return rval;
281 }
282 
283 union dinode *curinode;
284 ino_t curinum, ocurrent;
285 
286 #define GETINUM(ac,inum)    inum = strtoul(argv[ac], &cp, 0); \
287     if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
288 	printf("inode %d out of range; range is [%d,%d]\n", \
289 	       inum, ROOTINO, maxino); \
290 	return 1; \
291     }
292 
293 /*
294  * Focus on given inode number
295  */
296 CMDFUNCSTART(focus)
297 {
298     ino_t inum;
299     char *cp;
300 
301     GETINUM(1,inum);
302     curinode = ginode(inum);
303     ocurrent = curinum;
304     curinum = inum;
305     printactive(0);
306     return 0;
307 }
308 
309 CMDFUNCSTART(back)
310 {
311     curinum = ocurrent;
312     curinode = ginode(curinum);
313     printactive(0);
314     return 0;
315 }
316 
317 CMDFUNCSTART(zapi)
318 {
319     ino_t inum;
320     union dinode *dp;
321     char *cp;
322 
323     GETINUM(1,inum);
324     dp = ginode(inum);
325     clearinode(dp);
326     inodirty();
327     if (curinode)			/* re-set after potential change */
328 	curinode = ginode(curinum);
329     return 0;
330 }
331 
332 CMDFUNCSTART(active)
333 {
334     printactive(0);
335     return 0;
336 }
337 
338 CMDFUNCSTART(blocks)
339 {
340     printactive(1);
341     return 0;
342 }
343 
344 CMDFUNCSTART(quit)
345 {
346     return -1;
347 }
348 
349 CMDFUNCSTART(uplink)
350 {
351     if (!checkactive())
352 	return 1;
353     DIP(curinode, di_nlink) += 1;
354     printf("inode %d link count now %d\n", curinum, DIP(curinode, di_nlink));
355     inodirty();
356     return 0;
357 }
358 
359 CMDFUNCSTART(downlink)
360 {
361     if (!checkactive())
362 	return 1;
363     DIP(curinode, di_nlink) -= 1;
364     printf("inode %d link count now %d\n", curinum, DIP(curinode, di_nlink));
365     inodirty();
366     return 0;
367 }
368 
369 const char *typename[] = {
370     "unknown",
371     "fifo",
372     "char special",
373     "unregistered #3",
374     "directory",
375     "unregistered #5",
376     "blk special",
377     "unregistered #7",
378     "regular",
379     "unregistered #9",
380     "symlink",
381     "unregistered #11",
382     "socket",
383     "unregistered #13",
384     "whiteout",
385 };
386 
387 int slot;
388 
389 int
390 scannames(struct inodesc *idesc)
391 {
392 	struct direct *dirp = idesc->id_dirp;
393 
394 	printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
395 	       slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
396 	       dirp->d_namlen, dirp->d_name);
397 	return (KEEPON);
398 }
399 
400 CMDFUNCSTART(ls)
401 {
402     struct inodesc idesc;
403     checkactivedir();			/* let it go on anyway */
404 
405     slot = 0;
406     idesc.id_number = curinum;
407     idesc.id_func = scannames;
408     idesc.id_type = DATA;
409     idesc.id_fix = IGNORE;
410     ckinode(curinode, &idesc);
411     curinode = ginode(curinum);
412 
413     return 0;
414 }
415 
416 int findino(struct inodesc *idesc); /* from fsck */
417 static int dolookup(char *name);
418 
419 static int
420 dolookup(char *name)
421 {
422     struct inodesc idesc;
423 
424     if (!checkactivedir())
425 	    return 0;
426     idesc.id_number = curinum;
427     idesc.id_func = findino;
428     idesc.id_name = name;
429     idesc.id_type = DATA;
430     idesc.id_fix = IGNORE;
431     if (ckinode(curinode, &idesc) & FOUND) {
432 	curinum = idesc.id_parent;
433 	curinode = ginode(curinum);
434 	printactive(0);
435 	return 1;
436     } else {
437 	warnx("name `%s' not found in current inode directory", name);
438 	return 0;
439     }
440 }
441 
442 CMDFUNCSTART(focusname)
443 {
444     char *p, *val;
445 
446     if (!checkactive())
447 	return 1;
448 
449     ocurrent = curinum;
450 
451     if (argv[1][0] == '/') {
452 	curinum = ROOTINO;
453 	curinode = ginode(ROOTINO);
454     } else {
455 	if (!checkactivedir())
456 	    return 1;
457     }
458     for (p = argv[1]; p != NULL;) {
459 	while ((val = strsep(&p, "/")) != NULL && *val == '\0');
460 	if (val) {
461 	    printf("component `%s': ", val);
462 	    fflush(stdout);
463 	    if (!dolookup(val)) {
464 		curinode = ginode(curinum);
465 		return(1);
466 	    }
467 	}
468     }
469     return 0;
470 }
471 
472 CMDFUNCSTART(ln)
473 {
474     ino_t inum;
475     int rval;
476     char *cp;
477 
478     GETINUM(1,inum);
479 
480     if (!checkactivedir())
481 	return 1;
482     rval = makeentry(curinum, inum, argv[2]);
483     if (rval)
484 	printf("Ino %d entered as `%s'\n", inum, argv[2]);
485     else
486 	printf("could not enter name? weird.\n");
487     curinode = ginode(curinum);
488     return rval;
489 }
490 
491 CMDFUNCSTART(rm)
492 {
493     int rval;
494 
495     if (!checkactivedir())
496 	return 1;
497     rval = changeino(curinum, argv[1], 0);
498     if (rval & ALTERED) {
499 	printf("Name `%s' removed\n", argv[1]);
500 	return 0;
501     } else {
502 	printf("could not remove name ('%s')? weird.\n", argv[1]);
503 	return 1;
504     }
505 }
506 
507 long slotcount, desired;
508 
509 int
510 chinumfunc(struct inodesc *idesc)
511 {
512 	struct direct *dirp = idesc->id_dirp;
513 
514 	if (slotcount++ == desired) {
515 	    dirp->d_ino = idesc->id_parent;
516 	    return STOP|ALTERED|FOUND;
517 	}
518 	return KEEPON;
519 }
520 
521 CMDFUNCSTART(chinum)
522 {
523     char *cp;
524     ino_t inum;
525     struct inodesc idesc;
526 
527     slotcount = 0;
528     if (!checkactivedir())
529 	return 1;
530     GETINUM(2,inum);
531 
532     desired = strtol(argv[1], &cp, 0);
533     if (cp == argv[1] || *cp != '\0' || desired < 0) {
534 	printf("invalid slot number `%s'\n", argv[1]);
535 	return 1;
536     }
537 
538     idesc.id_number = curinum;
539     idesc.id_func = chinumfunc;
540     idesc.id_fix = IGNORE;
541     idesc.id_type = DATA;
542     idesc.id_parent = inum;		/* XXX convenient hiding place */
543 
544     if (ckinode(curinode, &idesc) & FOUND)
545 	return 0;
546     else {
547 	warnx("no %sth slot in current directory", argv[1]);
548 	return 1;
549     }
550 }
551 
552 int
553 chnamefunc(struct inodesc *idesc)
554 {
555 	struct direct *dirp = idesc->id_dirp;
556 	struct direct testdir;
557 
558 	if (slotcount++ == desired) {
559 	    /* will name fit? */
560 	    testdir.d_namlen = strlen(idesc->id_name);
561 	    if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
562 		dirp->d_namlen = testdir.d_namlen;
563 		strcpy(dirp->d_name, idesc->id_name);
564 		return STOP|ALTERED|FOUND;
565 	    } else
566 		return STOP|FOUND;	/* won't fit, so give up */
567 	}
568 	return KEEPON;
569 }
570 
571 CMDFUNCSTART(chname)
572 {
573     int rval;
574     char *cp;
575     struct inodesc idesc;
576 
577     slotcount = 0;
578     if (!checkactivedir())
579 	return 1;
580 
581     desired = strtoul(argv[1], &cp, 0);
582     if (cp == argv[1] || *cp != '\0') {
583 	printf("invalid slot number `%s'\n", argv[1]);
584 	return 1;
585     }
586 
587     idesc.id_number = curinum;
588     idesc.id_func = chnamefunc;
589     idesc.id_fix = IGNORE;
590     idesc.id_type = DATA;
591     idesc.id_name = argv[2];
592 
593     rval = ckinode(curinode, &idesc);
594     if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
595 	return 0;
596     else if (rval & FOUND) {
597 	warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
598 	return 1;
599     } else {
600 	warnx("no %sth slot in current directory", argv[1]);
601 	return 1;
602     }
603 }
604 
605 struct typemap {
606     const char *typename;
607     int typebits;
608 } typenamemap[]  = {
609     {"file", IFREG},
610     {"dir", IFDIR},
611     {"socket", IFSOCK},
612     {"fifo", IFIFO},
613 };
614 
615 CMDFUNCSTART(newtype)
616 {
617     int type;
618     struct typemap *tp;
619 
620     if (!checkactive())
621 	return 1;
622     type = DIP(curinode, di_mode) & IFMT;
623     for (tp = typenamemap;
624 	 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
625 	 tp++) {
626 	if (!strcmp(argv[1], tp->typename)) {
627 	    printf("setting type to %s\n", tp->typename);
628 	    type = tp->typebits;
629 	    break;
630 	}
631     }
632     if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
633 	warnx("type `%s' not known", argv[1]);
634 	warnx("try one of `file', `dir', `socket', `fifo'");
635 	return 1;
636     }
637     DIP(curinode, di_mode) &= ~IFMT;
638     DIP(curinode, di_mode) |= type;
639     inodirty();
640     printactive(0);
641     return 0;
642 }
643 
644 CMDFUNCSTART(chlen)
645 {
646     int rval = 1;
647     long len;
648     char *cp;
649 
650     if (!checkactive())
651 	return 1;
652 
653     len = strtol(argv[1], &cp, 0);
654     if (cp == argv[1] || *cp != '\0' || len < 0) {
655 	warnx("bad length `%s'", argv[1]);
656 	return 1;
657     }
658 
659     DIP(curinode, di_size) = len;
660     inodirty();
661     printactive(0);
662     return rval;
663 }
664 
665 CMDFUNCSTART(chmode)
666 {
667     int rval = 1;
668     long modebits;
669     char *cp;
670 
671     if (!checkactive())
672 	return 1;
673 
674     modebits = strtol(argv[1], &cp, 8);
675     if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
676 	warnx("bad modebits `%s'", argv[1]);
677 	return 1;
678     }
679 
680     DIP(curinode, di_mode) &= ~07777;
681     DIP(curinode, di_mode) |= modebits;
682     inodirty();
683     printactive(0);
684     return rval;
685 }
686 
687 CMDFUNCSTART(chaflags)
688 {
689     int rval = 1;
690     u_long flags;
691     char *cp;
692 
693     if (!checkactive())
694 	return 1;
695 
696     flags = strtoul(argv[1], &cp, 0);
697     if (cp == argv[1] || *cp != '\0' ) {
698 	warnx("bad flags `%s'", argv[1]);
699 	return 1;
700     }
701 
702     if (flags > UINT_MAX) {
703 	warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
704 	return(1);
705     }
706     DIP(curinode, di_flags) = flags;
707     inodirty();
708     printactive(0);
709     return rval;
710 }
711 
712 CMDFUNCSTART(chgen)
713 {
714     int rval = 1;
715     long gen;
716     char *cp;
717 
718     if (!checkactive())
719 	return 1;
720 
721     gen = strtol(argv[1], &cp, 0);
722     if (cp == argv[1] || *cp != '\0' ) {
723 	warnx("bad gen `%s'", argv[1]);
724 	return 1;
725     }
726 
727     if (gen > INT_MAX || gen < INT_MIN) {
728 	warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
729 	return(1);
730     }
731     DIP(curinode, di_gen) = gen;
732     inodirty();
733     printactive(0);
734     return rval;
735 }
736 
737 CMDFUNCSTART(linkcount)
738 {
739     int rval = 1;
740     int lcnt;
741     char *cp;
742 
743     if (!checkactive())
744 	return 1;
745 
746     lcnt = strtol(argv[1], &cp, 0);
747     if (cp == argv[1] || *cp != '\0' ) {
748 	warnx("bad link count `%s'", argv[1]);
749 	return 1;
750     }
751     if (lcnt > USHRT_MAX || lcnt < 0) {
752 	warnx("max link count is %d\n", USHRT_MAX);
753 	return 1;
754     }
755 
756     DIP(curinode, di_nlink) = lcnt;
757     inodirty();
758     printactive(0);
759     return rval;
760 }
761 
762 CMDFUNCSTART(chowner)
763 {
764     int rval = 1;
765     unsigned long uid;
766     char *cp;
767     struct passwd *pwd;
768 
769     if (!checkactive())
770 	return 1;
771 
772     uid = strtoul(argv[1], &cp, 0);
773     if (cp == argv[1] || *cp != '\0' ) {
774 	/* try looking up name */
775 	if ((pwd = getpwnam(argv[1]))) {
776 	    uid = pwd->pw_uid;
777 	} else {
778 	    warnx("bad uid `%s'", argv[1]);
779 	    return 1;
780 	}
781     }
782 
783     DIP(curinode, di_uid) = uid;
784     inodirty();
785     printactive(0);
786     return rval;
787 }
788 
789 CMDFUNCSTART(chgroup)
790 {
791     int rval = 1;
792     unsigned long gid;
793     char *cp;
794     struct group *grp;
795 
796     if (!checkactive())
797 	return 1;
798 
799     gid = strtoul(argv[1], &cp, 0);
800     if (cp == argv[1] || *cp != '\0' ) {
801 	if ((grp = getgrnam(argv[1]))) {
802 	    gid = grp->gr_gid;
803 	} else {
804 	    warnx("bad gid `%s'", argv[1]);
805 	    return 1;
806 	}
807     }
808 
809     DIP(curinode, di_gid) = gid;
810     inodirty();
811     printactive(0);
812     return rval;
813 }
814 
815 int
816 dotime(char *name, time_t *secp, int32_t *nsecp)
817 {
818     char *p, *val;
819     struct tm t;
820     int32_t nsec;
821     p = strchr(name, '.');
822     if (p) {
823 	*p = '\0';
824 	nsec = strtoul(++p, &val, 0);
825 	if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
826 		warnx("invalid nanoseconds");
827 		goto badformat;
828 	}
829     } else
830 	nsec = 0;
831     if (strlen(name) != 14) {
832 badformat:
833 	warnx("date format: YYYYMMDDHHMMSS[.nsec]");
834 	return 1;
835     }
836     *nsecp = nsec;
837 
838     for (p = name; *p; p++)
839 	if (*p < '0' || *p > '9')
840 	    goto badformat;
841 
842     p = name;
843 #define VAL() ((*p++) - '0')
844     t.tm_year = VAL();
845     t.tm_year = VAL() + t.tm_year * 10;
846     t.tm_year = VAL() + t.tm_year * 10;
847     t.tm_year = VAL() + t.tm_year * 10 - 1900;
848     t.tm_mon = VAL();
849     t.tm_mon = VAL() + t.tm_mon * 10 - 1;
850     t.tm_mday = VAL();
851     t.tm_mday = VAL() + t.tm_mday * 10;
852     t.tm_hour = VAL();
853     t.tm_hour = VAL() + t.tm_hour * 10;
854     t.tm_min = VAL();
855     t.tm_min = VAL() + t.tm_min * 10;
856     t.tm_sec = VAL();
857     t.tm_sec = VAL() + t.tm_sec * 10;
858     t.tm_isdst = -1;
859 
860     *secp = mktime(&t);
861     if (*secp == -1) {
862 	warnx("date/time out of range");
863 	return 1;
864     }
865     return 0;
866 }
867 
868 CMDFUNCSTART(chmtime)
869 {
870     time_t secs;
871     int32_t nsecs;
872 
873     if (dotime(argv[1], &secs, &nsecs))
874 	return 1;
875     if (sblock.fs_magic == FS_UFS1_MAGIC)
876 	curinode->dp1.di_mtime = _time_to_time32(secs);
877     else
878 	curinode->dp2.di_mtime = _time_to_time64(secs);
879     DIP(curinode, di_mtimensec) = nsecs;
880     inodirty();
881     printactive(0);
882     return 0;
883 }
884 
885 CMDFUNCSTART(chatime)
886 {
887     time_t secs;
888     int32_t nsecs;
889 
890     if (dotime(argv[1], &secs, &nsecs))
891 	return 1;
892     if (sblock.fs_magic == FS_UFS1_MAGIC)
893 	curinode->dp1.di_atime = _time_to_time32(secs);
894     else
895 	curinode->dp2.di_atime = _time_to_time64(secs);
896     DIP(curinode, di_atimensec) = nsecs;
897     inodirty();
898     printactive(0);
899     return 0;
900 }
901 
902 CMDFUNCSTART(chctime)
903 {
904     time_t secs;
905     int32_t nsecs;
906 
907     if (dotime(argv[1], &secs, &nsecs))
908 	return 1;
909     if (sblock.fs_magic == FS_UFS1_MAGIC)
910 	curinode->dp1.di_ctime = _time_to_time32(secs);
911     else
912 	curinode->dp2.di_ctime = _time_to_time64(secs);
913     DIP(curinode, di_ctimensec) = nsecs;
914     inodirty();
915     printactive(0);
916     return 0;
917 }
918