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