1148Seric # include <stdio.h> 26784Smckusick # include <sys/param.h> 3148Seric # include <sys/stat.h> 46784Smckusick # include <dir.h> 51433Seric # include <errno.h> 61433Seric # include <signal.h> 7148Seric # include <sysexits.h> 82140Seric # include <pwd.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*12153Ssam static char SccsId[] = "@(#)sccs.c 1.70 04/30/83"; 961432Seric 971270Seric /******************* Configuration Information ********************/ 981270Seric 991437Seric # ifndef SCCSPATH 1001432Seric # define SCCSPATH "SCCS" /* pathname in which to find s-files */ 1011437Seric # endif NOT SCCSPATH 102828Seric 1031437Seric # ifndef MYNAME 1041437Seric # define MYNAME "sccs" /* name used for printing errors */ 1051437Seric # endif NOT MYNAME 1061270Seric 1071432Seric # ifndef PROGPATH 1086784Smckusick # define PROGPATH(name) "/usr/local/name" /* place to find binaries */ 1091432Seric # endif PROGPATH 1101432Seric 1111270Seric /**************** End of Configuration Information ****************/ 1121432Seric 113157Seric typedef char bool; 114200Seric # define TRUE 1 115200Seric # define FALSE 0 116157Seric 1171438Seric # define bitset(bit, word) ((bool) ((bit) & (word))) 1181438Seric 119148Seric struct sccsprog 120148Seric { 121148Seric char *sccsname; /* name of SCCS routine */ 122200Seric short sccsoper; /* opcode, see below */ 123200Seric short sccsflags; /* flags, see below */ 124148Seric char *sccspath; /* pathname of binary implementing */ 125148Seric }; 126148Seric 127200Seric /* values for sccsoper */ 128200Seric # define PROG 0 /* call a program */ 129201Seric # define CMACRO 1 /* command substitution macro */ 130226Seric # define FIX 2 /* fix a delta */ 131261Seric # define CLEAN 3 /* clean out recreatable files */ 132396Seric # define UNEDIT 4 /* unedit a file */ 1331431Seric # define SHELL 5 /* call a shell file (like PROG) */ 1341433Seric # define DIFFS 6 /* diff between sccs & file out */ 1351871Seric # define DODIFF 7 /* internal call to diff program */ 13610104Srrh # define ENTER 8 /* enter new files */ 137200Seric 138157Seric /* bits for sccsflags */ 139200Seric # define NO_SDOT 0001 /* no s. on front of args */ 140200Seric # define REALUSER 0002 /* protected (e.g., admin) */ 141148Seric 142819Seric /* modes for the "clean", "info", "check" ops */ 143819Seric # define CLEANC 0 /* clean command */ 144819Seric # define INFOC 1 /* info command */ 145819Seric # define CHECKC 2 /* check command */ 1461730Seric # define TELLC 3 /* give list of files being edited */ 147819Seric 1481432Seric /* 1491432Seric ** Description of commands known to this program. 1501432Seric ** First argument puts the command into a class. Second arg is 1511432Seric ** info regarding treatment of this command. Third arg is a 1521432Seric ** list of flags this command accepts from macros, etc. Fourth 1531432Seric ** arg is the pathname of the implementing program, or the 1541432Seric ** macro definition, or the arg to a sub-algorithm. 1551432Seric */ 156202Seric 157148Seric struct sccsprog SccsProg[] = 158148Seric { 1591864Seric "admin", PROG, REALUSER, PROGPATH(admin), 1601864Seric "chghist", PROG, 0, PROGPATH(rmdel), 1611864Seric "comb", PROG, 0, PROGPATH(comb), 1621864Seric "delta", PROG, 0, PROGPATH(delta), 1631864Seric "get", PROG, 0, PROGPATH(get), 1641864Seric "help", PROG, NO_SDOT, PROGPATH(help), 1652136Seric "prs", PROG, 0, PROGPATH(prs), 1661864Seric "prt", PROG, 0, PROGPATH(prt), 1671864Seric "rmdel", PROG, REALUSER, PROGPATH(rmdel), 1682136Seric "val", PROG, 0, PROGPATH(val), 1691864Seric "what", PROG, NO_SDOT, PROGPATH(what), 1701864Seric "sccsdiff", SHELL, REALUSER, PROGPATH(sccsdiff), 1711864Seric "edit", CMACRO, NO_SDOT, "get -e", 1721864Seric "delget", CMACRO, NO_SDOT, "delta:mysrp/get:ixbeskcl -t", 1732138Seric "deledit", CMACRO, NO_SDOT, "delta:mysrp -n/get:ixbskcl -e -t -g", 1741864Seric "fix", FIX, NO_SDOT, NULL, 1751864Seric "clean", CLEAN, REALUSER|NO_SDOT, (char *) CLEANC, 1761864Seric "info", CLEAN, REALUSER|NO_SDOT, (char *) INFOC, 1771864Seric "check", CLEAN, REALUSER|NO_SDOT, (char *) CHECKC, 1781864Seric "tell", CLEAN, REALUSER|NO_SDOT, (char *) TELLC, 1791864Seric "unedit", UNEDIT, NO_SDOT, NULL, 1801864Seric "diffs", DIFFS, NO_SDOT|REALUSER, NULL, 1811871Seric "-diff", DODIFF, NO_SDOT|REALUSER, PROGPATH(bdiff), 1822137Seric "print", CMACRO, 0, "prt -e/get -p -m -s", 1832226Seric "branch", CMACRO, NO_SDOT, 1842226Seric "get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g", 18510104Srrh "enter", ENTER, NO_SDOT, NULL, 18610104Srrh "create", CMACRO, NO_SDOT, "enter/get:ixbeskcl -t", 1871864Seric NULL, -1, 0, NULL 188148Seric }; 189148Seric 1901432Seric /* one line from a p-file */ 191396Seric struct pfile 192396Seric { 193396Seric char *p_osid; /* old SID */ 194396Seric char *p_nsid; /* new SID */ 195396Seric char *p_user; /* user who did edit */ 196396Seric char *p_date; /* date of get */ 197396Seric char *p_time; /* time of get */ 1982161Seric char *p_aux; /* extra info at end */ 199396Seric }; 200396Seric 2011270Seric char *SccsPath = SCCSPATH; /* pathname of SCCS files */ 2021270Seric # ifdef SCCSDIR 2031270Seric char *SccsDir = SCCSDIR; /* directory to begin search from */ 2041205Seric # else 2051270Seric char *SccsDir = ""; 2061205Seric # endif 2071437Seric char MyName[] = MYNAME; /* name used in messages */ 2081433Seric int OutFile = -1; /* override output file for commands */ 209157Seric bool RealUser; /* if set, running as real user */ 210393Seric # ifdef DEBUG 211393Seric bool Debug; /* turn on tracing */ 212393Seric # endif 2132139Seric # ifndef V6 2142139Seric extern char *getenv(); 2152139Seric # endif V6 21610110Srrh 21710110Srrh char *gstrcat(), *strcat(); 21810110Srrh char *gstrncat(), *strncat(); 21910110Srrh char *gstrcpy(), *strcpy(); 22010110Srrh #define FBUFSIZ BUFSIZ 22110110Srrh #define PFILELG 120 2221432Seric 223148Seric main(argc, argv) 224148Seric int argc; 225148Seric char **argv; 226148Seric { 227148Seric register char *p; 228262Seric extern struct sccsprog *lookup(); 2291282Seric register int i; 2302139Seric # ifndef V6 2312139Seric # ifndef SCCSDIR 2322140Seric register struct passwd *pw; 2332140Seric extern struct passwd *getpwnam(); 23410110Srrh char buf[FBUFSIZ]; 2352140Seric 2362139Seric /* pull "SccsDir" out of the environment (possibly) */ 23710260Seric p = getenv("PROJECTDIR"); 2382140Seric if (p != NULL && p[0] != '\0') 2392140Seric { 2402140Seric if (p[0] == '/') 2412140Seric SccsDir = p; 2422140Seric else 2432140Seric { 2442140Seric pw = getpwnam(p); 2452140Seric if (pw == NULL) 2462140Seric { 2472140Seric usrerr("user %s does not exist", p); 2482140Seric exit(EX_USAGE); 2492140Seric } 25010110Srrh gstrcpy(buf, pw->pw_dir, sizeof(buf)); 25110110Srrh gstrcat(buf, "/src", sizeof(buf)); 2522140Seric if (access(buf, 0) < 0) 2532140Seric { 25410110Srrh gstrcpy(buf, pw->pw_dir, sizeof(buf)); 25510110Srrh gstrcat(buf, "/source", sizeof(buf)); 2562140Seric if (access(buf, 0) < 0) 2572140Seric { 2582140Seric usrerr("project %s has no source!", p); 2592140Seric exit(EX_USAGE); 2602140Seric } 2612140Seric } 2622140Seric SccsDir = buf; 2632140Seric } 2642140Seric } 2652139Seric # endif SCCSDIR 2662139Seric # endif V6 2672139Seric 268148Seric /* 269148Seric ** Detect and decode flags intended for this program. 270148Seric */ 271148Seric 272200Seric if (argc < 2) 273148Seric { 2741205Seric fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName); 275200Seric exit(EX_USAGE); 276200Seric } 277200Seric argv[argc] = NULL; 278200Seric 279262Seric if (lookup(argv[0]) == NULL) 280200Seric { 281262Seric while ((p = *++argv) != NULL) 282148Seric { 283262Seric if (*p != '-') 284262Seric break; 285262Seric switch (*++p) 286262Seric { 287262Seric case 'r': /* run as real user */ 288262Seric setuid(getuid()); 289262Seric RealUser++; 290262Seric break; 291148Seric 2921270Seric # ifndef SCCSDIR 293262Seric case 'p': /* path of sccs files */ 294262Seric SccsPath = ++p; 2952348Seric if (SccsPath[0] == '\0' && argv[1] != NULL) 2962348Seric SccsPath = *++argv; 297262Seric break; 298148Seric 299588Seric case 'd': /* directory to search from */ 300588Seric SccsDir = ++p; 3012348Seric if (SccsDir[0] == '\0' && argv[1] != NULL) 3022348Seric SccsDir = *++argv; 303588Seric break; 3041205Seric # endif 305588Seric 306393Seric # ifdef DEBUG 307393Seric case 'T': /* trace */ 308393Seric Debug++; 309393Seric break; 310393Seric # endif 311393Seric 312262Seric default: 3131205Seric usrerr("unknown option -%s", p); 314262Seric break; 315262Seric } 316148Seric } 317262Seric if (SccsPath[0] == '\0') 318262Seric SccsPath = "."; 319148Seric } 320148Seric 3211737Seric i = command(argv, FALSE, ""); 3221282Seric exit(i); 323200Seric } 3241432Seric 3251432Seric /* 3261282Seric ** COMMAND -- look up and perform a command 3271282Seric ** 3281282Seric ** This routine is the guts of this program. Given an 3291282Seric ** argument vector, it looks up the "command" (argv[0]) 3301282Seric ** in the configuration table and does the necessary stuff. 3311282Seric ** 3321282Seric ** Parameters: 3331282Seric ** argv -- an argument vector to process. 3341282Seric ** forkflag -- if set, fork before executing the command. 3351316Seric ** editflag -- if set, only include flags listed in the 3361316Seric ** sccsklets field of the command descriptor. 3371316Seric ** arg0 -- a space-seperated list of arguments to insert 3381316Seric ** before argv. 3391282Seric ** 3401282Seric ** Returns: 3411282Seric ** zero -- command executed ok. 3421282Seric ** else -- error status. 3431282Seric ** 3441282Seric ** Side Effects: 3451282Seric ** none. 3461282Seric */ 347157Seric 3481737Seric command(argv, forkflag, arg0) 349200Seric char **argv; 350201Seric bool forkflag; 3511316Seric char *arg0; 352200Seric { 353200Seric register struct sccsprog *cmd; 354200Seric register char *p; 35510110Srrh char buf[FBUFSIZ]; 356262Seric extern struct sccsprog *lookup(); 3571316Seric char *nav[1000]; 3581316Seric char **np; 3591431Seric register char **ap; 360585Seric register int i; 3611431Seric register char *q; 362585Seric extern bool unedit(); 3631282Seric int rval = 0; 3641316Seric extern char *index(); 3651316Seric extern char *makefile(); 3661737Seric char *editchs; 3671435Seric extern char *tail(); 368200Seric 369393Seric # ifdef DEBUG 370393Seric if (Debug) 371393Seric { 3721316Seric printf("command:\n\t\"%s\"\n", arg0); 3731316Seric for (np = argv; *np != NULL; np++) 3741316Seric printf("\t\"%s\"\n", *np); 375393Seric } 376393Seric # endif 377393Seric 378157Seric /* 3791316Seric ** Copy arguments. 3801438Seric ** Copy from arg0 & if necessary at most one arg 3811438Seric ** from argv[0]. 3821316Seric */ 3831316Seric 3841431Seric np = ap = &nav[1]; 3851737Seric editchs = NULL; 3861821Seric for (p = arg0, q = buf; *p != '\0' && *p != '/'; ) 3871316Seric { 3881316Seric *np++ = q; 3891316Seric while (*p == ' ') 3901316Seric p++; 3911737Seric while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':') 3921316Seric *q++ = *p++; 3931316Seric *q++ = '\0'; 3941737Seric if (*p == ':') 3951737Seric { 3961737Seric editchs = q; 3971821Seric while (*++p != '\0' && *p != '/' && *p != ' ') 3981737Seric *q++ = *p; 3991737Seric *q++ = '\0'; 4001737Seric } 4011316Seric } 4021316Seric *np = NULL; 4031431Seric if (*ap == NULL) 4041316Seric *np++ = *argv++; 4051316Seric 4061316Seric /* 407148Seric ** Look up command. 4081431Seric ** At this point, *ap is the command name. 409148Seric */ 410148Seric 4111431Seric cmd = lookup(*ap); 412262Seric if (cmd == NULL) 413148Seric { 4141431Seric usrerr("Unknown command \"%s\"", *ap); 4151282Seric return (EX_USAGE); 416148Seric } 417148Seric 418148Seric /* 4191316Seric ** Copy remaining arguments doing editing as appropriate. 4201316Seric */ 4211316Seric 4221316Seric for (; *argv != NULL; argv++) 4231316Seric { 4241316Seric p = *argv; 4251316Seric if (*p == '-') 4261316Seric { 4271737Seric if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL) 4281316Seric *np++ = p; 4291316Seric } 4301316Seric else 4311316Seric { 4321316Seric if (!bitset(NO_SDOT, cmd->sccsflags)) 4331316Seric p = makefile(p); 4341316Seric if (p != NULL) 4351316Seric *np++ = p; 4361316Seric } 4371316Seric } 4381316Seric *np = NULL; 4391316Seric 4401316Seric /* 441200Seric ** Interpret operation associated with this command. 442157Seric */ 443157Seric 444200Seric switch (cmd->sccsoper) 445200Seric { 4461431Seric case SHELL: /* call a shell file */ 4471431Seric *ap = cmd->sccspath; 4481431Seric *--ap = "sh"; 4491431Seric rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag); 4501431Seric break; 4511431Seric 452200Seric case PROG: /* call an sccs prog */ 4531431Seric rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag); 454201Seric break; 455201Seric 456201Seric case CMACRO: /* command macro */ 4571438Seric /* step through & execute each part of the macro */ 458201Seric for (p = cmd->sccspath; *p != '\0'; p++) 459201Seric { 4601316Seric q = p; 4611316Seric while (*p != '\0' && *p != '/') 4621316Seric p++; 4631737Seric rval = command(&ap[1], *p != '\0', q); 4641282Seric if (rval != 0) 4651282Seric break; 466201Seric } 4671282Seric break; 468157Seric 469226Seric case FIX: /* fix a delta */ 4701431Seric if (strncmp(ap[1], "-r", 2) != 0) 471226Seric { 4721205Seric usrerr("-r flag needed for fix command"); 4731282Seric rval = EX_USAGE; 474226Seric break; 475226Seric } 4761438Seric 4771438Seric /* get the version with all changes */ 4781737Seric rval = command(&ap[1], TRUE, "get -k"); 4791438Seric 4801438Seric /* now remove that version from the s-file */ 4811282Seric if (rval == 0) 4821737Seric rval = command(&ap[1], TRUE, "rmdel:r"); 4831438Seric 4841438Seric /* and edit the old version (but don't clobber new vers) */ 4851282Seric if (rval == 0) 4861737Seric rval = command(&ap[2], FALSE, "get -e -g"); 4871282Seric break; 488226Seric 489261Seric case CLEAN: 4901822Seric rval = clean((int) cmd->sccspath, ap); 491261Seric break; 492261Seric 493396Seric case UNEDIT: 4941431Seric for (argv = np = &ap[1]; *argv != NULL; argv++) 495585Seric { 4961316Seric if (unedit(*argv)) 4971316Seric *np++ = *argv; 498585Seric } 4991316Seric *np = NULL; 5001438Seric 5011438Seric /* get all the files that we unedited successfully */ 5021738Seric if (np > &ap[1]) 5031737Seric rval = command(&ap[1], FALSE, "get"); 504396Seric break; 505396Seric 5061433Seric case DIFFS: /* diff between s-file & edit file */ 5071433Seric /* find the end of the flag arguments */ 5081433Seric for (np = &ap[1]; *np != NULL && **np == '-'; np++) 5091433Seric continue; 5101433Seric argv = np; 5111433Seric 5121433Seric /* for each file, do the diff */ 5131502Seric p = argv[1]; 5141433Seric while (*np != NULL) 5151433Seric { 5161438Seric /* messy, but we need a null terminated argv */ 5171433Seric *argv = *np++; 5181502Seric argv[1] = NULL; 5191435Seric i = dodiff(ap, tail(*argv)); 5201433Seric if (rval == 0) 5211433Seric rval = i; 5221502Seric argv[1] = p; 5231433Seric } 5241433Seric break; 5251433Seric 5261871Seric case DODIFF: /* internal diff call */ 5271871Seric setuid(getuid()); 5281871Seric for (np = ap; *np != NULL; np++) 5291871Seric { 5301871Seric if ((*np)[0] == '-' && (*np)[1] == 'C') 5311871Seric (*np)[1] = 'c'; 5321871Seric } 5331871Seric 5341871Seric /* insert "-" argument */ 5351871Seric np[1] = NULL; 5361871Seric np[0] = np[-1]; 5371871Seric np[-1] = "-"; 5381871Seric 5391871Seric /* execute the diff program of choice */ 5401871Seric # ifndef V6 5411871Seric execvp("diff", ap); 5421871Seric # endif 5431871Seric execv(cmd->sccspath, argv); 5441871Seric syserr("cannot exec %s", cmd->sccspath); 5451871Seric exit(EX_OSERR); 5461871Seric 54710104Srrh case ENTER: /* enter new sccs files */ 5486785Smckusick /* skip over flag arguments */ 5496785Smckusick for (np = &ap[1]; *np != NULL && **np == '-'; np++) 5506785Smckusick continue; 5516785Smckusick argv = np; 5526785Smckusick 5536785Smckusick /* do an admin for each file */ 5546785Smckusick p = argv[1]; 5556785Smckusick while (*np != NULL) 5566785Smckusick { 5576785Smckusick printf("\n%s:\n", *np); 55810110Srrh strcpy(buf, "-i"); 55910110Srrh gstrcat(buf, *np, sizeof(buf)); 5606785Smckusick ap[0] = buf; 5616785Smckusick argv[0] = tail(*np); 5626785Smckusick argv[1] = NULL; 5636785Smckusick rval = command(ap, TRUE, "admin"); 5646785Smckusick argv[1] = p; 5656785Smckusick if (rval == 0) 5666785Smckusick { 56710110Srrh strcpy(buf, ","); 56810110Srrh gstrcat(buf, tail(*np), sizeof(buf)); 5696785Smckusick if (link(*np, buf) >= 0) 5706785Smckusick unlink(*np); 5716785Smckusick } 5726785Smckusick np++; 5736785Smckusick } 5746785Smckusick break; 5756785Smckusick 576200Seric default: 5771205Seric syserr("oper %d", cmd->sccsoper); 578200Seric exit(EX_SOFTWARE); 579200Seric } 5801282Seric # ifdef DEBUG 5811282Seric if (Debug) 5821282Seric printf("command: rval=%d\n", rval); 5831282Seric # endif 5841282Seric return (rval); 585200Seric } 5861432Seric 5871432Seric /* 588262Seric ** LOOKUP -- look up an SCCS command name. 589262Seric ** 590262Seric ** Parameters: 591262Seric ** name -- the name of the command to look up. 592262Seric ** 593262Seric ** Returns: 594262Seric ** ptr to command descriptor for this command. 595262Seric ** NULL if no such entry. 596262Seric ** 597262Seric ** Side Effects: 598262Seric ** none. 599262Seric */ 600200Seric 601262Seric struct sccsprog * 602262Seric lookup(name) 603262Seric char *name; 604262Seric { 605262Seric register struct sccsprog *cmd; 606226Seric 607262Seric for (cmd = SccsProg; cmd->sccsname != NULL; cmd++) 608262Seric { 609262Seric if (strcmp(cmd->sccsname, name) == 0) 610262Seric return (cmd); 611262Seric } 612262Seric return (NULL); 613262Seric } 6141432Seric 6151432Seric /* 6161282Seric ** CALLPROG -- call a program 6171282Seric ** 6181316Seric ** Used to call the SCCS programs. 6191282Seric ** 6201282Seric ** Parameters: 6211282Seric ** progpath -- pathname of the program to call. 6221282Seric ** flags -- status flags from the command descriptors. 6231282Seric ** argv -- an argument vector to pass to the program. 6241282Seric ** forkflag -- if true, fork before calling, else just 6251282Seric ** exec. 6261282Seric ** 6271282Seric ** Returns: 6281282Seric ** The exit status of the program. 6291282Seric ** Nothing if forkflag == FALSE. 6301282Seric ** 6311282Seric ** Side Effects: 6321282Seric ** Can exit if forkflag == FALSE. 6331282Seric */ 634226Seric 635200Seric callprog(progpath, flags, argv, forkflag) 636200Seric char *progpath; 637200Seric short flags; 638200Seric char **argv; 639200Seric bool forkflag; 640200Seric { 641200Seric register int i; 642201Seric auto int st; 643200Seric 6441316Seric # ifdef DEBUG 6451316Seric if (Debug) 6461316Seric { 6471316Seric printf("callprog:\n"); 6481316Seric for (i = 0; argv[i] != NULL; i++) 6491316Seric printf("\t\"%s\"\n", argv[i]); 6501316Seric } 6511316Seric # endif 6521316Seric 653200Seric if (*argv == NULL) 654200Seric return (-1); 655200Seric 656157Seric /* 657226Seric ** Fork if appropriate. 658148Seric */ 659148Seric 660200Seric if (forkflag) 661200Seric { 662393Seric # ifdef DEBUG 663393Seric if (Debug) 664393Seric printf("Forking\n"); 665393Seric # endif 666200Seric i = fork(); 667200Seric if (i < 0) 668200Seric { 6691205Seric syserr("cannot fork"); 670200Seric exit(EX_OSERR); 671200Seric } 672200Seric else if (i > 0) 673201Seric { 674201Seric wait(&st); 6751282Seric if ((st & 0377) == 0) 6761282Seric st = (st >> 8) & 0377; 6771433Seric if (OutFile >= 0) 6781433Seric { 6791433Seric close(OutFile); 6801433Seric OutFile = -1; 6811433Seric } 682201Seric return (st); 683201Seric } 684200Seric } 6851433Seric else if (OutFile >= 0) 6861433Seric { 6871433Seric syserr("callprog: setting stdout w/o forking"); 6881433Seric exit(EX_SOFTWARE); 6891433Seric } 690200Seric 6911433Seric /* set protection as appropriate */ 692200Seric if (bitset(REALUSER, flags)) 693200Seric setuid(getuid()); 6941433Seric 6951433Seric /* change standard input & output if needed */ 6961433Seric if (OutFile >= 0) 6971433Seric { 6981433Seric close(1); 6991433Seric dup(OutFile); 7001433Seric close(OutFile); 7011433Seric } 702226Seric 7031433Seric /* call real SCCS program */ 704226Seric execv(progpath, argv); 7051205Seric syserr("cannot execute %s", progpath); 706148Seric exit(EX_UNAVAILABLE); 7071738Seric /*NOTREACHED*/ 708148Seric } 7091432Seric 7101432Seric /* 711586Seric ** MAKEFILE -- make filename of SCCS file 712586Seric ** 713586Seric ** If the name passed is already the name of an SCCS file, 714586Seric ** just return it. Otherwise, munge the name into the name 715586Seric ** of the actual SCCS file. 716586Seric ** 717586Seric ** There are cases when it is not clear what you want to 718586Seric ** do. For example, if SccsPath is an absolute pathname 719586Seric ** and the name given is also an absolute pathname, we go 720586Seric ** for SccsPath (& only use the last component of the name 721586Seric ** passed) -- this is important for security reasons (if 722586Seric ** sccs is being used as a setuid front end), but not 723586Seric ** particularly intuitive. 724586Seric ** 725586Seric ** Parameters: 726586Seric ** name -- the file name to be munged. 727586Seric ** 728586Seric ** Returns: 729586Seric ** The pathname of the sccs file. 730586Seric ** NULL on error. 731586Seric ** 732586Seric ** Side Effects: 733586Seric ** none. 734586Seric */ 735148Seric 736148Seric char * 737148Seric makefile(name) 738148Seric char *name; 739148Seric { 740148Seric register char *p; 74110110Srrh char buf[3*FBUFSIZ]; 742148Seric extern char *malloc(); 743586Seric extern char *rindex(); 744588Seric extern bool safepath(); 745587Seric extern bool isdir(); 746587Seric register char *q; 747148Seric 748586Seric p = rindex(name, '/'); 749586Seric if (p == NULL) 750586Seric p = name; 751586Seric else 752586Seric p++; 753586Seric 754148Seric /* 755588Seric ** Check to see that the path is "safe", i.e., that we 756588Seric ** are not letting some nasty person use the setuid part 757588Seric ** of this program to look at or munge some presumably 758588Seric ** hidden files. 759148Seric */ 760148Seric 761588Seric if (SccsDir[0] == '/' && !safepath(name)) 762588Seric return (NULL); 763586Seric 764586Seric /* 765588Seric ** Create the base pathname. 766586Seric */ 767586Seric 7681438Seric /* first the directory part */ 769588Seric if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0) 770148Seric { 77110110Srrh gstrcpy(buf, SccsDir, sizeof(buf)); 77210110Srrh gstrcat(buf, "/", sizeof(buf)); 773586Seric } 774586Seric else 77510110Srrh gstrcpy(buf, "", sizeof(buf)); 7761438Seric 7771438Seric /* then the head of the pathname */ 77810110Srrh gstrncat(buf, name, p - name, sizeof(buf)); 779587Seric q = &buf[strlen(buf)]; 7801438Seric 7811438Seric /* now copy the final part of the name, in case useful */ 78210110Srrh gstrcpy(q, p, sizeof(buf)); 7831438Seric 7841438Seric /* so is it useful? */ 785587Seric if (strncmp(p, "s.", 2) != 0 && !isdir(buf)) 786586Seric { 7871438Seric /* sorry, no; copy the SCCS pathname & the "s." */ 78810110Srrh gstrcpy(q, SccsPath, sizeof(buf)); 78910110Srrh gstrcat(buf, "/s.", sizeof(buf)); 7901438Seric 7911438Seric /* and now the end of the name */ 79210110Srrh gstrcat(buf, p, sizeof(buf)); 793586Seric } 794148Seric 7951438Seric /* if i haven't changed it, why did I do all this? */ 796588Seric if (strcmp(buf, name) == 0) 797588Seric p = name; 798588Seric else 799148Seric { 8001438Seric /* but if I have, squirrel it away */ 801588Seric p = malloc(strlen(buf) + 1); 802588Seric if (p == NULL) 803588Seric { 804588Seric perror("Sccs: no mem"); 805588Seric exit(EX_OSERR); 806588Seric } 807588Seric strcpy(p, buf); 808148Seric } 8091438Seric 810148Seric return (p); 811148Seric } 8121432Seric 8131432Seric /* 814587Seric ** ISDIR -- return true if the argument is a directory. 815587Seric ** 816587Seric ** Parameters: 817587Seric ** name -- the pathname of the file to check. 818587Seric ** 819587Seric ** Returns: 820587Seric ** TRUE if 'name' is a directory, FALSE otherwise. 821587Seric ** 822587Seric ** Side Effects: 823587Seric ** none. 824587Seric */ 825587Seric 826587Seric bool 827587Seric isdir(name) 828587Seric char *name; 829587Seric { 830587Seric struct stat stbuf; 831587Seric 832587Seric return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR); 833587Seric } 8341432Seric 8351432Seric /* 836586Seric ** SAFEPATH -- determine whether a pathname is "safe" 837586Seric ** 838586Seric ** "Safe" pathnames only allow you to get deeper into the 839586Seric ** directory structure, i.e., full pathnames and ".." are 840586Seric ** not allowed. 841586Seric ** 842586Seric ** Parameters: 843586Seric ** p -- the name to check. 844586Seric ** 845586Seric ** Returns: 846586Seric ** TRUE -- if the path is safe. 847586Seric ** FALSE -- if the path is not safe. 848586Seric ** 849586Seric ** Side Effects: 850586Seric ** Prints a message if the path is not safe. 851586Seric */ 852586Seric 853586Seric bool 854586Seric safepath(p) 855586Seric register char *p; 856586Seric { 857586Seric extern char *index(); 858586Seric 859586Seric if (*p != '/') 860586Seric { 861586Seric while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0) 862586Seric { 863586Seric p = index(p, '/'); 864586Seric if (p == NULL) 865586Seric return (TRUE); 866586Seric p++; 867586Seric } 868586Seric } 869586Seric 870586Seric printf("You may not use full pathnames or \"..\"\n"); 871586Seric return (FALSE); 872586Seric } 8731432Seric 8741432Seric /* 875261Seric ** CLEAN -- clean out recreatable files 876261Seric ** 877261Seric ** Any file for which an "s." file exists but no "p." file 878261Seric ** exists in the current directory is purged. 879261Seric ** 880261Seric ** Parameters: 8811822Seric ** mode -- tells whether this came from a "clean", "info", or 8821822Seric ** "check" command. 8831822Seric ** argv -- the rest of the argument vector. 884261Seric ** 885261Seric ** Returns: 886261Seric ** none. 887261Seric ** 888261Seric ** Side Effects: 889819Seric ** Removes files in the current directory. 890819Seric ** Prints information regarding files being edited. 891819Seric ** Exits if a "check" command. 892261Seric */ 893261Seric 8941822Seric clean(mode, argv) 895819Seric int mode; 8961822Seric char **argv; 897261Seric { 8986784Smckusick struct direct *dir; 89910110Srrh char buf[FBUFSIZ]; 9002140Seric char *bufend; 9016784Smckusick register DIR *dirfd; 902346Seric register char *basefile; 903351Seric bool gotedit; 9041822Seric bool gotpfent; 905394Seric FILE *pfp; 9061822Seric bool nobranch = FALSE; 9071822Seric extern struct pfile *getpfent(); 9081822Seric register struct pfile *pf; 9091822Seric register char **ap; 9101864Seric extern char *username(); 9111864Seric char *usernm = NULL; 9122140Seric char *subdir = NULL; 9132140Seric char *cmdname; 914261Seric 9151438Seric /* 9161822Seric ** Process the argv 9171822Seric */ 9181822Seric 9192140Seric cmdname = *argv; 9202140Seric for (ap = argv; *++ap != NULL; ) 9211822Seric { 9221864Seric if (**ap == '-') 9231864Seric { 9241864Seric /* we have a flag */ 9251864Seric switch ((*ap)[1]) 9261864Seric { 9271864Seric case 'b': 9281864Seric nobranch = TRUE; 9291864Seric break; 9301864Seric 9311864Seric case 'u': 9321864Seric if ((*ap)[2] != '\0') 9331864Seric usernm = &(*ap)[2]; 9341864Seric else if (ap[1] != NULL && ap[1][0] != '-') 9351864Seric usernm = *++ap; 9361864Seric else 9371864Seric usernm = username(); 9381864Seric break; 9391864Seric } 9401864Seric } 9412140Seric else 9422140Seric { 9432140Seric if (subdir != NULL) 9442140Seric usrerr("too many args"); 9452140Seric else 9462140Seric subdir = *ap; 9472140Seric } 9481822Seric } 9491822Seric 9501822Seric /* 9511438Seric ** Find and open the SCCS directory. 9521438Seric */ 9531438Seric 95410110Srrh gstrcpy(buf, SccsDir, sizeof(buf)); 9551207Seric if (buf[0] != '\0') 95610110Srrh gstrcat(buf, "/", sizeof(buf)); 9572140Seric if (subdir != NULL) 9582140Seric { 95910110Srrh gstrcat(buf, subdir, sizeof(buf)); 96010110Srrh gstrcat(buf, "/", sizeof(buf)); 9612140Seric } 96210110Srrh gstrcat(buf, SccsPath, sizeof(buf)); 9632140Seric bufend = &buf[strlen(buf)]; 9641438Seric 9656784Smckusick dirfd = opendir(buf); 966261Seric if (dirfd == NULL) 967261Seric { 9681207Seric usrerr("cannot open %s", buf); 9691282Seric return (EX_NOINPUT); 970261Seric } 971261Seric 972261Seric /* 973261Seric ** Scan the SCCS directory looking for s. files. 9741438Seric ** gotedit tells whether we have tried to clean any 9751438Seric ** files that are being edited. 976261Seric */ 977261Seric 978351Seric gotedit = FALSE; 9796784Smckusick while (dir = readdir(dirfd)) { 9806784Smckusick if (strncmp(dir->d_name, "s.", 2) != 0) 981261Seric continue; 982261Seric 983261Seric /* got an s. file -- see if the p. file exists */ 98410110Srrh gstrcpy(bufend, "/p.", sizeof(buf)); 9852140Seric basefile = bufend + 3; 98610110Srrh gstrcpy(basefile, &dir->d_name[2], sizeof(buf)); 9871822Seric 9881822Seric /* 9891822Seric ** open and scan the p-file. 9901822Seric ** 'gotpfent' tells if we have found a valid p-file 9911822Seric ** entry. 9921822Seric */ 9931822Seric 994394Seric pfp = fopen(buf, "r"); 9951822Seric gotpfent = FALSE; 996394Seric if (pfp != NULL) 997346Seric { 9981438Seric /* the file exists -- report it's contents */ 9991822Seric while ((pf = getpfent(pfp)) != NULL) 10001730Seric { 10011822Seric if (nobranch && isbranch(pf->p_nsid)) 10021822Seric continue; 10031864Seric if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC) 10041864Seric continue; 10051822Seric gotedit = TRUE; 10061822Seric gotpfent = TRUE; 10071822Seric if (mode == TELLC) 10081822Seric { 10091822Seric printf("%s\n", basefile); 10101822Seric break; 10111822Seric } 10122161Seric printf("%12s: being edited: ", basefile); 10132161Seric putpfent(pf, stdout); 10141730Seric } 1015394Seric fclose(pfp); 10161822Seric } 1017261Seric 1018261Seric /* the s. file exists and no p. file exists -- unlink the g-file */ 10191870Seric if (mode == CLEANC && !gotpfent) 1020346Seric { 102110110Srrh char unlinkbuf[FBUFSIZ]; 102210110Srrh gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf)); 102310103Srrh unlink(unlinkbuf); 1024346Seric } 1025261Seric } 1026261Seric 10271438Seric /* cleanup & report results */ 10286784Smckusick closedir(dirfd); 1029819Seric if (!gotedit && mode == INFOC) 10301864Seric { 10311864Seric printf("Nothing being edited"); 10321864Seric if (nobranch) 10331864Seric printf(" (on trunk)"); 10341864Seric if (usernm == NULL) 10351864Seric printf("\n"); 10361864Seric else 10371864Seric printf(" by %s\n", usernm); 10381864Seric } 1039819Seric if (mode == CHECKC) 1040819Seric exit(gotedit); 10411282Seric return (EX_OK); 1042261Seric } 10431432Seric 10441432Seric /* 10451822Seric ** ISBRANCH -- is the SID a branch? 10461822Seric ** 10471822Seric ** Parameters: 10481822Seric ** sid -- the sid to check. 10491822Seric ** 10501822Seric ** Returns: 10511822Seric ** TRUE if the sid represents a branch. 10521822Seric ** FALSE otherwise. 10531822Seric ** 10541822Seric ** Side Effects: 10551822Seric ** none. 10561822Seric */ 10571822Seric 10581822Seric isbranch(sid) 10591822Seric char *sid; 10601822Seric { 10611822Seric register char *p; 10621822Seric int dots; 10631822Seric 10641822Seric dots = 0; 10651822Seric for (p = sid; *p != '\0'; p++) 10661822Seric { 10671822Seric if (*p == '.') 10681822Seric dots++; 10691822Seric if (dots > 1) 10701822Seric return (TRUE); 10711822Seric } 10721822Seric return (FALSE); 10731822Seric } 10741822Seric 10751822Seric /* 1076396Seric ** UNEDIT -- unedit a file 1077396Seric ** 1078396Seric ** Checks to see that the current user is actually editting 1079396Seric ** the file and arranges that s/he is not editting it. 1080396Seric ** 1081396Seric ** Parameters: 1082416Seric ** fn -- the name of the file to be unedited. 1083396Seric ** 1084396Seric ** Returns: 1085585Seric ** TRUE -- if the file was successfully unedited. 1086585Seric ** FALSE -- if the file was not unedited for some 1087585Seric ** reason. 1088396Seric ** 1089396Seric ** Side Effects: 1090396Seric ** fn is removed 1091396Seric ** entries are removed from pfile. 1092396Seric */ 1093396Seric 1094585Seric bool 1095396Seric unedit(fn) 1096396Seric char *fn; 1097396Seric { 1098396Seric register FILE *pfp; 1099*12153Ssam char *cp, *pfn; 1100396Seric static char tfn[] = "/tmp/sccsXXXXX"; 1101396Seric FILE *tfp; 1102396Seric register char *q; 1103396Seric bool delete = FALSE; 1104396Seric bool others = FALSE; 1105396Seric char *myname; 11061864Seric extern char *username(); 1107396Seric struct pfile *pent; 11081822Seric extern struct pfile *getpfent(); 110910110Srrh char buf[PFILELG]; 11101316Seric extern char *makefile(); 1111396Seric 1112396Seric /* make "s." filename & find the trailing component */ 1113396Seric pfn = makefile(fn); 1114586Seric if (pfn == NULL) 1115586Seric return (FALSE); 1116586Seric q = rindex(pfn, '/'); 1117586Seric if (q == NULL) 1118586Seric q = &pfn[-1]; 1119586Seric if (q[1] != 's' || q[2] != '.') 1120396Seric { 11211205Seric usrerr("bad file name \"%s\"", fn); 1122585Seric return (FALSE); 1123396Seric } 1124396Seric 11251438Seric /* turn "s." into "p." & try to open it */ 1126396Seric *++q = 'p'; 1127396Seric 1128396Seric pfp = fopen(pfn, "r"); 1129396Seric if (pfp == NULL) 1130396Seric { 1131416Seric printf("%12s: not being edited\n", fn); 1132585Seric return (FALSE); 1133396Seric } 1134396Seric 11351438Seric /* create temp file for editing p-file */ 1136396Seric mktemp(tfn); 1137396Seric tfp = fopen(tfn, "w"); 1138396Seric if (tfp == NULL) 1139396Seric { 11401205Seric usrerr("cannot create \"%s\"", tfn); 1141396Seric exit(EX_OSERR); 1142396Seric } 1143396Seric 11441438Seric /* figure out who I am */ 11451864Seric myname = username(); 11461438Seric 11471438Seric /* 11481438Seric ** Copy p-file to temp file, doing deletions as needed. 11491438Seric */ 11501438Seric 11511822Seric while ((pent = getpfent(pfp)) != NULL) 1152396Seric { 1153396Seric if (strcmp(pent->p_user, myname) == 0) 1154396Seric { 1155396Seric /* a match */ 1156396Seric delete++; 1157396Seric } 1158396Seric else 1159396Seric { 11601438Seric /* output it again */ 11612161Seric putpfent(pent, tfp); 1162396Seric others++; 1163396Seric } 1164396Seric } 1165396Seric 1166*12153Ssam /* 1167*12153Ssam * Before changing anything, make sure we can remove 1168*12153Ssam * the file in question (assuming it exists). 1169*12153Ssam */ 1170*12153Ssam if (delete) { 1171*12153Ssam extern int errno; 1172*12153Ssam 1173*12153Ssam cp = tail(fn); 1174*12153Ssam errno = 0; 1175*12153Ssam if (access(cp, 0) < 0 && errno != ENOENT) 1176*12153Ssam goto bad; 1177*12153Ssam if (errno == 0) 1178*12153Ssam /* 1179*12153Ssam * This is wrong, but the rest of the program 1180*12153Ssam * has built in assumptions about "." as well, 1181*12153Ssam * so why make unedit a special case? 1182*12153Ssam */ 1183*12153Ssam if (access(".", 2) < 0) { 1184*12153Ssam bad: 1185*12153Ssam printf("%12s: can't remove\n", cp); 1186*12153Ssam fclose(tfp); 1187*12153Ssam fclose(pfp); 1188*12153Ssam unlink(tfn); 1189*12153Ssam return (FALSE); 1190*12153Ssam } 1191*12153Ssam } 1192396Seric /* do final cleanup */ 1193396Seric if (others) 1194396Seric { 11951438Seric /* copy it back (perhaps it should be linked?) */ 1196396Seric if (freopen(tfn, "r", tfp) == NULL) 1197396Seric { 11981205Seric syserr("cannot reopen \"%s\"", tfn); 1199396Seric exit(EX_OSERR); 1200396Seric } 1201396Seric if (freopen(pfn, "w", pfp) == NULL) 1202396Seric { 12031205Seric usrerr("cannot create \"%s\"", pfn); 1204585Seric return (FALSE); 1205396Seric } 1206396Seric while (fgets(buf, sizeof buf, tfp) != NULL) 1207396Seric fputs(buf, pfp); 1208396Seric } 1209396Seric else 1210396Seric { 12111438Seric /* it's empty -- remove it */ 1212396Seric unlink(pfn); 1213396Seric } 1214396Seric fclose(tfp); 1215396Seric fclose(pfp); 1216396Seric unlink(tfn); 1217396Seric 12181438Seric /* actually remove the g-file */ 1219396Seric if (delete) 1220396Seric { 1221*12153Ssam /* 1222*12153Ssam * Since we've checked above, we can 1223*12153Ssam * use the return from unlink to 1224*12153Ssam * determine if the file existed or not. 1225*12153Ssam */ 1226*12153Ssam if (unlink(cp) >= 0) 1227*12153Ssam printf("%12s: removed\n", cp); 1228585Seric return (TRUE); 1229396Seric } 1230396Seric else 1231396Seric { 1232416Seric printf("%12s: not being edited by you\n", fn); 1233585Seric return (FALSE); 1234396Seric } 1235396Seric } 12361432Seric 12371432Seric /* 12381433Seric ** DODIFF -- diff an s-file against a g-file 12391433Seric ** 12401433Seric ** Parameters: 12411433Seric ** getv -- argv for the 'get' command. 12421433Seric ** gfile -- name of the g-file to diff against. 12431433Seric ** 12441433Seric ** Returns: 12451433Seric ** Result of get. 12461433Seric ** 12471433Seric ** Side Effects: 12481433Seric ** none. 12491433Seric */ 12501433Seric 12511433Seric dodiff(getv, gfile) 12521433Seric char **getv; 12531433Seric char *gfile; 12541433Seric { 12551433Seric int pipev[2]; 12561433Seric int rval; 12571433Seric register int i; 12581433Seric register int pid; 12591433Seric auto int st; 12601433Seric extern int errno; 12611433Seric int (*osig)(); 12621433Seric 12631905Seric printf("\n------- %s -------\n", gfile); 12641871Seric fflush(stdout); 12651501Seric 12661438Seric /* create context for diff to run in */ 12671433Seric if (pipe(pipev) < 0) 12681433Seric { 12691433Seric syserr("dodiff: pipe failed"); 12701433Seric exit(EX_OSERR); 12711433Seric } 12721433Seric if ((pid = fork()) < 0) 12731433Seric { 12741433Seric syserr("dodiff: fork failed"); 12751433Seric exit(EX_OSERR); 12761433Seric } 12771433Seric else if (pid > 0) 12781433Seric { 12791433Seric /* in parent; run get */ 12801433Seric OutFile = pipev[1]; 12811433Seric close(pipev[0]); 12821871Seric rval = command(&getv[1], TRUE, "get:rcixt -s -k -p"); 12831433Seric osig = signal(SIGINT, SIG_IGN); 12841433Seric while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR) 12851433Seric errno = 0; 12861433Seric signal(SIGINT, osig); 12871433Seric /* ignore result of diff */ 12881433Seric } 12891433Seric else 12901433Seric { 12911433Seric /* in child, run diff */ 12921433Seric if (close(pipev[1]) < 0 || close(0) < 0 || 12931433Seric dup(pipev[0]) != 0 || close(pipev[0]) < 0) 12941433Seric { 12951433Seric syserr("dodiff: magic failed"); 12961433Seric exit(EX_OSERR); 12971433Seric } 12981871Seric command(&getv[1], FALSE, "-diff:elsfhbC"); 12991433Seric } 13001433Seric return (rval); 13011433Seric } 13021433Seric 13031433Seric /* 13041435Seric ** TAIL -- return tail of filename. 13051435Seric ** 13061435Seric ** Parameters: 13071435Seric ** fn -- the filename. 13081435Seric ** 13091435Seric ** Returns: 13101435Seric ** a pointer to the tail of the filename; e.g., given 13111435Seric ** "cmd/ls.c", "ls.c" is returned. 13121435Seric ** 13131435Seric ** Side Effects: 13141435Seric ** none. 13151435Seric */ 13161435Seric 13171435Seric char * 13181435Seric tail(fn) 13191435Seric register char *fn; 13201435Seric { 13211435Seric register char *p; 13221435Seric 13231435Seric for (p = fn; *p != 0; p++) 13241435Seric if (*p == '/' && p[1] != '\0' && p[1] != '/') 13251435Seric fn = &p[1]; 13261435Seric return (fn); 13271435Seric } 13281435Seric 13291435Seric /* 13301822Seric ** GETPFENT -- get an entry from the p-file 1331396Seric ** 1332396Seric ** Parameters: 1333396Seric ** pfp -- p-file file pointer 1334396Seric ** 1335396Seric ** Returns: 1336396Seric ** pointer to p-file struct for next entry 1337396Seric ** NULL on EOF or error 1338396Seric ** 1339396Seric ** Side Effects: 1340396Seric ** Each call wipes out results of previous call. 1341396Seric */ 1342396Seric 1343396Seric struct pfile * 13441822Seric getpfent(pfp) 1345396Seric FILE *pfp; 1346396Seric { 1347396Seric static struct pfile ent; 134810110Srrh static char buf[PFILELG]; 1349396Seric register char *p; 1350396Seric extern char *nextfield(); 1351396Seric 1352396Seric if (fgets(buf, sizeof buf, pfp) == NULL) 1353396Seric return (NULL); 1354396Seric 1355396Seric ent.p_osid = p = buf; 1356396Seric ent.p_nsid = p = nextfield(p); 1357396Seric ent.p_user = p = nextfield(p); 1358396Seric ent.p_date = p = nextfield(p); 1359396Seric ent.p_time = p = nextfield(p); 13602161Seric ent.p_aux = p = nextfield(p); 1361396Seric 1362396Seric return (&ent); 1363396Seric } 1364396Seric 1365396Seric 1366396Seric char * 1367396Seric nextfield(p) 1368396Seric register char *p; 1369396Seric { 1370396Seric if (p == NULL || *p == '\0') 1371396Seric return (NULL); 1372396Seric while (*p != ' ' && *p != '\n' && *p != '\0') 1373396Seric p++; 1374396Seric if (*p == '\n' || *p == '\0') 1375396Seric { 1376396Seric *p = '\0'; 1377396Seric return (NULL); 1378396Seric } 1379396Seric *p++ = '\0'; 1380396Seric return (p); 1381396Seric } 13822161Seric /* 13832161Seric ** PUTPFENT -- output a p-file entry to a file 13842161Seric ** 13852161Seric ** Parameters: 13862161Seric ** pf -- the p-file entry 13872161Seric ** f -- the file to put it on. 13882161Seric ** 13892161Seric ** Returns: 13902161Seric ** none. 13912161Seric ** 13922161Seric ** Side Effects: 13932161Seric ** pf is written onto file f. 13942161Seric */ 13952161Seric 13962161Seric putpfent(pf, f) 13972161Seric register struct pfile *pf; 13982161Seric register FILE *f; 13992161Seric { 14002161Seric fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid, 14012161Seric pf->p_user, pf->p_date, pf->p_time); 14022161Seric if (pf->p_aux != NULL) 14032161Seric fprintf(f, " %s", pf->p_aux); 14042161Seric else 14052161Seric fprintf(f, "\n"); 14062161Seric } 14071432Seric 14081432Seric /* 14091205Seric ** USRERR -- issue user-level error 14101205Seric ** 14111205Seric ** Parameters: 14121205Seric ** f -- format string. 14131205Seric ** p1-p3 -- parameters to a printf. 14141205Seric ** 14151205Seric ** Returns: 14161205Seric ** -1 14171205Seric ** 14181205Seric ** Side Effects: 14191205Seric ** none. 14201205Seric */ 14211205Seric 14221738Seric /*VARARGS1*/ 14231205Seric usrerr(f, p1, p2, p3) 14241205Seric char *f; 14251205Seric { 14261205Seric fprintf(stderr, "\n%s: ", MyName); 14271205Seric fprintf(stderr, f, p1, p2, p3); 14281205Seric fprintf(stderr, "\n"); 14291205Seric 14301205Seric return (-1); 14311205Seric } 14321432Seric 14331432Seric /* 14341205Seric ** SYSERR -- print system-generated error. 14351205Seric ** 14361205Seric ** Parameters: 14371205Seric ** f -- format string to a printf. 14381205Seric ** p1, p2, p3 -- parameters to f. 14391205Seric ** 14401205Seric ** Returns: 14411205Seric ** never. 14421205Seric ** 14431205Seric ** Side Effects: 14441205Seric ** none. 14451205Seric */ 14461205Seric 14471738Seric /*VARARGS1*/ 14481205Seric syserr(f, p1, p2, p3) 14491205Seric char *f; 14501205Seric { 14511205Seric extern int errno; 14521205Seric 14531205Seric fprintf(stderr, "\n%s SYSERR: ", MyName); 14541205Seric fprintf(stderr, f, p1, p2, p3); 14551205Seric fprintf(stderr, "\n"); 14561205Seric if (errno == 0) 14571205Seric exit(EX_SOFTWARE); 14581205Seric else 14591205Seric { 14601738Seric perror(NULL); 14611205Seric exit(EX_OSERR); 14621205Seric } 14631205Seric } 14641864Seric /* 14651864Seric ** USERNAME -- return name of the current user 14661864Seric ** 14671864Seric ** Parameters: 14681864Seric ** none 14691864Seric ** 14701864Seric ** Returns: 14711864Seric ** name of current user 14721864Seric ** 14731864Seric ** Side Effects: 14741864Seric ** none 14751864Seric */ 14761864Seric 14771864Seric char * 14781864Seric username() 14791864Seric { 14801864Seric # ifdef UIDUSER 14811864Seric extern struct passwd *getpwuid(); 14821864Seric register struct passwd *pw; 14831864Seric 14841864Seric pw = getpwuid(getuid()); 14851864Seric if (pw == NULL) 14861864Seric { 14871864Seric syserr("who are you? (uid=%d)", getuid()); 14881864Seric exit(EX_OSERR); 14891864Seric } 14901864Seric return (pw->pw_name); 14911864Seric # else 14921905Seric extern char *getlogin(); 14936785Smckusick extern char *getenv(); 14946785Smckusick register char *p; 14951905Seric 14966785Smckusick p = getenv("USER"); 14976785Smckusick if (p == NULL || p[0] == '\0') 14986785Smckusick p = getlogin(); 14996785Smckusick return (p); 15001864Seric # endif UIDUSER 15011864Seric } 150210110Srrh 150310110Srrh /* 150410110Srrh ** Guarded string manipulation routines; the last argument 150510110Srrh ** is the length of the buffer into which the strcpy or strcat 150610110Srrh ** is to be done. 150710110Srrh */ 150810110Srrh char *gstrcat(to, from, length) 150910110Srrh char *to, *from; 151010110Srrh int length; 151110110Srrh { 151210110Srrh if (strlen(from) + strlen(to) >= length) { 151310110Srrh gstrbotch(to, from); 151410110Srrh } 151510110Srrh return(strcat(to, from)); 151610110Srrh } 151710110Srrh 151810110Srrh char *gstrncat(to, from, n, length) 151910110Srrh char *to, *from; 152010110Srrh int n; 152110110Srrh int length; 152210110Srrh { 152310110Srrh if (n + strlen(to) >= length) { 152410110Srrh gstrbotch(to, from); 152510110Srrh } 152610110Srrh return(strncat(to, from, n)); 152710110Srrh } 152810110Srrh 152910110Srrh char *gstrcpy(to, from, length) 153010110Srrh char *to, *from; 153110110Srrh int length; 153210110Srrh { 153310110Srrh if (strlen(from) >= length) { 153410110Srrh gstrbotch(from, (char *)0); 153510110Srrh } 153610110Srrh return(strcpy(to, from)); 153710110Srrh } 153810110Srrh gstrbotch(str1, str2) 153910110Srrh char *str1, *str2; 154010110Srrh { 154110110Srrh usrerr("Filename(s) too long: %s %s", str1, str2); 154210110Srrh } 1543