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