121574Sdist /* 221574Sdist * Copyright (c) 1980 Regents of the University of California. 3*34204Sbostic * All rights reserved. 4*34204Sbostic * 5*34204Sbostic * Redistribution and use in source and binary forms are permitted 6*34204Sbostic * provided that this notice is preserved and that due credit is given 7*34204Sbostic * to the University of California at Berkeley. The name of the University 8*34204Sbostic * may not be used to endorse or promote products derived from this 9*34204Sbostic * software without specific prior written permission. This software 10*34204Sbostic * is provided ``as is'' without express or implied warranty. 1121574Sdist */ 1221574Sdist 1313622Ssam #ifndef lint 1421574Sdist char copyright[] = 1521574Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1621574Sdist All rights reserved.\n"; 17*34204Sbostic #endif /* not lint */ 1813622Ssam 1921574Sdist #ifndef lint 20*34204Sbostic static char sccsid[] = "@(#)sccs.c 5.5 (Berkeley) 05/05/88"; 21*34204Sbostic #endif /* not lint */ 2221574Sdist 23148Seric # include <stdio.h> 246784Smckusick # include <sys/param.h> 25148Seric # include <sys/stat.h> 2613622Ssam # include <sys/dir.h> 271433Seric # include <errno.h> 281433Seric # include <signal.h> 29148Seric # include <sysexits.h> 302140Seric # include <pwd.h> 31148Seric 32828Seric /* 33828Seric ** SCCS.C -- human-oriented front end to the SCCS system. 34828Seric ** 35828Seric ** Without trying to add any functionality to speak of, this 36828Seric ** program tries to make SCCS a little more accessible to human 37828Seric ** types. The main thing it does is automatically put the 38828Seric ** string "SCCS/s." on the front of names. Also, it has a 39828Seric ** couple of things that are designed to shorten frequent 40828Seric ** combinations, e.g., "delget" which expands to a "delta" 41828Seric ** and a "get". 42828Seric ** 43828Seric ** This program can also function as a setuid front end. 44828Seric ** To do this, you should copy the source, renaming it to 45828Seric ** whatever you want, e.g., "syssccs". Change any defaults 46828Seric ** in the program (e.g., syssccs might default -d to 47828Seric ** "/usr/src/sys"). Then recompile and put the result 48828Seric ** as setuid to whomever you want. In this mode, sccs 49828Seric ** knows to not run setuid for certain programs in order 50828Seric ** to preserve security, and so forth. 51828Seric ** 52828Seric ** Usage: 53828Seric ** sccs [flags] command [args] 54828Seric ** 55828Seric ** Flags: 56828Seric ** -d<dir> <dir> represents a directory to search 57828Seric ** out of. It should be a full pathname 58828Seric ** for general usage. E.g., if <dir> is 59828Seric ** "/usr/src/sys", then a reference to the 60828Seric ** file "dev/bio.c" becomes a reference to 61828Seric ** "/usr/src/sys/dev/bio.c". 62828Seric ** -p<path> prepends <path> to the final component 63828Seric ** of the pathname. By default, this is 64828Seric ** "SCCS". For example, in the -d example 65828Seric ** above, the path then gets modified to 66828Seric ** "/usr/src/sys/dev/SCCS/s.bio.c". In 67828Seric ** more common usage (without the -d flag), 68828Seric ** "prog.c" would get modified to 69828Seric ** "SCCS/s.prog.c". In both cases, the 70828Seric ** "s." gets automatically prepended. 71828Seric ** -r run as the real user. 72828Seric ** 73828Seric ** Commands: 74828Seric ** admin, 75828Seric ** get, 76828Seric ** delta, 77828Seric ** rmdel, 7830959Sbostic ** cdc, 79828Seric ** etc. Straight out of SCCS; only difference 80828Seric ** is that pathnames get modified as 81828Seric ** described above. 8230959Sbostic ** enter Front end doing "sccs admin -i<name> <name>" 8330959Sbostic ** create Macro for "enter" followed by "get". 84828Seric ** edit Macro for "get -e". 85828Seric ** unedit Removes a file being edited, knowing 86828Seric ** about p-files, etc. 87828Seric ** delget Macro for "delta" followed by "get". 88828Seric ** deledit Macro for "delta" followed by "get -e". 8930959Sbostic ** branch Macro for "get -b -e", followed by "delta 9030959Sbostic ** -s -n", followd by "get -e -t -g". 9130959Sbostic ** diffs "diff" the specified version of files 9230959Sbostic ** and the checked-out version. 9330959Sbostic ** print Macro for "prs -e" followed by "get -p -m". 9430959Sbostic ** tell List what files are being edited. 9530959Sbostic ** info Print information about files being edited. 96828Seric ** clean Remove all files that can be 97828Seric ** regenerated from SCCS files. 981205Seric ** check Like info, but return exit status, for 99828Seric ** use in makefiles. 100828Seric ** fix Remove a top delta & reedit, but save 101828Seric ** the previous changes in that delta. 102828Seric ** 103828Seric ** Compilation Flags: 104828Seric ** UIDUSER -- determine who the user is by looking at the 105828Seric ** uid rather than the login name -- for machines 106828Seric ** where SCCS gets the user in this way. 1071270Seric ** SCCSDIR -- if defined, forces the -d flag to take on 1081205Seric ** this value. This is so that the setuid 1091205Seric ** aspects of this program cannot be abused. 1101270Seric ** This flag also disables the -p flag. 1111270Seric ** SCCSPATH -- the default for the -p flag. 1121437Seric ** MYNAME -- the title this program should print when it 1131437Seric ** gives error messages. 114828Seric ** 115828Seric ** Compilation Instructions: 116828Seric ** cc -O -n -s sccs.c 1171437Seric ** The flags listed above can be -D defined to simplify 1181437Seric ** recompilation for variant versions. 119828Seric ** 120828Seric ** Author: 121828Seric ** Eric Allman, UCB/INGRES 1221270Seric ** Copyright 1980 Regents of the University of California 123828Seric */ 124155Seric 1251432Seric 1261270Seric /******************* Configuration Information ********************/ 1271270Seric 1281437Seric # ifndef SCCSPATH 1291432Seric # define SCCSPATH "SCCS" /* pathname in which to find s-files */ 1301437Seric # endif NOT SCCSPATH 131828Seric 1321437Seric # ifndef MYNAME 1331437Seric # define MYNAME "sccs" /* name used for printing errors */ 1341437Seric # endif NOT MYNAME 1351270Seric 1361432Seric # ifndef PROGPATH 1376784Smckusick # define PROGPATH(name) "/usr/local/name" /* place to find binaries */ 1381432Seric # endif PROGPATH 1391432Seric 1401270Seric /**************** End of Configuration Information ****************/ 1411432Seric 142157Seric typedef char bool; 143200Seric # define TRUE 1 144200Seric # define FALSE 0 145157Seric 1461438Seric # define bitset(bit, word) ((bool) ((bit) & (word))) 1471438Seric 148148Seric struct sccsprog 149148Seric { 150148Seric char *sccsname; /* name of SCCS routine */ 151200Seric short sccsoper; /* opcode, see below */ 152200Seric short sccsflags; /* flags, see below */ 153148Seric char *sccspath; /* pathname of binary implementing */ 154148Seric }; 155148Seric 156200Seric /* values for sccsoper */ 157200Seric # define PROG 0 /* call a program */ 158201Seric # define CMACRO 1 /* command substitution macro */ 159226Seric # define FIX 2 /* fix a delta */ 160261Seric # define CLEAN 3 /* clean out recreatable files */ 161396Seric # define UNEDIT 4 /* unedit a file */ 1621431Seric # define SHELL 5 /* call a shell file (like PROG) */ 1631433Seric # define DIFFS 6 /* diff between sccs & file out */ 1641871Seric # define DODIFF 7 /* internal call to diff program */ 16510104Srrh # define ENTER 8 /* enter new files */ 166200Seric 167157Seric /* bits for sccsflags */ 168200Seric # define NO_SDOT 0001 /* no s. on front of args */ 169200Seric # define REALUSER 0002 /* protected (e.g., admin) */ 170148Seric 171819Seric /* modes for the "clean", "info", "check" ops */ 172819Seric # define CLEANC 0 /* clean command */ 173819Seric # define INFOC 1 /* info command */ 174819Seric # define CHECKC 2 /* check command */ 1751730Seric # define TELLC 3 /* give list of files being edited */ 176819Seric 1771432Seric /* 1781432Seric ** Description of commands known to this program. 1791432Seric ** First argument puts the command into a class. Second arg is 1801432Seric ** info regarding treatment of this command. Third arg is a 1811432Seric ** list of flags this command accepts from macros, etc. Fourth 1821432Seric ** arg is the pathname of the implementing program, or the 1831432Seric ** macro definition, or the arg to a sub-algorithm. 1841432Seric */ 185202Seric 186148Seric struct sccsprog SccsProg[] = 187148Seric { 1881864Seric "admin", PROG, REALUSER, PROGPATH(admin), 18930959Sbostic "cdc", PROG, 0, PROGPATH(rmdel), 1901864Seric "comb", PROG, 0, PROGPATH(comb), 1911864Seric "delta", PROG, 0, PROGPATH(delta), 1921864Seric "get", PROG, 0, PROGPATH(get), 1931864Seric "help", PROG, NO_SDOT, PROGPATH(help), 1942136Seric "prs", PROG, 0, PROGPATH(prs), 1951864Seric "prt", PROG, 0, PROGPATH(prt), 1961864Seric "rmdel", PROG, REALUSER, PROGPATH(rmdel), 1972136Seric "val", PROG, 0, PROGPATH(val), 1981864Seric "what", PROG, NO_SDOT, PROGPATH(what), 1991864Seric "sccsdiff", SHELL, REALUSER, PROGPATH(sccsdiff), 2001864Seric "edit", CMACRO, NO_SDOT, "get -e", 2011864Seric "delget", CMACRO, NO_SDOT, "delta:mysrp/get:ixbeskcl -t", 2022138Seric "deledit", CMACRO, NO_SDOT, "delta:mysrp -n/get:ixbskcl -e -t -g", 2031864Seric "fix", FIX, NO_SDOT, NULL, 2041864Seric "clean", CLEAN, REALUSER|NO_SDOT, (char *) CLEANC, 2051864Seric "info", CLEAN, REALUSER|NO_SDOT, (char *) INFOC, 2061864Seric "check", CLEAN, REALUSER|NO_SDOT, (char *) CHECKC, 2071864Seric "tell", CLEAN, REALUSER|NO_SDOT, (char *) TELLC, 2081864Seric "unedit", UNEDIT, NO_SDOT, NULL, 2091864Seric "diffs", DIFFS, NO_SDOT|REALUSER, NULL, 2101871Seric "-diff", DODIFF, NO_SDOT|REALUSER, PROGPATH(bdiff), 21130959Sbostic "print", CMACRO, 0, "prs -e/get -p -m -s", 2122226Seric "branch", CMACRO, NO_SDOT, 2132226Seric "get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g", 21410104Srrh "enter", ENTER, NO_SDOT, NULL, 21510104Srrh "create", CMACRO, NO_SDOT, "enter/get:ixbeskcl -t", 2161864Seric NULL, -1, 0, NULL 217148Seric }; 218148Seric 2191432Seric /* one line from a p-file */ 220396Seric struct pfile 221396Seric { 222396Seric char *p_osid; /* old SID */ 223396Seric char *p_nsid; /* new SID */ 224396Seric char *p_user; /* user who did edit */ 225396Seric char *p_date; /* date of get */ 226396Seric char *p_time; /* time of get */ 2272161Seric char *p_aux; /* extra info at end */ 228396Seric }; 229396Seric 2301270Seric char *SccsPath = SCCSPATH; /* pathname of SCCS files */ 2311270Seric # ifdef SCCSDIR 2321270Seric char *SccsDir = SCCSDIR; /* directory to begin search from */ 2331205Seric # else 2341270Seric char *SccsDir = ""; 2351205Seric # endif 2361437Seric char MyName[] = MYNAME; /* name used in messages */ 2371433Seric int OutFile = -1; /* override output file for commands */ 238157Seric bool RealUser; /* if set, running as real user */ 239393Seric # ifdef DEBUG 240393Seric bool Debug; /* turn on tracing */ 241393Seric # endif 2422139Seric # ifndef V6 2432139Seric extern char *getenv(); 2442139Seric # endif V6 24510110Srrh 24630959Sbostic extern char *sys_siglist[]; 24730959Sbostic 24810110Srrh char *gstrcat(), *strcat(); 24910110Srrh char *gstrncat(), *strncat(); 25010110Srrh char *gstrcpy(), *strcpy(); 25110110Srrh #define FBUFSIZ BUFSIZ 25210110Srrh #define PFILELG 120 2531432Seric 254148Seric main(argc, argv) 255148Seric int argc; 256148Seric char **argv; 257148Seric { 258148Seric register char *p; 259262Seric extern struct sccsprog *lookup(); 2601282Seric register int i; 2612139Seric # ifndef V6 2622139Seric # ifndef SCCSDIR 2632140Seric register struct passwd *pw; 2642140Seric extern struct passwd *getpwnam(); 26510110Srrh char buf[FBUFSIZ]; 2662140Seric 2672139Seric /* pull "SccsDir" out of the environment (possibly) */ 26810260Seric p = getenv("PROJECTDIR"); 2692140Seric if (p != NULL && p[0] != '\0') 2702140Seric { 2712140Seric if (p[0] == '/') 2722140Seric SccsDir = p; 2732140Seric else 2742140Seric { 2752140Seric pw = getpwnam(p); 2762140Seric if (pw == NULL) 2772140Seric { 2782140Seric usrerr("user %s does not exist", p); 2792140Seric exit(EX_USAGE); 2802140Seric } 28110110Srrh gstrcpy(buf, pw->pw_dir, sizeof(buf)); 28210110Srrh gstrcat(buf, "/src", sizeof(buf)); 2832140Seric if (access(buf, 0) < 0) 2842140Seric { 28510110Srrh gstrcpy(buf, pw->pw_dir, sizeof(buf)); 28610110Srrh gstrcat(buf, "/source", sizeof(buf)); 2872140Seric if (access(buf, 0) < 0) 2882140Seric { 2892140Seric usrerr("project %s has no source!", p); 2902140Seric exit(EX_USAGE); 2912140Seric } 2922140Seric } 2932140Seric SccsDir = buf; 2942140Seric } 2952140Seric } 2962139Seric # endif SCCSDIR 2972139Seric # endif V6 2982139Seric 299148Seric /* 300148Seric ** Detect and decode flags intended for this program. 301148Seric */ 302148Seric 303200Seric if (argc < 2) 304148Seric { 3051205Seric fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName); 306200Seric exit(EX_USAGE); 307200Seric } 308200Seric argv[argc] = NULL; 309200Seric 310262Seric if (lookup(argv[0]) == NULL) 311200Seric { 312262Seric while ((p = *++argv) != NULL) 313148Seric { 314262Seric if (*p != '-') 315262Seric break; 316262Seric switch (*++p) 317262Seric { 318262Seric case 'r': /* run as real user */ 319262Seric setuid(getuid()); 320262Seric RealUser++; 321262Seric break; 322148Seric 3231270Seric # ifndef SCCSDIR 324262Seric case 'p': /* path of sccs files */ 325262Seric SccsPath = ++p; 3262348Seric if (SccsPath[0] == '\0' && argv[1] != NULL) 3272348Seric SccsPath = *++argv; 328262Seric break; 329148Seric 330588Seric case 'd': /* directory to search from */ 331588Seric SccsDir = ++p; 3322348Seric if (SccsDir[0] == '\0' && argv[1] != NULL) 3332348Seric SccsDir = *++argv; 334588Seric break; 3351205Seric # endif 336588Seric 337393Seric # ifdef DEBUG 338393Seric case 'T': /* trace */ 339393Seric Debug++; 340393Seric break; 341393Seric # endif 342393Seric 343262Seric default: 3441205Seric usrerr("unknown option -%s", p); 345262Seric break; 346262Seric } 347148Seric } 348262Seric if (SccsPath[0] == '\0') 349262Seric SccsPath = "."; 350148Seric } 351148Seric 3521737Seric i = command(argv, FALSE, ""); 3531282Seric exit(i); 354200Seric } 3551432Seric 3561432Seric /* 3571282Seric ** COMMAND -- look up and perform a command 3581282Seric ** 3591282Seric ** This routine is the guts of this program. Given an 3601282Seric ** argument vector, it looks up the "command" (argv[0]) 3611282Seric ** in the configuration table and does the necessary stuff. 3621282Seric ** 3631282Seric ** Parameters: 3641282Seric ** argv -- an argument vector to process. 3651282Seric ** forkflag -- if set, fork before executing the command. 3661316Seric ** editflag -- if set, only include flags listed in the 3671316Seric ** sccsklets field of the command descriptor. 3681316Seric ** arg0 -- a space-seperated list of arguments to insert 3691316Seric ** before argv. 3701282Seric ** 3711282Seric ** Returns: 3721282Seric ** zero -- command executed ok. 3731282Seric ** else -- error status. 3741282Seric ** 3751282Seric ** Side Effects: 3761282Seric ** none. 3771282Seric */ 378157Seric 3791737Seric command(argv, forkflag, arg0) 380200Seric char **argv; 381201Seric bool forkflag; 3821316Seric char *arg0; 383200Seric { 384200Seric register struct sccsprog *cmd; 385200Seric register char *p; 38610110Srrh char buf[FBUFSIZ]; 387262Seric extern struct sccsprog *lookup(); 3881316Seric char *nav[1000]; 3891316Seric char **np; 3901431Seric register char **ap; 391585Seric register int i; 3921431Seric register char *q; 393585Seric extern bool unedit(); 3941282Seric int rval = 0; 3951316Seric extern char *index(); 3961316Seric extern char *makefile(); 3971737Seric char *editchs; 3981435Seric extern char *tail(); 399200Seric 400393Seric # ifdef DEBUG 401393Seric if (Debug) 402393Seric { 4031316Seric printf("command:\n\t\"%s\"\n", arg0); 4041316Seric for (np = argv; *np != NULL; np++) 4051316Seric printf("\t\"%s\"\n", *np); 406393Seric } 407393Seric # endif 408393Seric 409157Seric /* 4101316Seric ** Copy arguments. 4111438Seric ** Copy from arg0 & if necessary at most one arg 4121438Seric ** from argv[0]. 4131316Seric */ 4141316Seric 4151431Seric np = ap = &nav[1]; 4161737Seric editchs = NULL; 4171821Seric for (p = arg0, q = buf; *p != '\0' && *p != '/'; ) 4181316Seric { 4191316Seric *np++ = q; 4201316Seric while (*p == ' ') 4211316Seric p++; 4221737Seric while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':') 4231316Seric *q++ = *p++; 4241316Seric *q++ = '\0'; 4251737Seric if (*p == ':') 4261737Seric { 4271737Seric editchs = q; 4281821Seric while (*++p != '\0' && *p != '/' && *p != ' ') 4291737Seric *q++ = *p; 4301737Seric *q++ = '\0'; 4311737Seric } 4321316Seric } 4331316Seric *np = NULL; 4341431Seric if (*ap == NULL) 4351316Seric *np++ = *argv++; 4361316Seric 4371316Seric /* 438148Seric ** Look up command. 4391431Seric ** At this point, *ap is the command name. 440148Seric */ 441148Seric 4421431Seric cmd = lookup(*ap); 443262Seric if (cmd == NULL) 444148Seric { 4451431Seric usrerr("Unknown command \"%s\"", *ap); 4461282Seric return (EX_USAGE); 447148Seric } 448148Seric 449148Seric /* 4501316Seric ** Copy remaining arguments doing editing as appropriate. 4511316Seric */ 4521316Seric 4531316Seric for (; *argv != NULL; argv++) 4541316Seric { 4551316Seric p = *argv; 4561316Seric if (*p == '-') 4571316Seric { 4581737Seric if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL) 4591316Seric *np++ = p; 4601316Seric } 4611316Seric else 4621316Seric { 4631316Seric if (!bitset(NO_SDOT, cmd->sccsflags)) 4641316Seric p = makefile(p); 4651316Seric if (p != NULL) 4661316Seric *np++ = p; 4671316Seric } 4681316Seric } 4691316Seric *np = NULL; 4701316Seric 4711316Seric /* 472200Seric ** Interpret operation associated with this command. 473157Seric */ 474157Seric 475200Seric switch (cmd->sccsoper) 476200Seric { 4771431Seric case SHELL: /* call a shell file */ 4781431Seric *ap = cmd->sccspath; 4791431Seric *--ap = "sh"; 4801431Seric rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag); 4811431Seric break; 4821431Seric 483200Seric case PROG: /* call an sccs prog */ 4841431Seric rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag); 485201Seric break; 486201Seric 487201Seric case CMACRO: /* command macro */ 4881438Seric /* step through & execute each part of the macro */ 489201Seric for (p = cmd->sccspath; *p != '\0'; p++) 490201Seric { 4911316Seric q = p; 4921316Seric while (*p != '\0' && *p != '/') 4931316Seric p++; 4941737Seric rval = command(&ap[1], *p != '\0', q); 4951282Seric if (rval != 0) 4961282Seric break; 497201Seric } 4981282Seric break; 499157Seric 500226Seric case FIX: /* fix a delta */ 50130959Sbostic if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0) 502226Seric { 5031205Seric usrerr("-r flag needed for fix command"); 5041282Seric rval = EX_USAGE; 505226Seric break; 506226Seric } 5071438Seric 5081438Seric /* get the version with all changes */ 5091737Seric rval = command(&ap[1], TRUE, "get -k"); 5101438Seric 5111438Seric /* now remove that version from the s-file */ 5121282Seric if (rval == 0) 5131737Seric rval = command(&ap[1], TRUE, "rmdel:r"); 5141438Seric 5151438Seric /* and edit the old version (but don't clobber new vers) */ 5161282Seric if (rval == 0) 5171737Seric rval = command(&ap[2], FALSE, "get -e -g"); 5181282Seric break; 519226Seric 520261Seric case CLEAN: 5211822Seric rval = clean((int) cmd->sccspath, ap); 522261Seric break; 523261Seric 524396Seric case UNEDIT: 5251431Seric for (argv = np = &ap[1]; *argv != NULL; argv++) 526585Seric { 5271316Seric if (unedit(*argv)) 5281316Seric *np++ = *argv; 529585Seric } 5301316Seric *np = NULL; 5311438Seric 5321438Seric /* get all the files that we unedited successfully */ 5331738Seric if (np > &ap[1]) 5341737Seric rval = command(&ap[1], FALSE, "get"); 535396Seric break; 536396Seric 5371433Seric case DIFFS: /* diff between s-file & edit file */ 5381433Seric /* find the end of the flag arguments */ 5391433Seric for (np = &ap[1]; *np != NULL && **np == '-'; np++) 5401433Seric continue; 5411433Seric argv = np; 5421433Seric 5431433Seric /* for each file, do the diff */ 5441502Seric p = argv[1]; 5451433Seric while (*np != NULL) 5461433Seric { 5471438Seric /* messy, but we need a null terminated argv */ 5481433Seric *argv = *np++; 5491502Seric argv[1] = NULL; 5501435Seric i = dodiff(ap, tail(*argv)); 5511433Seric if (rval == 0) 5521433Seric rval = i; 5531502Seric argv[1] = p; 5541433Seric } 5551433Seric break; 5561433Seric 5571871Seric case DODIFF: /* internal diff call */ 5581871Seric setuid(getuid()); 5591871Seric for (np = ap; *np != NULL; np++) 5601871Seric { 5611871Seric if ((*np)[0] == '-' && (*np)[1] == 'C') 5621871Seric (*np)[1] = 'c'; 5631871Seric } 5641871Seric 5651871Seric /* insert "-" argument */ 5661871Seric np[1] = NULL; 5671871Seric np[0] = np[-1]; 5681871Seric np[-1] = "-"; 5691871Seric 5701871Seric /* execute the diff program of choice */ 5711871Seric # ifndef V6 5721871Seric execvp("diff", ap); 5731871Seric # endif 5741871Seric execv(cmd->sccspath, argv); 5751871Seric syserr("cannot exec %s", cmd->sccspath); 5761871Seric exit(EX_OSERR); 5771871Seric 57810104Srrh case ENTER: /* enter new sccs files */ 5796785Smckusick /* skip over flag arguments */ 5806785Smckusick for (np = &ap[1]; *np != NULL && **np == '-'; np++) 5816785Smckusick continue; 5826785Smckusick argv = np; 5836785Smckusick 5846785Smckusick /* do an admin for each file */ 5856785Smckusick p = argv[1]; 5866785Smckusick while (*np != NULL) 5876785Smckusick { 5886785Smckusick printf("\n%s:\n", *np); 58910110Srrh strcpy(buf, "-i"); 59010110Srrh gstrcat(buf, *np, sizeof(buf)); 5916785Smckusick ap[0] = buf; 5926785Smckusick argv[0] = tail(*np); 5936785Smckusick argv[1] = NULL; 5946785Smckusick rval = command(ap, TRUE, "admin"); 5956785Smckusick argv[1] = p; 5966785Smckusick if (rval == 0) 5976785Smckusick { 59810110Srrh strcpy(buf, ","); 59910110Srrh gstrcat(buf, tail(*np), sizeof(buf)); 6006785Smckusick if (link(*np, buf) >= 0) 6016785Smckusick unlink(*np); 6026785Smckusick } 6036785Smckusick np++; 6046785Smckusick } 6056785Smckusick break; 6066785Smckusick 607200Seric default: 6081205Seric syserr("oper %d", cmd->sccsoper); 609200Seric exit(EX_SOFTWARE); 610200Seric } 6111282Seric # ifdef DEBUG 6121282Seric if (Debug) 6131282Seric printf("command: rval=%d\n", rval); 6141282Seric # endif 6151282Seric return (rval); 616200Seric } 6171432Seric 6181432Seric /* 619262Seric ** LOOKUP -- look up an SCCS command name. 620262Seric ** 621262Seric ** Parameters: 622262Seric ** name -- the name of the command to look up. 623262Seric ** 624262Seric ** Returns: 625262Seric ** ptr to command descriptor for this command. 626262Seric ** NULL if no such entry. 627262Seric ** 628262Seric ** Side Effects: 629262Seric ** none. 630262Seric */ 631200Seric 632262Seric struct sccsprog * 633262Seric lookup(name) 634262Seric char *name; 635262Seric { 636262Seric register struct sccsprog *cmd; 637226Seric 638262Seric for (cmd = SccsProg; cmd->sccsname != NULL; cmd++) 639262Seric { 640262Seric if (strcmp(cmd->sccsname, name) == 0) 641262Seric return (cmd); 642262Seric } 643262Seric return (NULL); 644262Seric } 6451432Seric 6461432Seric /* 6471282Seric ** CALLPROG -- call a program 6481282Seric ** 6491316Seric ** Used to call the SCCS programs. 6501282Seric ** 6511282Seric ** Parameters: 6521282Seric ** progpath -- pathname of the program to call. 6531282Seric ** flags -- status flags from the command descriptors. 6541282Seric ** argv -- an argument vector to pass to the program. 6551282Seric ** forkflag -- if true, fork before calling, else just 6561282Seric ** exec. 6571282Seric ** 6581282Seric ** Returns: 6591282Seric ** The exit status of the program. 6601282Seric ** Nothing if forkflag == FALSE. 6611282Seric ** 6621282Seric ** Side Effects: 6631282Seric ** Can exit if forkflag == FALSE. 6641282Seric */ 665226Seric 666200Seric callprog(progpath, flags, argv, forkflag) 667200Seric char *progpath; 668200Seric short flags; 669200Seric char **argv; 670200Seric bool forkflag; 671200Seric { 672200Seric register int i; 67330959Sbostic register int wpid; 674201Seric auto int st; 67530959Sbostic register int sigcode; 67630959Sbostic register int coredumped; 67730959Sbostic register char *sigmsg; 67830959Sbostic auto char sigmsgbuf[10+1]; /* "Signal 127" + terminating '\0' */ 679200Seric 6801316Seric # ifdef DEBUG 6811316Seric if (Debug) 6821316Seric { 6831316Seric printf("callprog:\n"); 6841316Seric for (i = 0; argv[i] != NULL; i++) 6851316Seric printf("\t\"%s\"\n", argv[i]); 6861316Seric } 6871316Seric # endif 6881316Seric 689200Seric if (*argv == NULL) 690200Seric return (-1); 691200Seric 692157Seric /* 693226Seric ** Fork if appropriate. 694148Seric */ 695148Seric 696200Seric if (forkflag) 697200Seric { 698393Seric # ifdef DEBUG 699393Seric if (Debug) 700393Seric printf("Forking\n"); 701393Seric # endif 702200Seric i = fork(); 703200Seric if (i < 0) 704200Seric { 7051205Seric syserr("cannot fork"); 706200Seric exit(EX_OSERR); 707200Seric } 708200Seric else if (i > 0) 709201Seric { 71030959Sbostic while ((wpid = wait(&st)) != -1 && wpid != i) 71130959Sbostic ; 71230959Sbostic if ((sigcode = st & 0377) == 0) 7131282Seric st = (st >> 8) & 0377; 71430959Sbostic else 71530959Sbostic { 71630959Sbostic coredumped = sigcode & 0200; 71730959Sbostic sigcode &= 0177; 71830959Sbostic if (sigcode != SIGINT && sigcode != SIGPIPE) 71930959Sbostic { 72030959Sbostic if (sigcode < NSIG) 72130959Sbostic sigmsg = sys_siglist[sigcode]; 72230959Sbostic else 72330959Sbostic { 72430959Sbostic sprintf(sigmsgbuf, "Signal %d", 72530959Sbostic sigcode); 72630959Sbostic sigmsg = sigmsgbuf; 72730959Sbostic } 72830959Sbostic fprintf(stderr, "sccs: %s: %s%s", argv[0], 72930959Sbostic sigmsg, 73030959Sbostic coredumped ? " - core dumped": ""); 73130959Sbostic } 73230959Sbostic st = EX_SOFTWARE; 73330959Sbostic } 7341433Seric if (OutFile >= 0) 7351433Seric { 7361433Seric close(OutFile); 7371433Seric OutFile = -1; 7381433Seric } 739201Seric return (st); 740201Seric } 741200Seric } 7421433Seric else if (OutFile >= 0) 7431433Seric { 7441433Seric syserr("callprog: setting stdout w/o forking"); 7451433Seric exit(EX_SOFTWARE); 7461433Seric } 747200Seric 7481433Seric /* set protection as appropriate */ 749200Seric if (bitset(REALUSER, flags)) 750200Seric setuid(getuid()); 7511433Seric 7521433Seric /* change standard input & output if needed */ 7531433Seric if (OutFile >= 0) 7541433Seric { 7551433Seric close(1); 7561433Seric dup(OutFile); 7571433Seric close(OutFile); 7581433Seric } 759226Seric 7601433Seric /* call real SCCS program */ 761226Seric execv(progpath, argv); 7621205Seric syserr("cannot execute %s", progpath); 763148Seric exit(EX_UNAVAILABLE); 7641738Seric /*NOTREACHED*/ 765148Seric } 7661432Seric 7671432Seric /* 768586Seric ** MAKEFILE -- make filename of SCCS file 769586Seric ** 770586Seric ** If the name passed is already the name of an SCCS file, 771586Seric ** just return it. Otherwise, munge the name into the name 772586Seric ** of the actual SCCS file. 773586Seric ** 774586Seric ** There are cases when it is not clear what you want to 775586Seric ** do. For example, if SccsPath is an absolute pathname 776586Seric ** and the name given is also an absolute pathname, we go 777586Seric ** for SccsPath (& only use the last component of the name 778586Seric ** passed) -- this is important for security reasons (if 779586Seric ** sccs is being used as a setuid front end), but not 780586Seric ** particularly intuitive. 781586Seric ** 782586Seric ** Parameters: 783586Seric ** name -- the file name to be munged. 784586Seric ** 785586Seric ** Returns: 786586Seric ** The pathname of the sccs file. 787586Seric ** NULL on error. 788586Seric ** 789586Seric ** Side Effects: 790586Seric ** none. 791586Seric */ 792148Seric 793148Seric char * 794148Seric makefile(name) 795148Seric char *name; 796148Seric { 797148Seric register char *p; 79810110Srrh char buf[3*FBUFSIZ]; 799148Seric extern char *malloc(); 800586Seric extern char *rindex(); 801588Seric extern bool safepath(); 802587Seric extern bool isdir(); 803587Seric register char *q; 804148Seric 805586Seric p = rindex(name, '/'); 806586Seric if (p == NULL) 807586Seric p = name; 808586Seric else 809586Seric p++; 810586Seric 811148Seric /* 812588Seric ** Check to see that the path is "safe", i.e., that we 813588Seric ** are not letting some nasty person use the setuid part 814588Seric ** of this program to look at or munge some presumably 815588Seric ** hidden files. 816148Seric */ 817148Seric 818588Seric if (SccsDir[0] == '/' && !safepath(name)) 819588Seric return (NULL); 820586Seric 821586Seric /* 822588Seric ** Create the base pathname. 823586Seric */ 824586Seric 8251438Seric /* first the directory part */ 826588Seric if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0) 827148Seric { 82810110Srrh gstrcpy(buf, SccsDir, sizeof(buf)); 82910110Srrh gstrcat(buf, "/", sizeof(buf)); 830586Seric } 831586Seric else 83210110Srrh gstrcpy(buf, "", sizeof(buf)); 8331438Seric 8341438Seric /* then the head of the pathname */ 83510110Srrh gstrncat(buf, name, p - name, sizeof(buf)); 836587Seric q = &buf[strlen(buf)]; 8371438Seric 8381438Seric /* now copy the final part of the name, in case useful */ 83910110Srrh gstrcpy(q, p, sizeof(buf)); 8401438Seric 8411438Seric /* so is it useful? */ 842587Seric if (strncmp(p, "s.", 2) != 0 && !isdir(buf)) 843586Seric { 8441438Seric /* sorry, no; copy the SCCS pathname & the "s." */ 84510110Srrh gstrcpy(q, SccsPath, sizeof(buf)); 84610110Srrh gstrcat(buf, "/s.", sizeof(buf)); 8471438Seric 8481438Seric /* and now the end of the name */ 84910110Srrh gstrcat(buf, p, sizeof(buf)); 850586Seric } 851148Seric 8521438Seric /* if i haven't changed it, why did I do all this? */ 853588Seric if (strcmp(buf, name) == 0) 854588Seric p = name; 855588Seric else 856148Seric { 8571438Seric /* but if I have, squirrel it away */ 858588Seric p = malloc(strlen(buf) + 1); 859588Seric if (p == NULL) 860588Seric { 861588Seric perror("Sccs: no mem"); 862588Seric exit(EX_OSERR); 863588Seric } 864588Seric strcpy(p, buf); 865148Seric } 8661438Seric 867148Seric return (p); 868148Seric } 8691432Seric 8701432Seric /* 871587Seric ** ISDIR -- return true if the argument is a directory. 872587Seric ** 873587Seric ** Parameters: 874587Seric ** name -- the pathname of the file to check. 875587Seric ** 876587Seric ** Returns: 877587Seric ** TRUE if 'name' is a directory, FALSE otherwise. 878587Seric ** 879587Seric ** Side Effects: 880587Seric ** none. 881587Seric */ 882587Seric 883587Seric bool 884587Seric isdir(name) 885587Seric char *name; 886587Seric { 887587Seric struct stat stbuf; 888587Seric 889587Seric return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR); 890587Seric } 8911432Seric 8921432Seric /* 893586Seric ** SAFEPATH -- determine whether a pathname is "safe" 894586Seric ** 895586Seric ** "Safe" pathnames only allow you to get deeper into the 896586Seric ** directory structure, i.e., full pathnames and ".." are 897586Seric ** not allowed. 898586Seric ** 899586Seric ** Parameters: 900586Seric ** p -- the name to check. 901586Seric ** 902586Seric ** Returns: 903586Seric ** TRUE -- if the path is safe. 904586Seric ** FALSE -- if the path is not safe. 905586Seric ** 906586Seric ** Side Effects: 907586Seric ** Prints a message if the path is not safe. 908586Seric */ 909586Seric 910586Seric bool 911586Seric safepath(p) 912586Seric register char *p; 913586Seric { 914586Seric extern char *index(); 915586Seric 916586Seric if (*p != '/') 917586Seric { 918586Seric while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0) 919586Seric { 920586Seric p = index(p, '/'); 921586Seric if (p == NULL) 922586Seric return (TRUE); 923586Seric p++; 924586Seric } 925586Seric } 926586Seric 927586Seric printf("You may not use full pathnames or \"..\"\n"); 928586Seric return (FALSE); 929586Seric } 9301432Seric 9311432Seric /* 932261Seric ** CLEAN -- clean out recreatable files 933261Seric ** 934261Seric ** Any file for which an "s." file exists but no "p." file 935261Seric ** exists in the current directory is purged. 936261Seric ** 937261Seric ** Parameters: 9381822Seric ** mode -- tells whether this came from a "clean", "info", or 9391822Seric ** "check" command. 9401822Seric ** argv -- the rest of the argument vector. 941261Seric ** 942261Seric ** Returns: 943261Seric ** none. 944261Seric ** 945261Seric ** Side Effects: 946819Seric ** Removes files in the current directory. 947819Seric ** Prints information regarding files being edited. 948819Seric ** Exits if a "check" command. 949261Seric */ 950261Seric 9511822Seric clean(mode, argv) 952819Seric int mode; 9531822Seric char **argv; 954261Seric { 9556784Smckusick struct direct *dir; 95610110Srrh char buf[FBUFSIZ]; 9572140Seric char *bufend; 95830466Smckusick register DIR *dirp; 959346Seric register char *basefile; 960351Seric bool gotedit; 9611822Seric bool gotpfent; 962394Seric FILE *pfp; 9631822Seric bool nobranch = FALSE; 9641822Seric extern struct pfile *getpfent(); 9651822Seric register struct pfile *pf; 9661822Seric register char **ap; 9671864Seric extern char *username(); 9681864Seric char *usernm = NULL; 9692140Seric char *subdir = NULL; 9702140Seric char *cmdname; 971261Seric 9721438Seric /* 9731822Seric ** Process the argv 9741822Seric */ 9751822Seric 9762140Seric cmdname = *argv; 9772140Seric for (ap = argv; *++ap != NULL; ) 9781822Seric { 9791864Seric if (**ap == '-') 9801864Seric { 9811864Seric /* we have a flag */ 9821864Seric switch ((*ap)[1]) 9831864Seric { 9841864Seric case 'b': 9851864Seric nobranch = TRUE; 9861864Seric break; 9871864Seric 9881864Seric case 'u': 9891864Seric if ((*ap)[2] != '\0') 9901864Seric usernm = &(*ap)[2]; 9911864Seric else if (ap[1] != NULL && ap[1][0] != '-') 9921864Seric usernm = *++ap; 9931864Seric else 9941864Seric usernm = username(); 9951864Seric break; 9961864Seric } 9971864Seric } 9982140Seric else 9992140Seric { 10002140Seric if (subdir != NULL) 10012140Seric usrerr("too many args"); 10022140Seric else 10032140Seric subdir = *ap; 10042140Seric } 10051822Seric } 10061822Seric 10071822Seric /* 10081438Seric ** Find and open the SCCS directory. 10091438Seric */ 10101438Seric 101110110Srrh gstrcpy(buf, SccsDir, sizeof(buf)); 10121207Seric if (buf[0] != '\0') 101310110Srrh gstrcat(buf, "/", sizeof(buf)); 10142140Seric if (subdir != NULL) 10152140Seric { 101610110Srrh gstrcat(buf, subdir, sizeof(buf)); 101710110Srrh gstrcat(buf, "/", sizeof(buf)); 10182140Seric } 101910110Srrh gstrcat(buf, SccsPath, sizeof(buf)); 10202140Seric bufend = &buf[strlen(buf)]; 10211438Seric 102230466Smckusick dirp = opendir(buf); 102330466Smckusick if (dirp == NULL) 1024261Seric { 10251207Seric usrerr("cannot open %s", buf); 10261282Seric return (EX_NOINPUT); 1027261Seric } 1028261Seric 1029261Seric /* 1030261Seric ** Scan the SCCS directory looking for s. files. 10311438Seric ** gotedit tells whether we have tried to clean any 10321438Seric ** files that are being edited. 1033261Seric */ 1034261Seric 1035351Seric gotedit = FALSE; 103630466Smckusick while (dir = readdir(dirp)) { 10376784Smckusick if (strncmp(dir->d_name, "s.", 2) != 0) 1038261Seric continue; 1039261Seric 1040261Seric /* got an s. file -- see if the p. file exists */ 104110110Srrh gstrcpy(bufend, "/p.", sizeof(buf)); 10422140Seric basefile = bufend + 3; 104310110Srrh gstrcpy(basefile, &dir->d_name[2], sizeof(buf)); 10441822Seric 10451822Seric /* 10461822Seric ** open and scan the p-file. 10471822Seric ** 'gotpfent' tells if we have found a valid p-file 10481822Seric ** entry. 10491822Seric */ 10501822Seric 1051394Seric pfp = fopen(buf, "r"); 10521822Seric gotpfent = FALSE; 1053394Seric if (pfp != NULL) 1054346Seric { 10551438Seric /* the file exists -- report it's contents */ 10561822Seric while ((pf = getpfent(pfp)) != NULL) 10571730Seric { 10581822Seric if (nobranch && isbranch(pf->p_nsid)) 10591822Seric continue; 10601864Seric if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC) 10611864Seric continue; 10621822Seric gotedit = TRUE; 10631822Seric gotpfent = TRUE; 10641822Seric if (mode == TELLC) 10651822Seric { 10661822Seric printf("%s\n", basefile); 10671822Seric break; 10681822Seric } 10692161Seric printf("%12s: being edited: ", basefile); 10702161Seric putpfent(pf, stdout); 10711730Seric } 1072394Seric fclose(pfp); 10731822Seric } 1074261Seric 1075261Seric /* the s. file exists and no p. file exists -- unlink the g-file */ 10761870Seric if (mode == CLEANC && !gotpfent) 1077346Seric { 107810110Srrh char unlinkbuf[FBUFSIZ]; 107910110Srrh gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf)); 108010103Srrh unlink(unlinkbuf); 1081346Seric } 1082261Seric } 1083261Seric 10841438Seric /* cleanup & report results */ 108530466Smckusick closedir(dirp); 1086819Seric if (!gotedit && mode == INFOC) 10871864Seric { 10881864Seric printf("Nothing being edited"); 10891864Seric if (nobranch) 10901864Seric printf(" (on trunk)"); 10911864Seric if (usernm == NULL) 10921864Seric printf("\n"); 10931864Seric else 10941864Seric printf(" by %s\n", usernm); 10951864Seric } 1096819Seric if (mode == CHECKC) 1097819Seric exit(gotedit); 10981282Seric return (EX_OK); 1099261Seric } 11001432Seric 11011432Seric /* 11021822Seric ** ISBRANCH -- is the SID a branch? 11031822Seric ** 11041822Seric ** Parameters: 11051822Seric ** sid -- the sid to check. 11061822Seric ** 11071822Seric ** Returns: 11081822Seric ** TRUE if the sid represents a branch. 11091822Seric ** FALSE otherwise. 11101822Seric ** 11111822Seric ** Side Effects: 11121822Seric ** none. 11131822Seric */ 11141822Seric 11151822Seric isbranch(sid) 11161822Seric char *sid; 11171822Seric { 11181822Seric register char *p; 11191822Seric int dots; 11201822Seric 11211822Seric dots = 0; 11221822Seric for (p = sid; *p != '\0'; p++) 11231822Seric { 11241822Seric if (*p == '.') 11251822Seric dots++; 11261822Seric if (dots > 1) 11271822Seric return (TRUE); 11281822Seric } 11291822Seric return (FALSE); 11301822Seric } 11311822Seric 11321822Seric /* 1133396Seric ** UNEDIT -- unedit a file 1134396Seric ** 1135396Seric ** Checks to see that the current user is actually editting 1136396Seric ** the file and arranges that s/he is not editting it. 1137396Seric ** 1138396Seric ** Parameters: 1139416Seric ** fn -- the name of the file to be unedited. 1140396Seric ** 1141396Seric ** Returns: 1142585Seric ** TRUE -- if the file was successfully unedited. 1143585Seric ** FALSE -- if the file was not unedited for some 1144585Seric ** reason. 1145396Seric ** 1146396Seric ** Side Effects: 1147396Seric ** fn is removed 1148396Seric ** entries are removed from pfile. 1149396Seric */ 1150396Seric 1151585Seric bool 1152396Seric unedit(fn) 1153396Seric char *fn; 1154396Seric { 1155396Seric register FILE *pfp; 115612153Ssam char *cp, *pfn; 1157396Seric static char tfn[] = "/tmp/sccsXXXXX"; 1158396Seric FILE *tfp; 1159396Seric register char *q; 1160396Seric bool delete = FALSE; 1161396Seric bool others = FALSE; 1162396Seric char *myname; 11631864Seric extern char *username(); 1164396Seric struct pfile *pent; 11651822Seric extern struct pfile *getpfent(); 116610110Srrh char buf[PFILELG]; 116733251Sbostic extern char *makefile(), *rindex(), *tail(); 1168396Seric 1169396Seric /* make "s." filename & find the trailing component */ 1170396Seric pfn = makefile(fn); 1171586Seric if (pfn == NULL) 1172586Seric return (FALSE); 1173586Seric q = rindex(pfn, '/'); 1174586Seric if (q == NULL) 1175586Seric q = &pfn[-1]; 1176586Seric if (q[1] != 's' || q[2] != '.') 1177396Seric { 11781205Seric usrerr("bad file name \"%s\"", fn); 1179585Seric return (FALSE); 1180396Seric } 1181396Seric 11821438Seric /* turn "s." into "p." & try to open it */ 1183396Seric *++q = 'p'; 1184396Seric 1185396Seric pfp = fopen(pfn, "r"); 1186396Seric if (pfp == NULL) 1187396Seric { 1188416Seric printf("%12s: not being edited\n", fn); 1189585Seric return (FALSE); 1190396Seric } 1191396Seric 11921438Seric /* create temp file for editing p-file */ 1193396Seric mktemp(tfn); 1194396Seric tfp = fopen(tfn, "w"); 1195396Seric if (tfp == NULL) 1196396Seric { 11971205Seric usrerr("cannot create \"%s\"", tfn); 1198396Seric exit(EX_OSERR); 1199396Seric } 1200396Seric 12011438Seric /* figure out who I am */ 12021864Seric myname = username(); 12031438Seric 12041438Seric /* 12051438Seric ** Copy p-file to temp file, doing deletions as needed. 12061438Seric */ 12071438Seric 12081822Seric while ((pent = getpfent(pfp)) != NULL) 1209396Seric { 1210396Seric if (strcmp(pent->p_user, myname) == 0) 1211396Seric { 1212396Seric /* a match */ 1213396Seric delete++; 1214396Seric } 1215396Seric else 1216396Seric { 12171438Seric /* output it again */ 12182161Seric putpfent(pent, tfp); 1219396Seric others++; 1220396Seric } 1221396Seric } 1222396Seric 122312153Ssam /* 122412153Ssam * Before changing anything, make sure we can remove 122512153Ssam * the file in question (assuming it exists). 122612153Ssam */ 122712153Ssam if (delete) { 122812153Ssam extern int errno; 122912153Ssam 123012153Ssam cp = tail(fn); 123112153Ssam errno = 0; 123212153Ssam if (access(cp, 0) < 0 && errno != ENOENT) 123312153Ssam goto bad; 123412153Ssam if (errno == 0) 123512153Ssam /* 123612153Ssam * This is wrong, but the rest of the program 123712153Ssam * has built in assumptions about "." as well, 123812153Ssam * so why make unedit a special case? 123912153Ssam */ 124012153Ssam if (access(".", 2) < 0) { 124112153Ssam bad: 124212153Ssam printf("%12s: can't remove\n", cp); 124312153Ssam fclose(tfp); 124412153Ssam fclose(pfp); 124512153Ssam unlink(tfn); 124612153Ssam return (FALSE); 124712153Ssam } 124812153Ssam } 1249396Seric /* do final cleanup */ 1250396Seric if (others) 1251396Seric { 12521438Seric /* copy it back (perhaps it should be linked?) */ 1253396Seric if (freopen(tfn, "r", tfp) == NULL) 1254396Seric { 12551205Seric syserr("cannot reopen \"%s\"", tfn); 1256396Seric exit(EX_OSERR); 1257396Seric } 1258396Seric if (freopen(pfn, "w", pfp) == NULL) 1259396Seric { 12601205Seric usrerr("cannot create \"%s\"", pfn); 1261585Seric return (FALSE); 1262396Seric } 1263396Seric while (fgets(buf, sizeof buf, tfp) != NULL) 1264396Seric fputs(buf, pfp); 1265396Seric } 1266396Seric else 1267396Seric { 12681438Seric /* it's empty -- remove it */ 1269396Seric unlink(pfn); 1270396Seric } 1271396Seric fclose(tfp); 1272396Seric fclose(pfp); 1273396Seric unlink(tfn); 1274396Seric 12751438Seric /* actually remove the g-file */ 1276396Seric if (delete) 1277396Seric { 127812153Ssam /* 127912153Ssam * Since we've checked above, we can 128012153Ssam * use the return from unlink to 128112153Ssam * determine if the file existed or not. 128212153Ssam */ 128312153Ssam if (unlink(cp) >= 0) 128412153Ssam printf("%12s: removed\n", cp); 1285585Seric return (TRUE); 1286396Seric } 1287396Seric else 1288396Seric { 1289416Seric printf("%12s: not being edited by you\n", fn); 1290585Seric return (FALSE); 1291396Seric } 1292396Seric } 12931432Seric 12941432Seric /* 12951433Seric ** DODIFF -- diff an s-file against a g-file 12961433Seric ** 12971433Seric ** Parameters: 12981433Seric ** getv -- argv for the 'get' command. 12991433Seric ** gfile -- name of the g-file to diff against. 13001433Seric ** 13011433Seric ** Returns: 13021433Seric ** Result of get. 13031433Seric ** 13041433Seric ** Side Effects: 13051433Seric ** none. 13061433Seric */ 13071433Seric 13081433Seric dodiff(getv, gfile) 13091433Seric char **getv; 13101433Seric char *gfile; 13111433Seric { 13121433Seric int pipev[2]; 13131433Seric int rval; 13141433Seric register int i; 13151433Seric register int pid; 13161433Seric auto int st; 13171433Seric extern int errno; 13181433Seric int (*osig)(); 13191433Seric 13201905Seric printf("\n------- %s -------\n", gfile); 13211871Seric fflush(stdout); 13221501Seric 13231438Seric /* create context for diff to run in */ 13241433Seric if (pipe(pipev) < 0) 13251433Seric { 13261433Seric syserr("dodiff: pipe failed"); 13271433Seric exit(EX_OSERR); 13281433Seric } 13291433Seric if ((pid = fork()) < 0) 13301433Seric { 13311433Seric syserr("dodiff: fork failed"); 13321433Seric exit(EX_OSERR); 13331433Seric } 13341433Seric else if (pid > 0) 13351433Seric { 13361433Seric /* in parent; run get */ 13371433Seric OutFile = pipev[1]; 13381433Seric close(pipev[0]); 13391871Seric rval = command(&getv[1], TRUE, "get:rcixt -s -k -p"); 13401433Seric osig = signal(SIGINT, SIG_IGN); 13411433Seric while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR) 13421433Seric errno = 0; 13431433Seric signal(SIGINT, osig); 13441433Seric /* ignore result of diff */ 13451433Seric } 13461433Seric else 13471433Seric { 13481433Seric /* in child, run diff */ 13491433Seric if (close(pipev[1]) < 0 || close(0) < 0 || 13501433Seric dup(pipev[0]) != 0 || close(pipev[0]) < 0) 13511433Seric { 13521433Seric syserr("dodiff: magic failed"); 13531433Seric exit(EX_OSERR); 13541433Seric } 13551871Seric command(&getv[1], FALSE, "-diff:elsfhbC"); 13561433Seric } 13571433Seric return (rval); 13581433Seric } 13591433Seric 13601433Seric /* 13611435Seric ** TAIL -- return tail of filename. 13621435Seric ** 13631435Seric ** Parameters: 13641435Seric ** fn -- the filename. 13651435Seric ** 13661435Seric ** Returns: 13671435Seric ** a pointer to the tail of the filename; e.g., given 13681435Seric ** "cmd/ls.c", "ls.c" is returned. 13691435Seric ** 13701435Seric ** Side Effects: 13711435Seric ** none. 13721435Seric */ 13731435Seric 13741435Seric char * 13751435Seric tail(fn) 13761435Seric register char *fn; 13771435Seric { 13781435Seric register char *p; 13791435Seric 13801435Seric for (p = fn; *p != 0; p++) 13811435Seric if (*p == '/' && p[1] != '\0' && p[1] != '/') 13821435Seric fn = &p[1]; 13831435Seric return (fn); 13841435Seric } 13851435Seric 13861435Seric /* 13871822Seric ** GETPFENT -- get an entry from the p-file 1388396Seric ** 1389396Seric ** Parameters: 1390396Seric ** pfp -- p-file file pointer 1391396Seric ** 1392396Seric ** Returns: 1393396Seric ** pointer to p-file struct for next entry 1394396Seric ** NULL on EOF or error 1395396Seric ** 1396396Seric ** Side Effects: 1397396Seric ** Each call wipes out results of previous call. 1398396Seric */ 1399396Seric 1400396Seric struct pfile * 14011822Seric getpfent(pfp) 1402396Seric FILE *pfp; 1403396Seric { 1404396Seric static struct pfile ent; 140510110Srrh static char buf[PFILELG]; 1406396Seric register char *p; 1407396Seric extern char *nextfield(); 1408396Seric 1409396Seric if (fgets(buf, sizeof buf, pfp) == NULL) 1410396Seric return (NULL); 1411396Seric 1412396Seric ent.p_osid = p = buf; 1413396Seric ent.p_nsid = p = nextfield(p); 1414396Seric ent.p_user = p = nextfield(p); 1415396Seric ent.p_date = p = nextfield(p); 1416396Seric ent.p_time = p = nextfield(p); 14172161Seric ent.p_aux = p = nextfield(p); 1418396Seric 1419396Seric return (&ent); 1420396Seric } 1421396Seric 1422396Seric 1423396Seric char * 1424396Seric nextfield(p) 1425396Seric register char *p; 1426396Seric { 1427396Seric if (p == NULL || *p == '\0') 1428396Seric return (NULL); 1429396Seric while (*p != ' ' && *p != '\n' && *p != '\0') 1430396Seric p++; 1431396Seric if (*p == '\n' || *p == '\0') 1432396Seric { 1433396Seric *p = '\0'; 1434396Seric return (NULL); 1435396Seric } 1436396Seric *p++ = '\0'; 1437396Seric return (p); 1438396Seric } 14392161Seric /* 14402161Seric ** PUTPFENT -- output a p-file entry to a file 14412161Seric ** 14422161Seric ** Parameters: 14432161Seric ** pf -- the p-file entry 14442161Seric ** f -- the file to put it on. 14452161Seric ** 14462161Seric ** Returns: 14472161Seric ** none. 14482161Seric ** 14492161Seric ** Side Effects: 14502161Seric ** pf is written onto file f. 14512161Seric */ 14522161Seric 14532161Seric putpfent(pf, f) 14542161Seric register struct pfile *pf; 14552161Seric register FILE *f; 14562161Seric { 14572161Seric fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid, 14582161Seric pf->p_user, pf->p_date, pf->p_time); 14592161Seric if (pf->p_aux != NULL) 14602161Seric fprintf(f, " %s", pf->p_aux); 14612161Seric else 14622161Seric fprintf(f, "\n"); 14632161Seric } 14641432Seric 14651432Seric /* 14661205Seric ** USRERR -- issue user-level error 14671205Seric ** 14681205Seric ** Parameters: 14691205Seric ** f -- format string. 14701205Seric ** p1-p3 -- parameters to a printf. 14711205Seric ** 14721205Seric ** Returns: 14731205Seric ** -1 14741205Seric ** 14751205Seric ** Side Effects: 14761205Seric ** none. 14771205Seric */ 14781205Seric 14791738Seric /*VARARGS1*/ 14801205Seric usrerr(f, p1, p2, p3) 14811205Seric char *f; 14821205Seric { 14831205Seric fprintf(stderr, "\n%s: ", MyName); 14841205Seric fprintf(stderr, f, p1, p2, p3); 14851205Seric fprintf(stderr, "\n"); 14861205Seric 14871205Seric return (-1); 14881205Seric } 14891432Seric 14901432Seric /* 14911205Seric ** SYSERR -- print system-generated error. 14921205Seric ** 14931205Seric ** Parameters: 14941205Seric ** f -- format string to a printf. 14951205Seric ** p1, p2, p3 -- parameters to f. 14961205Seric ** 14971205Seric ** Returns: 14981205Seric ** never. 14991205Seric ** 15001205Seric ** Side Effects: 15011205Seric ** none. 15021205Seric */ 15031205Seric 15041738Seric /*VARARGS1*/ 15051205Seric syserr(f, p1, p2, p3) 15061205Seric char *f; 15071205Seric { 15081205Seric extern int errno; 15091205Seric 15101205Seric fprintf(stderr, "\n%s SYSERR: ", MyName); 15111205Seric fprintf(stderr, f, p1, p2, p3); 15121205Seric fprintf(stderr, "\n"); 15131205Seric if (errno == 0) 15141205Seric exit(EX_SOFTWARE); 15151205Seric else 15161205Seric { 15171738Seric perror(NULL); 15181205Seric exit(EX_OSERR); 15191205Seric } 15201205Seric } 15211864Seric /* 15221864Seric ** USERNAME -- return name of the current user 15231864Seric ** 15241864Seric ** Parameters: 15251864Seric ** none 15261864Seric ** 15271864Seric ** Returns: 15281864Seric ** name of current user 15291864Seric ** 15301864Seric ** Side Effects: 15311864Seric ** none 15321864Seric */ 15331864Seric 15341864Seric char * 15351864Seric username() 15361864Seric { 15371864Seric # ifdef UIDUSER 15381864Seric extern struct passwd *getpwuid(); 15391864Seric register struct passwd *pw; 15401864Seric 15411864Seric pw = getpwuid(getuid()); 15421864Seric if (pw == NULL) 15431864Seric { 15441864Seric syserr("who are you? (uid=%d)", getuid()); 15451864Seric exit(EX_OSERR); 15461864Seric } 15471864Seric return (pw->pw_name); 15481864Seric # else 15491905Seric extern char *getlogin(); 15506785Smckusick register char *p; 15511905Seric 15526785Smckusick p = getenv("USER"); 15536785Smckusick if (p == NULL || p[0] == '\0') 15546785Smckusick p = getlogin(); 15556785Smckusick return (p); 15561864Seric # endif UIDUSER 15571864Seric } 155810110Srrh 155910110Srrh /* 156010110Srrh ** Guarded string manipulation routines; the last argument 156110110Srrh ** is the length of the buffer into which the strcpy or strcat 156210110Srrh ** is to be done. 156310110Srrh */ 156410110Srrh char *gstrcat(to, from, length) 156510110Srrh char *to, *from; 156610110Srrh int length; 156710110Srrh { 156810110Srrh if (strlen(from) + strlen(to) >= length) { 156910110Srrh gstrbotch(to, from); 157010110Srrh } 157110110Srrh return(strcat(to, from)); 157210110Srrh } 157310110Srrh 157410110Srrh char *gstrncat(to, from, n, length) 157510110Srrh char *to, *from; 157610110Srrh int n; 157710110Srrh int length; 157810110Srrh { 157910110Srrh if (n + strlen(to) >= length) { 158010110Srrh gstrbotch(to, from); 158110110Srrh } 158210110Srrh return(strncat(to, from, n)); 158310110Srrh } 158410110Srrh 158510110Srrh char *gstrcpy(to, from, length) 158610110Srrh char *to, *from; 158710110Srrh int length; 158810110Srrh { 158910110Srrh if (strlen(from) >= length) { 159010110Srrh gstrbotch(from, (char *)0); 159110110Srrh } 159210110Srrh return(strcpy(to, from)); 159310110Srrh } 159410110Srrh gstrbotch(str1, str2) 159510110Srrh char *str1, *str2; 159610110Srrh { 159710110Srrh usrerr("Filename(s) too long: %s %s", str1, str2); 159810110Srrh } 1599