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 */ 32 33 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <ctype.h> 36 #include <err.h> 37 #include <grp.h> 38 #include <histedit.h> 39 #include <pwd.h> 40 #include <string.h> 41 42 #include <vfs/ufs/dinode.h> 43 #include <vfs/ufs/dir.h> 44 #include <vfs/ufs/fs.h> 45 46 #include "fsdb.h" 47 #include "fsck.h" 48 49 static void usage(void); 50 int cmdloop(void); 51 52 static void 53 usage(void) 54 { 55 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n"); 56 exit(1); 57 } 58 59 int returntosingle = 0; 60 61 /* 62 * We suck in lots of fsck code, and just pick & choose the stuff we want. 63 * 64 * fsreadfd is set up to read from the file system, fswritefd to write to 65 * the file system. 66 */ 67 int 68 main(int argc, char **argv) 69 { 70 int ch, rval; 71 char *fsys = NULL; 72 73 while (-1 != (ch = getopt(argc, argv, "fdr"))) { 74 switch (ch) { 75 case 'f': 76 /* The -f option is left for historical 77 * reasons and has no meaning. 78 */ 79 break; 80 case 'd': 81 debug++; 82 break; 83 case 'r': 84 nflag++; /* "no" in fsck, readonly for us */ 85 break; 86 default: 87 usage(); 88 } 89 } 90 argc -= optind; 91 argv += optind; 92 if (argc != 1) 93 usage(); 94 else 95 fsys = argv[0]; 96 97 if (!setup(fsys)) 98 errx(1, "cannot set up file system `%s'", fsys); 99 printf("%s file system `%s'\nLast Mounted on %s\n", 100 nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt); 101 rval = cmdloop(); 102 if (!nflag) { 103 sblock.fs_clean = 0; /* mark it dirty */ 104 sbdirty(); 105 ckfini(0); 106 printf("*** FILE SYSTEM MARKED DIRTY\n"); 107 printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n"); 108 printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n"); 109 } 110 exit(rval); 111 } 112 113 #define CMDFUNC(func) int func (int argc, char **argv) 114 #define CMDFUNCSTART(func) int func(int argc, char **argv) 115 116 CMDFUNC(helpfn); 117 CMDFUNC(focus); /* focus on inode */ 118 CMDFUNC(active); /* print active inode */ 119 CMDFUNC(blocks); /* print blocks for active inode */ 120 CMDFUNC(focusname); /* focus by name */ 121 CMDFUNC(zapi); /* clear inode */ 122 CMDFUNC(uplink); /* incr link */ 123 CMDFUNC(downlink); /* decr link */ 124 CMDFUNC(linkcount); /* set link count */ 125 CMDFUNC(quit); /* quit */ 126 CMDFUNC(ls); /* list directory */ 127 CMDFUNC(rm); /* remove name */ 128 CMDFUNC(ln); /* add name */ 129 CMDFUNC(newtype); /* change type */ 130 CMDFUNC(chmode); /* change mode */ 131 CMDFUNC(chlen); /* change length */ 132 CMDFUNC(chaflags); /* change flags */ 133 CMDFUNC(chgen); /* change generation */ 134 CMDFUNC(chowner); /* change owner */ 135 CMDFUNC(chgroup); /* Change group */ 136 CMDFUNC(back); /* pop back to last ino */ 137 CMDFUNC(chmtime); /* Change mtime */ 138 CMDFUNC(chctime); /* Change ctime */ 139 CMDFUNC(chatime); /* Change atime */ 140 CMDFUNC(chinum); /* Change inode # of dirent */ 141 CMDFUNC(chname); /* Change dirname of dirent */ 142 143 struct cmdtable cmds[] = { 144 { "help", "Print out help", 1, 1, FL_RO, helpfn }, 145 { "?", "Print out help", 1, 1, FL_RO, helpfn }, 146 { "inode", "Set active inode to INUM", 2, 2, FL_RO, focus }, 147 { "clri", "Clear inode INUM", 2, 2, FL_WR, zapi }, 148 { "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname }, 149 { "cd", "Set active inode by looking up NAME", 2, 2, FL_RO, focusname }, 150 { "back", "Go to previous active inode", 1, 1, FL_RO, back }, 151 { "active", "Print active inode", 1, 1, FL_RO, active }, 152 { "print", "Print active inode", 1, 1, FL_RO, active }, 153 { "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks }, 154 { "uplink", "Increment link count", 1, 1, FL_WR, uplink }, 155 { "downlink", "Decrement link count", 1, 1, FL_WR, downlink }, 156 { "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount }, 157 { "ls", "List current inode as directory", 1, 1, FL_RO, ls }, 158 { "rm", "Remove NAME from current inode directory", 2, 2, FL_WR, rm }, 159 { "del", "Remove NAME from current inode directory", 2, 2, FL_WR, rm }, 160 { "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR, ln }, 161 { "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum }, 162 { "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR, chname }, 163 { "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype }, 164 { "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode }, 165 { "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen }, 166 { "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner }, 167 { "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup }, 168 { "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags }, 169 { "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen }, 170 { "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime }, 171 { "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime }, 172 { "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime }, 173 { "quit", "Exit", 1, 1, FL_RO, quit }, 174 { "q", "Exit", 1, 1, FL_RO, quit }, 175 { "exit", "Exit", 1, 1, FL_RO, quit }, 176 { NULL, 0, 0, 0 }, 177 }; 178 179 int 180 helpfn(int argc, char **argv) 181 { 182 struct cmdtable *cmdtp; 183 184 printf("Commands are:\n%-10s %5s %5s %s\n", 185 "command", "min argc", "max argc", "what"); 186 187 for (cmdtp = cmds; cmdtp->cmd; cmdtp++) 188 printf("%-10s %5u %5u %s\n", 189 cmdtp->cmd, cmdtp->minargc, cmdtp->maxargc, cmdtp->helptxt); 190 return 0; 191 } 192 193 char * 194 prompt(EditLine *el) 195 { 196 static char pstring[64]; 197 snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ", 198 (uintmax_t)curinum); 199 return pstring; 200 } 201 202 203 int 204 cmdloop(void) 205 { 206 char *line; 207 const char *elline; 208 int cmd_argc, rval = 0, known; 209 #define scratch known 210 char **cmd_argv; 211 struct cmdtable *cmdp; 212 History *hist; 213 HistEvent he; 214 EditLine *elptr; 215 216 curinode = ginode(UFS_ROOTINO); 217 curinum = UFS_ROOTINO; 218 printactive(0); 219 220 hist = history_init(); 221 history(hist, &he, H_SETSIZE, 100); /* 100 elt history buffer */ 222 223 elptr = el_init("fsdb", stdin, stdout, stderr); 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); 228 229 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) { 230 if (debug) 231 printf("command `%s'\n", elline); 232 233 history(hist, &he, H_ENTER, elline); 234 235 line = strdup(elline); 236 cmd_argv = crack(line, &cmd_argc); 237 /* 238 * el_parse returns -1 to signal that it's not been handled 239 * internally. 240 */ 241 if (el_parse(elptr, cmd_argc, (const char **)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; 257 } 258 } 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); 268 } 269 el_end(elptr); 270 history_end(hist); 271 return rval; 272 } 273 274 struct ufs1_dinode *curinode; 275 ino_t curinum, ocurrent; 276 277 #define GETINUM(ac,inum) inum = strtoul(argv[ac], &cp, 0); \ 278 if (inum < UFS_ROOTINO || inum > maxino || \ 279 cp == argv[ac] || *cp != '\0' ) { \ 280 printf("inode %ju out of range; range is [%ju,%ju]\n", \ 281 (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)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 %ju link count now %d\n", (uintmax_t)curinum, 346 ++curinode->di_nlink); 347 inodirty(); 348 return 0; 349 } 350 351 CMDFUNCSTART(downlink) 352 { 353 if (!checkactive()) 354 return 1; 355 printf("inode %ju link count now %d\n", (uintmax_t)curinum, 356 --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(struct inodesc *idesc) 383 { 384 struct direct *dirp = idesc->id_dirp; 385 386 printf("slot %d ino %d reclen %d: %s, `%.*s'\n", 387 slot++, dirp->d_ino, dirp->d_reclen, typename[dirp->d_type], 388 dirp->d_namlen, dirp->d_name); 389 return (KEEPON); 390 } 391 392 CMDFUNCSTART(ls) 393 { 394 struct inodesc idesc; 395 checkactivedir(); /* let it go on anyway */ 396 397 slot = 0; 398 idesc.id_number = curinum; 399 idesc.id_func = scannames; 400 idesc.id_type = DATA; 401 idesc.id_fix = IGNORE; 402 ckinode(curinode, &idesc); 403 curinode = ginode(curinum); 404 405 return 0; 406 } 407 408 int findino(struct inodesc *idesc); /* from fsck */ 409 static int dolookup(char *name); 410 411 static int 412 dolookup(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(0); 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 = UFS_ROOTINO; 445 curinode = ginode(UFS_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 int rval; 468 char *cp; 469 470 GETINUM(1,inum); 471 472 if (!checkactivedir()) 473 return 1; 474 rval = makeentry(curinum, inum, argv[2]); 475 if (rval) 476 printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]); 477 else 478 printf("could not enter name? weird.\n"); 479 curinode = ginode(curinum); 480 return rval; 481 } 482 483 CMDFUNCSTART(rm) 484 { 485 int rval; 486 487 if (!checkactivedir()) 488 return 1; 489 rval = changeino(curinum, argv[1], 0); 490 if (rval & ALTERED) { 491 printf("Name `%s' removed\n", argv[1]); 492 return 0; 493 } else { 494 printf("could not remove name? weird.\n"); 495 return 1; 496 } 497 } 498 499 long slotcount, desired; 500 501 int 502 chinumfunc(struct inodesc *idesc) 503 { 504 struct direct *dirp = idesc->id_dirp; 505 506 if (slotcount++ == desired) { 507 dirp->d_ino = idesc->id_parent; 508 return STOP|ALTERED|FOUND; 509 } 510 return KEEPON; 511 } 512 513 CMDFUNCSTART(chinum) 514 { 515 char *cp; 516 ino_t inum; 517 struct inodesc idesc; 518 519 slotcount = 0; 520 if (!checkactivedir()) 521 return 1; 522 GETINUM(2,inum); 523 524 desired = strtol(argv[1], &cp, 0); 525 if (cp == argv[1] || *cp != '\0' || desired < 0) { 526 printf("invalid slot number `%s'\n", argv[1]); 527 return 1; 528 } 529 530 idesc.id_number = curinum; 531 idesc.id_func = chinumfunc; 532 idesc.id_fix = IGNORE; 533 idesc.id_type = DATA; 534 idesc.id_parent = inum; /* XXX convenient hiding place */ 535 536 if (ckinode(curinode, &idesc) & FOUND) 537 return 0; 538 else { 539 warnx("no %sth slot in current directory", argv[1]); 540 return 1; 541 } 542 } 543 544 int 545 chnamefunc(struct inodesc *idesc) 546 { 547 struct direct *dirp = idesc->id_dirp; 548 struct direct testdir; 549 550 if (slotcount++ == desired) { 551 /* will name fit? */ 552 testdir.d_namlen = strlen(idesc->id_name); 553 if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) { 554 dirp->d_namlen = testdir.d_namlen; 555 strcpy(dirp->d_name, idesc->id_name); 556 return STOP|ALTERED|FOUND; 557 } else 558 return STOP|FOUND; /* won't fit, so give up */ 559 } 560 return KEEPON; 561 } 562 563 CMDFUNCSTART(chname) 564 { 565 int rval; 566 char *cp; 567 struct inodesc idesc; 568 569 slotcount = 0; 570 if (!checkactivedir()) 571 return 1; 572 573 desired = strtoul(argv[1], &cp, 0); 574 if (cp == argv[1] || *cp != '\0') { 575 printf("invalid slot number `%s'\n", argv[1]); 576 return 1; 577 } 578 579 idesc.id_number = curinum; 580 idesc.id_func = chnamefunc; 581 idesc.id_fix = IGNORE; 582 idesc.id_type = DATA; 583 idesc.id_name = argv[2]; 584 585 rval = ckinode(curinode, &idesc); 586 if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED)) 587 return 0; 588 else if (rval & FOUND) { 589 warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]); 590 return 1; 591 } else { 592 warnx("no %sth slot in current directory", argv[1]); 593 return 1; 594 } 595 } 596 597 struct typemap { 598 const char *typename; 599 int typebits; 600 } typenamemap[] = { 601 {"file", IFREG}, 602 {"dir", IFDIR}, 603 {"socket", IFSOCK}, 604 {"fifo", IFIFO}, 605 }; 606 607 CMDFUNCSTART(newtype) 608 { 609 int type; 610 struct typemap *tp; 611 612 if (!checkactive()) 613 return 1; 614 type = curinode->di_mode & IFMT; 615 for (tp = typenamemap; tp < &typenamemap[NELEM(typenamemap)]; 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[NELEM(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, int32_t *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[0] = sec; 856 rts[1] = 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