xref: /dragonfly/sbin/fsdb/fsdb.c (revision 38a690d7)
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  * $DragonFly: src/sbin/fsdb/fsdb.c,v 1.3 2003/08/08 04:18:38 dillon Exp $
32  */
33 
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <grp.h>
39 #include <histedit.h>
40 #include <pwd.h>
41 #include <string.h>
42 
43 #include <vfs/ufs/dinode.h>
44 #include <vfs/ufs/dir.h>
45 #include <vfs/ufs/fs.h>
46 
47 #include "fsdb.h"
48 #include "fsck.h"
49 
50 static void usage __P((void));
51 int cmdloop __P((void));
52 
53 static void
54 usage()
55 {
56 	fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
57 	exit(1);
58 }
59 
60 int returntosingle = 0;
61 char nflag = 0;
62 
63 /*
64  * We suck in lots of fsck code, and just pick & choose the stuff we want.
65  *
66  * fsreadfd is set up to read from the file system, fswritefd to write to
67  * the file system.
68  */
69 int
70 main(argc, argv)
71 	int argc;
72 	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 	if (!setup(fsys))
102 		errx(1, "cannot set up file system `%s'", fsys);
103 	printf("%s file system `%s'\nLast Mounted on %s\n",
104 	       nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
105 	rval = cmdloop();
106 	if (!nflag) {
107 		sblock.fs_clean = 0;	/* mark it dirty */
108 		sbdirty();
109 		ckfini(0);
110 		printf("*** FILE SYSTEM MARKED DIRTY\n");
111 		printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
112 		printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
113 	}
114 	exit(rval);
115 }
116 
117 #define CMDFUNC(func) int func __P((int argc, char *argv[]))
118 #define CMDFUNCSTART(func) int func(argc, argv)		\
119 				int argc;		\
120 				char *argv[];
121 
122 CMDFUNC(helpfn);
123 CMDFUNC(focus);				/* focus on inode */
124 CMDFUNC(active);			/* print active inode */
125 CMDFUNC(blocks);			/* print blocks for active inode */
126 CMDFUNC(focusname);			/* focus by name */
127 CMDFUNC(zapi);				/* clear inode */
128 CMDFUNC(uplink);			/* incr link */
129 CMDFUNC(downlink);			/* decr link */
130 CMDFUNC(linkcount);			/* set link count */
131 CMDFUNC(quit);				/* quit */
132 CMDFUNC(ls);				/* list directory */
133 CMDFUNC(rm);				/* remove name */
134 CMDFUNC(ln);				/* add name */
135 CMDFUNC(newtype);			/* change type */
136 CMDFUNC(chmode);			/* change mode */
137 CMDFUNC(chlen);				/* change length */
138 CMDFUNC(chaflags);			/* change flags */
139 CMDFUNC(chgen);				/* change generation */
140 CMDFUNC(chowner);			/* change owner */
141 CMDFUNC(chgroup);			/* Change group */
142 CMDFUNC(back);				/* pop back to last ino */
143 CMDFUNC(chmtime);			/* Change mtime */
144 CMDFUNC(chctime);			/* Change ctime */
145 CMDFUNC(chatime);			/* Change atime */
146 CMDFUNC(chinum);			/* Change inode # of dirent */
147 CMDFUNC(chname);			/* Change dirname of dirent */
148 
149 struct cmdtable cmds[] = {
150 	{ "help", "Print out help", 1, 1, FL_RO, helpfn },
151 	{ "?", "Print out help", 1, 1, FL_RO, helpfn },
152 	{ "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
153 	{ "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
154 	{ "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
155 	{ "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
156 	{ "back", "Go to previous active inode", 1, 1, FL_RO, back },
157 	{ "active", "Print active inode", 1, 1, FL_RO, active },
158 	{ "print", "Print active inode", 1, 1, FL_RO, active },
159 	{ "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
160 	{ "uplink", "Increment link count", 1, 1, FL_WR, uplink },
161 	{ "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
162 	{ "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
163 	{ "ls", "List current inode as directory", 1, 1, FL_RO, ls },
164 	{ "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
165 	{ "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
166 	{ "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln },
167 	{ "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
168 	{ "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname },
169 	{ "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
170 	{ "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
171 	{ "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
172 	{ "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
173 	{ "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
174 	{ "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
175 	{ "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
176 	{ "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
177 	{ "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
178 	{ "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
179 	{ "quit", "Exit", 1, 1, FL_RO, quit },
180 	{ "q", "Exit", 1, 1, FL_RO, quit },
181 	{ "exit", "Exit", 1, 1, FL_RO, quit },
182 	{ NULL, 0, 0, 0 },
183 };
184 
185 int
186 helpfn(argc, argv)
187 	int argc;
188 	char *argv[];
189 {
190     register struct cmdtable *cmdtp;
191 
192     printf("Commands are:\n%-10s %5s %5s   %s\n",
193 	   "command", "min argc", "max argc", "what");
194 
195     for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
196 	printf("%-10s %5u %5u   %s\n",
197 	       cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
198     return 0;
199 }
200 
201 char *
202 prompt(el)
203 	EditLine *el;
204 {
205     static char pstring[64];
206     snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
207     return pstring;
208 }
209 
210 
211 int
212 cmdloop()
213 {
214     char *line;
215     const char *elline;
216     int cmd_argc, rval = 0, known;
217 #define scratch known
218     char **cmd_argv;
219     struct cmdtable *cmdp;
220     History *hist;
221     EditLine *elptr;
222 
223     curinode = ginode(ROOTINO);
224     curinum = ROOTINO;
225     printactive(0);
226 
227     hist = history_init();
228     history(hist, H_EVENT, 100);	/* 100 elt history buffer */
229 
230     elptr = el_init("fsdb", stdin, stdout);
231     el_set(elptr, EL_EDITOR, "emacs");
232     el_set(elptr, EL_PROMPT, prompt);
233     el_set(elptr, EL_HIST, history, hist);
234     el_source(elptr, NULL);
235 
236     while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
237 	if (debug)
238 	    printf("command `%s'\n", elline);
239 
240 	history(hist, H_ENTER, elline);
241 
242 	line = strdup(elline);
243 	cmd_argv = crack(line, &cmd_argc);
244 	/*
245 	 * el_parse returns -1 to signal that it's not been handled
246 	 * internally.
247 	 */
248 	if (el_parse(elptr, cmd_argc, cmd_argv) != -1)
249 	    continue;
250 	if (cmd_argc) {
251 	    known = 0;
252 	    for (cmdp = cmds; cmdp->cmd; cmdp++) {
253 		if (!strcmp(cmdp->cmd, cmd_argv[0])) {
254 		    if ((cmdp->flags & FL_WR) == FL_WR && nflag)
255 			warnx("`%s' requires write access", cmd_argv[0]),
256 			    rval = 1;
257 		    else if (cmd_argc >= cmdp->minargc &&
258 			cmd_argc <= cmdp->maxargc)
259 			rval = (*cmdp->handler)(cmd_argc, cmd_argv);
260 		    else
261 			rval = argcount(cmdp, cmd_argc, cmd_argv);
262 		    known = 1;
263 		    break;
264 		}
265 	    }
266 	    if (!known)
267 		warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
268 	} else
269 	    rval = 0;
270 	free(line);
271 	if (rval < 0)
272 	    return rval;
273 	if (rval)
274 	    warnx("rval was %d", rval);
275     }
276     el_end(elptr);
277     history_end(hist);
278     return rval;
279 }
280 
281 struct dinode *curinode;
282 ino_t curinum, ocurrent;
283 
284 #define GETINUM(ac,inum)    inum = strtoul(argv[ac], &cp, 0); \
285     if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
286 	printf("inode %d out of range; range is [%d,%d]\n", \
287 	       inum, ROOTINO, maxino); \
288 	return 1; \
289     }
290 
291 /*
292  * Focus on given inode number
293  */
294 CMDFUNCSTART(focus)
295 {
296     ino_t inum;
297     char *cp;
298 
299     GETINUM(1,inum);
300     curinode = ginode(inum);
301     ocurrent = curinum;
302     curinum = inum;
303     printactive(0);
304     return 0;
305 }
306 
307 CMDFUNCSTART(back)
308 {
309     curinum = ocurrent;
310     curinode = ginode(curinum);
311     printactive(0);
312     return 0;
313 }
314 
315 CMDFUNCSTART(zapi)
316 {
317     ino_t inum;
318     struct dinode *dp;
319     char *cp;
320 
321     GETINUM(1,inum);
322     dp = ginode(inum);
323     clearinode(dp);
324     inodirty();
325     if (curinode)			/* re-set after potential change */
326 	curinode = ginode(curinum);
327     return 0;
328 }
329 
330 CMDFUNCSTART(active)
331 {
332     printactive(0);
333     return 0;
334 }
335 
336 CMDFUNCSTART(blocks)
337 {
338     printactive(1);
339     return 0;
340 }
341 
342 CMDFUNCSTART(quit)
343 {
344     return -1;
345 }
346 
347 CMDFUNCSTART(uplink)
348 {
349     if (!checkactive())
350 	return 1;
351     printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink);
352     inodirty();
353     return 0;
354 }
355 
356 CMDFUNCSTART(downlink)
357 {
358     if (!checkactive())
359 	return 1;
360     printf("inode %d link count now %d\n", curinum, --curinode->di_nlink);
361     inodirty();
362     return 0;
363 }
364 
365 const char *typename[] = {
366     "unknown",
367     "fifo",
368     "char special",
369     "unregistered #3",
370     "directory",
371     "unregistered #5",
372     "blk special",
373     "unregistered #7",
374     "regular",
375     "unregistered #9",
376     "symlink",
377     "unregistered #11",
378     "socket",
379     "unregistered #13",
380     "whiteout",
381 };
382 
383 int slot;
384 
385 int
386 scannames(idesc)
387 	struct inodesc *idesc;
388 {
389 	register struct direct *dirp = idesc->id_dirp;
390 
391 	printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
392 	       slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
393 	       dirp->d_namlen, dirp->d_name);
394 	return (KEEPON);
395 }
396 
397 CMDFUNCSTART(ls)
398 {
399     struct inodesc idesc;
400     checkactivedir();			/* let it go on anyway */
401 
402     slot = 0;
403     idesc.id_number = curinum;
404     idesc.id_func = scannames;
405     idesc.id_type = DATA;
406     idesc.id_fix = IGNORE;
407     ckinode(curinode, &idesc);
408     curinode = ginode(curinum);
409 
410     return 0;
411 }
412 
413 int findino __P((struct inodesc *idesc)); /* from fsck */
414 static int dolookup __P((char *name));
415 
416 static int
417 dolookup(name)
418 	char *name;
419 {
420     struct inodesc idesc;
421 
422     if (!checkactivedir())
423 	    return 0;
424     idesc.id_number = curinum;
425     idesc.id_func = findino;
426     idesc.id_name = name;
427     idesc.id_type = DATA;
428     idesc.id_fix = IGNORE;
429     if (ckinode(curinode, &idesc) & FOUND) {
430 	curinum = idesc.id_parent;
431 	curinode = ginode(curinum);
432 	printactive(0);
433 	return 1;
434     } else {
435 	warnx("name `%s' not found in current inode directory", name);
436 	return 0;
437     }
438 }
439 
440 CMDFUNCSTART(focusname)
441 {
442     char *p, *val;
443 
444     if (!checkactive())
445 	return 1;
446 
447     ocurrent = curinum;
448 
449     if (argv[1][0] == '/') {
450 	curinum = ROOTINO;
451 	curinode = ginode(ROOTINO);
452     } else {
453 	if (!checkactivedir())
454 	    return 1;
455     }
456     for (p = argv[1]; p != NULL;) {
457 	while ((val = strsep(&p, "/")) != NULL && *val == '\0');
458 	if (val) {
459 	    printf("component `%s': ", val);
460 	    fflush(stdout);
461 	    if (!dolookup(val)) {
462 		curinode = ginode(curinum);
463 		return(1);
464 	    }
465 	}
466     }
467     return 0;
468 }
469 
470 CMDFUNCSTART(ln)
471 {
472     ino_t inum;
473     int rval;
474     char *cp;
475 
476     GETINUM(1,inum);
477 
478     if (!checkactivedir())
479 	return 1;
480     rval = makeentry(curinum, inum, argv[2]);
481     if (rval)
482 	printf("Ino %d entered as `%s'\n", inum, argv[2]);
483     else
484 	printf("could not enter name? weird.\n");
485     curinode = ginode(curinum);
486     return rval;
487 }
488 
489 CMDFUNCSTART(rm)
490 {
491     int rval;
492 
493     if (!checkactivedir())
494 	return 1;
495     rval = changeino(curinum, argv[1], 0);
496     if (rval & ALTERED) {
497 	printf("Name `%s' removed\n", argv[1]);
498 	return 0;
499     } else {
500 	printf("could not remove name? weird.\n");
501 	return 1;
502     }
503 }
504 
505 long slotcount, desired;
506 
507 int
508 chinumfunc(idesc)
509 	struct inodesc *idesc;
510 {
511 	register struct direct *dirp = idesc->id_dirp;
512 
513 	if (slotcount++ == desired) {
514 	    dirp->d_ino = idesc->id_parent;
515 	    return STOP|ALTERED|FOUND;
516 	}
517 	return KEEPON;
518 }
519 
520 CMDFUNCSTART(chinum)
521 {
522     char *cp;
523     ino_t inum;
524     struct inodesc idesc;
525 
526     slotcount = 0;
527     if (!checkactivedir())
528 	return 1;
529     GETINUM(2,inum);
530 
531     desired = strtol(argv[1], &cp, 0);
532     if (cp == argv[1] || *cp != '\0' || desired < 0) {
533 	printf("invalid slot number `%s'\n", argv[1]);
534 	return 1;
535     }
536 
537     idesc.id_number = curinum;
538     idesc.id_func = chinumfunc;
539     idesc.id_fix = IGNORE;
540     idesc.id_type = DATA;
541     idesc.id_parent = inum;		/* XXX convenient hiding place */
542 
543     if (ckinode(curinode, &idesc) & FOUND)
544 	return 0;
545     else {
546 	warnx("no %sth slot in current directory", argv[1]);
547 	return 1;
548     }
549 }
550 
551 int
552 chnamefunc(idesc)
553 	struct inodesc *idesc;
554 {
555 	register 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 = 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     curinode->di_mode &= ~IFMT;
638     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     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     curinode->di_mode &= ~07777;
681     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     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     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     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     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     curinode->di_gid = gid;
810     inodirty();
811     printactive(0);
812     return rval;
813 }
814 
815 int
816 dotime(name, rts)
817 	char *name;
818 	struct timespec *rts;
819 {
820     char *p, *val;
821     struct tm t;
822     int32_t sec;
823     int32_t nsec;
824     p = strchr(name, '.');
825     if (p) {
826 	*p = '\0';
827 	nsec = strtoul(++p, &val, 0);
828 	if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
829 		warnx("invalid nanoseconds");
830 		goto badformat;
831 	}
832     } else
833 	nsec = 0;
834     if (strlen(name) != 14) {
835 badformat:
836 	warnx("date format: YYYYMMDDHHMMSS[.nsec]");
837 	return 1;
838     }
839 
840     for (p = name; *p; p++)
841 	if (*p < '0' || *p > '9')
842 	    goto badformat;
843 
844     p = name;
845 #define VAL() ((*p++) - '0')
846     t.tm_year = VAL();
847     t.tm_year = VAL() + t.tm_year * 10;
848     t.tm_year = VAL() + t.tm_year * 10;
849     t.tm_year = VAL() + t.tm_year * 10 - 1900;
850     t.tm_mon = VAL();
851     t.tm_mon = VAL() + t.tm_mon * 10 - 1;
852     t.tm_mday = VAL();
853     t.tm_mday = VAL() + t.tm_mday * 10;
854     t.tm_hour = VAL();
855     t.tm_hour = VAL() + t.tm_hour * 10;
856     t.tm_min = VAL();
857     t.tm_min = VAL() + t.tm_min * 10;
858     t.tm_sec = VAL();
859     t.tm_sec = VAL() + t.tm_sec * 10;
860     t.tm_isdst = -1;
861 
862     sec = mktime(&t);
863     if (sec == -1) {
864 	warnx("date/time out of range");
865 	return 1;
866     }
867     rts->tv_sec = sec;
868     rts->tv_nsec = nsec;
869     return 0;
870 }
871 
872 CMDFUNCSTART(chmtime)
873 {
874     if (dotime(argv[1], &curinode->di_ctime))
875 	return 1;
876     inodirty();
877     printactive(0);
878     return 0;
879 }
880 
881 CMDFUNCSTART(chatime)
882 {
883     if (dotime(argv[1], &curinode->di_ctime))
884 	return 1;
885     inodirty();
886     printactive(0);
887     return 0;
888 }
889 
890 CMDFUNCSTART(chctime)
891 {
892     if (dotime(argv[1], &curinode->di_ctime))
893 	return 1;
894     inodirty();
895     printactive(0);
896     return 0;
897 }
898