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*1737Seric static char SccsId[] = "@(#)sccs.c 1.45 11/05/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 { 175*1737Seric "admin", PROG, REALUSER, PROGPATH(admin), 176*1737Seric "chghist", PROG, 0, PROGPATH(rmdel), 177*1737Seric "comb", PROG, 0, PROGPATH(comb), 178*1737Seric "delta", PROG, 0, PROGPATH(delta), 179*1737Seric "get", PROG, 0, PROGPATH(get), 180*1737Seric "help", PROG, NO_SDOT, PROGPATH(help), 181*1737Seric "prt", PROG, 0, PROGPATH(prt), 182*1737Seric "rmdel", PROG, REALUSER, PROGPATH(rmdel), 183*1737Seric "what", PROG, NO_SDOT, PROGPATH(what), 184*1737Seric "sccsdiff", SHELL, REALUSER, PROGPATH(sccsdiff), 185*1737Seric "edit", CMACRO, NO_SDOT, "get -e", 186*1737Seric "delget", CMACRO, NO_SDOT, "delta:mysrp/get:ixbeskecl -t", 187*1737Seric "deledit", CMACRO, NO_SDOT, "delta:mysrp/get:ixbeskecl -e -t", 188*1737Seric "fix", FIX, NO_SDOT, NULL, 189*1737Seric "clean", CLEAN, REALUSER, (char *) CLEANC, 190*1737Seric "info", CLEAN, REALUSER, (char *) INFOC, 191*1737Seric "check", CLEAN, REALUSER, (char *) CHECKC, 192*1737Seric "tell", CLEAN, REALUSER, (char *) TELLC, 193*1737Seric "unedit", UNEDIT, NO_SDOT, NULL, 194*1737Seric "diffs", DIFFS, NO_SDOT|REALUSER, NULL, 195*1737Seric 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 278*1737Seric 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 305*1737Seric 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(); 323*1737Seric 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]; 342*1737Seric editchs = NULL; 343*1737Seric for (p = arg0, q = buf; *p != '\0' && *p != '/' && *p != ':'; ) 3441316Seric { 3451316Seric *np++ = q; 3461316Seric while (*p == ' ') 3471316Seric p++; 348*1737Seric while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':') 3491316Seric *q++ = *p++; 3501316Seric *q++ = '\0'; 351*1737Seric if (*p == ':') 352*1737Seric { 353*1737Seric editchs = q; 354*1737Seric while (*++p != '\0' && *p != '/') 355*1737Seric *q++ = *p; 356*1737Seric *q++ = '\0'; 357*1737Seric } 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 { 384*1737Seric 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++; 420*1737Seric 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 */ 435*1737Seric rval = command(&ap[1], TRUE, "get -k"); 4361438Seric 4371438Seric /* now remove that version from the s-file */ 4381282Seric if (rval == 0) 439*1737Seric rval = command(&ap[1], TRUE, "rmdel:r"); 4401438Seric 4411438Seric /* and edit the old version (but don't clobber new vers) */ 4421282Seric if (rval == 0) 443*1737Seric rval = command(&ap[2], FALSE, "get -e -g"); 4441282Seric break; 445226Seric 446261Seric case CLEAN: 4471282Seric rval = clean((int) cmd->sccspath); 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 */ 459585Seric if (i > 0) 460*1737Seric 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); 614148Seric } 6151432Seric 6161432Seric /* 617586Seric ** MAKEFILE -- make filename of SCCS file 618586Seric ** 619586Seric ** If the name passed is already the name of an SCCS file, 620586Seric ** just return it. Otherwise, munge the name into the name 621586Seric ** of the actual SCCS file. 622586Seric ** 623586Seric ** There are cases when it is not clear what you want to 624586Seric ** do. For example, if SccsPath is an absolute pathname 625586Seric ** and the name given is also an absolute pathname, we go 626586Seric ** for SccsPath (& only use the last component of the name 627586Seric ** passed) -- this is important for security reasons (if 628586Seric ** sccs is being used as a setuid front end), but not 629586Seric ** particularly intuitive. 630586Seric ** 631586Seric ** Parameters: 632586Seric ** name -- the file name to be munged. 633586Seric ** 634586Seric ** Returns: 635586Seric ** The pathname of the sccs file. 636586Seric ** NULL on error. 637586Seric ** 638586Seric ** Side Effects: 639586Seric ** none. 640586Seric */ 641148Seric 642148Seric char * 643148Seric makefile(name) 644148Seric char *name; 645148Seric { 646148Seric register char *p; 647148Seric register char c; 648148Seric char buf[512]; 649588Seric struct stat stbuf; 650148Seric extern char *malloc(); 651586Seric extern char *rindex(); 652588Seric extern bool safepath(); 653587Seric extern bool isdir(); 654587Seric register char *q; 655148Seric 656586Seric p = rindex(name, '/'); 657586Seric if (p == NULL) 658586Seric p = name; 659586Seric else 660586Seric p++; 661586Seric 662148Seric /* 663588Seric ** Check to see that the path is "safe", i.e., that we 664588Seric ** are not letting some nasty person use the setuid part 665588Seric ** of this program to look at or munge some presumably 666588Seric ** hidden files. 667148Seric */ 668148Seric 669588Seric if (SccsDir[0] == '/' && !safepath(name)) 670588Seric return (NULL); 671586Seric 672586Seric /* 673588Seric ** Create the base pathname. 674586Seric */ 675586Seric 6761438Seric /* first the directory part */ 677588Seric if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0) 678148Seric { 679588Seric strcpy(buf, SccsDir); 680586Seric strcat(buf, "/"); 681586Seric } 682586Seric else 683586Seric strcpy(buf, ""); 6841438Seric 6851438Seric /* then the head of the pathname */ 686587Seric strncat(buf, name, p - name); 687587Seric q = &buf[strlen(buf)]; 6881438Seric 6891438Seric /* now copy the final part of the name, in case useful */ 690587Seric strcpy(q, p); 6911438Seric 6921438Seric /* so is it useful? */ 693587Seric if (strncmp(p, "s.", 2) != 0 && !isdir(buf)) 694586Seric { 6951438Seric /* sorry, no; copy the SCCS pathname & the "s." */ 696588Seric strcpy(q, SccsPath); 697588Seric strcat(buf, "/s."); 6981438Seric 6991438Seric /* and now the end of the name */ 700586Seric strcat(buf, p); 701586Seric } 702148Seric 7031438Seric /* if i haven't changed it, why did I do all this? */ 704588Seric if (strcmp(buf, name) == 0) 705588Seric p = name; 706588Seric else 707148Seric { 7081438Seric /* but if I have, squirrel it away */ 709588Seric p = malloc(strlen(buf) + 1); 710588Seric if (p == NULL) 711588Seric { 712588Seric perror("Sccs: no mem"); 713588Seric exit(EX_OSERR); 714588Seric } 715588Seric strcpy(p, buf); 716148Seric } 7171438Seric 718148Seric return (p); 719148Seric } 7201432Seric 7211432Seric /* 722587Seric ** ISDIR -- return true if the argument is a directory. 723587Seric ** 724587Seric ** Parameters: 725587Seric ** name -- the pathname of the file to check. 726587Seric ** 727587Seric ** Returns: 728587Seric ** TRUE if 'name' is a directory, FALSE otherwise. 729587Seric ** 730587Seric ** Side Effects: 731587Seric ** none. 732587Seric */ 733587Seric 734587Seric bool 735587Seric isdir(name) 736587Seric char *name; 737587Seric { 738587Seric struct stat stbuf; 739587Seric 740587Seric return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR); 741587Seric } 7421432Seric 7431432Seric /* 744586Seric ** SAFEPATH -- determine whether a pathname is "safe" 745586Seric ** 746586Seric ** "Safe" pathnames only allow you to get deeper into the 747586Seric ** directory structure, i.e., full pathnames and ".." are 748586Seric ** not allowed. 749586Seric ** 750586Seric ** Parameters: 751586Seric ** p -- the name to check. 752586Seric ** 753586Seric ** Returns: 754586Seric ** TRUE -- if the path is safe. 755586Seric ** FALSE -- if the path is not safe. 756586Seric ** 757586Seric ** Side Effects: 758586Seric ** Prints a message if the path is not safe. 759586Seric */ 760586Seric 761586Seric bool 762586Seric safepath(p) 763586Seric register char *p; 764586Seric { 765586Seric extern char *index(); 766586Seric 767586Seric if (*p != '/') 768586Seric { 769586Seric while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0) 770586Seric { 771586Seric p = index(p, '/'); 772586Seric if (p == NULL) 773586Seric return (TRUE); 774586Seric p++; 775586Seric } 776586Seric } 777586Seric 778586Seric printf("You may not use full pathnames or \"..\"\n"); 779586Seric return (FALSE); 780586Seric } 7811432Seric 7821432Seric /* 783261Seric ** CLEAN -- clean out recreatable files 784261Seric ** 785261Seric ** Any file for which an "s." file exists but no "p." file 786261Seric ** exists in the current directory is purged. 787261Seric ** 788261Seric ** Parameters: 789819Seric ** tells whether this came from a "clean", "info", or 790819Seric ** "check" command. 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 801819Seric clean(mode) 802819Seric int mode; 803261Seric { 804261Seric struct direct dir; 805261Seric struct stat stbuf; 806261Seric char buf[100]; 807394Seric char pline[120]; 808346Seric register FILE *dirfd; 809346Seric register char *basefile; 810351Seric bool gotedit; 811394Seric FILE *pfp; 812261Seric 8131438Seric /* 8141438Seric ** Find and open the SCCS directory. 8151438Seric */ 8161438Seric 8171207Seric strcpy(buf, SccsDir); 8181207Seric if (buf[0] != '\0') 8191207Seric strcat(buf, "/"); 8201207Seric strcat(buf, SccsPath); 8211438Seric 8221207Seric dirfd = fopen(buf, "r"); 823261Seric if (dirfd == NULL) 824261Seric { 8251207Seric usrerr("cannot open %s", buf); 8261282Seric return (EX_NOINPUT); 827261Seric } 828261Seric 829261Seric /* 830261Seric ** Scan the SCCS directory looking for s. files. 8311438Seric ** gotedit tells whether we have tried to clean any 8321438Seric ** files that are being edited. 833261Seric */ 834261Seric 835351Seric gotedit = FALSE; 836261Seric while (fread(&dir, sizeof dir, 1, dirfd) != NULL) 837261Seric { 838568Seric if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0) 839261Seric continue; 840261Seric 841261Seric /* got an s. file -- see if the p. file exists */ 8421207Seric strcpy(buf, SccsDir); 8431207Seric if (buf[0] != '\0') 8441207Seric strcat(buf, "/"); 8451207Seric strcat(buf, SccsPath); 846261Seric strcat(buf, "/p."); 847346Seric basefile = &buf[strlen(buf)]; 848568Seric strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2); 849346Seric basefile[sizeof dir.d_name - 2] = '\0'; 850394Seric pfp = fopen(buf, "r"); 851394Seric if (pfp != NULL) 852346Seric { 8531438Seric /* the file exists -- report it's contents */ 8541730Seric if (mode == TELLC) 8551730Seric printf("%s\n", basefile); 8561730Seric else 8571730Seric { 8581730Seric while (fgets(pline, sizeof pline, pfp) != NULL) 8591730Seric printf("%12s: being edited: %s", basefile, pline); 8601730Seric } 861394Seric fclose(pfp); 862351Seric gotedit = TRUE; 863261Seric continue; 864346Seric } 865261Seric 866261Seric /* the s. file exists and no p. file exists -- unlink the g-file */ 867819Seric if (mode == CLEANC) 868346Seric { 869568Seric strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2); 870346Seric buf[sizeof dir.d_name - 2] = '\0'; 871346Seric unlink(buf); 872346Seric } 873261Seric } 874261Seric 8751438Seric /* cleanup & report results */ 876261Seric fclose(dirfd); 877819Seric if (!gotedit && mode == INFOC) 878416Seric printf("Nothing being edited\n"); 879819Seric if (mode == CHECKC) 880819Seric exit(gotedit); 8811282Seric return (EX_OK); 882261Seric } 8831432Seric 8841432Seric /* 885396Seric ** UNEDIT -- unedit a file 886396Seric ** 887396Seric ** Checks to see that the current user is actually editting 888396Seric ** the file and arranges that s/he is not editting it. 889396Seric ** 890396Seric ** Parameters: 891416Seric ** fn -- the name of the file to be unedited. 892396Seric ** 893396Seric ** Returns: 894585Seric ** TRUE -- if the file was successfully unedited. 895585Seric ** FALSE -- if the file was not unedited for some 896585Seric ** reason. 897396Seric ** 898396Seric ** Side Effects: 899396Seric ** fn is removed 900396Seric ** entries are removed from pfile. 901396Seric */ 902396Seric 903585Seric bool 904396Seric unedit(fn) 905396Seric char *fn; 906396Seric { 907396Seric register FILE *pfp; 908396Seric char *pfn; 909396Seric static char tfn[] = "/tmp/sccsXXXXX"; 910396Seric FILE *tfp; 911396Seric register char *p; 912396Seric register char *q; 913396Seric bool delete = FALSE; 914396Seric bool others = FALSE; 915396Seric char *myname; 916396Seric extern char *getlogin(); 917396Seric struct pfile *pent; 918396Seric extern struct pfile *getpfile(); 919396Seric char buf[120]; 9201316Seric extern char *makefile(); 921828Seric # ifdef UIDUSER 922828Seric struct passwd *pw; 923828Seric extern struct passwd *getpwuid(); 924828Seric # endif UIDUSER 925396Seric 926396Seric /* make "s." filename & find the trailing component */ 927396Seric pfn = makefile(fn); 928586Seric if (pfn == NULL) 929586Seric return (FALSE); 930586Seric q = rindex(pfn, '/'); 931586Seric if (q == NULL) 932586Seric q = &pfn[-1]; 933586Seric if (q[1] != 's' || q[2] != '.') 934396Seric { 9351205Seric usrerr("bad file name \"%s\"", fn); 936585Seric return (FALSE); 937396Seric } 938396Seric 9391438Seric /* turn "s." into "p." & try to open it */ 940396Seric *++q = 'p'; 941396Seric 942396Seric pfp = fopen(pfn, "r"); 943396Seric if (pfp == NULL) 944396Seric { 945416Seric printf("%12s: not being edited\n", fn); 946585Seric return (FALSE); 947396Seric } 948396Seric 9491438Seric /* create temp file for editing p-file */ 950396Seric mktemp(tfn); 951396Seric tfp = fopen(tfn, "w"); 952396Seric if (tfp == NULL) 953396Seric { 9541205Seric usrerr("cannot create \"%s\"", tfn); 955396Seric exit(EX_OSERR); 956396Seric } 957396Seric 9581438Seric /* figure out who I am */ 959828Seric # ifdef UIDUSER 960828Seric pw = getpwuid(getuid()); 961828Seric if (pw == NULL) 962828Seric { 9631205Seric syserr("who are you? (uid=%d)", getuid()); 964828Seric exit(EX_OSERR); 965828Seric } 966828Seric myname = pw->pw_name; 967828Seric # else 968396Seric myname = getlogin(); 969828Seric # endif UIDUSER 9701438Seric 9711438Seric /* 9721438Seric ** Copy p-file to temp file, doing deletions as needed. 9731438Seric */ 9741438Seric 975396Seric while ((pent = getpfile(pfp)) != NULL) 976396Seric { 977396Seric if (strcmp(pent->p_user, myname) == 0) 978396Seric { 979396Seric /* a match */ 980396Seric delete++; 981396Seric } 982396Seric else 983396Seric { 9841438Seric /* output it again */ 985396Seric fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid, 986396Seric pent->p_nsid, pent->p_user, pent->p_date, 987396Seric pent->p_time); 988396Seric others++; 989396Seric } 990396Seric } 991396Seric 992396Seric /* do final cleanup */ 993396Seric if (others) 994396Seric { 9951438Seric /* copy it back (perhaps it should be linked?) */ 996396Seric if (freopen(tfn, "r", tfp) == NULL) 997396Seric { 9981205Seric syserr("cannot reopen \"%s\"", tfn); 999396Seric exit(EX_OSERR); 1000396Seric } 1001396Seric if (freopen(pfn, "w", pfp) == NULL) 1002396Seric { 10031205Seric usrerr("cannot create \"%s\"", pfn); 1004585Seric return (FALSE); 1005396Seric } 1006396Seric while (fgets(buf, sizeof buf, tfp) != NULL) 1007396Seric fputs(buf, pfp); 1008396Seric } 1009396Seric else 1010396Seric { 10111438Seric /* it's empty -- remove it */ 1012396Seric unlink(pfn); 1013396Seric } 1014396Seric fclose(tfp); 1015396Seric fclose(pfp); 1016396Seric unlink(tfn); 1017396Seric 10181438Seric /* actually remove the g-file */ 1019396Seric if (delete) 1020396Seric { 10211435Seric unlink(tail(fn)); 10221435Seric printf("%12s: removed\n", tail(fn)); 1023585Seric return (TRUE); 1024396Seric } 1025396Seric else 1026396Seric { 1027416Seric printf("%12s: not being edited by you\n", fn); 1028585Seric return (FALSE); 1029396Seric } 1030396Seric } 10311432Seric 10321432Seric /* 10331433Seric ** DODIFF -- diff an s-file against a g-file 10341433Seric ** 10351433Seric ** Parameters: 10361433Seric ** getv -- argv for the 'get' command. 10371433Seric ** gfile -- name of the g-file to diff against. 10381433Seric ** 10391433Seric ** Returns: 10401433Seric ** Result of get. 10411433Seric ** 10421433Seric ** Side Effects: 10431433Seric ** none. 10441433Seric */ 10451433Seric 10461433Seric dodiff(getv, gfile) 10471433Seric char **getv; 10481433Seric char *gfile; 10491433Seric { 10501433Seric int pipev[2]; 10511433Seric int rval; 10521433Seric register int i; 10531433Seric register int pid; 10541433Seric auto int st; 10551433Seric extern int errno; 10561433Seric int (*osig)(); 10571433Seric 10581501Seric printf("\n------- %s -------\n", gfile); 10591501Seric 10601438Seric /* create context for diff to run in */ 10611433Seric if (pipe(pipev) < 0) 10621433Seric { 10631433Seric syserr("dodiff: pipe failed"); 10641433Seric exit(EX_OSERR); 10651433Seric } 10661433Seric if ((pid = fork()) < 0) 10671433Seric { 10681433Seric syserr("dodiff: fork failed"); 10691433Seric exit(EX_OSERR); 10701433Seric } 10711433Seric else if (pid > 0) 10721433Seric { 10731433Seric /* in parent; run get */ 10741433Seric OutFile = pipev[1]; 10751433Seric close(pipev[0]); 1076*1737Seric rval = command(&getv[1], TRUE, "get -s -k -p"); 10771433Seric osig = signal(SIGINT, SIG_IGN); 10781433Seric while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR) 10791433Seric errno = 0; 10801433Seric signal(SIGINT, osig); 10811433Seric /* ignore result of diff */ 10821433Seric } 10831433Seric else 10841433Seric { 10851433Seric /* in child, run diff */ 10861433Seric if (close(pipev[1]) < 0 || close(0) < 0 || 10871433Seric dup(pipev[0]) != 0 || close(pipev[0]) < 0) 10881433Seric { 10891433Seric syserr("dodiff: magic failed"); 10901433Seric exit(EX_OSERR); 10911433Seric } 10921433Seric execl(PROGPATH(bdiff), "bdiff", "-", gfile, NULL); 10931433Seric # ifndef V6 10941433Seric execlp("bdiff", "bdiff", "-", gfile, NULL); 10951433Seric execlp("diff", "diff", "-", gfile, NULL); 10961433Seric # endif NOT V6 10971433Seric syserr("bdiff: cannot execute"); 10981433Seric exit(EX_OSERR); 10991433Seric } 11001433Seric return (rval); 11011433Seric } 11021433Seric 11031433Seric /* 11041435Seric ** TAIL -- return tail of filename. 11051435Seric ** 11061435Seric ** Parameters: 11071435Seric ** fn -- the filename. 11081435Seric ** 11091435Seric ** Returns: 11101435Seric ** a pointer to the tail of the filename; e.g., given 11111435Seric ** "cmd/ls.c", "ls.c" is returned. 11121435Seric ** 11131435Seric ** Side Effects: 11141435Seric ** none. 11151435Seric */ 11161435Seric 11171435Seric char * 11181435Seric tail(fn) 11191435Seric register char *fn; 11201435Seric { 11211435Seric register char *p; 11221435Seric 11231435Seric for (p = fn; *p != 0; p++) 11241435Seric if (*p == '/' && p[1] != '\0' && p[1] != '/') 11251435Seric fn = &p[1]; 11261435Seric return (fn); 11271435Seric } 11281435Seric 11291435Seric /* 1130396Seric ** GETPFILE -- get an entry from the p-file 1131396Seric ** 1132396Seric ** Parameters: 1133396Seric ** pfp -- p-file file pointer 1134396Seric ** 1135396Seric ** Returns: 1136396Seric ** pointer to p-file struct for next entry 1137396Seric ** NULL on EOF or error 1138396Seric ** 1139396Seric ** Side Effects: 1140396Seric ** Each call wipes out results of previous call. 1141396Seric */ 1142396Seric 1143396Seric struct pfile * 1144396Seric getpfile(pfp) 1145396Seric FILE *pfp; 1146396Seric { 1147396Seric static struct pfile ent; 1148396Seric static char buf[120]; 1149396Seric register char *p; 1150396Seric extern char *nextfield(); 1151396Seric 1152396Seric if (fgets(buf, sizeof buf, pfp) == NULL) 1153396Seric return (NULL); 1154396Seric 1155396Seric ent.p_osid = p = buf; 1156396Seric ent.p_nsid = p = nextfield(p); 1157396Seric ent.p_user = p = nextfield(p); 1158396Seric ent.p_date = p = nextfield(p); 1159396Seric ent.p_time = p = nextfield(p); 1160396Seric if (p == NULL || nextfield(p) != NULL) 1161396Seric return (NULL); 1162396Seric 1163396Seric return (&ent); 1164396Seric } 1165396Seric 1166396Seric 1167396Seric char * 1168396Seric nextfield(p) 1169396Seric register char *p; 1170396Seric { 1171396Seric if (p == NULL || *p == '\0') 1172396Seric return (NULL); 1173396Seric while (*p != ' ' && *p != '\n' && *p != '\0') 1174396Seric p++; 1175396Seric if (*p == '\n' || *p == '\0') 1176396Seric { 1177396Seric *p = '\0'; 1178396Seric return (NULL); 1179396Seric } 1180396Seric *p++ = '\0'; 1181396Seric return (p); 1182396Seric } 11831432Seric 11841432Seric /* 11851205Seric ** USRERR -- issue user-level error 11861205Seric ** 11871205Seric ** Parameters: 11881205Seric ** f -- format string. 11891205Seric ** p1-p3 -- parameters to a printf. 11901205Seric ** 11911205Seric ** Returns: 11921205Seric ** -1 11931205Seric ** 11941205Seric ** Side Effects: 11951205Seric ** none. 11961205Seric */ 11971205Seric 11981205Seric usrerr(f, p1, p2, p3) 11991205Seric char *f; 12001205Seric { 12011205Seric fprintf(stderr, "\n%s: ", MyName); 12021205Seric fprintf(stderr, f, p1, p2, p3); 12031205Seric fprintf(stderr, "\n"); 12041205Seric 12051205Seric return (-1); 12061205Seric } 12071432Seric 12081432Seric /* 12091205Seric ** SYSERR -- print system-generated error. 12101205Seric ** 12111205Seric ** Parameters: 12121205Seric ** f -- format string to a printf. 12131205Seric ** p1, p2, p3 -- parameters to f. 12141205Seric ** 12151205Seric ** Returns: 12161205Seric ** never. 12171205Seric ** 12181205Seric ** Side Effects: 12191205Seric ** none. 12201205Seric */ 12211205Seric 12221205Seric syserr(f, p1, p2, p3) 12231205Seric char *f; 12241205Seric { 12251205Seric extern int errno; 12261205Seric 12271205Seric fprintf(stderr, "\n%s SYSERR: ", MyName); 12281205Seric fprintf(stderr, f, p1, p2, p3); 12291205Seric fprintf(stderr, "\n"); 12301205Seric if (errno == 0) 12311205Seric exit(EX_SOFTWARE); 12321205Seric else 12331205Seric { 12341205Seric perror(0); 12351205Seric exit(EX_OSERR); 12361205Seric } 12371205Seric } 1238