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 char rcsid[] = "$Id$"; 33 #endif /* not lint */ 34 35 #include <sys/types.h> 36 #include <sys/stat.h> 37 #include <sys/param.h> 38 #include <sys/time.h> 39 #include <sys/mount.h> 40 #include <ctype.h> 41 #include <fcntl.h> 42 #include <grp.h> 43 #include <histedit.h> 44 #include <limits.h> 45 #include <pwd.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #include <ufs/ufs/dinode.h> 52 #include <ufs/ufs/dir.h> 53 #include <ufs/ffs/fs.h> 54 55 #include "fsdb.h" 56 #include "fsck.h" 57 58 extern char *__progname; /* from crt0.o */ 59 60 void usage __P((void)); 61 int cmdloop __P((void)); 62 63 void 64 usage() 65 { 66 errx(1, "usage: %s [-d] -f <fsname>", __progname); 67 } 68 69 int returntosingle = 0; 70 71 /* 72 * We suck in lots of fsck code, and just pick & choose the stuff we want. 73 * 74 * fsreadfd is set up to read from the file system, fswritefd to write to 75 * the file system. 76 */ 77 void 78 main(argc, argv) 79 int argc; 80 char *argv[]; 81 { 82 int ch, rval; 83 char *fsys = NULL; 84 struct stat stb; 85 86 while (-1 != (ch = getopt(argc, argv, "f:d"))) { 87 switch (ch) { 88 case 'f': 89 fsys = optarg; 90 break; 91 case 'd': 92 debug++; 93 break; 94 default: 95 usage(); 96 } 97 } 98 argc -= optind; 99 argv += optind; 100 if (fsys == NULL) { 101 if (argc != 1) 102 usage(); 103 else 104 fsys = argv[0]; 105 } 106 107 if (!setup(fsys)) 108 errx(1, "cannot set up file system `%s'", fsys); 109 printf("Editing file system `%s'\nLast Mounted on %s\n", fsys, 110 sblock.fs_fsmnt); 111 rval = cmdloop(); 112 sblock.fs_clean = 0; /* mark it dirty */ 113 sbdirty(); 114 ckfini(0); 115 printf("*** FILE SYSTEM MARKED DIRTY\n"); 116 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n"); 117 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n"); 118 exit(rval); 119 } 120 121 #define CMDFUNC(func) int func __P((int argc, char *argv[])) 122 #define CMDFUNCSTART(func) int func(argc, argv) \ 123 int argc; \ 124 char *argv[]; 125 126 CMDFUNC(helpfn); 127 CMDFUNC(focus); /* focus on inode */ 128 CMDFUNC(active); /* print active inode */ 129 CMDFUNC(focusname); /* focus by name */ 130 CMDFUNC(zapi); /* clear inode */ 131 CMDFUNC(uplink); /* incr link */ 132 CMDFUNC(downlink); /* decr link */ 133 CMDFUNC(linkcount); /* set link count */ 134 CMDFUNC(quit); /* quit */ 135 CMDFUNC(ls); /* list directory */ 136 CMDFUNC(rm); /* remove name */ 137 CMDFUNC(ln); /* add name */ 138 CMDFUNC(newtype); /* change type */ 139 CMDFUNC(chmode); /* change mode */ 140 CMDFUNC(chlen); /* change length */ 141 CMDFUNC(chaflags); /* change flags */ 142 CMDFUNC(chgen); /* change generation */ 143 CMDFUNC(chowner); /* change owner */ 144 CMDFUNC(chgroup); /* Change group */ 145 CMDFUNC(back); /* pop back to last ino */ 146 CMDFUNC(chmtime); /* Change mtime */ 147 CMDFUNC(chctime); /* Change ctime */ 148 CMDFUNC(chatime); /* Change atime */ 149 CMDFUNC(chinum); /* Change inode # of dirent */ 150 CMDFUNC(chname); /* Change dirname of dirent */ 151 152 struct cmdtable cmds[] = { 153 { "help", "Print out help", 1, 1, helpfn }, 154 { "?", "Print out help", 1, 1, helpfn }, 155 { "inode", "Set active inode to INUM", 2, 2, focus }, 156 { "clri", "Clear inode INUM", 2, 2, zapi }, 157 { "lookup", "Set active inode by looking up NAME", 2, 2, focusname }, 158 { "cd", "Set active inode by looking up NAME", 2, 2, focusname }, 159 { "back", "Go to previous active inode", 1, 1, back }, 160 { "active", "Print active inode", 1, 1, active }, 161 { "print", "Print active inode", 1, 1, active }, 162 { "uplink", "Increment link count", 1, 1, uplink }, 163 { "downlink", "Decrement link count", 1, 1, downlink }, 164 { "linkcount", "Set link count to COUNT", 2, 2, linkcount }, 165 { "ls", "List current inode as directory", 1, 1, ls }, 166 { "rm", "Remove NAME from current inode directory", 2, 2, rm }, 167 { "del", "Remove NAME from current inode directory", 2, 2, rm }, 168 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, ln }, 169 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, chinum }, 170 { "chname", "Change dir entry number INDEX to NAME", 3, 3, chname }, 171 { "chtype", "Change type of current inode to TYPE", 2, 2, newtype }, 172 { "chmod", "Change mode of current inode to MODE", 2, 2, chmode }, 173 { "chlen", "Change length of current inode to LENGTH", 2, 2, chlen }, 174 { "chown", "Change owner of current inode to OWNER", 2, 2, chowner }, 175 { "chgrp", "Change group of current inode to GROUP", 2, 2, chgroup }, 176 { "chflags", "Change flags of current inode to FLAGS", 2, 2, chaflags }, 177 { "chgen", "Change generation number of current inode to GEN", 2, 2, chgen }, 178 { "mtime", "Change mtime of current inode to MTIME", 2, 2, chmtime }, 179 { "ctime", "Change ctime of current inode to CTIME", 2, 2, chctime }, 180 { "atime", "Change atime of current inode to ATIME", 2, 2, chatime }, 181 { "quit", "Exit", 1, 1, quit }, 182 { "q", "Exit", 1, 1, quit }, 183 { "exit", "Exit", 1, 1, quit }, 184 { NULL, 0, 0, 0 }, 185 }; 186 187 int 188 helpfn(argc, argv) 189 int argc; 190 char *argv[]; 191 { 192 register struct cmdtable *cmdtp; 193 194 printf("Commands are:\n%-10s %5s %5s %s\n", 195 "command", "min argc", "max argc", "what"); 196 197 for (cmdtp = cmds; cmdtp->cmd; cmdtp++) 198 printf("%-10s %5u %5u %s\n", 199 cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt); 200 return 0; 201 } 202 203 char * 204 prompt(el) 205 EditLine *el; 206 { 207 static char pstring[64]; 208 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum); 209 return pstring; 210 } 211 212 213 int 214 cmdloop() 215 { 216 char *line; 217 const char *elline; 218 int cmd_argc, rval = 0, known; 219 #define scratch known 220 char **cmd_argv; 221 struct cmdtable *cmdp; 222 History *hist; 223 EditLine *elptr; 224 225 curinode = ginode(ROOTINO); 226 curinum = ROOTINO; 227 printactive(); 228 229 hist = history_init(); 230 history(hist, H_EVENT, 100); /* 100 elt history buffer */ 231 232 elptr = el_init(__progname, stdin, stdout); 233 el_set(elptr, EL_EDITOR, "emacs"); 234 el_set(elptr, EL_PROMPT, prompt); 235 el_set(elptr, EL_HIST, history, hist); 236 el_source(elptr, NULL); 237 238 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) { 239 if (debug) 240 printf("command `%s'\n", line); 241 242 history(hist, H_ENTER, elline); 243 244 line = strdup(elline); 245 cmd_argv = crack(line, &cmd_argc); 246 /* 247 * el_parse returns -1 to signal that it's not been handled 248 * internally. 249 */ 250 if (el_parse(elptr, cmd_argc, cmd_argv) != -1) 251 continue; 252 if (cmd_argc) { 253 known = 0; 254 for (cmdp = cmds; cmdp->cmd; cmdp++) { 255 if (!strcmp(cmdp->cmd, cmd_argv[0])) { 256 if (cmd_argc >= cmdp->minargc && 257 cmd_argc <= cmdp->maxargc) 258 rval = (*cmdp->handler)(cmd_argc, cmd_argv); 259 else 260 rval = argcount(cmdp, cmd_argc, cmd_argv); 261 known = 1; 262 break; 263 } 264 } 265 if (!known) 266 warnx("unknown command `%s'", cmd_argv[0]), rval = 1; 267 } else 268 rval = 0; 269 free(line); 270 if (rval < 0) 271 return rval; 272 if (rval) 273 warnx("rval was %d", rval); 274 } 275 el_end(elptr); 276 history_end(hist); 277 return rval; 278 } 279 280 struct dinode *curinode; 281 ino_t curinum, ocurrent; 282 283 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \ 284 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \ 285 printf("inode %d out of range; range is [%d,%d]\n", \ 286 inum, ROOTINO, maxino); \ 287 return 1; \ 288 } 289 290 /* 291 * Focus on given inode number 292 */ 293 CMDFUNCSTART(focus) 294 { 295 ino_t inum; 296 char *cp; 297 298 GETINUM(1,inum); 299 curinode = ginode(inum); 300 ocurrent = curinum; 301 curinum = inum; 302 printactive(); 303 return 0; 304 } 305 306 CMDFUNCSTART(back) 307 { 308 curinum = ocurrent; 309 curinode = ginode(curinum); 310 printactive(); 311 return 0; 312 } 313 314 CMDFUNCSTART(zapi) 315 { 316 ino_t inum; 317 struct dinode *dp; 318 char *cp; 319 320 GETINUM(1,inum); 321 dp = ginode(inum); 322 clearinode(dp); 323 inodirty(); 324 if (curinode) /* re-set after potential change */ 325 curinode = ginode(curinum); 326 return 0; 327 } 328 329 CMDFUNCSTART(active) 330 { 331 printactive(); 332 return 0; 333 } 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(idesc) 381 struct inodesc *idesc; 382 { 383 register struct direct *dirp = idesc->id_dirp; 384 385 printf("slot %d ino %d reclen %d: %s, `%.*s'\n", 386 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type], 387 dirp->d_namlen, dirp->d_name); 388 return (KEEPON); 389 } 390 391 CMDFUNCSTART(ls) 392 { 393 struct inodesc idesc; 394 checkactivedir(); /* let it go on anyway */ 395 396 slot = 0; 397 idesc.id_number = curinum; 398 idesc.id_func = scannames; 399 idesc.id_type = DATA; 400 idesc.id_fix = IGNORE; 401 ckinode(curinode, &idesc); 402 curinode = ginode(curinum); 403 404 return 0; 405 } 406 407 int findino __P((struct inodesc *idesc)); /* from fsck */ 408 static int dolookup __P((char *name)); 409 410 static int 411 dolookup(name) 412 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(); 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 struct dinode *dp; 468 int rval; 469 char *cp; 470 471 GETINUM(1,inum); 472 473 if (!checkactivedir()) 474 return 1; 475 rval = makeentry(curinum, inum, argv[2]); 476 if (rval) 477 printf("Ino %d entered as `%s'\n", inum, argv[2]); 478 else 479 printf("could not enter name? weird.\n"); 480 curinode = ginode(curinum); 481 return rval; 482 } 483 484 CMDFUNCSTART(rm) 485 { 486 int rval; 487 488 if (!checkactivedir()) 489 return 1; 490 rval = changeino(curinum, argv[1], 0); 491 if (rval & ALTERED) { 492 printf("Name `%s' removed\n", argv[1]); 493 return 0; 494 } else { 495 printf("could not remove name? weird.\n"); 496 return 1; 497 } 498 } 499 500 long slotcount, desired; 501 502 int 503 chinumfunc(idesc) 504 struct inodesc *idesc; 505 { 506 register struct direct *dirp = idesc->id_dirp; 507 508 if (slotcount++ == desired) { 509 dirp->d_ino = idesc->id_parent; 510 return STOP|ALTERED|FOUND; 511 } 512 return KEEPON; 513 } 514 515 CMDFUNCSTART(chinum) 516 { 517 int rval; 518 char *cp; 519 ino_t inum; 520 struct inodesc idesc; 521 522 slotcount = 0; 523 if (!checkactivedir()) 524 return 1; 525 GETINUM(2,inum); 526 527 desired = strtol(argv[1], &cp, 0); 528 if (cp == argv[1] || *cp != '\0' || desired < 0) { 529 printf("invalid slot number `%s'\n", argv[1]); 530 return 1; 531 } 532 533 idesc.id_number = curinum; 534 idesc.id_func = chinumfunc; 535 idesc.id_fix = IGNORE; 536 idesc.id_type = DATA; 537 idesc.id_parent = inum; /* XXX convenient hiding place */ 538 539 if (ckinode(curinode, &idesc) & FOUND) 540 return 0; 541 else { 542 warnx("no %sth slot in current directory", argv[1]); 543 return 1; 544 } 545 } 546 547 int 548 chnamefunc(idesc) 549 struct inodesc *idesc; 550 { 551 register struct direct *dirp = idesc->id_dirp; 552 struct direct testdir; 553 554 if (slotcount++ == desired) { 555 /* will name fit? */ 556 testdir.d_namlen = strlen(idesc->id_name); 557 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) { 558 dirp->d_namlen = testdir.d_namlen; 559 strcpy(dirp->d_name, idesc->id_name); 560 return STOP|ALTERED|FOUND; 561 } else 562 return STOP|FOUND; /* won't fit, so give up */ 563 } 564 return KEEPON; 565 } 566 567 CMDFUNCSTART(chname) 568 { 569 int rval; 570 char *cp; 571 ino_t inum; 572 struct inodesc idesc; 573 574 slotcount = 0; 575 if (!checkactivedir()) 576 return 1; 577 578 desired = strtoul(argv[1], &cp, 0); 579 if (cp == argv[1] || *cp != '\0') { 580 printf("invalid slot number `%s'\n", argv[1]); 581 return 1; 582 } 583 584 idesc.id_number = curinum; 585 idesc.id_func = chnamefunc; 586 idesc.id_fix = IGNORE; 587 idesc.id_type = DATA; 588 idesc.id_name = argv[2]; 589 590 rval = ckinode(curinode, &idesc); 591 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED)) 592 return 0; 593 else if (rval & FOUND) { 594 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]); 595 return 1; 596 } else { 597 warnx("no %sth slot in current directory", argv[1]); 598 return 1; 599 } 600 } 601 602 struct typemap { 603 const char *typename; 604 int typebits; 605 } typenamemap[] = { 606 {"file", IFREG}, 607 {"dir", IFDIR}, 608 {"socket", IFSOCK}, 609 {"fifo", IFIFO}, 610 }; 611 612 CMDFUNCSTART(newtype) 613 { 614 int rval = 1; 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(typemap)/sizeof(*typemap)]; 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(typemap)/sizeof(*typemap)]) { 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(); 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(); 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' ) { 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(); 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(); 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(); 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(); 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(); 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(); 810 return rval; 811 } 812 813 int 814 dotime(name, rts) 815 char *name; 816 struct timespec *rts; 817 { 818 char *p, *val; 819 struct tm t; 820 int32_t sec; 821 int32_t nsec; 822 p = strchr(name, '.'); 823 if (p) { 824 *p = '\0'; 825 nsec = strtoul(++p, &val, 0); 826 if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) { 827 warnx("invalid nanoseconds"); 828 goto badformat; 829 } 830 } else 831 nsec = 0; 832 if (strlen(name) != 14) { 833 badformat: 834 warnx("date format: YYYYMMDDHHMMSS[.nsec]"); 835 return 1; 836 } 837 838 for (p = name; *p; p++) 839 if (*p < '0' || *p > '9') 840 goto badformat; 841 842 p = name; 843 #define VAL() ((*p++) - '0') 844 t.tm_year = VAL(); 845 t.tm_year = VAL() + t.tm_year * 10; 846 t.tm_year = VAL() + t.tm_year * 10; 847 t.tm_year = VAL() + t.tm_year * 10 - 1900; 848 t.tm_mon = VAL(); 849 t.tm_mon = VAL() + t.tm_mon * 10 - 1; 850 t.tm_mday = VAL(); 851 t.tm_mday = VAL() + t.tm_mday * 10; 852 t.tm_hour = VAL(); 853 t.tm_hour = VAL() + t.tm_hour * 10; 854 t.tm_min = VAL(); 855 t.tm_min = VAL() + t.tm_min * 10; 856 t.tm_sec = VAL(); 857 t.tm_sec = VAL() + t.tm_sec * 10; 858 t.tm_isdst = -1; 859 860 sec = mktime(&t); 861 if (sec == -1) { 862 warnx("date/time out of range"); 863 return 1; 864 } 865 rts->tv_sec = sec; 866 rts->tv_nsec = nsec; 867 return 0; 868 } 869 870 CMDFUNCSTART(chmtime) 871 { 872 if (dotime(argv[1], &curinode->di_ctime)) 873 return 1; 874 inodirty(); 875 printactive(); 876 return 0; 877 } 878 879 CMDFUNCSTART(chatime) 880 { 881 if (dotime(argv[1], &curinode->di_ctime)) 882 return 1; 883 inodirty(); 884 printactive(); 885 return 0; 886 } 887 888 CMDFUNCSTART(chctime) 889 { 890 if (dotime(argv[1], &curinode->di_ctime)) 891 return 1; 892 inodirty(); 893 printactive(); 894 return 0; 895 } 896