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