1148Seric # include <stdio.h> 2148Seric # include <sys/types.h> 3148Seric # include <sys/stat.h> 4261Seric # include <sys/dir.h> 51433Seric # include <errno.h> 61433Seric # include <signal.h> 7148Seric # include <sysexits.h> 8202Seric # include <whoami.h> 9148Seric 10828Seric /* 11828Seric ** SCCS.C -- human-oriented front end to the SCCS system. 12828Seric ** 13828Seric ** Without trying to add any functionality to speak of, this 14828Seric ** program tries to make SCCS a little more accessible to human 15828Seric ** types. The main thing it does is automatically put the 16828Seric ** string "SCCS/s." on the front of names. Also, it has a 17828Seric ** couple of things that are designed to shorten frequent 18828Seric ** combinations, e.g., "delget" which expands to a "delta" 19828Seric ** and a "get". 20828Seric ** 21828Seric ** This program can also function as a setuid front end. 22828Seric ** To do this, you should copy the source, renaming it to 23828Seric ** whatever you want, e.g., "syssccs". Change any defaults 24828Seric ** in the program (e.g., syssccs might default -d to 25828Seric ** "/usr/src/sys"). Then recompile and put the result 26828Seric ** as setuid to whomever you want. In this mode, sccs 27828Seric ** knows to not run setuid for certain programs in order 28828Seric ** to preserve security, and so forth. 29828Seric ** 30828Seric ** Usage: 31828Seric ** sccs [flags] command [args] 32828Seric ** 33828Seric ** Flags: 34828Seric ** -d<dir> <dir> represents a directory to search 35828Seric ** out of. It should be a full pathname 36828Seric ** for general usage. E.g., if <dir> is 37828Seric ** "/usr/src/sys", then a reference to the 38828Seric ** file "dev/bio.c" becomes a reference to 39828Seric ** "/usr/src/sys/dev/bio.c". 40828Seric ** -p<path> prepends <path> to the final component 41828Seric ** of the pathname. By default, this is 42828Seric ** "SCCS". For example, in the -d example 43828Seric ** above, the path then gets modified to 44828Seric ** "/usr/src/sys/dev/SCCS/s.bio.c". In 45828Seric ** more common usage (without the -d flag), 46828Seric ** "prog.c" would get modified to 47828Seric ** "SCCS/s.prog.c". In both cases, the 48828Seric ** "s." gets automatically prepended. 49828Seric ** -r run as the real user. 50828Seric ** 51828Seric ** Commands: 52828Seric ** admin, 53828Seric ** get, 54828Seric ** delta, 55828Seric ** rmdel, 56828Seric ** chghist, 57828Seric ** etc. Straight out of SCCS; only difference 58828Seric ** is that pathnames get modified as 59828Seric ** described above. 60828Seric ** edit Macro for "get -e". 61828Seric ** unedit Removes a file being edited, knowing 62828Seric ** about p-files, etc. 63828Seric ** delget Macro for "delta" followed by "get". 64828Seric ** deledit Macro for "delta" followed by "get -e". 65828Seric ** info Tell what files being edited. 66828Seric ** clean Remove all files that can be 67828Seric ** regenerated from SCCS files. 681205Seric ** check Like info, but return exit status, for 69828Seric ** use in makefiles. 70828Seric ** fix Remove a top delta & reedit, but save 71828Seric ** the previous changes in that delta. 72828Seric ** 73828Seric ** Compilation Flags: 74828Seric ** UIDUSER -- determine who the user is by looking at the 75828Seric ** uid rather than the login name -- for machines 76828Seric ** where SCCS gets the user in this way. 771270Seric ** SCCSDIR -- if defined, forces the -d flag to take on 781205Seric ** this value. This is so that the setuid 791205Seric ** aspects of this program cannot be abused. 801270Seric ** This flag also disables the -p flag. 811270Seric ** SCCSPATH -- the default for the -p flag. 821437Seric ** MYNAME -- the title this program should print when it 831437Seric ** gives error messages. 84828Seric ** 85828Seric ** Compilation Instructions: 86828Seric ** cc -O -n -s sccs.c 871437Seric ** The flags listed above can be -D defined to simplify 881437Seric ** recompilation for variant versions. 89828Seric ** 90828Seric ** Author: 91828Seric ** Eric Allman, UCB/INGRES 921270Seric ** Copyright 1980 Regents of the University of California 93828Seric */ 94155Seric 95*1822Seric static char SccsId[] = "@(#)sccs.c 1.48 11/20/80"; 961432Seric 971270Seric /******************* Configuration Information ********************/ 981270Seric 991437Seric /* special defines for local berkeley systems */ 1001437Seric # include <whoami.h> 1011437Seric 102828Seric # ifdef CSVAX 103828Seric # define UIDUSER 1041270Seric # define PROGPATH(name) "/usr/local/name" 1051270Seric # endif CSVAX 106828Seric 1071618Seric # ifdef INGVAX 1081618Seric # define PROGPATH(name) "/usr/local/name" 1091618Seric # endif INGVAX 1101618Seric 1111437Seric /* end of berkeley systems defines */ 1121437Seric 1131437Seric # ifndef SCCSPATH 1141432Seric # define SCCSPATH "SCCS" /* pathname in which to find s-files */ 1151437Seric # endif NOT SCCSPATH 116828Seric 1171437Seric # ifndef MYNAME 1181437Seric # define MYNAME "sccs" /* name used for printing errors */ 1191437Seric # endif NOT MYNAME 1201270Seric 1211432Seric # ifndef PROGPATH 1221432Seric # define PROGPATH(name) "/usr/sccs/name" /* place to find binaries */ 1231432Seric # endif PROGPATH 1241432Seric 1251270Seric /**************** End of Configuration Information ****************/ 1261432Seric 127157Seric typedef char bool; 128200Seric # define TRUE 1 129200Seric # define FALSE 0 130157Seric 1311438Seric # define bitset(bit, word) ((bool) ((bit) & (word))) 1321438Seric 133828Seric # ifdef UIDUSER 134828Seric # include <pwd.h> 135828Seric # endif UIDUSER 136828Seric 137148Seric struct sccsprog 138148Seric { 139148Seric char *sccsname; /* name of SCCS routine */ 140200Seric short sccsoper; /* opcode, see below */ 141200Seric short sccsflags; /* flags, see below */ 142148Seric char *sccspath; /* pathname of binary implementing */ 143148Seric }; 144148Seric 145200Seric /* values for sccsoper */ 146200Seric # define PROG 0 /* call a program */ 147201Seric # define CMACRO 1 /* command substitution macro */ 148226Seric # define FIX 2 /* fix a delta */ 149261Seric # define CLEAN 3 /* clean out recreatable files */ 150396Seric # define UNEDIT 4 /* unedit a file */ 1511431Seric # define SHELL 5 /* call a shell file (like PROG) */ 1521433Seric # define DIFFS 6 /* diff between sccs & file out */ 153200Seric 154157Seric /* bits for sccsflags */ 155200Seric # define NO_SDOT 0001 /* no s. on front of args */ 156200Seric # define REALUSER 0002 /* protected (e.g., admin) */ 157148Seric 158819Seric /* modes for the "clean", "info", "check" ops */ 159819Seric # define CLEANC 0 /* clean command */ 160819Seric # define INFOC 1 /* info command */ 161819Seric # define CHECKC 2 /* check command */ 1621730Seric # define TELLC 3 /* give list of files being edited */ 163819Seric 1641432Seric /* 1651432Seric ** Description of commands known to this program. 1661432Seric ** First argument puts the command into a class. Second arg is 1671432Seric ** info regarding treatment of this command. Third arg is a 1681432Seric ** list of flags this command accepts from macros, etc. Fourth 1691432Seric ** arg is the pathname of the implementing program, or the 1701432Seric ** macro definition, or the arg to a sub-algorithm. 1711432Seric */ 172202Seric 173148Seric struct sccsprog SccsProg[] = 174148Seric { 1751737Seric "admin", PROG, REALUSER, PROGPATH(admin), 1761737Seric "chghist", PROG, 0, PROGPATH(rmdel), 1771737Seric "comb", PROG, 0, PROGPATH(comb), 1781737Seric "delta", PROG, 0, PROGPATH(delta), 1791737Seric "get", PROG, 0, PROGPATH(get), 1801737Seric "help", PROG, NO_SDOT, PROGPATH(help), 1811737Seric "prt", PROG, 0, PROGPATH(prt), 1821737Seric "rmdel", PROG, REALUSER, PROGPATH(rmdel), 1831737Seric "what", PROG, NO_SDOT, PROGPATH(what), 1841737Seric "sccsdiff", SHELL, REALUSER, PROGPATH(sccsdiff), 1851737Seric "edit", CMACRO, NO_SDOT, "get -e", 1861821Seric "delget", CMACRO, NO_SDOT, "delta:mysrp/get:ixbeskcl -t", 1871821Seric "deledit", CMACRO, NO_SDOT, "delta:mysrp/get:ixbskcl -e -t", 1881737Seric "fix", FIX, NO_SDOT, NULL, 1891737Seric "clean", CLEAN, REALUSER, (char *) CLEANC, 1901737Seric "info", CLEAN, REALUSER, (char *) INFOC, 1911737Seric "check", CLEAN, REALUSER, (char *) CHECKC, 1921737Seric "tell", CLEAN, REALUSER, (char *) TELLC, 1931737Seric "unedit", UNEDIT, NO_SDOT, NULL, 1941737Seric "diffs", DIFFS, NO_SDOT|REALUSER, NULL, 1951737Seric NULL, -1, 0, NULL 196148Seric }; 197148Seric 1981432Seric /* one line from a p-file */ 199396Seric struct pfile 200396Seric { 201396Seric char *p_osid; /* old SID */ 202396Seric char *p_nsid; /* new SID */ 203396Seric char *p_user; /* user who did edit */ 204396Seric char *p_date; /* date of get */ 205396Seric char *p_time; /* time of get */ 206396Seric }; 207396Seric 2081270Seric char *SccsPath = SCCSPATH; /* pathname of SCCS files */ 2091270Seric # ifdef SCCSDIR 2101270Seric char *SccsDir = SCCSDIR; /* directory to begin search from */ 2111205Seric # else 2121270Seric char *SccsDir = ""; 2131205Seric # endif 2141437Seric char MyName[] = MYNAME; /* name used in messages */ 2151433Seric int OutFile = -1; /* override output file for commands */ 216157Seric bool RealUser; /* if set, running as real user */ 217393Seric # ifdef DEBUG 218393Seric bool Debug; /* turn on tracing */ 219393Seric # endif 2201432Seric 221148Seric main(argc, argv) 222148Seric int argc; 223148Seric char **argv; 224148Seric { 225148Seric register char *p; 226262Seric extern struct sccsprog *lookup(); 2271282Seric register int i; 228148Seric 229148Seric /* 230148Seric ** Detect and decode flags intended for this program. 231148Seric */ 232148Seric 233200Seric if (argc < 2) 234148Seric { 2351205Seric fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName); 236200Seric exit(EX_USAGE); 237200Seric } 238200Seric argv[argc] = NULL; 239200Seric 240262Seric if (lookup(argv[0]) == NULL) 241200Seric { 242262Seric while ((p = *++argv) != NULL) 243148Seric { 244262Seric if (*p != '-') 245262Seric break; 246262Seric switch (*++p) 247262Seric { 248262Seric case 'r': /* run as real user */ 249262Seric setuid(getuid()); 250262Seric RealUser++; 251262Seric break; 252148Seric 2531270Seric # ifndef SCCSDIR 254262Seric case 'p': /* path of sccs files */ 255262Seric SccsPath = ++p; 256262Seric break; 257148Seric 258588Seric case 'd': /* directory to search from */ 259588Seric SccsDir = ++p; 260588Seric break; 2611205Seric # endif 262588Seric 263393Seric # ifdef DEBUG 264393Seric case 'T': /* trace */ 265393Seric Debug++; 266393Seric break; 267393Seric # endif 268393Seric 269262Seric default: 2701205Seric usrerr("unknown option -%s", p); 271262Seric break; 272262Seric } 273148Seric } 274262Seric if (SccsPath[0] == '\0') 275262Seric SccsPath = "."; 276148Seric } 277148Seric 2781737Seric i = command(argv, FALSE, ""); 2791282Seric exit(i); 280200Seric } 2811432Seric 2821432Seric /* 2831282Seric ** COMMAND -- look up and perform a command 2841282Seric ** 2851282Seric ** This routine is the guts of this program. Given an 2861282Seric ** argument vector, it looks up the "command" (argv[0]) 2871282Seric ** in the configuration table and does the necessary stuff. 2881282Seric ** 2891282Seric ** Parameters: 2901282Seric ** argv -- an argument vector to process. 2911282Seric ** forkflag -- if set, fork before executing the command. 2921316Seric ** editflag -- if set, only include flags listed in the 2931316Seric ** sccsklets field of the command descriptor. 2941316Seric ** arg0 -- a space-seperated list of arguments to insert 2951316Seric ** before argv. 2961282Seric ** 2971282Seric ** Returns: 2981282Seric ** zero -- command executed ok. 2991282Seric ** else -- error status. 3001282Seric ** 3011282Seric ** Side Effects: 3021282Seric ** none. 3031282Seric */ 304157Seric 3051737Seric command(argv, forkflag, arg0) 306200Seric char **argv; 307201Seric bool forkflag; 3081316Seric char *arg0; 309200Seric { 310200Seric register struct sccsprog *cmd; 311200Seric register char *p; 312201Seric char buf[40]; 313262Seric extern struct sccsprog *lookup(); 3141316Seric char *nav[1000]; 3151316Seric char **np; 3161431Seric register char **ap; 317585Seric register int i; 3181431Seric register char *q; 319585Seric extern bool unedit(); 3201282Seric int rval = 0; 3211316Seric extern char *index(); 3221316Seric extern char *makefile(); 3231737Seric char *editchs; 3241435Seric extern char *tail(); 325200Seric 326393Seric # ifdef DEBUG 327393Seric if (Debug) 328393Seric { 3291316Seric printf("command:\n\t\"%s\"\n", arg0); 3301316Seric for (np = argv; *np != NULL; np++) 3311316Seric printf("\t\"%s\"\n", *np); 332393Seric } 333393Seric # endif 334393Seric 335157Seric /* 3361316Seric ** Copy arguments. 3371438Seric ** Copy from arg0 & if necessary at most one arg 3381438Seric ** from argv[0]. 3391316Seric */ 3401316Seric 3411431Seric np = ap = &nav[1]; 3421737Seric editchs = NULL; 3431821Seric for (p = arg0, q = buf; *p != '\0' && *p != '/'; ) 3441316Seric { 3451316Seric *np++ = q; 3461316Seric while (*p == ' ') 3471316Seric p++; 3481737Seric while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':') 3491316Seric *q++ = *p++; 3501316Seric *q++ = '\0'; 3511737Seric if (*p == ':') 3521737Seric { 3531737Seric editchs = q; 3541821Seric while (*++p != '\0' && *p != '/' && *p != ' ') 3551737Seric *q++ = *p; 3561737Seric *q++ = '\0'; 3571737Seric } 3581316Seric } 3591316Seric *np = NULL; 3601431Seric if (*ap == NULL) 3611316Seric *np++ = *argv++; 3621316Seric 3631316Seric /* 364148Seric ** Look up command. 3651431Seric ** At this point, *ap is the command name. 366148Seric */ 367148Seric 3681431Seric cmd = lookup(*ap); 369262Seric if (cmd == NULL) 370148Seric { 3711431Seric usrerr("Unknown command \"%s\"", *ap); 3721282Seric return (EX_USAGE); 373148Seric } 374148Seric 375148Seric /* 3761316Seric ** Copy remaining arguments doing editing as appropriate. 3771316Seric */ 3781316Seric 3791316Seric for (; *argv != NULL; argv++) 3801316Seric { 3811316Seric p = *argv; 3821316Seric if (*p == '-') 3831316Seric { 3841737Seric if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL) 3851316Seric *np++ = p; 3861316Seric } 3871316Seric else 3881316Seric { 3891316Seric if (!bitset(NO_SDOT, cmd->sccsflags)) 3901316Seric p = makefile(p); 3911316Seric if (p != NULL) 3921316Seric *np++ = p; 3931316Seric } 3941316Seric } 3951316Seric *np = NULL; 3961316Seric 3971316Seric /* 398200Seric ** Interpret operation associated with this command. 399157Seric */ 400157Seric 401200Seric switch (cmd->sccsoper) 402200Seric { 4031431Seric case SHELL: /* call a shell file */ 4041431Seric *ap = cmd->sccspath; 4051431Seric *--ap = "sh"; 4061431Seric rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag); 4071431Seric break; 4081431Seric 409200Seric case PROG: /* call an sccs prog */ 4101431Seric rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag); 411201Seric break; 412201Seric 413201Seric case CMACRO: /* command macro */ 4141438Seric /* step through & execute each part of the macro */ 415201Seric for (p = cmd->sccspath; *p != '\0'; p++) 416201Seric { 4171316Seric q = p; 4181316Seric while (*p != '\0' && *p != '/') 4191316Seric p++; 4201737Seric rval = command(&ap[1], *p != '\0', q); 4211282Seric if (rval != 0) 4221282Seric break; 423201Seric } 4241282Seric break; 425157Seric 426226Seric case FIX: /* fix a delta */ 4271431Seric if (strncmp(ap[1], "-r", 2) != 0) 428226Seric { 4291205Seric usrerr("-r flag needed for fix command"); 4301282Seric rval = EX_USAGE; 431226Seric break; 432226Seric } 4331438Seric 4341438Seric /* get the version with all changes */ 4351737Seric rval = command(&ap[1], TRUE, "get -k"); 4361438Seric 4371438Seric /* now remove that version from the s-file */ 4381282Seric if (rval == 0) 4391737Seric rval = command(&ap[1], TRUE, "rmdel:r"); 4401438Seric 4411438Seric /* and edit the old version (but don't clobber new vers) */ 4421282Seric if (rval == 0) 4431737Seric rval = command(&ap[2], FALSE, "get -e -g"); 4441282Seric break; 445226Seric 446261Seric case CLEAN: 447*1822Seric rval = clean((int) cmd->sccspath, ap); 448261Seric break; 449261Seric 450396Seric case UNEDIT: 4511431Seric for (argv = np = &ap[1]; *argv != NULL; argv++) 452585Seric { 4531316Seric if (unedit(*argv)) 4541316Seric *np++ = *argv; 455585Seric } 4561316Seric *np = NULL; 4571438Seric 4581438Seric /* get all the files that we unedited successfully */ 4591738Seric if (np > &ap[1]) 4601737Seric rval = command(&ap[1], FALSE, "get"); 461396Seric break; 462396Seric 4631433Seric case DIFFS: /* diff between s-file & edit file */ 4641433Seric /* find the end of the flag arguments */ 4651433Seric for (np = &ap[1]; *np != NULL && **np == '-'; np++) 4661433Seric continue; 4671433Seric argv = np; 4681433Seric 4691433Seric /* for each file, do the diff */ 4701502Seric p = argv[1]; 4711433Seric while (*np != NULL) 4721433Seric { 4731438Seric /* messy, but we need a null terminated argv */ 4741433Seric *argv = *np++; 4751502Seric argv[1] = NULL; 4761435Seric i = dodiff(ap, tail(*argv)); 4771433Seric if (rval == 0) 4781433Seric rval = i; 4791502Seric argv[1] = p; 4801433Seric } 4811433Seric break; 4821433Seric 483200Seric default: 4841205Seric syserr("oper %d", cmd->sccsoper); 485200Seric exit(EX_SOFTWARE); 486200Seric } 4871282Seric # ifdef DEBUG 4881282Seric if (Debug) 4891282Seric printf("command: rval=%d\n", rval); 4901282Seric # endif 4911282Seric return (rval); 492200Seric } 4931432Seric 4941432Seric /* 495262Seric ** LOOKUP -- look up an SCCS command name. 496262Seric ** 497262Seric ** Parameters: 498262Seric ** name -- the name of the command to look up. 499262Seric ** 500262Seric ** Returns: 501262Seric ** ptr to command descriptor for this command. 502262Seric ** NULL if no such entry. 503262Seric ** 504262Seric ** Side Effects: 505262Seric ** none. 506262Seric */ 507200Seric 508262Seric struct sccsprog * 509262Seric lookup(name) 510262Seric char *name; 511262Seric { 512262Seric register struct sccsprog *cmd; 513226Seric 514262Seric for (cmd = SccsProg; cmd->sccsname != NULL; cmd++) 515262Seric { 516262Seric if (strcmp(cmd->sccsname, name) == 0) 517262Seric return (cmd); 518262Seric } 519262Seric return (NULL); 520262Seric } 5211432Seric 5221432Seric /* 5231282Seric ** CALLPROG -- call a program 5241282Seric ** 5251316Seric ** Used to call the SCCS programs. 5261282Seric ** 5271282Seric ** Parameters: 5281282Seric ** progpath -- pathname of the program to call. 5291282Seric ** flags -- status flags from the command descriptors. 5301282Seric ** argv -- an argument vector to pass to the program. 5311282Seric ** forkflag -- if true, fork before calling, else just 5321282Seric ** exec. 5331282Seric ** 5341282Seric ** Returns: 5351282Seric ** The exit status of the program. 5361282Seric ** Nothing if forkflag == FALSE. 5371282Seric ** 5381282Seric ** Side Effects: 5391282Seric ** Can exit if forkflag == FALSE. 5401282Seric */ 541226Seric 542200Seric callprog(progpath, flags, argv, forkflag) 543200Seric char *progpath; 544200Seric short flags; 545200Seric char **argv; 546200Seric bool forkflag; 547200Seric { 548200Seric register int i; 549201Seric auto int st; 550200Seric 5511316Seric # ifdef DEBUG 5521316Seric if (Debug) 5531316Seric { 5541316Seric printf("callprog:\n"); 5551316Seric for (i = 0; argv[i] != NULL; i++) 5561316Seric printf("\t\"%s\"\n", argv[i]); 5571316Seric } 5581316Seric # endif 5591316Seric 560200Seric if (*argv == NULL) 561200Seric return (-1); 562200Seric 563157Seric /* 564226Seric ** Fork if appropriate. 565148Seric */ 566148Seric 567200Seric if (forkflag) 568200Seric { 569393Seric # ifdef DEBUG 570393Seric if (Debug) 571393Seric printf("Forking\n"); 572393Seric # endif 573200Seric i = fork(); 574200Seric if (i < 0) 575200Seric { 5761205Seric syserr("cannot fork"); 577200Seric exit(EX_OSERR); 578200Seric } 579200Seric else if (i > 0) 580201Seric { 581201Seric wait(&st); 5821282Seric if ((st & 0377) == 0) 5831282Seric st = (st >> 8) & 0377; 5841433Seric if (OutFile >= 0) 5851433Seric { 5861433Seric close(OutFile); 5871433Seric OutFile = -1; 5881433Seric } 589201Seric return (st); 590201Seric } 591200Seric } 5921433Seric else if (OutFile >= 0) 5931433Seric { 5941433Seric syserr("callprog: setting stdout w/o forking"); 5951433Seric exit(EX_SOFTWARE); 5961433Seric } 597200Seric 5981433Seric /* set protection as appropriate */ 599200Seric if (bitset(REALUSER, flags)) 600200Seric setuid(getuid()); 6011433Seric 6021433Seric /* change standard input & output if needed */ 6031433Seric if (OutFile >= 0) 6041433Seric { 6051433Seric close(1); 6061433Seric dup(OutFile); 6071433Seric close(OutFile); 6081433Seric } 609226Seric 6101433Seric /* call real SCCS program */ 611226Seric execv(progpath, argv); 6121205Seric syserr("cannot execute %s", progpath); 613148Seric exit(EX_UNAVAILABLE); 6141738Seric /*NOTREACHED*/ 615148Seric } 6161432Seric 6171432Seric /* 618586Seric ** MAKEFILE -- make filename of SCCS file 619586Seric ** 620586Seric ** If the name passed is already the name of an SCCS file, 621586Seric ** just return it. Otherwise, munge the name into the name 622586Seric ** of the actual SCCS file. 623586Seric ** 624586Seric ** There are cases when it is not clear what you want to 625586Seric ** do. For example, if SccsPath is an absolute pathname 626586Seric ** and the name given is also an absolute pathname, we go 627586Seric ** for SccsPath (& only use the last component of the name 628586Seric ** passed) -- this is important for security reasons (if 629586Seric ** sccs is being used as a setuid front end), but not 630586Seric ** particularly intuitive. 631586Seric ** 632586Seric ** Parameters: 633586Seric ** name -- the file name to be munged. 634586Seric ** 635586Seric ** Returns: 636586Seric ** The pathname of the sccs file. 637586Seric ** NULL on error. 638586Seric ** 639586Seric ** Side Effects: 640586Seric ** none. 641586Seric */ 642148Seric 643148Seric char * 644148Seric makefile(name) 645148Seric char *name; 646148Seric { 647148Seric register char *p; 648148Seric char buf[512]; 649148Seric extern char *malloc(); 650586Seric extern char *rindex(); 651588Seric extern bool safepath(); 652587Seric extern bool isdir(); 653587Seric register char *q; 654148Seric 655586Seric p = rindex(name, '/'); 656586Seric if (p == NULL) 657586Seric p = name; 658586Seric else 659586Seric p++; 660586Seric 661148Seric /* 662588Seric ** Check to see that the path is "safe", i.e., that we 663588Seric ** are not letting some nasty person use the setuid part 664588Seric ** of this program to look at or munge some presumably 665588Seric ** hidden files. 666148Seric */ 667148Seric 668588Seric if (SccsDir[0] == '/' && !safepath(name)) 669588Seric return (NULL); 670586Seric 671586Seric /* 672588Seric ** Create the base pathname. 673586Seric */ 674586Seric 6751438Seric /* first the directory part */ 676588Seric if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0) 677148Seric { 678588Seric strcpy(buf, SccsDir); 679586Seric strcat(buf, "/"); 680586Seric } 681586Seric else 682586Seric strcpy(buf, ""); 6831438Seric 6841438Seric /* then the head of the pathname */ 685587Seric strncat(buf, name, p - name); 686587Seric q = &buf[strlen(buf)]; 6871438Seric 6881438Seric /* now copy the final part of the name, in case useful */ 689587Seric strcpy(q, p); 6901438Seric 6911438Seric /* so is it useful? */ 692587Seric if (strncmp(p, "s.", 2) != 0 && !isdir(buf)) 693586Seric { 6941438Seric /* sorry, no; copy the SCCS pathname & the "s." */ 695588Seric strcpy(q, SccsPath); 696588Seric strcat(buf, "/s."); 6971438Seric 6981438Seric /* and now the end of the name */ 699586Seric strcat(buf, p); 700586Seric } 701148Seric 7021438Seric /* if i haven't changed it, why did I do all this? */ 703588Seric if (strcmp(buf, name) == 0) 704588Seric p = name; 705588Seric else 706148Seric { 7071438Seric /* but if I have, squirrel it away */ 708588Seric p = malloc(strlen(buf) + 1); 709588Seric if (p == NULL) 710588Seric { 711588Seric perror("Sccs: no mem"); 712588Seric exit(EX_OSERR); 713588Seric } 714588Seric strcpy(p, buf); 715148Seric } 7161438Seric 717148Seric return (p); 718148Seric } 7191432Seric 7201432Seric /* 721587Seric ** ISDIR -- return true if the argument is a directory. 722587Seric ** 723587Seric ** Parameters: 724587Seric ** name -- the pathname of the file to check. 725587Seric ** 726587Seric ** Returns: 727587Seric ** TRUE if 'name' is a directory, FALSE otherwise. 728587Seric ** 729587Seric ** Side Effects: 730587Seric ** none. 731587Seric */ 732587Seric 733587Seric bool 734587Seric isdir(name) 735587Seric char *name; 736587Seric { 737587Seric struct stat stbuf; 738587Seric 739587Seric return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR); 740587Seric } 7411432Seric 7421432Seric /* 743586Seric ** SAFEPATH -- determine whether a pathname is "safe" 744586Seric ** 745586Seric ** "Safe" pathnames only allow you to get deeper into the 746586Seric ** directory structure, i.e., full pathnames and ".." are 747586Seric ** not allowed. 748586Seric ** 749586Seric ** Parameters: 750586Seric ** p -- the name to check. 751586Seric ** 752586Seric ** Returns: 753586Seric ** TRUE -- if the path is safe. 754586Seric ** FALSE -- if the path is not safe. 755586Seric ** 756586Seric ** Side Effects: 757586Seric ** Prints a message if the path is not safe. 758586Seric */ 759586Seric 760586Seric bool 761586Seric safepath(p) 762586Seric register char *p; 763586Seric { 764586Seric extern char *index(); 765586Seric 766586Seric if (*p != '/') 767586Seric { 768586Seric while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0) 769586Seric { 770586Seric p = index(p, '/'); 771586Seric if (p == NULL) 772586Seric return (TRUE); 773586Seric p++; 774586Seric } 775586Seric } 776586Seric 777586Seric printf("You may not use full pathnames or \"..\"\n"); 778586Seric return (FALSE); 779586Seric } 7801432Seric 7811432Seric /* 782261Seric ** CLEAN -- clean out recreatable files 783261Seric ** 784261Seric ** Any file for which an "s." file exists but no "p." file 785261Seric ** exists in the current directory is purged. 786261Seric ** 787261Seric ** Parameters: 788*1822Seric ** mode -- tells whether this came from a "clean", "info", or 789*1822Seric ** "check" command. 790*1822Seric ** argv -- the rest of the argument vector. 791261Seric ** 792261Seric ** Returns: 793261Seric ** none. 794261Seric ** 795261Seric ** Side Effects: 796819Seric ** Removes files in the current directory. 797819Seric ** Prints information regarding files being edited. 798819Seric ** Exits if a "check" command. 799261Seric */ 800261Seric 801*1822Seric clean(mode, argv) 802819Seric int mode; 803*1822Seric char **argv; 804261Seric { 805261Seric struct direct dir; 806261Seric char buf[100]; 807346Seric register FILE *dirfd; 808346Seric register char *basefile; 809351Seric bool gotedit; 810*1822Seric bool gotpfent; 811394Seric FILE *pfp; 812*1822Seric bool nobranch = FALSE; 813*1822Seric extern struct pfile *getpfent(); 814*1822Seric register struct pfile *pf; 815*1822Seric register char **ap; 816261Seric 8171438Seric /* 818*1822Seric ** Process the argv 819*1822Seric */ 820*1822Seric 821*1822Seric for (ap = argv; *ap != NULL; ap++) 822*1822Seric { 823*1822Seric if (strcmp(*ap, "-b") == 0) 824*1822Seric nobranch = TRUE; 825*1822Seric } 826*1822Seric 827*1822Seric /* 8281438Seric ** Find and open the SCCS directory. 8291438Seric */ 8301438Seric 8311207Seric strcpy(buf, SccsDir); 8321207Seric if (buf[0] != '\0') 8331207Seric strcat(buf, "/"); 8341207Seric strcat(buf, SccsPath); 8351438Seric 8361207Seric dirfd = fopen(buf, "r"); 837261Seric if (dirfd == NULL) 838261Seric { 8391207Seric usrerr("cannot open %s", buf); 8401282Seric return (EX_NOINPUT); 841261Seric } 842261Seric 843261Seric /* 844261Seric ** Scan the SCCS directory looking for s. files. 8451438Seric ** gotedit tells whether we have tried to clean any 8461438Seric ** files that are being edited. 847261Seric */ 848261Seric 849351Seric gotedit = FALSE; 8501738Seric while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL) 851261Seric { 852568Seric if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0) 853261Seric continue; 854261Seric 855261Seric /* got an s. file -- see if the p. file exists */ 8561207Seric strcpy(buf, SccsDir); 8571207Seric if (buf[0] != '\0') 8581207Seric strcat(buf, "/"); 8591207Seric strcat(buf, SccsPath); 860261Seric strcat(buf, "/p."); 861346Seric basefile = &buf[strlen(buf)]; 862568Seric strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2); 863346Seric basefile[sizeof dir.d_name - 2] = '\0'; 864*1822Seric 865*1822Seric /* 866*1822Seric ** open and scan the p-file. 867*1822Seric ** 'gotpfent' tells if we have found a valid p-file 868*1822Seric ** entry. 869*1822Seric */ 870*1822Seric 871394Seric pfp = fopen(buf, "r"); 872*1822Seric gotpfent = FALSE; 873394Seric if (pfp != NULL) 874346Seric { 8751438Seric /* the file exists -- report it's contents */ 876*1822Seric while ((pf = getpfent(pfp)) != NULL) 8771730Seric { 878*1822Seric if (nobranch && isbranch(pf->p_nsid)) 879*1822Seric continue; 880*1822Seric gotedit = TRUE; 881*1822Seric gotpfent = TRUE; 882*1822Seric if (mode == TELLC) 883*1822Seric { 884*1822Seric printf("%s\n", basefile); 885*1822Seric break; 886*1822Seric } 887*1822Seric printf("%12s: being edited: %s %s %s %s %s\n", 888*1822Seric basefile, pf->p_osid, pf->p_nsid, 889*1822Seric pf->p_user, pf->p_date, pf->p_time); 8901730Seric } 891394Seric fclose(pfp); 892*1822Seric } 893*1822Seric if (!gotpfent) 894261Seric continue; 895261Seric 896261Seric /* the s. file exists and no p. file exists -- unlink the g-file */ 897819Seric if (mode == CLEANC) 898346Seric { 899568Seric strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2); 900346Seric buf[sizeof dir.d_name - 2] = '\0'; 901346Seric unlink(buf); 902346Seric } 903261Seric } 904261Seric 9051438Seric /* cleanup & report results */ 906261Seric fclose(dirfd); 907819Seric if (!gotedit && mode == INFOC) 908416Seric printf("Nothing being edited\n"); 909819Seric if (mode == CHECKC) 910819Seric exit(gotedit); 9111282Seric return (EX_OK); 912261Seric } 9131432Seric 9141432Seric /* 915*1822Seric ** ISBRANCH -- is the SID a branch? 916*1822Seric ** 917*1822Seric ** Parameters: 918*1822Seric ** sid -- the sid to check. 919*1822Seric ** 920*1822Seric ** Returns: 921*1822Seric ** TRUE if the sid represents a branch. 922*1822Seric ** FALSE otherwise. 923*1822Seric ** 924*1822Seric ** Side Effects: 925*1822Seric ** none. 926*1822Seric */ 927*1822Seric 928*1822Seric isbranch(sid) 929*1822Seric char *sid; 930*1822Seric { 931*1822Seric register char *p; 932*1822Seric int dots; 933*1822Seric 934*1822Seric dots = 0; 935*1822Seric for (p = sid; *p != '\0'; p++) 936*1822Seric { 937*1822Seric if (*p == '.') 938*1822Seric dots++; 939*1822Seric if (dots > 1) 940*1822Seric return (TRUE); 941*1822Seric } 942*1822Seric return (FALSE); 943*1822Seric } 944*1822Seric 945*1822Seric /* 946396Seric ** UNEDIT -- unedit a file 947396Seric ** 948396Seric ** Checks to see that the current user is actually editting 949396Seric ** the file and arranges that s/he is not editting it. 950396Seric ** 951396Seric ** Parameters: 952416Seric ** fn -- the name of the file to be unedited. 953396Seric ** 954396Seric ** Returns: 955585Seric ** TRUE -- if the file was successfully unedited. 956585Seric ** FALSE -- if the file was not unedited for some 957585Seric ** reason. 958396Seric ** 959396Seric ** Side Effects: 960396Seric ** fn is removed 961396Seric ** entries are removed from pfile. 962396Seric */ 963396Seric 964585Seric bool 965396Seric unedit(fn) 966396Seric char *fn; 967396Seric { 968396Seric register FILE *pfp; 969396Seric char *pfn; 970396Seric static char tfn[] = "/tmp/sccsXXXXX"; 971396Seric FILE *tfp; 972396Seric register char *q; 973396Seric bool delete = FALSE; 974396Seric bool others = FALSE; 975396Seric char *myname; 976396Seric extern char *getlogin(); 977396Seric struct pfile *pent; 978*1822Seric extern struct pfile *getpfent(); 979396Seric char buf[120]; 9801316Seric extern char *makefile(); 981828Seric # ifdef UIDUSER 982828Seric struct passwd *pw; 983828Seric extern struct passwd *getpwuid(); 984828Seric # endif UIDUSER 985396Seric 986396Seric /* make "s." filename & find the trailing component */ 987396Seric pfn = makefile(fn); 988586Seric if (pfn == NULL) 989586Seric return (FALSE); 990586Seric q = rindex(pfn, '/'); 991586Seric if (q == NULL) 992586Seric q = &pfn[-1]; 993586Seric if (q[1] != 's' || q[2] != '.') 994396Seric { 9951205Seric usrerr("bad file name \"%s\"", fn); 996585Seric return (FALSE); 997396Seric } 998396Seric 9991438Seric /* turn "s." into "p." & try to open it */ 1000396Seric *++q = 'p'; 1001396Seric 1002396Seric pfp = fopen(pfn, "r"); 1003396Seric if (pfp == NULL) 1004396Seric { 1005416Seric printf("%12s: not being edited\n", fn); 1006585Seric return (FALSE); 1007396Seric } 1008396Seric 10091438Seric /* create temp file for editing p-file */ 1010396Seric mktemp(tfn); 1011396Seric tfp = fopen(tfn, "w"); 1012396Seric if (tfp == NULL) 1013396Seric { 10141205Seric usrerr("cannot create \"%s\"", tfn); 1015396Seric exit(EX_OSERR); 1016396Seric } 1017396Seric 10181438Seric /* figure out who I am */ 1019828Seric # ifdef UIDUSER 1020828Seric pw = getpwuid(getuid()); 1021828Seric if (pw == NULL) 1022828Seric { 10231205Seric syserr("who are you? (uid=%d)", getuid()); 1024828Seric exit(EX_OSERR); 1025828Seric } 1026828Seric myname = pw->pw_name; 1027828Seric # else 1028396Seric myname = getlogin(); 1029828Seric # endif UIDUSER 10301438Seric 10311438Seric /* 10321438Seric ** Copy p-file to temp file, doing deletions as needed. 10331438Seric */ 10341438Seric 1035*1822Seric while ((pent = getpfent(pfp)) != NULL) 1036396Seric { 1037396Seric if (strcmp(pent->p_user, myname) == 0) 1038396Seric { 1039396Seric /* a match */ 1040396Seric delete++; 1041396Seric } 1042396Seric else 1043396Seric { 10441438Seric /* output it again */ 1045396Seric fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid, 1046396Seric pent->p_nsid, pent->p_user, pent->p_date, 1047396Seric pent->p_time); 1048396Seric others++; 1049396Seric } 1050396Seric } 1051396Seric 1052396Seric /* do final cleanup */ 1053396Seric if (others) 1054396Seric { 10551438Seric /* copy it back (perhaps it should be linked?) */ 1056396Seric if (freopen(tfn, "r", tfp) == NULL) 1057396Seric { 10581205Seric syserr("cannot reopen \"%s\"", tfn); 1059396Seric exit(EX_OSERR); 1060396Seric } 1061396Seric if (freopen(pfn, "w", pfp) == NULL) 1062396Seric { 10631205Seric usrerr("cannot create \"%s\"", pfn); 1064585Seric return (FALSE); 1065396Seric } 1066396Seric while (fgets(buf, sizeof buf, tfp) != NULL) 1067396Seric fputs(buf, pfp); 1068396Seric } 1069396Seric else 1070396Seric { 10711438Seric /* it's empty -- remove it */ 1072396Seric unlink(pfn); 1073396Seric } 1074396Seric fclose(tfp); 1075396Seric fclose(pfp); 1076396Seric unlink(tfn); 1077396Seric 10781438Seric /* actually remove the g-file */ 1079396Seric if (delete) 1080396Seric { 10811435Seric unlink(tail(fn)); 10821435Seric printf("%12s: removed\n", tail(fn)); 1083585Seric return (TRUE); 1084396Seric } 1085396Seric else 1086396Seric { 1087416Seric printf("%12s: not being edited by you\n", fn); 1088585Seric return (FALSE); 1089396Seric } 1090396Seric } 10911432Seric 10921432Seric /* 10931433Seric ** DODIFF -- diff an s-file against a g-file 10941433Seric ** 10951433Seric ** Parameters: 10961433Seric ** getv -- argv for the 'get' command. 10971433Seric ** gfile -- name of the g-file to diff against. 10981433Seric ** 10991433Seric ** Returns: 11001433Seric ** Result of get. 11011433Seric ** 11021433Seric ** Side Effects: 11031433Seric ** none. 11041433Seric */ 11051433Seric 11061433Seric dodiff(getv, gfile) 11071433Seric char **getv; 11081433Seric char *gfile; 11091433Seric { 11101433Seric int pipev[2]; 11111433Seric int rval; 11121433Seric register int i; 11131433Seric register int pid; 11141433Seric auto int st; 11151433Seric extern int errno; 11161433Seric int (*osig)(); 11171433Seric 11181501Seric printf("\n------- %s -------\n", gfile); 11191501Seric 11201438Seric /* create context for diff to run in */ 11211433Seric if (pipe(pipev) < 0) 11221433Seric { 11231433Seric syserr("dodiff: pipe failed"); 11241433Seric exit(EX_OSERR); 11251433Seric } 11261433Seric if ((pid = fork()) < 0) 11271433Seric { 11281433Seric syserr("dodiff: fork failed"); 11291433Seric exit(EX_OSERR); 11301433Seric } 11311433Seric else if (pid > 0) 11321433Seric { 11331433Seric /* in parent; run get */ 11341433Seric OutFile = pipev[1]; 11351433Seric close(pipev[0]); 11361737Seric rval = command(&getv[1], TRUE, "get -s -k -p"); 11371433Seric osig = signal(SIGINT, SIG_IGN); 11381433Seric while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR) 11391433Seric errno = 0; 11401433Seric signal(SIGINT, osig); 11411433Seric /* ignore result of diff */ 11421433Seric } 11431433Seric else 11441433Seric { 11451433Seric /* in child, run diff */ 11461433Seric if (close(pipev[1]) < 0 || close(0) < 0 || 11471433Seric dup(pipev[0]) != 0 || close(pipev[0]) < 0) 11481433Seric { 11491433Seric syserr("dodiff: magic failed"); 11501433Seric exit(EX_OSERR); 11511433Seric } 11521433Seric execl(PROGPATH(bdiff), "bdiff", "-", gfile, NULL); 11531433Seric # ifndef V6 11541433Seric execlp("bdiff", "bdiff", "-", gfile, NULL); 11551433Seric execlp("diff", "diff", "-", gfile, NULL); 11561433Seric # endif NOT V6 11571433Seric syserr("bdiff: cannot execute"); 11581433Seric exit(EX_OSERR); 11591433Seric } 11601433Seric return (rval); 11611433Seric } 11621433Seric 11631433Seric /* 11641435Seric ** TAIL -- return tail of filename. 11651435Seric ** 11661435Seric ** Parameters: 11671435Seric ** fn -- the filename. 11681435Seric ** 11691435Seric ** Returns: 11701435Seric ** a pointer to the tail of the filename; e.g., given 11711435Seric ** "cmd/ls.c", "ls.c" is returned. 11721435Seric ** 11731435Seric ** Side Effects: 11741435Seric ** none. 11751435Seric */ 11761435Seric 11771435Seric char * 11781435Seric tail(fn) 11791435Seric register char *fn; 11801435Seric { 11811435Seric register char *p; 11821435Seric 11831435Seric for (p = fn; *p != 0; p++) 11841435Seric if (*p == '/' && p[1] != '\0' && p[1] != '/') 11851435Seric fn = &p[1]; 11861435Seric return (fn); 11871435Seric } 11881435Seric 11891435Seric /* 1190*1822Seric ** GETPFENT -- get an entry from the p-file 1191396Seric ** 1192396Seric ** Parameters: 1193396Seric ** pfp -- p-file file pointer 1194396Seric ** 1195396Seric ** Returns: 1196396Seric ** pointer to p-file struct for next entry 1197396Seric ** NULL on EOF or error 1198396Seric ** 1199396Seric ** Side Effects: 1200396Seric ** Each call wipes out results of previous call. 1201396Seric */ 1202396Seric 1203396Seric struct pfile * 1204*1822Seric getpfent(pfp) 1205396Seric FILE *pfp; 1206396Seric { 1207396Seric static struct pfile ent; 1208396Seric static char buf[120]; 1209396Seric register char *p; 1210396Seric extern char *nextfield(); 1211396Seric 1212396Seric if (fgets(buf, sizeof buf, pfp) == NULL) 1213396Seric return (NULL); 1214396Seric 1215396Seric ent.p_osid = p = buf; 1216396Seric ent.p_nsid = p = nextfield(p); 1217396Seric ent.p_user = p = nextfield(p); 1218396Seric ent.p_date = p = nextfield(p); 1219396Seric ent.p_time = p = nextfield(p); 1220396Seric if (p == NULL || nextfield(p) != NULL) 1221396Seric return (NULL); 1222396Seric 1223396Seric return (&ent); 1224396Seric } 1225396Seric 1226396Seric 1227396Seric char * 1228396Seric nextfield(p) 1229396Seric register char *p; 1230396Seric { 1231396Seric if (p == NULL || *p == '\0') 1232396Seric return (NULL); 1233396Seric while (*p != ' ' && *p != '\n' && *p != '\0') 1234396Seric p++; 1235396Seric if (*p == '\n' || *p == '\0') 1236396Seric { 1237396Seric *p = '\0'; 1238396Seric return (NULL); 1239396Seric } 1240396Seric *p++ = '\0'; 1241396Seric return (p); 1242396Seric } 12431432Seric 12441432Seric /* 12451205Seric ** USRERR -- issue user-level error 12461205Seric ** 12471205Seric ** Parameters: 12481205Seric ** f -- format string. 12491205Seric ** p1-p3 -- parameters to a printf. 12501205Seric ** 12511205Seric ** Returns: 12521205Seric ** -1 12531205Seric ** 12541205Seric ** Side Effects: 12551205Seric ** none. 12561205Seric */ 12571205Seric 12581738Seric /*VARARGS1*/ 12591205Seric usrerr(f, p1, p2, p3) 12601205Seric char *f; 12611205Seric { 12621205Seric fprintf(stderr, "\n%s: ", MyName); 12631205Seric fprintf(stderr, f, p1, p2, p3); 12641205Seric fprintf(stderr, "\n"); 12651205Seric 12661205Seric return (-1); 12671205Seric } 12681432Seric 12691432Seric /* 12701205Seric ** SYSERR -- print system-generated error. 12711205Seric ** 12721205Seric ** Parameters: 12731205Seric ** f -- format string to a printf. 12741205Seric ** p1, p2, p3 -- parameters to f. 12751205Seric ** 12761205Seric ** Returns: 12771205Seric ** never. 12781205Seric ** 12791205Seric ** Side Effects: 12801205Seric ** none. 12811205Seric */ 12821205Seric 12831738Seric /*VARARGS1*/ 12841205Seric syserr(f, p1, p2, p3) 12851205Seric char *f; 12861205Seric { 12871205Seric extern int errno; 12881205Seric 12891205Seric fprintf(stderr, "\n%s SYSERR: ", MyName); 12901205Seric fprintf(stderr, f, p1, p2, p3); 12911205Seric fprintf(stderr, "\n"); 12921205Seric if (errno == 0) 12931205Seric exit(EX_SOFTWARE); 12941205Seric else 12951205Seric { 12961738Seric perror(NULL); 12971205Seric exit(EX_OSERR); 12981205Seric } 12991205Seric } 1300