A cvsup file that pulls the "checked out" version of source. I'm referencing
[dragonfly.git] / sbin / fsdb / fsdb.c
blobb4619fd5e829cd5822d87c660cc53bac5a597f4a
1 /* $NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 thorpej Exp $ */
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.
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.
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.6 2003/11/01 17:15:59 drhodus Exp $
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>
43 #include <vfs/ufs/dinode.h>
44 #include <vfs/ufs/dir.h>
45 #include <vfs/ufs/fs.h>
47 #include "fsdb.h"
48 #include "fsck.h"
50 static void usage(void);
51 int cmdloop(void);
53 static void
54 usage(void)
56 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
57 exit(1);
60 int returntosingle = 0;
61 char nflag = 0;
64 * We suck in lots of fsck code, and just pick & choose the stuff we want.
66 * fsreadfd is set up to read from the file system, fswritefd to write to
67 * the file system.
69 int
70 main(int argc, char **argv)
72 int ch, rval;
73 char *fsys = NULL;
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.
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();
92 argc -= optind;
93 argv += optind;
94 if (argc != 1)
95 usage();
96 else
97 fsys = argv[0];
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");
112 exit(rval);
115 #define CMDFUNC(func) int func (int argc, char **argv)
116 #define CMDFUNCSTART(func) int func(int argc, char **argv)
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 */
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 },
182 helpfn(int argc, char **argv)
184 register struct cmdtable *cmdtp;
186 printf("Commands are:\n%-10s %5s %5s %s\n",
187 "command", "min argc", "max argc", "what");
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;
195 char *
196 prompt(EditLine *el)
198 static char pstring[64];
199 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum);
200 return pstring;
205 cmdloop(void)
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 EditLine *elptr;
216 curinode = ginode(ROOTINO);
217 curinum = ROOTINO;
218 printactive(0);
220 hist = history_init();
221 history(hist, H_EVENT, 100); /* 100 elt history buffer */
223 elptr = el_init("fsdb", stdin, stdout);
224 el_set(elptr, EL_EDITOR, "emacs");
225 el_set(elptr, EL_PROMPT, prompt);
226 el_set(elptr, EL_HIST, history, hist);
227 el_source(elptr, NULL);
229 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
230 if (debug)
231 printf("command `%s'\n", elline);
233 history(hist, H_ENTER, elline);
235 line = strdup(elline);
236 cmd_argv = crack(line, &cmd_argc);
238 * el_parse returns -1 to signal that it's not been handled
239 * internally.
241 if (el_parse(elptr, cmd_argc, cmd_argv) != -1)
242 continue;
243 if (cmd_argc) {
244 known = 0;
245 for (cmdp = cmds; cmdp->cmd; cmdp++) {
246 if (!strcmp(cmdp->cmd, cmd_argv[0])) {
247 if ((cmdp->flags & FL_WR) == FL_WR && nflag)
248 warnx("`%s' requires write access", cmd_argv[0]),
249 rval = 1;
250 else if (cmd_argc >= cmdp->minargc &&
251 cmd_argc <= cmdp->maxargc)
252 rval = (*cmdp->handler)(cmd_argc, cmd_argv);
253 else
254 rval = argcount(cmdp, cmd_argc, cmd_argv);
255 known = 1;
256 break;
259 if (!known)
260 warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
261 } else
262 rval = 0;
263 free(line);
264 if (rval < 0)
265 return rval;
266 if (rval)
267 warnx("rval was %d", rval);
269 el_end(elptr);
270 history_end(hist);
271 return rval;
274 struct dinode *curinode;
275 ino_t curinum, ocurrent;
277 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \
278 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
279 printf("inode %d out of range; range is [%d,%d]\n", \
280 inum, ROOTINO, maxino); \
281 return 1; \
285 * Focus on given inode number
287 CMDFUNCSTART(focus)
289 ino_t inum;
290 char *cp;
292 GETINUM(1,inum);
293 curinode = ginode(inum);
294 ocurrent = curinum;
295 curinum = inum;
296 printactive(0);
297 return 0;
300 CMDFUNCSTART(back)
302 curinum = ocurrent;
303 curinode = ginode(curinum);
304 printactive(0);
305 return 0;
308 CMDFUNCSTART(zapi)
310 ino_t inum;
311 struct dinode *dp;
312 char *cp;
314 GETINUM(1,inum);
315 dp = ginode(inum);
316 clearinode(dp);
317 inodirty();
318 if (curinode) /* re-set after potential change */
319 curinode = ginode(curinum);
320 return 0;
323 CMDFUNCSTART(active)
325 printactive(0);
326 return 0;
329 CMDFUNCSTART(blocks)
331 printactive(1);
332 return 0;
335 CMDFUNCSTART(quit)
337 return -1;
340 CMDFUNCSTART(uplink)
342 if (!checkactive())
343 return 1;
344 printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink);
345 inodirty();
346 return 0;
349 CMDFUNCSTART(downlink)
351 if (!checkactive())
352 return 1;
353 printf("inode %d link count now %d\n", curinum, --curinode->di_nlink);
354 inodirty();
355 return 0;
358 const char *typename[] = {
359 "unknown",
360 "fifo",
361 "char special",
362 "unregistered #3",
363 "directory",
364 "unregistered #5",
365 "blk special",
366 "unregistered #7",
367 "regular",
368 "unregistered #9",
369 "symlink",
370 "unregistered #11",
371 "socket",
372 "unregistered #13",
373 "whiteout",
376 int slot;
379 scannames(struct inodesc *idesc)
381 register struct direct *dirp = idesc->id_dirp;
383 printf("slot %d ino %d reclen %d: %s, `%.*s'\n",
384 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type],
385 dirp->d_namlen, dirp->d_name);
386 return (KEEPON);
389 CMDFUNCSTART(ls)
391 struct inodesc idesc;
392 checkactivedir(); /* let it go on anyway */
394 slot = 0;
395 idesc.id_number = curinum;
396 idesc.id_func = scannames;
397 idesc.id_type = DATA;
398 idesc.id_fix = IGNORE;
399 ckinode(curinode, &idesc);
400 curinode = ginode(curinum);
402 return 0;
405 int findino(struct inodesc *idesc); /* from fsck */
406 static int dolookup(char *name);
408 static int
409 dolookup(char *name)
411 struct inodesc idesc;
413 if (!checkactivedir())
414 return 0;
415 idesc.id_number = curinum;
416 idesc.id_func = findino;
417 idesc.id_name = name;
418 idesc.id_type = DATA;
419 idesc.id_fix = IGNORE;
420 if (ckinode(curinode, &idesc) & FOUND) {
421 curinum = idesc.id_parent;
422 curinode = ginode(curinum);
423 printactive(0);
424 return 1;
425 } else {
426 warnx("name `%s' not found in current inode directory", name);
427 return 0;
431 CMDFUNCSTART(focusname)
433 char *p, *val;
435 if (!checkactive())
436 return 1;
438 ocurrent = curinum;
440 if (argv[1][0] == '/') {
441 curinum = ROOTINO;
442 curinode = ginode(ROOTINO);
443 } else {
444 if (!checkactivedir())
445 return 1;
447 for (p = argv[1]; p != NULL;) {
448 while ((val = strsep(&p, "/")) != NULL && *val == '\0');
449 if (val) {
450 printf("component `%s': ", val);
451 fflush(stdout);
452 if (!dolookup(val)) {
453 curinode = ginode(curinum);
454 return(1);
458 return 0;
461 CMDFUNCSTART(ln)
463 ino_t inum;
464 int rval;
465 char *cp;
467 GETINUM(1,inum);
469 if (!checkactivedir())
470 return 1;
471 rval = makeentry(curinum, inum, argv[2]);
472 if (rval)
473 printf("Ino %d entered as `%s'\n", inum, argv[2]);
474 else
475 printf("could not enter name? weird.\n");
476 curinode = ginode(curinum);
477 return rval;
480 CMDFUNCSTART(rm)
482 int rval;
484 if (!checkactivedir())
485 return 1;
486 rval = changeino(curinum, argv[1], 0);
487 if (rval & ALTERED) {
488 printf("Name `%s' removed\n", argv[1]);
489 return 0;
490 } else {
491 printf("could not remove name? weird.\n");
492 return 1;
496 long slotcount, desired;
499 chinumfunc(struct inodesc *idesc)
501 register struct direct *dirp = idesc->id_dirp;
503 if (slotcount++ == desired) {
504 dirp->d_ino = idesc->id_parent;
505 return STOP|ALTERED|FOUND;
507 return KEEPON;
510 CMDFUNCSTART(chinum)
512 char *cp;
513 ino_t inum;
514 struct inodesc idesc;
516 slotcount = 0;
517 if (!checkactivedir())
518 return 1;
519 GETINUM(2,inum);
521 desired = strtol(argv[1], &cp, 0);
522 if (cp == argv[1] || *cp != '\0' || desired < 0) {
523 printf("invalid slot number `%s'\n", argv[1]);
524 return 1;
527 idesc.id_number = curinum;
528 idesc.id_func = chinumfunc;
529 idesc.id_fix = IGNORE;
530 idesc.id_type = DATA;
531 idesc.id_parent = inum; /* XXX convenient hiding place */
533 if (ckinode(curinode, &idesc) & FOUND)
534 return 0;
535 else {
536 warnx("no %sth slot in current directory", argv[1]);
537 return 1;
542 chnamefunc(struct inodesc *idesc)
544 register struct direct *dirp = idesc->id_dirp;
545 struct direct testdir;
547 if (slotcount++ == desired) {
548 /* will name fit? */
549 testdir.d_namlen = strlen(idesc->id_name);
550 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
551 dirp->d_namlen = testdir.d_namlen;
552 strcpy(dirp->d_name, idesc->id_name);
553 return STOP|ALTERED|FOUND;
554 } else
555 return STOP|FOUND; /* won't fit, so give up */
557 return KEEPON;
560 CMDFUNCSTART(chname)
562 int rval;
563 char *cp;
564 struct inodesc idesc;
566 slotcount = 0;
567 if (!checkactivedir())
568 return 1;
570 desired = strtoul(argv[1], &cp, 0);
571 if (cp == argv[1] || *cp != '\0') {
572 printf("invalid slot number `%s'\n", argv[1]);
573 return 1;
576 idesc.id_number = curinum;
577 idesc.id_func = chnamefunc;
578 idesc.id_fix = IGNORE;
579 idesc.id_type = DATA;
580 idesc.id_name = argv[2];
582 rval = ckinode(curinode, &idesc);
583 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
584 return 0;
585 else if (rval & FOUND) {
586 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
587 return 1;
588 } else {
589 warnx("no %sth slot in current directory", argv[1]);
590 return 1;
594 struct typemap {
595 const char *typename;
596 int typebits;
597 } typenamemap[] = {
598 {"file", IFREG},
599 {"dir", IFDIR},
600 {"socket", IFSOCK},
601 {"fifo", IFIFO},
604 CMDFUNCSTART(newtype)
606 int type;
607 struct typemap *tp;
609 if (!checkactive())
610 return 1;
611 type = curinode->di_mode & IFMT;
612 for (tp = typenamemap;
613 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)];
614 tp++) {
615 if (!strcmp(argv[1], tp->typename)) {
616 printf("setting type to %s\n", tp->typename);
617 type = tp->typebits;
618 break;
621 if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) {
622 warnx("type `%s' not known", argv[1]);
623 warnx("try one of `file', `dir', `socket', `fifo'");
624 return 1;
626 curinode->di_mode &= ~IFMT;
627 curinode->di_mode |= type;
628 inodirty();
629 printactive(0);
630 return 0;
633 CMDFUNCSTART(chlen)
635 int rval = 1;
636 long len;
637 char *cp;
639 if (!checkactive())
640 return 1;
642 len = strtol(argv[1], &cp, 0);
643 if (cp == argv[1] || *cp != '\0' || len < 0) {
644 warnx("bad length `%s'", argv[1]);
645 return 1;
648 curinode->di_size = len;
649 inodirty();
650 printactive(0);
651 return rval;
654 CMDFUNCSTART(chmode)
656 int rval = 1;
657 long modebits;
658 char *cp;
660 if (!checkactive())
661 return 1;
663 modebits = strtol(argv[1], &cp, 8);
664 if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
665 warnx("bad modebits `%s'", argv[1]);
666 return 1;
669 curinode->di_mode &= ~07777;
670 curinode->di_mode |= modebits;
671 inodirty();
672 printactive(0);
673 return rval;
676 CMDFUNCSTART(chaflags)
678 int rval = 1;
679 u_long flags;
680 char *cp;
682 if (!checkactive())
683 return 1;
685 flags = strtoul(argv[1], &cp, 0);
686 if (cp == argv[1] || *cp != '\0' ) {
687 warnx("bad flags `%s'", argv[1]);
688 return 1;
691 if (flags > UINT_MAX) {
692 warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
693 return(1);
695 curinode->di_flags = flags;
696 inodirty();
697 printactive(0);
698 return rval;
701 CMDFUNCSTART(chgen)
703 int rval = 1;
704 long gen;
705 char *cp;
707 if (!checkactive())
708 return 1;
710 gen = strtol(argv[1], &cp, 0);
711 if (cp == argv[1] || *cp != '\0' ) {
712 warnx("bad gen `%s'", argv[1]);
713 return 1;
716 if (gen > INT_MAX || gen < INT_MIN) {
717 warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
718 return(1);
720 curinode->di_gen = gen;
721 inodirty();
722 printactive(0);
723 return rval;
726 CMDFUNCSTART(linkcount)
728 int rval = 1;
729 int lcnt;
730 char *cp;
732 if (!checkactive())
733 return 1;
735 lcnt = strtol(argv[1], &cp, 0);
736 if (cp == argv[1] || *cp != '\0' ) {
737 warnx("bad link count `%s'", argv[1]);
738 return 1;
740 if (lcnt > USHRT_MAX || lcnt < 0) {
741 warnx("max link count is %d\n", USHRT_MAX);
742 return 1;
745 curinode->di_nlink = lcnt;
746 inodirty();
747 printactive(0);
748 return rval;
751 CMDFUNCSTART(chowner)
753 int rval = 1;
754 unsigned long uid;
755 char *cp;
756 struct passwd *pwd;
758 if (!checkactive())
759 return 1;
761 uid = strtoul(argv[1], &cp, 0);
762 if (cp == argv[1] || *cp != '\0' ) {
763 /* try looking up name */
764 if ((pwd = getpwnam(argv[1]))) {
765 uid = pwd->pw_uid;
766 } else {
767 warnx("bad uid `%s'", argv[1]);
768 return 1;
772 curinode->di_uid = uid;
773 inodirty();
774 printactive(0);
775 return rval;
778 CMDFUNCSTART(chgroup)
780 int rval = 1;
781 unsigned long gid;
782 char *cp;
783 struct group *grp;
785 if (!checkactive())
786 return 1;
788 gid = strtoul(argv[1], &cp, 0);
789 if (cp == argv[1] || *cp != '\0' ) {
790 if ((grp = getgrnam(argv[1]))) {
791 gid = grp->gr_gid;
792 } else {
793 warnx("bad gid `%s'", argv[1]);
794 return 1;
798 curinode->di_gid = gid;
799 inodirty();
800 printactive(0);
801 return rval;
805 dotime(char *name, struct timespec *rts)
807 char *p, *val;
808 struct tm t;
809 int32_t sec;
810 int32_t nsec;
811 p = strchr(name, '.');
812 if (p) {
813 *p = '\0';
814 nsec = strtoul(++p, &val, 0);
815 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
816 warnx("invalid nanoseconds");
817 goto badformat;
819 } else
820 nsec = 0;
821 if (strlen(name) != 14) {
822 badformat:
823 warnx("date format: YYYYMMDDHHMMSS[.nsec]");
824 return 1;
827 for (p = name; *p; p++)
828 if (*p < '0' || *p > '9')
829 goto badformat;
831 p = name;
832 #define VAL() ((*p++) - '0')
833 t.tm_year = VAL();
834 t.tm_year = VAL() + t.tm_year * 10;
835 t.tm_year = VAL() + t.tm_year * 10;
836 t.tm_year = VAL() + t.tm_year * 10 - 1900;
837 t.tm_mon = VAL();
838 t.tm_mon = VAL() + t.tm_mon * 10 - 1;
839 t.tm_mday = VAL();
840 t.tm_mday = VAL() + t.tm_mday * 10;
841 t.tm_hour = VAL();
842 t.tm_hour = VAL() + t.tm_hour * 10;
843 t.tm_min = VAL();
844 t.tm_min = VAL() + t.tm_min * 10;
845 t.tm_sec = VAL();
846 t.tm_sec = VAL() + t.tm_sec * 10;
847 t.tm_isdst = -1;
849 sec = mktime(&t);
850 if (sec == -1) {
851 warnx("date/time out of range");
852 return 1;
854 rts->tv_sec = sec;
855 rts->tv_nsec = nsec;
856 return 0;
859 CMDFUNCSTART(chmtime)
861 if (dotime(argv[1], &curinode->di_mtime))
862 return 1;
863 inodirty();
864 printactive(0);
865 return 0;
868 CMDFUNCSTART(chatime)
870 if (dotime(argv[1], &curinode->di_atime))
871 return 1;
872 inodirty();
873 printactive(0);
874 return 0;
877 CMDFUNCSTART(chctime)
879 if (dotime(argv[1], &curinode->di_ctime))
880 return 1;
881 inodirty();
882 printactive(0);
883 return 0;