xref: /dragonfly/sbin/fsdb/fsdb.c (revision 560e4370)
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;
616 	 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
617 	 tp++) {
618 	if (!strcmp(argv[1], tp->typename)) {
619 	    printf("setting type to %s\n", tp->typename);
620 	    type = tp->typebits;
621 	    break;
622 	}
623     }
624     if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
625 	warnx("type `%s' not known", argv[1]);
626 	warnx("try one of `file', `dir', `socket', `fifo'");
627 	return 1;
628     }
629     curinode->di_mode &= ~IFMT;
630     curinode->di_mode |= type;
631     inodirty();
632     printactive(0);
633     return 0;
634 }
635 
636 CMDFUNCSTART(chlen)
637 {
638     int rval = 1;
639     long len;
640     char *cp;
641 
642     if (!checkactive())
643 	return 1;
644 
645     len = strtol(argv[1], &cp, 0);
646     if (cp == argv[1] || *cp != '\0' || len < 0) {
647 	warnx("bad length `%s'", argv[1]);
648 	return 1;
649     }
650 
651     curinode->di_size = len;
652     inodirty();
653     printactive(0);
654     return rval;
655 }
656 
657 CMDFUNCSTART(chmode)
658 {
659     int rval = 1;
660     long modebits;
661     char *cp;
662 
663     if (!checkactive())
664 	return 1;
665 
666     modebits = strtol(argv[1], &cp, 8);
667     if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
668 	warnx("bad modebits `%s'", argv[1]);
669 	return 1;
670     }
671 
672     curinode->di_mode &= ~07777;
673     curinode->di_mode |= modebits;
674     inodirty();
675     printactive(0);
676     return rval;
677 }
678 
679 CMDFUNCSTART(chaflags)
680 {
681     int rval = 1;
682     u_long flags;
683     char *cp;
684 
685     if (!checkactive())
686 	return 1;
687 
688     flags = strtoul(argv[1], &cp, 0);
689     if (cp == argv[1] || *cp != '\0' ) {
690 	warnx("bad flags `%s'", argv[1]);
691 	return 1;
692     }
693 
694     if (flags > UINT_MAX) {
695 	warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
696 	return(1);
697     }
698     curinode->di_flags = flags;
699     inodirty();
700     printactive(0);
701     return rval;
702 }
703 
704 CMDFUNCSTART(chgen)
705 {
706     int rval = 1;
707     long gen;
708     char *cp;
709 
710     if (!checkactive())
711 	return 1;
712 
713     gen = strtol(argv[1], &cp, 0);
714     if (cp == argv[1] || *cp != '\0' ) {
715 	warnx("bad gen `%s'", argv[1]);
716 	return 1;
717     }
718 
719     if (gen > INT_MAX || gen < INT_MIN) {
720 	warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
721 	return(1);
722     }
723     curinode->di_gen = gen;
724     inodirty();
725     printactive(0);
726     return rval;
727 }
728 
729 CMDFUNCSTART(linkcount)
730 {
731     int rval = 1;
732     int lcnt;
733     char *cp;
734 
735     if (!checkactive())
736 	return 1;
737 
738     lcnt = strtol(argv[1], &cp, 0);
739     if (cp == argv[1] || *cp != '\0' ) {
740 	warnx("bad link count `%s'", argv[1]);
741 	return 1;
742     }
743     if (lcnt > USHRT_MAX || lcnt < 0) {
744 	warnx("max link count is %d\n", USHRT_MAX);
745 	return 1;
746     }
747 
748     curinode->di_nlink = lcnt;
749     inodirty();
750     printactive(0);
751     return rval;
752 }
753 
754 CMDFUNCSTART(chowner)
755 {
756     int rval = 1;
757     unsigned long uid;
758     char *cp;
759     struct passwd *pwd;
760 
761     if (!checkactive())
762 	return 1;
763 
764     uid = strtoul(argv[1], &cp, 0);
765     if (cp == argv[1] || *cp != '\0' ) {
766 	/* try looking up name */
767 	if ((pwd = getpwnam(argv[1]))) {
768 	    uid = pwd->pw_uid;
769 	} else {
770 	    warnx("bad uid `%s'", argv[1]);
771 	    return 1;
772 	}
773     }
774 
775     curinode->di_uid = uid;
776     inodirty();
777     printactive(0);
778     return rval;
779 }
780 
781 CMDFUNCSTART(chgroup)
782 {
783     int rval = 1;
784     unsigned long gid;
785     char *cp;
786     struct group *grp;
787 
788     if (!checkactive())
789 	return 1;
790 
791     gid = strtoul(argv[1], &cp, 0);
792     if (cp == argv[1] || *cp != '\0' ) {
793 	if ((grp = getgrnam(argv[1]))) {
794 	    gid = grp->gr_gid;
795 	} else {
796 	    warnx("bad gid `%s'", argv[1]);
797 	    return 1;
798 	}
799     }
800 
801     curinode->di_gid = gid;
802     inodirty();
803     printactive(0);
804     return rval;
805 }
806 
807 int
808 dotime(char *name, int32_t *rts)
809 {
810     char *p, *val;
811     struct tm t;
812     int32_t sec;
813     int32_t nsec;
814     p = strchr(name, '.');
815     if (p) {
816 	*p = '\0';
817 	nsec = strtoul(++p, &val, 0);
818 	if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
819 		warnx("invalid nanoseconds");
820 		goto badformat;
821 	}
822     } else
823 	nsec = 0;
824     if (strlen(name) != 14) {
825 badformat:
826 	warnx("date format: YYYYMMDDHHMMSS[.nsec]");
827 	return 1;
828     }
829 
830     for (p = name; *p; p++)
831 	if (*p < '0' || *p > '9')
832 	    goto badformat;
833 
834     p = name;
835 #define VAL() ((*p++) - '0')
836     t.tm_year = VAL();
837     t.tm_year = VAL() + t.tm_year * 10;
838     t.tm_year = VAL() + t.tm_year * 10;
839     t.tm_year = VAL() + t.tm_year * 10 - 1900;
840     t.tm_mon = VAL();
841     t.tm_mon = VAL() + t.tm_mon * 10 - 1;
842     t.tm_mday = VAL();
843     t.tm_mday = VAL() + t.tm_mday * 10;
844     t.tm_hour = VAL();
845     t.tm_hour = VAL() + t.tm_hour * 10;
846     t.tm_min = VAL();
847     t.tm_min = VAL() + t.tm_min * 10;
848     t.tm_sec = VAL();
849     t.tm_sec = VAL() + t.tm_sec * 10;
850     t.tm_isdst = -1;
851 
852     sec = mktime(&t);
853     if (sec == -1) {
854 	warnx("date/time out of range");
855 	return 1;
856     }
857     rts[0] = sec;
858     rts[1] = nsec;
859     return 0;
860 }
861 
862 CMDFUNCSTART(chmtime)
863 {
864     if (dotime(argv[1], &curinode->di_mtime))
865 	return 1;
866     inodirty();
867     printactive(0);
868     return 0;
869 }
870 
871 CMDFUNCSTART(chatime)
872 {
873     if (dotime(argv[1], &curinode->di_atime))
874 	return 1;
875     inodirty();
876     printactive(0);
877     return 0;
878 }
879 
880 CMDFUNCSTART(chctime)
881 {
882     if (dotime(argv[1], &curinode->di_ctime))
883 	return 1;
884     inodirty();
885     printactive(0);
886     return 0;
887 }
888