xref: /dragonfly/sbin/fsdb/fsdb.c (revision 5dfd06ac)
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.9 2006/04/03 01:58:49 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(void);
51 int cmdloop(void);
52 
53 static void
54 usage(void)
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(int argc, char **argv)
71 {
72 	int ch, rval;
73 	char *fsys = NULL;
74 
75 	while (-1 != (ch = getopt(argc, argv, "fdr"))) {
76 		switch (ch) {
77 		case 'f':
78 			/* The -f option is left for historical
79 			 * reasons and has no meaning.
80 			 */
81 			break;
82 		case 'd':
83 			debug++;
84 			break;
85 		case 'r':
86 			nflag++; /* "no" in fsck, readonly for us */
87 			break;
88 		default:
89 			usage();
90 		}
91 	}
92 	argc -= optind;
93 	argv += optind;
94 	if (argc != 1)
95 		usage();
96 	else
97 		fsys = argv[0];
98 
99 	if (!setup(fsys))
100 		errx(1, "cannot set up file system `%s'", fsys);
101 	printf("%s file system `%s'\nLast Mounted on %s\n",
102 	       nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
103 	rval = cmdloop();
104 	if (!nflag) {
105 		sblock.fs_clean = 0;	/* mark it dirty */
106 		sbdirty();
107 		ckfini(0);
108 		printf("*** FILE SYSTEM MARKED DIRTY\n");
109 		printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
110 		printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
111 	}
112 	exit(rval);
113 }
114 
115 #define CMDFUNC(func) int func (int argc, char **argv)
116 #define CMDFUNCSTART(func) int func(int argc, char **argv)
117 
118 CMDFUNC(helpfn);
119 CMDFUNC(focus);				/* focus on inode */
120 CMDFUNC(active);			/* print active inode */
121 CMDFUNC(blocks);			/* print blocks for active inode */
122 CMDFUNC(focusname);			/* focus by name */
123 CMDFUNC(zapi);				/* clear inode */
124 CMDFUNC(uplink);			/* incr link */
125 CMDFUNC(downlink);			/* decr link */
126 CMDFUNC(linkcount);			/* set link count */
127 CMDFUNC(quit);				/* quit */
128 CMDFUNC(ls);				/* list directory */
129 CMDFUNC(rm);				/* remove name */
130 CMDFUNC(ln);				/* add name */
131 CMDFUNC(newtype);			/* change type */
132 CMDFUNC(chmode);			/* change mode */
133 CMDFUNC(chlen);				/* change length */
134 CMDFUNC(chaflags);			/* change flags */
135 CMDFUNC(chgen);				/* change generation */
136 CMDFUNC(chowner);			/* change owner */
137 CMDFUNC(chgroup);			/* Change group */
138 CMDFUNC(back);				/* pop back to last ino */
139 CMDFUNC(chmtime);			/* Change mtime */
140 CMDFUNC(chctime);			/* Change ctime */
141 CMDFUNC(chatime);			/* Change atime */
142 CMDFUNC(chinum);			/* Change inode # of dirent */
143 CMDFUNC(chname);			/* Change dirname of dirent */
144 
145 struct cmdtable cmds[] = {
146 	{ "help", "Print out help", 1, 1, FL_RO, helpfn },
147 	{ "?", "Print out help", 1, 1, FL_RO, helpfn },
148 	{ "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
149 	{ "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
150 	{ "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
151 	{ "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname },
152 	{ "back", "Go to previous active inode", 1, 1, FL_RO, back },
153 	{ "active", "Print active inode", 1, 1, FL_RO, active },
154 	{ "print", "Print active inode", 1, 1, FL_RO, active },
155 	{ "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
156 	{ "uplink", "Increment link count", 1, 1, FL_WR, uplink },
157 	{ "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
158 	{ "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
159 	{ "ls", "List current inode as directory", 1, 1, FL_RO, ls },
160 	{ "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
161 	{ "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm },
162 	{ "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln },
163 	{ "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
164 	{ "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname },
165 	{ "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
166 	{ "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
167 	{ "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
168 	{ "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
169 	{ "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
170 	{ "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
171 	{ "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
172 	{ "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
173 	{ "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
174 	{ "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
175 	{ "quit", "Exit", 1, 1, FL_RO, quit },
176 	{ "q", "Exit", 1, 1, FL_RO, quit },
177 	{ "exit", "Exit", 1, 1, FL_RO, quit },
178 	{ NULL, 0, 0, 0 },
179 };
180 
181 int
182 helpfn(int argc, char **argv)
183 {
184     struct cmdtable *cmdtp;
185 
186     printf("Commands are:\n%-10s %5s %5s   %s\n",
187 	   "command", "min argc", "max argc", "what");
188 
189     for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
190 	printf("%-10s %5u %5u   %s\n",
191 	       cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt);
192     return 0;
193 }
194 
195 char *
196 prompt(EditLine *el)
197 {
198     static char pstring[64];
199     snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", 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 %d out of range; range is [%d,%d]\n", \
281 	       inum, ROOTINO, 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 %d link count now %d\n", curinum, ++curinode->di_nlink);
346     inodirty();
347     return 0;
348 }
349 
350 CMDFUNCSTART(downlink)
351 {
352     if (!checkactive())
353 	return 1;
354     printf("inode %d link count now %d\n", curinum, --curinode->di_nlink);
355     inodirty();
356     return 0;
357 }
358 
359 const char *typename[] = {
360     "unknown",
361     "fifo",
362     "char special",
363     "unregistered #3",
364     "directory",
365     "unregistered #5",
366     "blk special",
367     "unregistered #7",
368     "regular",
369     "unregistered #9",
370     "symlink",
371     "unregistered #11",
372     "socket",
373     "unregistered #13",
374     "whiteout",
375 };
376 
377 int slot;
378 
379 int
380 scannames(struct inodesc *idesc)
381 {
382 	struct direct *dirp = idesc->id_dirp;
383 
384 	printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
385 	       slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
386 	       dirp->d_namlen, dirp->d_name);
387 	return (KEEPON);
388 }
389 
390 CMDFUNCSTART(ls)
391 {
392     struct inodesc idesc;
393     checkactivedir();			/* let it go on anyway */
394 
395     slot = 0;
396     idesc.id_number = curinum;
397     idesc.id_func = scannames;
398     idesc.id_type = DATA;
399     idesc.id_fix = IGNORE;
400     ckinode(curinode, &idesc);
401     curinode = ginode(curinum);
402 
403     return 0;
404 }
405 
406 int findino(struct inodesc *idesc); /* from fsck */
407 static int dolookup(char *name);
408 
409 static int
410 dolookup(char *name)
411 {
412     struct inodesc idesc;
413 
414     if (!checkactivedir())
415 	    return 0;
416     idesc.id_number = curinum;
417     idesc.id_func = findino;
418     idesc.id_name = name;
419     idesc.id_type = DATA;
420     idesc.id_fix = IGNORE;
421     if (ckinode(curinode, &idesc) & FOUND) {
422 	curinum = idesc.id_parent;
423 	curinode = ginode(curinum);
424 	printactive(0);
425 	return 1;
426     } else {
427 	warnx("name `%s' not found in current inode directory", name);
428 	return 0;
429     }
430 }
431 
432 CMDFUNCSTART(focusname)
433 {
434     char *p, *val;
435 
436     if (!checkactive())
437 	return 1;
438 
439     ocurrent = curinum;
440 
441     if (argv[1][0] == '/') {
442 	curinum = ROOTINO;
443 	curinode = ginode(ROOTINO);
444     } else {
445 	if (!checkactivedir())
446 	    return 1;
447     }
448     for (p = argv[1]; p != NULL;) {
449 	while ((val = strsep(&p, "/")) != NULL && *val == '\0');
450 	if (val) {
451 	    printf("component `%s': ", val);
452 	    fflush(stdout);
453 	    if (!dolookup(val)) {
454 		curinode = ginode(curinum);
455 		return(1);
456 	    }
457 	}
458     }
459     return 0;
460 }
461 
462 CMDFUNCSTART(ln)
463 {
464     ino_t inum;
465     int rval;
466     char *cp;
467 
468     GETINUM(1,inum);
469 
470     if (!checkactivedir())
471 	return 1;
472     rval = makeentry(curinum, inum, argv[2]);
473     if (rval)
474 	printf("Ino %d entered as `%s'\n", inum, argv[2]);
475     else
476 	printf("could not enter name? weird.\n");
477     curinode = ginode(curinum);
478     return rval;
479 }
480 
481 CMDFUNCSTART(rm)
482 {
483     int rval;
484 
485     if (!checkactivedir())
486 	return 1;
487     rval = changeino(curinum, argv[1], 0);
488     if (rval & ALTERED) {
489 	printf("Name `%s' removed\n", argv[1]);
490 	return 0;
491     } else {
492 	printf("could not remove name? weird.\n");
493 	return 1;
494     }
495 }
496 
497 long slotcount, desired;
498 
499 int
500 chinumfunc(struct inodesc *idesc)
501 {
502 	struct direct *dirp = idesc->id_dirp;
503 
504 	if (slotcount++ == desired) {
505 	    dirp->d_ino = idesc->id_parent;
506 	    return STOP|ALTERED|FOUND;
507 	}
508 	return KEEPON;
509 }
510 
511 CMDFUNCSTART(chinum)
512 {
513     char *cp;
514     ino_t inum;
515     struct inodesc idesc;
516 
517     slotcount = 0;
518     if (!checkactivedir())
519 	return 1;
520     GETINUM(2,inum);
521 
522     desired = strtol(argv[1], &cp, 0);
523     if (cp == argv[1] || *cp != '\0' || desired < 0) {
524 	printf("invalid slot number `%s'\n", argv[1]);
525 	return 1;
526     }
527 
528     idesc.id_number = curinum;
529     idesc.id_func = chinumfunc;
530     idesc.id_fix = IGNORE;
531     idesc.id_type = DATA;
532     idesc.id_parent = inum;		/* XXX convenient hiding place */
533 
534     if (ckinode(curinode, &idesc) & FOUND)
535 	return 0;
536     else {
537 	warnx("no %sth slot in current directory", argv[1]);
538 	return 1;
539     }
540 }
541 
542 int
543 chnamefunc(struct inodesc *idesc)
544 {
545 	struct direct *dirp = idesc->id_dirp;
546 	struct direct testdir;
547 
548 	if (slotcount++ == desired) {
549 	    /* will name fit? */
550 	    testdir.d_namlen = strlen(idesc->id_name);
551 	    if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
552 		dirp->d_namlen = testdir.d_namlen;
553 		strcpy(dirp->d_name, idesc->id_name);
554 		return STOP|ALTERED|FOUND;
555 	    } else
556 		return STOP|FOUND;	/* won't fit, so give up */
557 	}
558 	return KEEPON;
559 }
560 
561 CMDFUNCSTART(chname)
562 {
563     int rval;
564     char *cp;
565     struct inodesc idesc;
566 
567     slotcount = 0;
568     if (!checkactivedir())
569 	return 1;
570 
571     desired = strtoul(argv[1], &cp, 0);
572     if (cp == argv[1] || *cp != '\0') {
573 	printf("invalid slot number `%s'\n", argv[1]);
574 	return 1;
575     }
576 
577     idesc.id_number = curinum;
578     idesc.id_func = chnamefunc;
579     idesc.id_fix = IGNORE;
580     idesc.id_type = DATA;
581     idesc.id_name = argv[2];
582 
583     rval = ckinode(curinode, &idesc);
584     if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
585 	return 0;
586     else if (rval & FOUND) {
587 	warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
588 	return 1;
589     } else {
590 	warnx("no %sth slot in current directory", argv[1]);
591 	return 1;
592     }
593 }
594 
595 struct typemap {
596     const char *typename;
597     int typebits;
598 } typenamemap[]  = {
599     {"file", IFREG},
600     {"dir", IFDIR},
601     {"socket", IFSOCK},
602     {"fifo", IFIFO},
603 };
604 
605 CMDFUNCSTART(newtype)
606 {
607     int type;
608     struct typemap *tp;
609 
610     if (!checkactive())
611 	return 1;
612     type = curinode->di_mode & IFMT;
613     for (tp = typenamemap;
614 	 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
615 	 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[sizeof(typenamemap)/sizeof(*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, struct timespec *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->tv_sec = sec;
856     rts->tv_nsec = 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