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 "$Id: fsdb.c,v 1.10 1998/06/15 07:12:19 charnier Exp $"; 34 #endif /* not lint */ 35 36 #include <sys/types.h> 37 #include <sys/param.h> 38 #include <sys/time.h> 39 #include <ctype.h> 40 #include <err.h> 41 #include <grp.h> 42 #include <histedit.h> 43 #include <pwd.h> 44 #include <string.h> 45 46 #include <ufs/ufs/dinode.h> 47 #include <ufs/ufs/dir.h> 48 #include <ufs/ffs/fs.h> 49 50 #include "fsdb.h" 51 #include "fsck.h" 52 53 static void usage __P((void)); 54 int cmdloop __P((void)); 55 56 static void 57 usage() 58 { 59 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n"); 60 exit(1); 61 } 62 63 int returntosingle = 0; 64 char nflag = 0; 65 66 /* 67 * We suck in lots of fsck code, and just pick & choose the stuff we want. 68 * 69 * fsreadfd is set up to read from the file system, fswritefd to write to 70 * the file system. 71 */ 72 void 73 main(argc, argv) 74 int argc; 75 char *argv[]; 76 { 77 int ch, rval; 78 char *fsys = NULL; 79 80 while (-1 != (ch = getopt(argc, argv, "fdr"))) { 81 switch (ch) { 82 case 'f': 83 /* The -f option is left for historical 84 * reasons and has no meaning. 85 */ 86 break; 87 case 'd': 88 debug++; 89 break; 90 case 'r': 91 nflag++; /* "no" in fsck, readonly for us */ 92 break; 93 default: 94 usage(); 95 } 96 } 97 argc -= optind; 98 argv += optind; 99 if (argc != 1) 100 usage(); 101 else 102 fsys = argv[0]; 103 104 if (!setup(fsys)) 105 errx(1, "cannot set up file system `%s'", fsys); 106 printf("%s file system `%s'\nLast Mounted on %s\n", 107 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt); 108 rval = cmdloop(); 109 if (!nflag) { 110 sblock.fs_clean = 0; /* mark it dirty */ 111 sbdirty(); 112 ckfini(0); 113 printf("*** FILE SYSTEM MARKED DIRTY\n"); 114 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n"); 115 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n"); 116 } 117 exit(rval); 118 } 119 120 #define CMDFUNC(func) int func __P((int argc, char *argv[])) 121 #define CMDFUNCSTART(func) int func(argc, argv) \ 122 int argc; \ 123 char *argv[]; 124 125 CMDFUNC(helpfn); 126 CMDFUNC(focus); /* focus on inode */ 127 CMDFUNC(active); /* print active inode */ 128 CMDFUNC(focusname); /* focus by name */ 129 CMDFUNC(zapi); /* clear inode */ 130 CMDFUNC(uplink); /* incr link */ 131 CMDFUNC(downlink); /* decr link */ 132 CMDFUNC(linkcount); /* set link count */ 133 CMDFUNC(quit); /* quit */ 134 CMDFUNC(ls); /* list directory */ 135 CMDFUNC(rm); /* remove name */ 136 CMDFUNC(ln); /* add name */ 137 CMDFUNC(newtype); /* change type */ 138 CMDFUNC(chmode); /* change mode */ 139 CMDFUNC(chlen); /* change length */ 140 CMDFUNC(chaflags); /* change flags */ 141 CMDFUNC(chgen); /* change generation */ 142 CMDFUNC(chowner); /* change owner */ 143 CMDFUNC(chgroup); /* Change group */ 144 CMDFUNC(back); /* pop back to last ino */ 145 CMDFUNC(chmtime); /* Change mtime */ 146 CMDFUNC(chctime); /* Change ctime */ 147 CMDFUNC(chatime); /* Change atime */ 148 CMDFUNC(chinum); /* Change inode # of dirent */ 149 CMDFUNC(chname); /* Change dirname of dirent */ 150 151 struct cmdtable cmds[] = { 152 { "help", "Print out help", 1, 1, FL_RO, helpfn }, 153 { "?", "Print out help", 1, 1, FL_RO, helpfn }, 154 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus }, 155 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi }, 156 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname }, 157 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname }, 158 { "back", "Go to previous active inode", 1, 1, FL_RO, back }, 159 { "active", "Print active inode", 1, 1, FL_RO, active }, 160 { "print", "Print active inode", 1, 1, FL_RO, active }, 161 { "uplink", "Increment link count", 1, 1, FL_WR, uplink }, 162 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink }, 163 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount }, 164 { "ls", "List current inode as directory", 1, 1, FL_RO, ls }, 165 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm }, 166 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm }, 167 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln }, 168 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum }, 169 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname }, 170 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype }, 171 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode }, 172 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen }, 173 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner }, 174 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup }, 175 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags }, 176 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen }, 177 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime }, 178 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime }, 179 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime }, 180 { "quit", "Exit", 1, 1, FL_RO, quit }, 181 { "q", "Exit", 1, 1, FL_RO, quit }, 182 { "exit", "Exit", 1, 1, FL_RO, quit }, 183 { NULL, 0, 0, 0 }, 184 }; 185 186 int 187 helpfn(argc, argv) 188 int argc; 189 char *argv[]; 190 { 191 register struct cmdtable *cmdtp; 192 193 printf("Commands are:\n%-10s %5s %5s %s\n", 194 "command", "min argc", "max argc", "what"); 195 196 for (cmdtp = cmds; cmdtp->cmd; cmdtp++) 197 printf("%-10s %5u %5u %s\n", 198 cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt); 199 return 0; 200 } 201 202 char * 203 prompt(el) 204 EditLine *el; 205 { 206 static char pstring[64]; 207 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum); 208 return pstring; 209 } 210 211 212 int 213 cmdloop() 214 { 215 char *line; 216 const char *elline; 217 int cmd_argc, rval = 0, known; 218 #define scratch known 219 char **cmd_argv; 220 struct cmdtable *cmdp; 221 History *hist; 222 EditLine *elptr; 223 224 curinode = ginode(ROOTINO); 225 curinum = ROOTINO; 226 printactive(); 227 228 hist = history_init(); 229 history(hist, H_EVENT, 100); /* 100 elt history buffer */ 230 231 elptr = el_init("fsdb", stdin, stdout); 232 el_set(elptr, EL_EDITOR, "emacs"); 233 el_set(elptr, EL_PROMPT, prompt); 234 el_set(elptr, EL_HIST, history, hist); 235 el_source(elptr, NULL); 236 237 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) { 238 if (debug) 239 printf("command `%s'\n", elline); 240 241 history(hist, H_ENTER, elline); 242 243 line = strdup(elline); 244 cmd_argv = crack(line, &cmd_argc); 245 /* 246 * el_parse returns -1 to signal that it's not been handled 247 * internally. 248 */ 249 if (el_parse(elptr, cmd_argc, cmd_argv) != -1) 250 continue; 251 if (cmd_argc) { 252 known = 0; 253 for (cmdp = cmds; cmdp->cmd; cmdp++) { 254 if (!strcmp(cmdp->cmd, cmd_argv[0])) { 255 if ((cmdp->flags & FL_WR) == FL_WR && nflag) 256 warnx("`%s' requires write access", cmd_argv[0]), 257 rval = 1; 258 else if (cmd_argc >= cmdp->minargc && 259 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 return rval; 274 if (rval) 275 warnx("rval was %d", rval); 276 } 277 el_end(elptr); 278 history_end(hist); 279 return rval; 280 } 281 282 struct dinode *curinode; 283 ino_t curinum, ocurrent; 284 285 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \ 286 if (inum < ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \ 287 printf("inode %d out of range; range is [%d,%d]\n", \ 288 inum, ROOTINO, maxino); \ 289 return 1; \ 290 } 291 292 /* 293 * Focus on given inode number 294 */ 295 CMDFUNCSTART(focus) 296 { 297 ino_t inum; 298 char *cp; 299 300 GETINUM(1,inum); 301 curinode = ginode(inum); 302 ocurrent = curinum; 303 curinum = inum; 304 printactive(); 305 return 0; 306 } 307 308 CMDFUNCSTART(back) 309 { 310 curinum = ocurrent; 311 curinode = ginode(curinum); 312 printactive(); 313 return 0; 314 } 315 316 CMDFUNCSTART(zapi) 317 { 318 ino_t inum; 319 struct dinode *dp; 320 char *cp; 321 322 GETINUM(1,inum); 323 dp = ginode(inum); 324 clearinode(dp); 325 inodirty(); 326 if (curinode) /* re-set after potential change */ 327 curinode = ginode(curinum); 328 return 0; 329 } 330 331 CMDFUNCSTART(active) 332 { 333 printactive(); 334 return 0; 335 } 336 337 338 CMDFUNCSTART(quit) 339 { 340 return -1; 341 } 342 343 CMDFUNCSTART(uplink) 344 { 345 if (!checkactive()) 346 return 1; 347 printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink); 348 inodirty(); 349 return 0; 350 } 351 352 CMDFUNCSTART(downlink) 353 { 354 if (!checkactive()) 355 return 1; 356 printf("inode %d link count now %d\n", curinum, --curinode->di_nlink); 357 inodirty(); 358 return 0; 359 } 360 361 const char *typename[] = { 362 "unknown", 363 "fifo", 364 "char special", 365 "unregistered #3", 366 "directory", 367 "unregistered #5", 368 "blk special", 369 "unregistered #7", 370 "regular", 371 "unregistered #9", 372 "symlink", 373 "unregistered #11", 374 "socket", 375 "unregistered #13", 376 "whiteout", 377 }; 378 379 int slot; 380 381 int 382 scannames(idesc) 383 struct inodesc *idesc; 384 { 385 register struct direct *dirp = idesc->id_dirp; 386 387 printf("slot %d ino %d reclen %d: %s, `%.*s'\n", 388 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type], 389 dirp->d_namlen, dirp->d_name); 390 return (KEEPON); 391 } 392 393 CMDFUNCSTART(ls) 394 { 395 struct inodesc idesc; 396 checkactivedir(); /* let it go on anyway */ 397 398 slot = 0; 399 idesc.id_number = curinum; 400 idesc.id_func = scannames; 401 idesc.id_type = DATA; 402 idesc.id_fix = IGNORE; 403 ckinode(curinode, &idesc); 404 curinode = ginode(curinum); 405 406 return 0; 407 } 408 409 int findino __P((struct inodesc *idesc)); /* from fsck */ 410 static int dolookup __P((char *name)); 411 412 static int 413 dolookup(name) 414 char *name; 415 { 416 struct inodesc idesc; 417 418 if (!checkactivedir()) 419 return 0; 420 idesc.id_number = curinum; 421 idesc.id_func = findino; 422 idesc.id_name = name; 423 idesc.id_type = DATA; 424 idesc.id_fix = IGNORE; 425 if (ckinode(curinode, &idesc) & FOUND) { 426 curinum = idesc.id_parent; 427 curinode = ginode(curinum); 428 printactive(); 429 return 1; 430 } else { 431 warnx("name `%s' not found in current inode directory", name); 432 return 0; 433 } 434 } 435 436 CMDFUNCSTART(focusname) 437 { 438 char *p, *val; 439 440 if (!checkactive()) 441 return 1; 442 443 ocurrent = curinum; 444 445 if (argv[1][0] == '/') { 446 curinum = ROOTINO; 447 curinode = ginode(ROOTINO); 448 } else { 449 if (!checkactivedir()) 450 return 1; 451 } 452 for (p = argv[1]; p != NULL;) { 453 while ((val = strsep(&p, "/")) != NULL && *val == '\0'); 454 if (val) { 455 printf("component `%s': ", val); 456 fflush(stdout); 457 if (!dolookup(val)) { 458 curinode = ginode(curinum); 459 return(1); 460 } 461 } 462 } 463 return 0; 464 } 465 466 CMDFUNCSTART(ln) 467 { 468 ino_t inum; 469 int rval; 470 char *cp; 471 472 GETINUM(1,inum); 473 474 if (!checkactivedir()) 475 return 1; 476 rval = makeentry(curinum, inum, argv[2]); 477 if (rval) 478 printf("Ino %d entered as `%s'\n", inum, argv[2]); 479 else 480 printf("could not enter name? weird.\n"); 481 curinode = ginode(curinum); 482 return rval; 483 } 484 485 CMDFUNCSTART(rm) 486 { 487 int rval; 488 489 if (!checkactivedir()) 490 return 1; 491 rval = changeino(curinum, argv[1], 0); 492 if (rval & ALTERED) { 493 printf("Name `%s' removed\n", argv[1]); 494 return 0; 495 } else { 496 printf("could not remove name? weird.\n"); 497 return 1; 498 } 499 } 500 501 long slotcount, desired; 502 503 int 504 chinumfunc(idesc) 505 struct inodesc *idesc; 506 { 507 register struct direct *dirp = idesc->id_dirp; 508 509 if (slotcount++ == desired) { 510 dirp->d_ino = idesc->id_parent; 511 return STOP|ALTERED|FOUND; 512 } 513 return KEEPON; 514 } 515 516 CMDFUNCSTART(chinum) 517 { 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 struct inodesc idesc; 572 573 slotcount = 0; 574 if (!checkactivedir()) 575 return 1; 576 577 desired = strtoul(argv[1], &cp, 0); 578 if (cp == argv[1] || *cp != '\0') { 579 printf("invalid slot number `%s'\n", argv[1]); 580 return 1; 581 } 582 583 idesc.id_number = curinum; 584 idesc.id_func = chnamefunc; 585 idesc.id_fix = IGNORE; 586 idesc.id_type = DATA; 587 idesc.id_name = argv[2]; 588 589 rval = ckinode(curinode, &idesc); 590 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED)) 591 return 0; 592 else if (rval & FOUND) { 593 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]); 594 return 1; 595 } else { 596 warnx("no %sth slot in current directory", argv[1]); 597 return 1; 598 } 599 } 600 601 struct typemap { 602 const char *typename; 603 int typebits; 604 } typenamemap[] = { 605 {"file", IFREG}, 606 {"dir", IFDIR}, 607 {"socket", IFSOCK}, 608 {"fifo", IFIFO}, 609 }; 610 611 CMDFUNCSTART(newtype) 612 { 613 int type; 614 struct typemap *tp; 615 616 if (!checkactive()) 617 return 1; 618 type = curinode->di_mode & IFMT; 619 for (tp = typenamemap; 620 tp < &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]; 621 tp++) { 622 if (!strcmp(argv[1], tp->typename)) { 623 printf("setting type to %s\n", tp->typename); 624 type = tp->typebits; 625 break; 626 } 627 } 628 if (tp == &typenamemap[sizeof(typenamemap)/sizeof(*typenamemap)]) { 629 warnx("type `%s' not known", argv[1]); 630 warnx("try one of `file', `dir', `socket', `fifo'"); 631 return 1; 632 } 633 curinode->di_mode &= ~IFMT; 634 curinode->di_mode |= type; 635 inodirty(); 636 printactive(); 637 return 0; 638 } 639 640 CMDFUNCSTART(chlen) 641 { 642 int rval = 1; 643 long len; 644 char *cp; 645 646 if (!checkactive()) 647 return 1; 648 649 len = strtol(argv[1], &cp, 0); 650 if (cp == argv[1] || *cp != '\0' || len < 0) { 651 warnx("bad length `%s'", argv[1]); 652 return 1; 653 } 654 655 curinode->di_size = len; 656 inodirty(); 657 printactive(); 658 return rval; 659 } 660 661 CMDFUNCSTART(chmode) 662 { 663 int rval = 1; 664 long modebits; 665 char *cp; 666 667 if (!checkactive()) 668 return 1; 669 670 modebits = strtol(argv[1], &cp, 8); 671 if (cp == argv[1] || *cp != '\0' ) { 672 warnx("bad modebits `%s'", argv[1]); 673 return 1; 674 } 675 676 curinode->di_mode &= ~07777; 677 curinode->di_mode |= modebits; 678 inodirty(); 679 printactive(); 680 return rval; 681 } 682 683 CMDFUNCSTART(chaflags) 684 { 685 int rval = 1; 686 u_long flags; 687 char *cp; 688 689 if (!checkactive()) 690 return 1; 691 692 flags = strtoul(argv[1], &cp, 0); 693 if (cp == argv[1] || *cp != '\0' ) { 694 warnx("bad flags `%s'", argv[1]); 695 return 1; 696 } 697 698 if (flags > UINT_MAX) { 699 warnx("flags set beyond 32-bit range of field (%lx)\n", flags); 700 return(1); 701 } 702 curinode->di_flags = flags; 703 inodirty(); 704 printactive(); 705 return rval; 706 } 707 708 CMDFUNCSTART(chgen) 709 { 710 int rval = 1; 711 long gen; 712 char *cp; 713 714 if (!checkactive()) 715 return 1; 716 717 gen = strtol(argv[1], &cp, 0); 718 if (cp == argv[1] || *cp != '\0' ) { 719 warnx("bad gen `%s'", argv[1]); 720 return 1; 721 } 722 723 if (gen > INT_MAX || gen < INT_MIN) { 724 warnx("gen set beyond 32-bit range of field (%lx)\n", gen); 725 return(1); 726 } 727 curinode->di_gen = gen; 728 inodirty(); 729 printactive(); 730 return rval; 731 } 732 733 CMDFUNCSTART(linkcount) 734 { 735 int rval = 1; 736 int lcnt; 737 char *cp; 738 739 if (!checkactive()) 740 return 1; 741 742 lcnt = strtol(argv[1], &cp, 0); 743 if (cp == argv[1] || *cp != '\0' ) { 744 warnx("bad link count `%s'", argv[1]); 745 return 1; 746 } 747 if (lcnt > USHRT_MAX || lcnt < 0) { 748 warnx("max link count is %d\n", USHRT_MAX); 749 return 1; 750 } 751 752 curinode->di_nlink = lcnt; 753 inodirty(); 754 printactive(); 755 return rval; 756 } 757 758 CMDFUNCSTART(chowner) 759 { 760 int rval = 1; 761 unsigned long uid; 762 char *cp; 763 struct passwd *pwd; 764 765 if (!checkactive()) 766 return 1; 767 768 uid = strtoul(argv[1], &cp, 0); 769 if (cp == argv[1] || *cp != '\0' ) { 770 /* try looking up name */ 771 if ((pwd = getpwnam(argv[1]))) { 772 uid = pwd->pw_uid; 773 } else { 774 warnx("bad uid `%s'", argv[1]); 775 return 1; 776 } 777 } 778 779 curinode->di_uid = uid; 780 inodirty(); 781 printactive(); 782 return rval; 783 } 784 785 CMDFUNCSTART(chgroup) 786 { 787 int rval = 1; 788 unsigned long gid; 789 char *cp; 790 struct group *grp; 791 792 if (!checkactive()) 793 return 1; 794 795 gid = strtoul(argv[1], &cp, 0); 796 if (cp == argv[1] || *cp != '\0' ) { 797 if ((grp = getgrnam(argv[1]))) { 798 gid = grp->gr_gid; 799 } else { 800 warnx("bad gid `%s'", argv[1]); 801 return 1; 802 } 803 } 804 805 curinode->di_gid = gid; 806 inodirty(); 807 printactive(); 808 return rval; 809 } 810 811 int 812 dotime(name, rts) 813 char *name; 814 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(); 874 return 0; 875 } 876 877 CMDFUNCSTART(chatime) 878 { 879 if (dotime(argv[1], &curinode->di_ctime)) 880 return 1; 881 inodirty(); 882 printactive(); 883 return 0; 884 } 885 886 CMDFUNCSTART(chctime) 887 { 888 if (dotime(argv[1], &curinode->di_ctime)) 889 return 1; 890 inodirty(); 891 printactive(); 892 return 0; 893 } 894