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 * $DragonFly: src/sbin/fsdb/fsdb.c,v 1.6 2003/11/01 17:15:59 drhodus Exp $ 32 */ 33 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> 42 43 #include <vfs/ufs/dinode.h> 44 #include <vfs/ufs/dir.h> 45 #include <vfs/ufs/fs.h> 46 47 #include "fsdb.h" 48 #include "fsck.h" 49 50 static void usage(void); 51 int cmdloop(void); 52 53 static void 54 usage(void) 55 { 56 fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n"); 57 exit(1); 58 } 59 60 int returntosingle = 0; 61 char nflag = 0; 62 63 /* 64 * We suck in lots of fsck code, and just pick & choose the stuff we want. 65 * 66 * fsreadfd is set up to read from the file system, fswritefd to write to 67 * the file system. 68 */ 69 int 70 main(int argc, char **argv) 71 { 72 int ch, rval; 73 char *fsys = NULL; 74 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. 80 */ 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(); 90 } 91 } 92 argc -= optind; 93 argv += optind; 94 if (argc != 1) 95 usage(); 96 else 97 fsys = argv[0]; 98 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"); 111 } 112 exit(rval); 113 } 114 115 #define CMDFUNC(func) int func (int argc, char **argv) 116 #define CMDFUNCSTART(func) int func(int argc, char **argv) 117 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 */ 144 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 }, 179 }; 180 181 int 182 helpfn(int argc, char **argv) 183 { 184 register struct cmdtable *cmdtp; 185 186 printf("Commands are:\n%-10s %5s %5s %s\n", 187 "command", "min argc", "max argc", "what"); 188 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; 193 } 194 195 char * 196 prompt(EditLine *el) 197 { 198 static char pstring[64]; 199 snprintf(pstring, sizeof(pstring), "fsdb (inum: %d)> ", curinum); 200 return pstring; 201 } 202 203 204 int 205 cmdloop(void) 206 { 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; 215 216 curinode = ginode(ROOTINO); 217 curinum = ROOTINO; 218 printactive(0); 219 220 hist = history_init(); 221 history(hist, H_EVENT, 100); /* 100 elt history buffer */ 222 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); 228 229 while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) { 230 if (debug) 231 printf("command `%s'\n", elline); 232 233 history(hist, 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, 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 dinode *curinode; 275 ino_t curinum, ocurrent; 276 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; \ 282 } 283 284 /* 285 * Focus on given inode number 286 */ 287 CMDFUNCSTART(focus) 288 { 289 ino_t inum; 290 char *cp; 291 292 GETINUM(1,inum); 293 curinode = ginode(inum); 294 ocurrent = curinum; 295 curinum = inum; 296 printactive(0); 297 return 0; 298 } 299 300 CMDFUNCSTART(back) 301 { 302 curinum = ocurrent; 303 curinode = ginode(curinum); 304 printactive(0); 305 return 0; 306 } 307 308 CMDFUNCSTART(zapi) 309 { 310 ino_t inum; 311 struct dinode *dp; 312 char *cp; 313 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; 321 } 322 323 CMDFUNCSTART(active) 324 { 325 printactive(0); 326 return 0; 327 } 328 329 CMDFUNCSTART(blocks) 330 { 331 printactive(1); 332 return 0; 333 } 334 335 CMDFUNCSTART(quit) 336 { 337 return -1; 338 } 339 340 CMDFUNCSTART(uplink) 341 { 342 if (!checkactive()) 343 return 1; 344 printf("inode %d link count now %d\n", curinum, ++curinode->di_nlink); 345 inodirty(); 346 return 0; 347 } 348 349 CMDFUNCSTART(downlink) 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 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", 374 }; 375 376 int slot; 377 378 int 379 scannames(struct inodesc *idesc) 380 { 381 register struct direct *dirp = idesc->id_dirp; 382 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); 387 } 388 389 CMDFUNCSTART(ls) 390 { 391 struct inodesc idesc; 392 checkactivedir(); /* let it go on anyway */ 393 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); 401 402 return 0; 403 } 404 405 int findino(struct inodesc *idesc); /* from fsck */ 406 static int dolookup(char *name); 407 408 static int 409 dolookup(char *name) 410 { 411 struct inodesc idesc; 412 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; 428 } 429 } 430 431 CMDFUNCSTART(focusname) 432 { 433 char *p, *val; 434 435 if (!checkactive()) 436 return 1; 437 438 ocurrent = curinum; 439 440 if (argv[1][0] == '/') { 441 curinum = ROOTINO; 442 curinode = ginode(ROOTINO); 443 } else { 444 if (!checkactivedir()) 445 return 1; 446 } 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); 455 } 456 } 457 } 458 return 0; 459 } 460 461 CMDFUNCSTART(ln) 462 { 463 ino_t inum; 464 int rval; 465 char *cp; 466 467 GETINUM(1,inum); 468 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; 478 } 479 480 CMDFUNCSTART(rm) 481 { 482 int rval; 483 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; 493 } 494 } 495 496 long slotcount, desired; 497 498 int 499 chinumfunc(struct inodesc *idesc) 500 { 501 register struct direct *dirp = idesc->id_dirp; 502 503 if (slotcount++ == desired) { 504 dirp->d_ino = idesc->id_parent; 505 return STOP|ALTERED|FOUND; 506 } 507 return KEEPON; 508 } 509 510 CMDFUNCSTART(chinum) 511 { 512 char *cp; 513 ino_t inum; 514 struct inodesc idesc; 515 516 slotcount = 0; 517 if (!checkactivedir()) 518 return 1; 519 GETINUM(2,inum); 520 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; 525 } 526 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 */ 532 533 if (ckinode(curinode, &idesc) & FOUND) 534 return 0; 535 else { 536 warnx("no %sth slot in current directory", argv[1]); 537 return 1; 538 } 539 } 540 541 int 542 chnamefunc(struct inodesc *idesc) 543 { 544 register struct direct *dirp = idesc->id_dirp; 545 struct direct testdir; 546 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 */ 556 } 557 return KEEPON; 558 } 559 560 CMDFUNCSTART(chname) 561 { 562 int rval; 563 char *cp; 564 struct inodesc idesc; 565 566 slotcount = 0; 567 if (!checkactivedir()) 568 return 1; 569 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; 574 } 575 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]; 581 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; 591 } 592 } 593 594 struct typemap { 595 const char *typename; 596 int typebits; 597 } typenamemap[] = { 598 {"file", IFREG}, 599 {"dir", IFDIR}, 600 {"socket", IFSOCK}, 601 {"fifo", IFIFO}, 602 }; 603 604 CMDFUNCSTART(newtype) 605 { 606 int type; 607 struct typemap *tp; 608 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; 619 } 620 } 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; 625 } 626 curinode->di_mode &= ~IFMT; 627 curinode->di_mode |= type; 628 inodirty(); 629 printactive(0); 630 return 0; 631 } 632 633 CMDFUNCSTART(chlen) 634 { 635 int rval = 1; 636 long len; 637 char *cp; 638 639 if (!checkactive()) 640 return 1; 641 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; 646 } 647 648 curinode->di_size = len; 649 inodirty(); 650 printactive(0); 651 return rval; 652 } 653 654 CMDFUNCSTART(chmode) 655 { 656 int rval = 1; 657 long modebits; 658 char *cp; 659 660 if (!checkactive()) 661 return 1; 662 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; 667 } 668 669 curinode->di_mode &= ~07777; 670 curinode->di_mode |= modebits; 671 inodirty(); 672 printactive(0); 673 return rval; 674 } 675 676 CMDFUNCSTART(chaflags) 677 { 678 int rval = 1; 679 u_long flags; 680 char *cp; 681 682 if (!checkactive()) 683 return 1; 684 685 flags = strtoul(argv[1], &cp, 0); 686 if (cp == argv[1] || *cp != '\0' ) { 687 warnx("bad flags `%s'", argv[1]); 688 return 1; 689 } 690 691 if (flags > UINT_MAX) { 692 warnx("flags set beyond 32-bit range of field (%lx)\n", flags); 693 return(1); 694 } 695 curinode->di_flags = flags; 696 inodirty(); 697 printactive(0); 698 return rval; 699 } 700 701 CMDFUNCSTART(chgen) 702 { 703 int rval = 1; 704 long gen; 705 char *cp; 706 707 if (!checkactive()) 708 return 1; 709 710 gen = strtol(argv[1], &cp, 0); 711 if (cp == argv[1] || *cp != '\0' ) { 712 warnx("bad gen `%s'", argv[1]); 713 return 1; 714 } 715 716 if (gen > INT_MAX || gen < INT_MIN) { 717 warnx("gen set beyond 32-bit range of field (%lx)\n", gen); 718 return(1); 719 } 720 curinode->di_gen = gen; 721 inodirty(); 722 printactive(0); 723 return rval; 724 } 725 726 CMDFUNCSTART(linkcount) 727 { 728 int rval = 1; 729 int lcnt; 730 char *cp; 731 732 if (!checkactive()) 733 return 1; 734 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; 739 } 740 if (lcnt > USHRT_MAX || lcnt < 0) { 741 warnx("max link count is %d\n", USHRT_MAX); 742 return 1; 743 } 744 745 curinode->di_nlink = lcnt; 746 inodirty(); 747 printactive(0); 748 return rval; 749 } 750 751 CMDFUNCSTART(chowner) 752 { 753 int rval = 1; 754 unsigned long uid; 755 char *cp; 756 struct passwd *pwd; 757 758 if (!checkactive()) 759 return 1; 760 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; 769 } 770 } 771 772 curinode->di_uid = uid; 773 inodirty(); 774 printactive(0); 775 return rval; 776 } 777 778 CMDFUNCSTART(chgroup) 779 { 780 int rval = 1; 781 unsigned long gid; 782 char *cp; 783 struct group *grp; 784 785 if (!checkactive()) 786 return 1; 787 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; 795 } 796 } 797 798 curinode->di_gid = gid; 799 inodirty(); 800 printactive(0); 801 return rval; 802 } 803 804 int 805 dotime(char *name, struct timespec *rts) 806 { 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; 818 } 819 } else 820 nsec = 0; 821 if (strlen(name) != 14) { 822 badformat: 823 warnx("date format: YYYYMMDDHHMMSS[.nsec]"); 824 return 1; 825 } 826 827 for (p = name; *p; p++) 828 if (*p < '0' || *p > '9') 829 goto badformat; 830 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; 848 849 sec = mktime(&t); 850 if (sec == -1) { 851 warnx("date/time out of range"); 852 return 1; 853 } 854 rts->tv_sec = sec; 855 rts->tv_nsec = nsec; 856 return 0; 857 } 858 859 CMDFUNCSTART(chmtime) 860 { 861 if (dotime(argv[1], &curinode->di_mtime)) 862 return 1; 863 inodirty(); 864 printactive(0); 865 return 0; 866 } 867 868 CMDFUNCSTART(chatime) 869 { 870 if (dotime(argv[1], &curinode->di_atime)) 871 return 1; 872 inodirty(); 873 printactive(0); 874 return 0; 875 } 876 877 CMDFUNCSTART(chctime) 878 { 879 if (dotime(argv[1], &curinode->di_ctime)) 880 return 1; 881 inodirty(); 882 printactive(0); 883 return 0; 884 } 885