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