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