1 # include <stdio.h> 2 # include <sys/types.h> 3 # include <sys/stat.h> 4 # include <sys/dir.h> 5 # include <errno.h> 6 # include <signal.h> 7 # include <sysexits.h> 8 # include <whoami.h> 9 # include <pwd.h> 10 11 /* 12 ** SCCS.C -- human-oriented front end to the SCCS system. 13 ** 14 ** Without trying to add any functionality to speak of, this 15 ** program tries to make SCCS a little more accessible to human 16 ** types. The main thing it does is automatically put the 17 ** string "SCCS/s." on the front of names. Also, it has a 18 ** couple of things that are designed to shorten frequent 19 ** combinations, e.g., "delget" which expands to a "delta" 20 ** and a "get". 21 ** 22 ** This program can also function as a setuid front end. 23 ** To do this, you should copy the source, renaming it to 24 ** whatever you want, e.g., "syssccs". Change any defaults 25 ** in the program (e.g., syssccs might default -d to 26 ** "/usr/src/sys"). Then recompile and put the result 27 ** as setuid to whomever you want. In this mode, sccs 28 ** knows to not run setuid for certain programs in order 29 ** to preserve security, and so forth. 30 ** 31 ** Usage: 32 ** sccs [flags] command [args] 33 ** 34 ** Flags: 35 ** -d<dir> <dir> represents a directory to search 36 ** out of. It should be a full pathname 37 ** for general usage. E.g., if <dir> is 38 ** "/usr/src/sys", then a reference to the 39 ** file "dev/bio.c" becomes a reference to 40 ** "/usr/src/sys/dev/bio.c". 41 ** -p<path> prepends <path> to the final component 42 ** of the pathname. By default, this is 43 ** "SCCS". For example, in the -d example 44 ** above, the path then gets modified to 45 ** "/usr/src/sys/dev/SCCS/s.bio.c". In 46 ** more common usage (without the -d flag), 47 ** "prog.c" would get modified to 48 ** "SCCS/s.prog.c". In both cases, the 49 ** "s." gets automatically prepended. 50 ** -r run as the real user. 51 ** 52 ** Commands: 53 ** admin, 54 ** get, 55 ** delta, 56 ** rmdel, 57 ** chghist, 58 ** etc. Straight out of SCCS; only difference 59 ** is that pathnames get modified as 60 ** described above. 61 ** edit Macro for "get -e". 62 ** unedit Removes a file being edited, knowing 63 ** about p-files, etc. 64 ** delget Macro for "delta" followed by "get". 65 ** deledit Macro for "delta" followed by "get -e". 66 ** info Tell what files being edited. 67 ** clean Remove all files that can be 68 ** regenerated from SCCS files. 69 ** check Like info, but return exit status, for 70 ** use in makefiles. 71 ** fix Remove a top delta & reedit, but save 72 ** the previous changes in that delta. 73 ** 74 ** Compilation Flags: 75 ** UIDUSER -- determine who the user is by looking at the 76 ** uid rather than the login name -- for machines 77 ** where SCCS gets the user in this way. 78 ** SCCSDIR -- if defined, forces the -d flag to take on 79 ** this value. This is so that the setuid 80 ** aspects of this program cannot be abused. 81 ** This flag also disables the -p flag. 82 ** SCCSPATH -- the default for the -p flag. 83 ** MYNAME -- the title this program should print when it 84 ** gives error messages. 85 ** 86 ** Compilation Instructions: 87 ** cc -O -n -s sccs.c 88 ** The flags listed above can be -D defined to simplify 89 ** recompilation for variant versions. 90 ** 91 ** Author: 92 ** Eric Allman, UCB/INGRES 93 ** Copyright 1980 Regents of the University of California 94 */ 95 96 static char SccsId[] = "@(#)sccs.c 1.64 05/27/81"; 97 98 /******************* Configuration Information ********************/ 99 100 /* special defines for local berkeley systems */ 101 # include <whoami.h> 102 103 # ifdef CSVAX 104 # define UIDUSER 105 # define PROGPATH(name) "/usr/local/name" 106 # endif CSVAX 107 108 # ifdef INGVAX 109 # define PROGPATH(name) "/usr/local/name" 110 # endif INGVAX 111 112 # ifdef CORY 113 # define PROGPATH(name) "/usr/eecs/bin/name" 114 # endif CORY 115 116 /* end of berkeley systems defines */ 117 118 # ifndef SCCSPATH 119 # define SCCSPATH "SCCS" /* pathname in which to find s-files */ 120 # endif NOT SCCSPATH 121 122 # ifndef MYNAME 123 # define MYNAME "sccs" /* name used for printing errors */ 124 # endif NOT MYNAME 125 126 # ifndef PROGPATH 127 # define PROGPATH(name) "/usr/sccs/name" /* place to find binaries */ 128 # endif PROGPATH 129 130 /**************** End of Configuration Information ****************/ 131 132 typedef char bool; 133 # define TRUE 1 134 # define FALSE 0 135 136 # define bitset(bit, word) ((bool) ((bit) & (word))) 137 138 struct sccsprog 139 { 140 char *sccsname; /* name of SCCS routine */ 141 short sccsoper; /* opcode, see below */ 142 short sccsflags; /* flags, see below */ 143 char *sccspath; /* pathname of binary implementing */ 144 }; 145 146 /* values for sccsoper */ 147 # define PROG 0 /* call a program */ 148 # define CMACRO 1 /* command substitution macro */ 149 # define FIX 2 /* fix a delta */ 150 # define CLEAN 3 /* clean out recreatable files */ 151 # define UNEDIT 4 /* unedit a file */ 152 # define SHELL 5 /* call a shell file (like PROG) */ 153 # define DIFFS 6 /* diff between sccs & file out */ 154 # define DODIFF 7 /* internal call to diff program */ 155 # define CREATE 8 /* create new files */ 156 157 /* bits for sccsflags */ 158 # define NO_SDOT 0001 /* no s. on front of args */ 159 # define REALUSER 0002 /* protected (e.g., admin) */ 160 161 /* modes for the "clean", "info", "check" ops */ 162 # define CLEANC 0 /* clean command */ 163 # define INFOC 1 /* info command */ 164 # define CHECKC 2 /* check command */ 165 # define TELLC 3 /* give list of files being edited */ 166 167 /* 168 ** Description of commands known to this program. 169 ** First argument puts the command into a class. Second arg is 170 ** info regarding treatment of this command. Third arg is a 171 ** list of flags this command accepts from macros, etc. Fourth 172 ** arg is the pathname of the implementing program, or the 173 ** macro definition, or the arg to a sub-algorithm. 174 */ 175 176 struct sccsprog SccsProg[] = 177 { 178 "admin", PROG, REALUSER, PROGPATH(admin), 179 "chghist", PROG, 0, PROGPATH(rmdel), 180 "comb", PROG, 0, PROGPATH(comb), 181 "delta", PROG, 0, PROGPATH(delta), 182 "get", PROG, 0, PROGPATH(get), 183 "help", PROG, NO_SDOT, PROGPATH(help), 184 "prs", PROG, 0, PROGPATH(prs), 185 "prt", PROG, 0, PROGPATH(prt), 186 "rmdel", PROG, REALUSER, PROGPATH(rmdel), 187 "val", PROG, 0, PROGPATH(val), 188 "what", PROG, NO_SDOT, PROGPATH(what), 189 "sccsdiff", SHELL, REALUSER, PROGPATH(sccsdiff), 190 "edit", CMACRO, NO_SDOT, "get -e", 191 "delget", CMACRO, NO_SDOT, "delta:mysrp/get:ixbeskcl -t", 192 "deledit", CMACRO, NO_SDOT, "delta:mysrp -n/get:ixbskcl -e -t -g", 193 "fix", FIX, NO_SDOT, NULL, 194 "clean", CLEAN, REALUSER|NO_SDOT, (char *) CLEANC, 195 "info", CLEAN, REALUSER|NO_SDOT, (char *) INFOC, 196 "check", CLEAN, REALUSER|NO_SDOT, (char *) CHECKC, 197 "tell", CLEAN, REALUSER|NO_SDOT, (char *) TELLC, 198 "unedit", UNEDIT, NO_SDOT, NULL, 199 "diffs", DIFFS, NO_SDOT|REALUSER, NULL, 200 "-diff", DODIFF, NO_SDOT|REALUSER, PROGPATH(bdiff), 201 "print", CMACRO, 0, "prt -e/get -p -m -s", 202 "branch", CMACRO, NO_SDOT, 203 "get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g", 204 "create", CREATE, NO_SDOT, NULL, 205 NULL, -1, 0, NULL 206 }; 207 208 /* one line from a p-file */ 209 struct pfile 210 { 211 char *p_osid; /* old SID */ 212 char *p_nsid; /* new SID */ 213 char *p_user; /* user who did edit */ 214 char *p_date; /* date of get */ 215 char *p_time; /* time of get */ 216 char *p_aux; /* extra info at end */ 217 }; 218 219 char *SccsPath = SCCSPATH; /* pathname of SCCS files */ 220 # ifdef SCCSDIR 221 char *SccsDir = SCCSDIR; /* directory to begin search from */ 222 # else 223 char *SccsDir = ""; 224 # endif 225 char MyName[] = MYNAME; /* name used in messages */ 226 int OutFile = -1; /* override output file for commands */ 227 bool RealUser; /* if set, running as real user */ 228 # ifdef DEBUG 229 bool Debug; /* turn on tracing */ 230 # endif 231 # ifndef V6 232 extern char *getenv(); 233 # endif V6 234 235 main(argc, argv) 236 int argc; 237 char **argv; 238 { 239 register char *p; 240 extern struct sccsprog *lookup(); 241 register int i; 242 # ifndef V6 243 # ifndef SCCSDIR 244 register struct passwd *pw; 245 extern struct passwd *getpwnam(); 246 char buf[100]; 247 248 /* pull "SccsDir" out of the environment (possibly) */ 249 p = getenv("PROJECT"); 250 if (p != NULL && p[0] != '\0') 251 { 252 if (p[0] == '/') 253 SccsDir = p; 254 else 255 { 256 pw = getpwnam(p); 257 if (pw == NULL) 258 { 259 usrerr("user %s does not exist", p); 260 exit(EX_USAGE); 261 } 262 strcpy(buf, pw->pw_dir); 263 strcat(buf, "/src"); 264 if (access(buf, 0) < 0) 265 { 266 strcpy(buf, pw->pw_dir); 267 strcat(buf, "/source"); 268 if (access(buf, 0) < 0) 269 { 270 usrerr("project %s has no source!", p); 271 exit(EX_USAGE); 272 } 273 } 274 SccsDir = buf; 275 } 276 } 277 # endif SCCSDIR 278 # endif V6 279 280 /* 281 ** Detect and decode flags intended for this program. 282 */ 283 284 if (argc < 2) 285 { 286 fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName); 287 exit(EX_USAGE); 288 } 289 argv[argc] = NULL; 290 291 if (lookup(argv[0]) == NULL) 292 { 293 while ((p = *++argv) != NULL) 294 { 295 if (*p != '-') 296 break; 297 switch (*++p) 298 { 299 case 'r': /* run as real user */ 300 setuid(getuid()); 301 RealUser++; 302 break; 303 304 # ifndef SCCSDIR 305 case 'p': /* path of sccs files */ 306 SccsPath = ++p; 307 if (SccsPath[0] == '\0' && argv[1] != NULL) 308 SccsPath = *++argv; 309 break; 310 311 case 'd': /* directory to search from */ 312 SccsDir = ++p; 313 if (SccsDir[0] == '\0' && argv[1] != NULL) 314 SccsDir = *++argv; 315 break; 316 # endif 317 318 # ifdef DEBUG 319 case 'T': /* trace */ 320 Debug++; 321 break; 322 # endif 323 324 default: 325 usrerr("unknown option -%s", p); 326 break; 327 } 328 } 329 if (SccsPath[0] == '\0') 330 SccsPath = "."; 331 } 332 333 i = command(argv, FALSE, ""); 334 exit(i); 335 } 336 337 /* 338 ** COMMAND -- look up and perform a command 339 ** 340 ** This routine is the guts of this program. Given an 341 ** argument vector, it looks up the "command" (argv[0]) 342 ** in the configuration table and does the necessary stuff. 343 ** 344 ** Parameters: 345 ** argv -- an argument vector to process. 346 ** forkflag -- if set, fork before executing the command. 347 ** editflag -- if set, only include flags listed in the 348 ** sccsklets field of the command descriptor. 349 ** arg0 -- a space-seperated list of arguments to insert 350 ** before argv. 351 ** 352 ** Returns: 353 ** zero -- command executed ok. 354 ** else -- error status. 355 ** 356 ** Side Effects: 357 ** none. 358 */ 359 360 command(argv, forkflag, arg0) 361 char **argv; 362 bool forkflag; 363 char *arg0; 364 { 365 register struct sccsprog *cmd; 366 register char *p; 367 char buf[100]; 368 extern struct sccsprog *lookup(); 369 char *nav[1000]; 370 char **np; 371 register char **ap; 372 register int i; 373 register char *q; 374 extern bool unedit(); 375 int rval = 0; 376 extern char *index(); 377 extern char *makefile(); 378 char *editchs; 379 extern char *tail(); 380 381 # ifdef DEBUG 382 if (Debug) 383 { 384 printf("command:\n\t\"%s\"\n", arg0); 385 for (np = argv; *np != NULL; np++) 386 printf("\t\"%s\"\n", *np); 387 } 388 # endif 389 390 /* 391 ** Copy arguments. 392 ** Copy from arg0 & if necessary at most one arg 393 ** from argv[0]. 394 */ 395 396 np = ap = &nav[1]; 397 editchs = NULL; 398 for (p = arg0, q = buf; *p != '\0' && *p != '/'; ) 399 { 400 *np++ = q; 401 while (*p == ' ') 402 p++; 403 while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':') 404 *q++ = *p++; 405 *q++ = '\0'; 406 if (*p == ':') 407 { 408 editchs = q; 409 while (*++p != '\0' && *p != '/' && *p != ' ') 410 *q++ = *p; 411 *q++ = '\0'; 412 } 413 } 414 *np = NULL; 415 if (*ap == NULL) 416 *np++ = *argv++; 417 418 /* 419 ** Look up command. 420 ** At this point, *ap is the command name. 421 */ 422 423 cmd = lookup(*ap); 424 if (cmd == NULL) 425 { 426 usrerr("Unknown command \"%s\"", *ap); 427 return (EX_USAGE); 428 } 429 430 /* 431 ** Copy remaining arguments doing editing as appropriate. 432 */ 433 434 for (; *argv != NULL; argv++) 435 { 436 p = *argv; 437 if (*p == '-') 438 { 439 if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL) 440 *np++ = p; 441 } 442 else 443 { 444 if (!bitset(NO_SDOT, cmd->sccsflags)) 445 p = makefile(p); 446 if (p != NULL) 447 *np++ = p; 448 } 449 } 450 *np = NULL; 451 452 /* 453 ** Interpret operation associated with this command. 454 */ 455 456 switch (cmd->sccsoper) 457 { 458 case SHELL: /* call a shell file */ 459 *ap = cmd->sccspath; 460 *--ap = "sh"; 461 rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag); 462 break; 463 464 case PROG: /* call an sccs prog */ 465 rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag); 466 break; 467 468 case CMACRO: /* command macro */ 469 /* step through & execute each part of the macro */ 470 for (p = cmd->sccspath; *p != '\0'; p++) 471 { 472 q = p; 473 while (*p != '\0' && *p != '/') 474 p++; 475 rval = command(&ap[1], *p != '\0', q); 476 if (rval != 0) 477 break; 478 } 479 break; 480 481 case FIX: /* fix a delta */ 482 if (strncmp(ap[1], "-r", 2) != 0) 483 { 484 usrerr("-r flag needed for fix command"); 485 rval = EX_USAGE; 486 break; 487 } 488 489 /* get the version with all changes */ 490 rval = command(&ap[1], TRUE, "get -k"); 491 492 /* now remove that version from the s-file */ 493 if (rval == 0) 494 rval = command(&ap[1], TRUE, "rmdel:r"); 495 496 /* and edit the old version (but don't clobber new vers) */ 497 if (rval == 0) 498 rval = command(&ap[2], FALSE, "get -e -g"); 499 break; 500 501 case CLEAN: 502 rval = clean((int) cmd->sccspath, ap); 503 break; 504 505 case UNEDIT: 506 for (argv = np = &ap[1]; *argv != NULL; argv++) 507 { 508 if (unedit(*argv)) 509 *np++ = *argv; 510 } 511 *np = NULL; 512 513 /* get all the files that we unedited successfully */ 514 if (np > &ap[1]) 515 rval = command(&ap[1], FALSE, "get"); 516 break; 517 518 case DIFFS: /* diff between s-file & edit file */ 519 /* find the end of the flag arguments */ 520 for (np = &ap[1]; *np != NULL && **np == '-'; np++) 521 continue; 522 argv = np; 523 524 /* for each file, do the diff */ 525 p = argv[1]; 526 while (*np != NULL) 527 { 528 /* messy, but we need a null terminated argv */ 529 *argv = *np++; 530 argv[1] = NULL; 531 i = dodiff(ap, tail(*argv)); 532 if (rval == 0) 533 rval = i; 534 argv[1] = p; 535 } 536 break; 537 538 case DODIFF: /* internal diff call */ 539 setuid(getuid()); 540 for (np = ap; *np != NULL; np++) 541 { 542 if ((*np)[0] == '-' && (*np)[1] == 'C') 543 (*np)[1] = 'c'; 544 } 545 546 /* insert "-" argument */ 547 np[1] = NULL; 548 np[0] = np[-1]; 549 np[-1] = "-"; 550 551 /* execute the diff program of choice */ 552 # ifndef V6 553 execvp("diff", ap); 554 # endif 555 execv(cmd->sccspath, argv); 556 syserr("cannot exec %s", cmd->sccspath); 557 exit(EX_OSERR); 558 559 case CREATE: /* create new sccs files */ 560 /* skip over flag arguments */ 561 for (np = &ap[1]; *np != NULL && **np == '-'; np++) 562 continue; 563 argv = np; 564 565 /* do an admin for each file */ 566 p = argv[1]; 567 while (*np != NULL) 568 { 569 printf("\n%s:\n", *np); 570 sprintf(buf, "-i%s", *np); 571 ap[0] = buf; 572 argv[0] = tail(*np); 573 argv[1] = NULL; 574 rval = command(ap, TRUE, "admin"); 575 argv[1] = p; 576 if (rval == 0) 577 { 578 sprintf(buf, ",%s", tail(*np)); 579 if (link(*np, buf) >= 0) 580 unlink(*np); 581 } 582 np++; 583 } 584 break; 585 586 default: 587 syserr("oper %d", cmd->sccsoper); 588 exit(EX_SOFTWARE); 589 } 590 # ifdef DEBUG 591 if (Debug) 592 printf("command: rval=%d\n", rval); 593 # endif 594 return (rval); 595 } 596 597 /* 598 ** LOOKUP -- look up an SCCS command name. 599 ** 600 ** Parameters: 601 ** name -- the name of the command to look up. 602 ** 603 ** Returns: 604 ** ptr to command descriptor for this command. 605 ** NULL if no such entry. 606 ** 607 ** Side Effects: 608 ** none. 609 */ 610 611 struct sccsprog * 612 lookup(name) 613 char *name; 614 { 615 register struct sccsprog *cmd; 616 617 for (cmd = SccsProg; cmd->sccsname != NULL; cmd++) 618 { 619 if (strcmp(cmd->sccsname, name) == 0) 620 return (cmd); 621 } 622 return (NULL); 623 } 624 625 /* 626 ** CALLPROG -- call a program 627 ** 628 ** Used to call the SCCS programs. 629 ** 630 ** Parameters: 631 ** progpath -- pathname of the program to call. 632 ** flags -- status flags from the command descriptors. 633 ** argv -- an argument vector to pass to the program. 634 ** forkflag -- if true, fork before calling, else just 635 ** exec. 636 ** 637 ** Returns: 638 ** The exit status of the program. 639 ** Nothing if forkflag == FALSE. 640 ** 641 ** Side Effects: 642 ** Can exit if forkflag == FALSE. 643 */ 644 645 callprog(progpath, flags, argv, forkflag) 646 char *progpath; 647 short flags; 648 char **argv; 649 bool forkflag; 650 { 651 register int i; 652 auto int st; 653 654 # ifdef DEBUG 655 if (Debug) 656 { 657 printf("callprog:\n"); 658 for (i = 0; argv[i] != NULL; i++) 659 printf("\t\"%s\"\n", argv[i]); 660 } 661 # endif 662 663 if (*argv == NULL) 664 return (-1); 665 666 /* 667 ** Fork if appropriate. 668 */ 669 670 if (forkflag) 671 { 672 # ifdef DEBUG 673 if (Debug) 674 printf("Forking\n"); 675 # endif 676 i = fork(); 677 if (i < 0) 678 { 679 syserr("cannot fork"); 680 exit(EX_OSERR); 681 } 682 else if (i > 0) 683 { 684 wait(&st); 685 if ((st & 0377) == 0) 686 st = (st >> 8) & 0377; 687 if (OutFile >= 0) 688 { 689 close(OutFile); 690 OutFile = -1; 691 } 692 return (st); 693 } 694 } 695 else if (OutFile >= 0) 696 { 697 syserr("callprog: setting stdout w/o forking"); 698 exit(EX_SOFTWARE); 699 } 700 701 /* set protection as appropriate */ 702 if (bitset(REALUSER, flags)) 703 setuid(getuid()); 704 705 /* change standard input & output if needed */ 706 if (OutFile >= 0) 707 { 708 close(1); 709 dup(OutFile); 710 close(OutFile); 711 } 712 713 /* call real SCCS program */ 714 execv(progpath, argv); 715 syserr("cannot execute %s", progpath); 716 exit(EX_UNAVAILABLE); 717 /*NOTREACHED*/ 718 } 719 720 /* 721 ** MAKEFILE -- make filename of SCCS file 722 ** 723 ** If the name passed is already the name of an SCCS file, 724 ** just return it. Otherwise, munge the name into the name 725 ** of the actual SCCS file. 726 ** 727 ** There are cases when it is not clear what you want to 728 ** do. For example, if SccsPath is an absolute pathname 729 ** and the name given is also an absolute pathname, we go 730 ** for SccsPath (& only use the last component of the name 731 ** passed) -- this is important for security reasons (if 732 ** sccs is being used as a setuid front end), but not 733 ** particularly intuitive. 734 ** 735 ** Parameters: 736 ** name -- the file name to be munged. 737 ** 738 ** Returns: 739 ** The pathname of the sccs file. 740 ** NULL on error. 741 ** 742 ** Side Effects: 743 ** none. 744 */ 745 746 char * 747 makefile(name) 748 char *name; 749 { 750 register char *p; 751 char buf[512]; 752 extern char *malloc(); 753 extern char *rindex(); 754 extern bool safepath(); 755 extern bool isdir(); 756 register char *q; 757 758 p = rindex(name, '/'); 759 if (p == NULL) 760 p = name; 761 else 762 p++; 763 764 /* 765 ** Check to see that the path is "safe", i.e., that we 766 ** are not letting some nasty person use the setuid part 767 ** of this program to look at or munge some presumably 768 ** hidden files. 769 */ 770 771 if (SccsDir[0] == '/' && !safepath(name)) 772 return (NULL); 773 774 /* 775 ** Create the base pathname. 776 */ 777 778 /* first the directory part */ 779 if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0) 780 { 781 strcpy(buf, SccsDir); 782 strcat(buf, "/"); 783 } 784 else 785 strcpy(buf, ""); 786 787 /* then the head of the pathname */ 788 strncat(buf, name, p - name); 789 q = &buf[strlen(buf)]; 790 791 /* now copy the final part of the name, in case useful */ 792 strcpy(q, p); 793 794 /* so is it useful? */ 795 if (strncmp(p, "s.", 2) != 0 && !isdir(buf)) 796 { 797 /* sorry, no; copy the SCCS pathname & the "s." */ 798 strcpy(q, SccsPath); 799 strcat(buf, "/s."); 800 801 /* and now the end of the name */ 802 strcat(buf, p); 803 } 804 805 /* if i haven't changed it, why did I do all this? */ 806 if (strcmp(buf, name) == 0) 807 p = name; 808 else 809 { 810 /* but if I have, squirrel it away */ 811 p = malloc(strlen(buf) + 1); 812 if (p == NULL) 813 { 814 perror("Sccs: no mem"); 815 exit(EX_OSERR); 816 } 817 strcpy(p, buf); 818 } 819 820 return (p); 821 } 822 823 /* 824 ** ISDIR -- return true if the argument is a directory. 825 ** 826 ** Parameters: 827 ** name -- the pathname of the file to check. 828 ** 829 ** Returns: 830 ** TRUE if 'name' is a directory, FALSE otherwise. 831 ** 832 ** Side Effects: 833 ** none. 834 */ 835 836 bool 837 isdir(name) 838 char *name; 839 { 840 struct stat stbuf; 841 842 return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR); 843 } 844 845 /* 846 ** SAFEPATH -- determine whether a pathname is "safe" 847 ** 848 ** "Safe" pathnames only allow you to get deeper into the 849 ** directory structure, i.e., full pathnames and ".." are 850 ** not allowed. 851 ** 852 ** Parameters: 853 ** p -- the name to check. 854 ** 855 ** Returns: 856 ** TRUE -- if the path is safe. 857 ** FALSE -- if the path is not safe. 858 ** 859 ** Side Effects: 860 ** Prints a message if the path is not safe. 861 */ 862 863 bool 864 safepath(p) 865 register char *p; 866 { 867 extern char *index(); 868 869 if (*p != '/') 870 { 871 while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0) 872 { 873 p = index(p, '/'); 874 if (p == NULL) 875 return (TRUE); 876 p++; 877 } 878 } 879 880 printf("You may not use full pathnames or \"..\"\n"); 881 return (FALSE); 882 } 883 884 /* 885 ** CLEAN -- clean out recreatable files 886 ** 887 ** Any file for which an "s." file exists but no "p." file 888 ** exists in the current directory is purged. 889 ** 890 ** Parameters: 891 ** mode -- tells whether this came from a "clean", "info", or 892 ** "check" command. 893 ** argv -- the rest of the argument vector. 894 ** 895 ** Returns: 896 ** none. 897 ** 898 ** Side Effects: 899 ** Removes files in the current directory. 900 ** Prints information regarding files being edited. 901 ** Exits if a "check" command. 902 */ 903 904 clean(mode, argv) 905 int mode; 906 char **argv; 907 { 908 struct direct dir; 909 char buf[100]; 910 char *bufend; 911 register FILE *dirfd; 912 register char *basefile; 913 bool gotedit; 914 bool gotpfent; 915 FILE *pfp; 916 bool nobranch = FALSE; 917 extern struct pfile *getpfent(); 918 register struct pfile *pf; 919 register char **ap; 920 extern char *username(); 921 char *usernm = NULL; 922 char *subdir = NULL; 923 char *cmdname; 924 925 /* 926 ** Process the argv 927 */ 928 929 cmdname = *argv; 930 for (ap = argv; *++ap != NULL; ) 931 { 932 if (**ap == '-') 933 { 934 /* we have a flag */ 935 switch ((*ap)[1]) 936 { 937 case 'b': 938 nobranch = TRUE; 939 break; 940 941 case 'u': 942 if ((*ap)[2] != '\0') 943 usernm = &(*ap)[2]; 944 else if (ap[1] != NULL && ap[1][0] != '-') 945 usernm = *++ap; 946 else 947 usernm = username(); 948 break; 949 } 950 } 951 else 952 { 953 if (subdir != NULL) 954 usrerr("too many args"); 955 else 956 subdir = *ap; 957 } 958 } 959 960 /* 961 ** Find and open the SCCS directory. 962 */ 963 964 strcpy(buf, SccsDir); 965 if (buf[0] != '\0') 966 strcat(buf, "/"); 967 if (subdir != NULL) 968 { 969 strcat(buf, subdir); 970 strcat(buf, "/"); 971 } 972 strcat(buf, SccsPath); 973 bufend = &buf[strlen(buf)]; 974 975 dirfd = fopen(buf, "r"); 976 if (dirfd == NULL) 977 { 978 usrerr("cannot open %s", buf); 979 return (EX_NOINPUT); 980 } 981 982 /* 983 ** Scan the SCCS directory looking for s. files. 984 ** gotedit tells whether we have tried to clean any 985 ** files that are being edited. 986 */ 987 988 gotedit = FALSE; 989 while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL) 990 { 991 if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0) 992 continue; 993 994 /* got an s. file -- see if the p. file exists */ 995 strcpy(bufend, "/p."); 996 basefile = bufend + 3; 997 strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2); 998 basefile[sizeof dir.d_name - 2] = '\0'; 999 1000 /* 1001 ** open and scan the p-file. 1002 ** 'gotpfent' tells if we have found a valid p-file 1003 ** entry. 1004 */ 1005 1006 pfp = fopen(buf, "r"); 1007 gotpfent = FALSE; 1008 if (pfp != NULL) 1009 { 1010 /* the file exists -- report it's contents */ 1011 while ((pf = getpfent(pfp)) != NULL) 1012 { 1013 if (nobranch && isbranch(pf->p_nsid)) 1014 continue; 1015 if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC) 1016 continue; 1017 gotedit = TRUE; 1018 gotpfent = TRUE; 1019 if (mode == TELLC) 1020 { 1021 printf("%s\n", basefile); 1022 break; 1023 } 1024 printf("%12s: being edited: ", basefile); 1025 putpfent(pf, stdout); 1026 } 1027 fclose(pfp); 1028 } 1029 1030 /* the s. file exists and no p. file exists -- unlink the g-file */ 1031 if (mode == CLEANC && !gotpfent) 1032 { 1033 strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2); 1034 buf[sizeof dir.d_name - 2] = '\0'; 1035 unlink(buf); 1036 } 1037 } 1038 1039 /* cleanup & report results */ 1040 fclose(dirfd); 1041 if (!gotedit && mode == INFOC) 1042 { 1043 printf("Nothing being edited"); 1044 if (nobranch) 1045 printf(" (on trunk)"); 1046 if (usernm == NULL) 1047 printf("\n"); 1048 else 1049 printf(" by %s\n", usernm); 1050 } 1051 if (mode == CHECKC) 1052 exit(gotedit); 1053 return (EX_OK); 1054 } 1055 1056 /* 1057 ** ISBRANCH -- is the SID a branch? 1058 ** 1059 ** Parameters: 1060 ** sid -- the sid to check. 1061 ** 1062 ** Returns: 1063 ** TRUE if the sid represents a branch. 1064 ** FALSE otherwise. 1065 ** 1066 ** Side Effects: 1067 ** none. 1068 */ 1069 1070 isbranch(sid) 1071 char *sid; 1072 { 1073 register char *p; 1074 int dots; 1075 1076 dots = 0; 1077 for (p = sid; *p != '\0'; p++) 1078 { 1079 if (*p == '.') 1080 dots++; 1081 if (dots > 1) 1082 return (TRUE); 1083 } 1084 return (FALSE); 1085 } 1086 1087 /* 1088 ** UNEDIT -- unedit a file 1089 ** 1090 ** Checks to see that the current user is actually editting 1091 ** the file and arranges that s/he is not editting it. 1092 ** 1093 ** Parameters: 1094 ** fn -- the name of the file to be unedited. 1095 ** 1096 ** Returns: 1097 ** TRUE -- if the file was successfully unedited. 1098 ** FALSE -- if the file was not unedited for some 1099 ** reason. 1100 ** 1101 ** Side Effects: 1102 ** fn is removed 1103 ** entries are removed from pfile. 1104 */ 1105 1106 bool 1107 unedit(fn) 1108 char *fn; 1109 { 1110 register FILE *pfp; 1111 char *pfn; 1112 static char tfn[] = "/tmp/sccsXXXXX"; 1113 FILE *tfp; 1114 register char *q; 1115 bool delete = FALSE; 1116 bool others = FALSE; 1117 char *myname; 1118 extern char *username(); 1119 struct pfile *pent; 1120 extern struct pfile *getpfent(); 1121 char buf[120]; 1122 extern char *makefile(); 1123 1124 /* make "s." filename & find the trailing component */ 1125 pfn = makefile(fn); 1126 if (pfn == NULL) 1127 return (FALSE); 1128 q = rindex(pfn, '/'); 1129 if (q == NULL) 1130 q = &pfn[-1]; 1131 if (q[1] != 's' || q[2] != '.') 1132 { 1133 usrerr("bad file name \"%s\"", fn); 1134 return (FALSE); 1135 } 1136 1137 /* turn "s." into "p." & try to open it */ 1138 *++q = 'p'; 1139 1140 pfp = fopen(pfn, "r"); 1141 if (pfp == NULL) 1142 { 1143 printf("%12s: not being edited\n", fn); 1144 return (FALSE); 1145 } 1146 1147 /* create temp file for editing p-file */ 1148 mktemp(tfn); 1149 tfp = fopen(tfn, "w"); 1150 if (tfp == NULL) 1151 { 1152 usrerr("cannot create \"%s\"", tfn); 1153 exit(EX_OSERR); 1154 } 1155 1156 /* figure out who I am */ 1157 myname = username(); 1158 1159 /* 1160 ** Copy p-file to temp file, doing deletions as needed. 1161 */ 1162 1163 while ((pent = getpfent(pfp)) != NULL) 1164 { 1165 if (strcmp(pent->p_user, myname) == 0) 1166 { 1167 /* a match */ 1168 delete++; 1169 } 1170 else 1171 { 1172 /* output it again */ 1173 putpfent(pent, tfp); 1174 others++; 1175 } 1176 } 1177 1178 /* do final cleanup */ 1179 if (others) 1180 { 1181 /* copy it back (perhaps it should be linked?) */ 1182 if (freopen(tfn, "r", tfp) == NULL) 1183 { 1184 syserr("cannot reopen \"%s\"", tfn); 1185 exit(EX_OSERR); 1186 } 1187 if (freopen(pfn, "w", pfp) == NULL) 1188 { 1189 usrerr("cannot create \"%s\"", pfn); 1190 return (FALSE); 1191 } 1192 while (fgets(buf, sizeof buf, tfp) != NULL) 1193 fputs(buf, pfp); 1194 } 1195 else 1196 { 1197 /* it's empty -- remove it */ 1198 unlink(pfn); 1199 } 1200 fclose(tfp); 1201 fclose(pfp); 1202 unlink(tfn); 1203 1204 /* actually remove the g-file */ 1205 if (delete) 1206 { 1207 unlink(tail(fn)); 1208 printf("%12s: removed\n", tail(fn)); 1209 return (TRUE); 1210 } 1211 else 1212 { 1213 printf("%12s: not being edited by you\n", fn); 1214 return (FALSE); 1215 } 1216 } 1217 1218 /* 1219 ** DODIFF -- diff an s-file against a g-file 1220 ** 1221 ** Parameters: 1222 ** getv -- argv for the 'get' command. 1223 ** gfile -- name of the g-file to diff against. 1224 ** 1225 ** Returns: 1226 ** Result of get. 1227 ** 1228 ** Side Effects: 1229 ** none. 1230 */ 1231 1232 dodiff(getv, gfile) 1233 char **getv; 1234 char *gfile; 1235 { 1236 int pipev[2]; 1237 int rval; 1238 register int i; 1239 register int pid; 1240 auto int st; 1241 extern int errno; 1242 int (*osig)(); 1243 1244 printf("\n------- %s -------\n", gfile); 1245 fflush(stdout); 1246 1247 /* create context for diff to run in */ 1248 if (pipe(pipev) < 0) 1249 { 1250 syserr("dodiff: pipe failed"); 1251 exit(EX_OSERR); 1252 } 1253 if ((pid = fork()) < 0) 1254 { 1255 syserr("dodiff: fork failed"); 1256 exit(EX_OSERR); 1257 } 1258 else if (pid > 0) 1259 { 1260 /* in parent; run get */ 1261 OutFile = pipev[1]; 1262 close(pipev[0]); 1263 rval = command(&getv[1], TRUE, "get:rcixt -s -k -p"); 1264 osig = signal(SIGINT, SIG_IGN); 1265 while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR) 1266 errno = 0; 1267 signal(SIGINT, osig); 1268 /* ignore result of diff */ 1269 } 1270 else 1271 { 1272 /* in child, run diff */ 1273 if (close(pipev[1]) < 0 || close(0) < 0 || 1274 dup(pipev[0]) != 0 || close(pipev[0]) < 0) 1275 { 1276 syserr("dodiff: magic failed"); 1277 exit(EX_OSERR); 1278 } 1279 command(&getv[1], FALSE, "-diff:elsfhbC"); 1280 } 1281 return (rval); 1282 } 1283 1284 /* 1285 ** TAIL -- return tail of filename. 1286 ** 1287 ** Parameters: 1288 ** fn -- the filename. 1289 ** 1290 ** Returns: 1291 ** a pointer to the tail of the filename; e.g., given 1292 ** "cmd/ls.c", "ls.c" is returned. 1293 ** 1294 ** Side Effects: 1295 ** none. 1296 */ 1297 1298 char * 1299 tail(fn) 1300 register char *fn; 1301 { 1302 register char *p; 1303 1304 for (p = fn; *p != 0; p++) 1305 if (*p == '/' && p[1] != '\0' && p[1] != '/') 1306 fn = &p[1]; 1307 return (fn); 1308 } 1309 1310 /* 1311 ** GETPFENT -- get an entry from the p-file 1312 ** 1313 ** Parameters: 1314 ** pfp -- p-file file pointer 1315 ** 1316 ** Returns: 1317 ** pointer to p-file struct for next entry 1318 ** NULL on EOF or error 1319 ** 1320 ** Side Effects: 1321 ** Each call wipes out results of previous call. 1322 */ 1323 1324 struct pfile * 1325 getpfent(pfp) 1326 FILE *pfp; 1327 { 1328 static struct pfile ent; 1329 static char buf[120]; 1330 register char *p; 1331 extern char *nextfield(); 1332 1333 if (fgets(buf, sizeof buf, pfp) == NULL) 1334 return (NULL); 1335 1336 ent.p_osid = p = buf; 1337 ent.p_nsid = p = nextfield(p); 1338 ent.p_user = p = nextfield(p); 1339 ent.p_date = p = nextfield(p); 1340 ent.p_time = p = nextfield(p); 1341 ent.p_aux = p = nextfield(p); 1342 1343 return (&ent); 1344 } 1345 1346 1347 char * 1348 nextfield(p) 1349 register char *p; 1350 { 1351 if (p == NULL || *p == '\0') 1352 return (NULL); 1353 while (*p != ' ' && *p != '\n' && *p != '\0') 1354 p++; 1355 if (*p == '\n' || *p == '\0') 1356 { 1357 *p = '\0'; 1358 return (NULL); 1359 } 1360 *p++ = '\0'; 1361 return (p); 1362 } 1363 /* 1364 ** PUTPFENT -- output a p-file entry to a file 1365 ** 1366 ** Parameters: 1367 ** pf -- the p-file entry 1368 ** f -- the file to put it on. 1369 ** 1370 ** Returns: 1371 ** none. 1372 ** 1373 ** Side Effects: 1374 ** pf is written onto file f. 1375 */ 1376 1377 putpfent(pf, f) 1378 register struct pfile *pf; 1379 register FILE *f; 1380 { 1381 fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid, 1382 pf->p_user, pf->p_date, pf->p_time); 1383 if (pf->p_aux != NULL) 1384 fprintf(f, " %s", pf->p_aux); 1385 else 1386 fprintf(f, "\n"); 1387 } 1388 1389 /* 1390 ** USRERR -- issue user-level error 1391 ** 1392 ** Parameters: 1393 ** f -- format string. 1394 ** p1-p3 -- parameters to a printf. 1395 ** 1396 ** Returns: 1397 ** -1 1398 ** 1399 ** Side Effects: 1400 ** none. 1401 */ 1402 1403 /*VARARGS1*/ 1404 usrerr(f, p1, p2, p3) 1405 char *f; 1406 { 1407 fprintf(stderr, "\n%s: ", MyName); 1408 fprintf(stderr, f, p1, p2, p3); 1409 fprintf(stderr, "\n"); 1410 1411 return (-1); 1412 } 1413 1414 /* 1415 ** SYSERR -- print system-generated error. 1416 ** 1417 ** Parameters: 1418 ** f -- format string to a printf. 1419 ** p1, p2, p3 -- parameters to f. 1420 ** 1421 ** Returns: 1422 ** never. 1423 ** 1424 ** Side Effects: 1425 ** none. 1426 */ 1427 1428 /*VARARGS1*/ 1429 syserr(f, p1, p2, p3) 1430 char *f; 1431 { 1432 extern int errno; 1433 1434 fprintf(stderr, "\n%s SYSERR: ", MyName); 1435 fprintf(stderr, f, p1, p2, p3); 1436 fprintf(stderr, "\n"); 1437 if (errno == 0) 1438 exit(EX_SOFTWARE); 1439 else 1440 { 1441 perror(NULL); 1442 exit(EX_OSERR); 1443 } 1444 } 1445 /* 1446 ** USERNAME -- return name of the current user 1447 ** 1448 ** Parameters: 1449 ** none 1450 ** 1451 ** Returns: 1452 ** name of current user 1453 ** 1454 ** Side Effects: 1455 ** none 1456 */ 1457 1458 char * 1459 username() 1460 { 1461 # ifdef UIDUSER 1462 extern struct passwd *getpwuid(); 1463 register struct passwd *pw; 1464 1465 pw = getpwuid(getuid()); 1466 if (pw == NULL) 1467 { 1468 syserr("who are you? (uid=%d)", getuid()); 1469 exit(EX_OSERR); 1470 } 1471 return (pw->pw_name); 1472 # else 1473 extern char *getlogin(); 1474 extern char *getenv(); 1475 register char *p; 1476 1477 p = getenv("USER"); 1478 if (p == NULL || p[0] == '\0') 1479 p = getlogin(); 1480 return (p); 1481 # endif UIDUSER 1482 } 1483