xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 37921)
121574Sdist /*
221574Sdist  * Copyright (c) 1980 Regents of the University of California.
334204Sbostic  * All rights reserved.
434204Sbostic  *
534204Sbostic  * Redistribution and use in source and binary forms are permitted
634911Sbostic  * provided that the above copyright notice and this paragraph are
734911Sbostic  * duplicated in all such forms and that any documentation,
834911Sbostic  * advertising materials, and other materials related to such
934911Sbostic  * distribution and use acknowledge that the software was developed
1034911Sbostic  * by the University of California, Berkeley.  The name of the
1134911Sbostic  * University may not be used to endorse or promote products derived
1234911Sbostic  * from this software without specific prior written permission.
1334911Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434911Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534911Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621574Sdist  */
1721574Sdist 
1813622Ssam #ifndef lint
1921574Sdist char copyright[] =
2021574Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
2121574Sdist  All rights reserved.\n";
2234204Sbostic #endif /* not lint */
2313622Ssam 
2421574Sdist #ifndef lint
25*37921Sbostic static char sccsid[] = "@(#)sccs.c	5.7 (Berkeley) 05/11/89";
2634204Sbostic #endif /* not lint */
2721574Sdist 
28148Seric # include <stdio.h>
296784Smckusick # include <sys/param.h>
30148Seric # include <sys/stat.h>
3113622Ssam # include <sys/dir.h>
321433Seric # include <errno.h>
331433Seric # include <signal.h>
34148Seric # include <sysexits.h>
352140Seric # include <pwd.h>
36*37921Sbostic # include "pathnames.h"
37148Seric 
38828Seric /*
39828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
40828Seric **
41828Seric **	Without trying to add any functionality to speak of, this
42828Seric **	program tries to make SCCS a little more accessible to human
43828Seric **	types.  The main thing it does is automatically put the
44828Seric **	string "SCCS/s." on the front of names.  Also, it has a
45828Seric **	couple of things that are designed to shorten frequent
46828Seric **	combinations, e.g., "delget" which expands to a "delta"
47828Seric **	and a "get".
48828Seric **
49828Seric **	This program can also function as a setuid front end.
50828Seric **	To do this, you should copy the source, renaming it to
51828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
52828Seric **	in the program (e.g., syssccs might default -d to
53828Seric **	"/usr/src/sys").  Then recompile and put the result
54828Seric **	as setuid to whomever you want.  In this mode, sccs
55828Seric **	knows to not run setuid for certain programs in order
56828Seric **	to preserve security, and so forth.
57828Seric **
58828Seric **	Usage:
59828Seric **		sccs [flags] command [args]
60828Seric **
61828Seric **	Flags:
62828Seric **		-d<dir>		<dir> represents a directory to search
63828Seric **				out of.  It should be a full pathname
64828Seric **				for general usage.  E.g., if <dir> is
65828Seric **				"/usr/src/sys", then a reference to the
66828Seric **				file "dev/bio.c" becomes a reference to
67828Seric **				"/usr/src/sys/dev/bio.c".
68828Seric **		-p<path>	prepends <path> to the final component
69828Seric **				of the pathname.  By default, this is
70828Seric **				"SCCS".  For example, in the -d example
71828Seric **				above, the path then gets modified to
72828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
73828Seric **				more common usage (without the -d flag),
74828Seric **				"prog.c" would get modified to
75828Seric **				"SCCS/s.prog.c".  In both cases, the
76828Seric **				"s." gets automatically prepended.
77828Seric **		-r		run as the real user.
78828Seric **
79828Seric **	Commands:
80828Seric **		admin,
81828Seric **		get,
82828Seric **		delta,
83828Seric **		rmdel,
8430959Sbostic **		cdc,
85828Seric **		etc.		Straight out of SCCS; only difference
86828Seric **				is that pathnames get modified as
87828Seric **				described above.
8830959Sbostic **		enter		Front end doing "sccs admin -i<name> <name>"
8930959Sbostic **		create		Macro for "enter" followed by "get".
90828Seric **		edit		Macro for "get -e".
91828Seric **		unedit		Removes a file being edited, knowing
92828Seric **				about p-files, etc.
93828Seric **		delget		Macro for "delta" followed by "get".
94828Seric **		deledit		Macro for "delta" followed by "get -e".
9530959Sbostic **		branch		Macro for "get -b -e", followed by "delta
9630959Sbostic **				-s -n", followd by "get -e -t -g".
9730959Sbostic **		diffs		"diff" the specified version of files
9830959Sbostic **				and the checked-out version.
9930959Sbostic **		print		Macro for "prs -e" followed by "get -p -m".
10030959Sbostic **		tell		List what files are being edited.
10130959Sbostic **		info		Print information about files being edited.
102828Seric **		clean		Remove all files that can be
103828Seric **				regenerated from SCCS files.
1041205Seric **		check		Like info, but return exit status, for
105828Seric **				use in makefiles.
106828Seric **		fix		Remove a top delta & reedit, but save
107828Seric **				the previous changes in that delta.
108828Seric **
109828Seric **	Compilation Flags:
110828Seric **		UIDUSER -- determine who the user is by looking at the
111828Seric **			uid rather than the login name -- for machines
112828Seric **			where SCCS gets the user in this way.
1131270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
1141205Seric **			this value.  This is so that the setuid
1151205Seric **			aspects of this program cannot be abused.
1161270Seric **			This flag also disables the -p flag.
1171270Seric **		SCCSPATH -- the default for the -p flag.
1181437Seric **		MYNAME -- the title this program should print when it
1191437Seric **			gives error messages.
120828Seric **
121828Seric **	Compilation Instructions:
122828Seric **		cc -O -n -s sccs.c
1231437Seric **		The flags listed above can be -D defined to simplify
1241437Seric **			recompilation for variant versions.
125828Seric **
126828Seric **	Author:
127828Seric **		Eric Allman, UCB/INGRES
1281270Seric **		Copyright 1980 Regents of the University of California
129828Seric */
130155Seric 
1311432Seric 
1321270Seric /*******************  Configuration Information  ********************/
1331270Seric 
1341437Seric # ifndef SCCSPATH
1351432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1361437Seric # endif NOT SCCSPATH
137828Seric 
1381437Seric # ifndef MYNAME
1391437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1401437Seric # endif NOT MYNAME
1411270Seric 
1421270Seric /****************  End of Configuration Information  ****************/
1431432Seric 
144157Seric typedef char	bool;
145200Seric # define TRUE	1
146200Seric # define FALSE	0
147157Seric 
1481438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1491438Seric 
150148Seric struct sccsprog
151148Seric {
152148Seric 	char	*sccsname;	/* name of SCCS routine */
153200Seric 	short	sccsoper;	/* opcode, see below */
154200Seric 	short	sccsflags;	/* flags, see below */
155148Seric 	char	*sccspath;	/* pathname of binary implementing */
156148Seric };
157148Seric 
158200Seric /* values for sccsoper */
159200Seric # define PROG		0	/* call a program */
160201Seric # define CMACRO		1	/* command substitution macro */
161226Seric # define FIX		2	/* fix a delta */
162261Seric # define CLEAN		3	/* clean out recreatable files */
163396Seric # define UNEDIT		4	/* unedit a file */
1641431Seric # define SHELL		5	/* call a shell file (like PROG) */
1651433Seric # define DIFFS		6	/* diff between sccs & file out */
1661871Seric # define DODIFF		7	/* internal call to diff program */
16710104Srrh # define ENTER		8	/* enter new files */
168200Seric 
169157Seric /* bits for sccsflags */
170200Seric # define NO_SDOT	0001	/* no s. on front of args */
171200Seric # define REALUSER	0002	/* protected (e.g., admin) */
172148Seric 
173819Seric /* modes for the "clean", "info", "check" ops */
174819Seric # define CLEANC		0	/* clean command */
175819Seric # define INFOC		1	/* info command */
176819Seric # define CHECKC		2	/* check command */
1771730Seric # define TELLC		3	/* give list of files being edited */
178819Seric 
1791432Seric /*
1801432Seric **  Description of commands known to this program.
1811432Seric **	First argument puts the command into a class.  Second arg is
1821432Seric **	info regarding treatment of this command.  Third arg is a
1831432Seric **	list of flags this command accepts from macros, etc.  Fourth
1841432Seric **	arg is the pathname of the implementing program, or the
1851432Seric **	macro definition, or the arg to a sub-algorithm.
1861432Seric */
187202Seric 
188148Seric struct sccsprog SccsProg[] =
189148Seric {
1901864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
19130959Sbostic 	"cdc",		PROG,	0,			PROGPATH(rmdel),
1921864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1931864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1941864Seric 	"get",		PROG,	0,			PROGPATH(get),
1951864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1962136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1971864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1981864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1992136Seric 	"val",		PROG,	0,			PROGPATH(val),
2001864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
2011864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
2021864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
2031864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
2042138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
2051864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
2061864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
2071864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
2081864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
2091864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
2101864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
2111864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
2121871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
21330959Sbostic 	"print",	CMACRO,	0,			"prs -e/get -p -m -s",
2142226Seric 	"branch",	CMACRO,	NO_SDOT,
2152226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
21610104Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
21710104Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
2181864Seric 	NULL,		-1,	0,			NULL
219148Seric };
220148Seric 
2211432Seric /* one line from a p-file */
222396Seric struct pfile
223396Seric {
224396Seric 	char	*p_osid;	/* old SID */
225396Seric 	char	*p_nsid;	/* new SID */
226396Seric 	char	*p_user;	/* user who did edit */
227396Seric 	char	*p_date;	/* date of get */
228396Seric 	char	*p_time;	/* time of get */
2292161Seric 	char	*p_aux;		/* extra info at end */
230396Seric };
231396Seric 
2321270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2331270Seric # ifdef SCCSDIR
2341270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2351205Seric # else
2361270Seric char	*SccsDir = "";
2371205Seric # endif
2381437Seric char	MyName[] = MYNAME;	/* name used in messages */
2391433Seric int	OutFile = -1;		/* override output file for commands */
240157Seric bool	RealUser;		/* if set, running as real user */
241393Seric # ifdef DEBUG
242393Seric bool	Debug;			/* turn on tracing */
243393Seric # endif
2442139Seric # ifndef V6
2452139Seric extern char	*getenv();
2462139Seric # endif V6
24710110Srrh 
24830959Sbostic extern char	*sys_siglist[];
24930959Sbostic 
25010110Srrh char *gstrcat(), *strcat();
25110110Srrh char *gstrncat(), *strncat();
25210110Srrh char *gstrcpy(), *strcpy();
25310110Srrh #define	FBUFSIZ	BUFSIZ
25410110Srrh #define	PFILELG	120
2551432Seric 
256148Seric main(argc, argv)
257148Seric 	int argc;
258148Seric 	char **argv;
259148Seric {
260148Seric 	register char *p;
261262Seric 	extern struct sccsprog *lookup();
2621282Seric 	register int i;
2632139Seric # ifndef V6
2642139Seric # ifndef SCCSDIR
2652140Seric 	register struct passwd *pw;
2662140Seric 	extern struct passwd *getpwnam();
26710110Srrh 	char buf[FBUFSIZ];
2682140Seric 
2692139Seric 	/* pull "SccsDir" out of the environment (possibly) */
27010260Seric 	p = getenv("PROJECTDIR");
2712140Seric 	if (p != NULL && p[0] != '\0')
2722140Seric 	{
2732140Seric 		if (p[0] == '/')
2742140Seric 			SccsDir = p;
2752140Seric 		else
2762140Seric 		{
2772140Seric 			pw = getpwnam(p);
2782140Seric 			if (pw == NULL)
2792140Seric 			{
2802140Seric 				usrerr("user %s does not exist", p);
2812140Seric 				exit(EX_USAGE);
2822140Seric 			}
28310110Srrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
28410110Srrh 			gstrcat(buf, "/src", sizeof(buf));
2852140Seric 			if (access(buf, 0) < 0)
2862140Seric 			{
28710110Srrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
28810110Srrh 				gstrcat(buf, "/source", sizeof(buf));
2892140Seric 				if (access(buf, 0) < 0)
2902140Seric 				{
2912140Seric 					usrerr("project %s has no source!", p);
2922140Seric 					exit(EX_USAGE);
2932140Seric 				}
2942140Seric 			}
2952140Seric 			SccsDir = buf;
2962140Seric 		}
2972140Seric 	}
2982139Seric # endif SCCSDIR
2992139Seric # endif V6
3002139Seric 
301148Seric 	/*
302148Seric 	**  Detect and decode flags intended for this program.
303148Seric 	*/
304148Seric 
305200Seric 	if (argc < 2)
306148Seric 	{
3071205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
308200Seric 		exit(EX_USAGE);
309200Seric 	}
310200Seric 	argv[argc] = NULL;
311200Seric 
312262Seric 	if (lookup(argv[0]) == NULL)
313200Seric 	{
314262Seric 		while ((p = *++argv) != NULL)
315148Seric 		{
316262Seric 			if (*p != '-')
317262Seric 				break;
318262Seric 			switch (*++p)
319262Seric 			{
320262Seric 			  case 'r':		/* run as real user */
321262Seric 				setuid(getuid());
322262Seric 				RealUser++;
323262Seric 				break;
324148Seric 
3251270Seric # ifndef SCCSDIR
326262Seric 			  case 'p':		/* path of sccs files */
327262Seric 				SccsPath = ++p;
3282348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3292348Seric 					SccsPath = *++argv;
330262Seric 				break;
331148Seric 
332588Seric 			  case 'd':		/* directory to search from */
333588Seric 				SccsDir = ++p;
3342348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3352348Seric 					SccsDir = *++argv;
336588Seric 				break;
3371205Seric # endif
338588Seric 
339393Seric # ifdef DEBUG
340393Seric 			  case 'T':		/* trace */
341393Seric 				Debug++;
342393Seric 				break;
343393Seric # endif
344393Seric 
345262Seric 			  default:
3461205Seric 				usrerr("unknown option -%s", p);
347262Seric 				break;
348262Seric 			}
349148Seric 		}
350262Seric 		if (SccsPath[0] == '\0')
351262Seric 			SccsPath = ".";
352148Seric 	}
353148Seric 
3541737Seric 	i = command(argv, FALSE, "");
3551282Seric 	exit(i);
356200Seric }
3571432Seric 
3581432Seric /*
3591282Seric **  COMMAND -- look up and perform a command
3601282Seric **
3611282Seric **	This routine is the guts of this program.  Given an
3621282Seric **	argument vector, it looks up the "command" (argv[0])
3631282Seric **	in the configuration table and does the necessary stuff.
3641282Seric **
3651282Seric **	Parameters:
3661282Seric **		argv -- an argument vector to process.
3671282Seric **		forkflag -- if set, fork before executing the command.
3681316Seric **		editflag -- if set, only include flags listed in the
3691316Seric **			sccsklets field of the command descriptor.
3701316Seric **		arg0 -- a space-seperated list of arguments to insert
3711316Seric **			before argv.
3721282Seric **
3731282Seric **	Returns:
3741282Seric **		zero -- command executed ok.
3751282Seric **		else -- error status.
3761282Seric **
3771282Seric **	Side Effects:
3781282Seric **		none.
3791282Seric */
380157Seric 
3811737Seric command(argv, forkflag, arg0)
382200Seric 	char **argv;
383201Seric 	bool forkflag;
3841316Seric 	char *arg0;
385200Seric {
386200Seric 	register struct sccsprog *cmd;
387200Seric 	register char *p;
38810110Srrh 	char buf[FBUFSIZ];
389262Seric 	extern struct sccsprog *lookup();
3901316Seric 	char *nav[1000];
3911316Seric 	char **np;
3921431Seric 	register char **ap;
393585Seric 	register int i;
3941431Seric 	register char *q;
395585Seric 	extern bool unedit();
3961282Seric 	int rval = 0;
3971316Seric 	extern char *index();
3981316Seric 	extern char *makefile();
3991737Seric 	char *editchs;
4001435Seric 	extern char *tail();
401200Seric 
402393Seric # ifdef DEBUG
403393Seric 	if (Debug)
404393Seric 	{
4051316Seric 		printf("command:\n\t\"%s\"\n", arg0);
4061316Seric 		for (np = argv; *np != NULL; np++)
4071316Seric 			printf("\t\"%s\"\n", *np);
408393Seric 	}
409393Seric # endif
410393Seric 
411157Seric 	/*
4121316Seric 	**  Copy arguments.
4131438Seric 	**	Copy from arg0 & if necessary at most one arg
4141438Seric 	**	from argv[0].
4151316Seric 	*/
4161316Seric 
4171431Seric 	np = ap = &nav[1];
4181737Seric 	editchs = NULL;
4191821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
4201316Seric 	{
4211316Seric 		*np++ = q;
4221316Seric 		while (*p == ' ')
4231316Seric 			p++;
4241737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
4251316Seric 			*q++ = *p++;
4261316Seric 		*q++ = '\0';
4271737Seric 		if (*p == ':')
4281737Seric 		{
4291737Seric 			editchs = q;
4301821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4311737Seric 				*q++ = *p;
4321737Seric 			*q++ = '\0';
4331737Seric 		}
4341316Seric 	}
4351316Seric 	*np = NULL;
4361431Seric 	if (*ap == NULL)
4371316Seric 		*np++ = *argv++;
4381316Seric 
4391316Seric 	/*
440148Seric 	**  Look up command.
4411431Seric 	**	At this point, *ap is the command name.
442148Seric 	*/
443148Seric 
4441431Seric 	cmd = lookup(*ap);
445262Seric 	if (cmd == NULL)
446148Seric 	{
4471431Seric 		usrerr("Unknown command \"%s\"", *ap);
4481282Seric 		return (EX_USAGE);
449148Seric 	}
450148Seric 
451148Seric 	/*
4521316Seric 	**  Copy remaining arguments doing editing as appropriate.
4531316Seric 	*/
4541316Seric 
4551316Seric 	for (; *argv != NULL; argv++)
4561316Seric 	{
4571316Seric 		p = *argv;
4581316Seric 		if (*p == '-')
4591316Seric 		{
4601737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4611316Seric 				*np++ = p;
4621316Seric 		}
4631316Seric 		else
4641316Seric 		{
4651316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4661316Seric 				p = makefile(p);
4671316Seric 			if (p != NULL)
4681316Seric 				*np++ = p;
4691316Seric 		}
4701316Seric 	}
4711316Seric 	*np = NULL;
4721316Seric 
4731316Seric 	/*
474200Seric 	**  Interpret operation associated with this command.
475157Seric 	*/
476157Seric 
477200Seric 	switch (cmd->sccsoper)
478200Seric 	{
4791431Seric 	  case SHELL:		/* call a shell file */
4801431Seric 		*ap = cmd->sccspath;
4811431Seric 		*--ap = "sh";
482*37921Sbostic 		rval = callprog(_PATH_BSHELL, cmd->sccsflags, ap, forkflag);
4831431Seric 		break;
4841431Seric 
485200Seric 	  case PROG:		/* call an sccs prog */
4861431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
487201Seric 		break;
488201Seric 
489201Seric 	  case CMACRO:		/* command macro */
4901438Seric 		/* step through & execute each part of the macro */
491201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
492201Seric 		{
4931316Seric 			q = p;
4941316Seric 			while (*p != '\0' && *p != '/')
4951316Seric 				p++;
4961737Seric 			rval = command(&ap[1], *p != '\0', q);
4971282Seric 			if (rval != 0)
4981282Seric 				break;
499201Seric 		}
5001282Seric 		break;
501157Seric 
502226Seric 	  case FIX:		/* fix a delta */
50330959Sbostic 		if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
504226Seric 		{
5051205Seric 			usrerr("-r flag needed for fix command");
5061282Seric 			rval = EX_USAGE;
507226Seric 			break;
508226Seric 		}
5091438Seric 
5101438Seric 		/* get the version with all changes */
5111737Seric 		rval = command(&ap[1], TRUE, "get -k");
5121438Seric 
5131438Seric 		/* now remove that version from the s-file */
5141282Seric 		if (rval == 0)
5151737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
5161438Seric 
5171438Seric 		/* and edit the old version (but don't clobber new vers) */
5181282Seric 		if (rval == 0)
5191737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
5201282Seric 		break;
521226Seric 
522261Seric 	  case CLEAN:
5231822Seric 		rval = clean((int) cmd->sccspath, ap);
524261Seric 		break;
525261Seric 
526396Seric 	  case UNEDIT:
5271431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
528585Seric 		{
5291316Seric 			if (unedit(*argv))
5301316Seric 				*np++ = *argv;
531585Seric 		}
5321316Seric 		*np = NULL;
5331438Seric 
5341438Seric 		/* get all the files that we unedited successfully */
5351738Seric 		if (np > &ap[1])
5361737Seric 			rval = command(&ap[1], FALSE, "get");
537396Seric 		break;
538396Seric 
5391433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5401433Seric 		/* find the end of the flag arguments */
5411433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5421433Seric 			continue;
5431433Seric 		argv = np;
5441433Seric 
5451433Seric 		/* for each file, do the diff */
5461502Seric 		p = argv[1];
5471433Seric 		while (*np != NULL)
5481433Seric 		{
5491438Seric 			/* messy, but we need a null terminated argv */
5501433Seric 			*argv = *np++;
5511502Seric 			argv[1] = NULL;
5521435Seric 			i = dodiff(ap, tail(*argv));
5531433Seric 			if (rval == 0)
5541433Seric 				rval = i;
5551502Seric 			argv[1] = p;
5561433Seric 		}
5571433Seric 		break;
5581433Seric 
5591871Seric 	  case DODIFF:		/* internal diff call */
5601871Seric 		setuid(getuid());
5611871Seric 		for (np = ap; *np != NULL; np++)
5621871Seric 		{
5631871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5641871Seric 				(*np)[1] = 'c';
5651871Seric 		}
5661871Seric 
5671871Seric 		/* insert "-" argument */
5681871Seric 		np[1] = NULL;
5691871Seric 		np[0] = np[-1];
5701871Seric 		np[-1] = "-";
5711871Seric 
5721871Seric 		/* execute the diff program of choice */
5731871Seric # ifndef V6
5741871Seric 		execvp("diff", ap);
5751871Seric # endif
5761871Seric 		execv(cmd->sccspath, argv);
5771871Seric 		syserr("cannot exec %s", cmd->sccspath);
5781871Seric 		exit(EX_OSERR);
5791871Seric 
58010104Srrh 	  case ENTER:		/* enter new sccs files */
5816785Smckusick 		/* skip over flag arguments */
5826785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5836785Smckusick 			continue;
5846785Smckusick 		argv = np;
5856785Smckusick 
5866785Smckusick 		/* do an admin for each file */
5876785Smckusick 		p = argv[1];
5886785Smckusick 		while (*np != NULL)
5896785Smckusick 		{
5906785Smckusick 			printf("\n%s:\n", *np);
59110110Srrh 			strcpy(buf, "-i");
59210110Srrh 			gstrcat(buf, *np, sizeof(buf));
5936785Smckusick 			ap[0] = buf;
5946785Smckusick 			argv[0] = tail(*np);
5956785Smckusick 			argv[1] = NULL;
5966785Smckusick 			rval = command(ap, TRUE, "admin");
5976785Smckusick 			argv[1] = p;
5986785Smckusick 			if (rval == 0)
5996785Smckusick 			{
60010110Srrh 				strcpy(buf, ",");
60110110Srrh 				gstrcat(buf, tail(*np), sizeof(buf));
6026785Smckusick 				if (link(*np, buf) >= 0)
6036785Smckusick 					unlink(*np);
6046785Smckusick 			}
6056785Smckusick 			np++;
6066785Smckusick 		}
6076785Smckusick 		break;
6086785Smckusick 
609200Seric 	  default:
6101205Seric 		syserr("oper %d", cmd->sccsoper);
611200Seric 		exit(EX_SOFTWARE);
612200Seric 	}
6131282Seric # ifdef DEBUG
6141282Seric 	if (Debug)
6151282Seric 		printf("command: rval=%d\n", rval);
6161282Seric # endif
6171282Seric 	return (rval);
618200Seric }
6191432Seric 
6201432Seric /*
621262Seric **  LOOKUP -- look up an SCCS command name.
622262Seric **
623262Seric **	Parameters:
624262Seric **		name -- the name of the command to look up.
625262Seric **
626262Seric **	Returns:
627262Seric **		ptr to command descriptor for this command.
628262Seric **		NULL if no such entry.
629262Seric **
630262Seric **	Side Effects:
631262Seric **		none.
632262Seric */
633200Seric 
634262Seric struct sccsprog *
635262Seric lookup(name)
636262Seric 	char *name;
637262Seric {
638262Seric 	register struct sccsprog *cmd;
639226Seric 
640262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
641262Seric 	{
642262Seric 		if (strcmp(cmd->sccsname, name) == 0)
643262Seric 			return (cmd);
644262Seric 	}
645262Seric 	return (NULL);
646262Seric }
6471432Seric 
6481432Seric /*
6491282Seric **  CALLPROG -- call a program
6501282Seric **
6511316Seric **	Used to call the SCCS programs.
6521282Seric **
6531282Seric **	Parameters:
6541282Seric **		progpath -- pathname of the program to call.
6551282Seric **		flags -- status flags from the command descriptors.
6561282Seric **		argv -- an argument vector to pass to the program.
6571282Seric **		forkflag -- if true, fork before calling, else just
6581282Seric **			exec.
6591282Seric **
6601282Seric **	Returns:
6611282Seric **		The exit status of the program.
6621282Seric **		Nothing if forkflag == FALSE.
6631282Seric **
6641282Seric **	Side Effects:
6651282Seric **		Can exit if forkflag == FALSE.
6661282Seric */
667226Seric 
668200Seric callprog(progpath, flags, argv, forkflag)
669200Seric 	char *progpath;
670200Seric 	short flags;
671200Seric 	char **argv;
672200Seric 	bool forkflag;
673200Seric {
674200Seric 	register int i;
67530959Sbostic 	register int wpid;
676201Seric 	auto int st;
67730959Sbostic 	register int sigcode;
67830959Sbostic 	register int coredumped;
67930959Sbostic 	register char *sigmsg;
68030959Sbostic 	auto char sigmsgbuf[10+1];	/* "Signal 127" + terminating '\0' */
681200Seric 
6821316Seric # ifdef DEBUG
6831316Seric 	if (Debug)
6841316Seric 	{
6851316Seric 		printf("callprog:\n");
6861316Seric 		for (i = 0; argv[i] != NULL; i++)
6871316Seric 			printf("\t\"%s\"\n", argv[i]);
6881316Seric 	}
6891316Seric # endif
6901316Seric 
691200Seric 	if (*argv == NULL)
692200Seric 		return (-1);
693200Seric 
694157Seric 	/*
695226Seric 	**  Fork if appropriate.
696148Seric 	*/
697148Seric 
698200Seric 	if (forkflag)
699200Seric 	{
700393Seric # ifdef DEBUG
701393Seric 		if (Debug)
702393Seric 			printf("Forking\n");
703393Seric # endif
704200Seric 		i = fork();
705200Seric 		if (i < 0)
706200Seric 		{
7071205Seric 			syserr("cannot fork");
708200Seric 			exit(EX_OSERR);
709200Seric 		}
710200Seric 		else if (i > 0)
711201Seric 		{
71230959Sbostic 			while ((wpid = wait(&st)) != -1 && wpid != i)
71330959Sbostic 				;
71430959Sbostic 			if ((sigcode = st & 0377) == 0)
7151282Seric 				st = (st >> 8) & 0377;
71630959Sbostic 			else
71730959Sbostic 			{
71830959Sbostic 				coredumped = sigcode & 0200;
71930959Sbostic 				sigcode &= 0177;
72030959Sbostic 				if (sigcode != SIGINT && sigcode != SIGPIPE)
72130959Sbostic 				{
72230959Sbostic 					if (sigcode < NSIG)
72330959Sbostic 						sigmsg = sys_siglist[sigcode];
72430959Sbostic 					else
72530959Sbostic 					{
72630959Sbostic 						sprintf(sigmsgbuf, "Signal %d",
72730959Sbostic 						    sigcode);
72830959Sbostic 						sigmsg = sigmsgbuf;
72930959Sbostic 					}
73030959Sbostic 					fprintf(stderr, "sccs: %s: %s%s", argv[0],
73130959Sbostic 					    sigmsg,
73230959Sbostic 					    coredumped ? " - core dumped": "");
73330959Sbostic 				}
73430959Sbostic 				st = EX_SOFTWARE;
73530959Sbostic 			}
7361433Seric 			if (OutFile >= 0)
7371433Seric 			{
7381433Seric 				close(OutFile);
7391433Seric 				OutFile = -1;
7401433Seric 			}
741201Seric 			return (st);
742201Seric 		}
743200Seric 	}
7441433Seric 	else if (OutFile >= 0)
7451433Seric 	{
7461433Seric 		syserr("callprog: setting stdout w/o forking");
7471433Seric 		exit(EX_SOFTWARE);
7481433Seric 	}
749200Seric 
7501433Seric 	/* set protection as appropriate */
751200Seric 	if (bitset(REALUSER, flags))
752200Seric 		setuid(getuid());
7531433Seric 
7541433Seric 	/* change standard input & output if needed */
7551433Seric 	if (OutFile >= 0)
7561433Seric 	{
7571433Seric 		close(1);
7581433Seric 		dup(OutFile);
7591433Seric 		close(OutFile);
7601433Seric 	}
761226Seric 
7621433Seric 	/* call real SCCS program */
763226Seric 	execv(progpath, argv);
7641205Seric 	syserr("cannot execute %s", progpath);
765148Seric 	exit(EX_UNAVAILABLE);
7661738Seric 	/*NOTREACHED*/
767148Seric }
7681432Seric 
7691432Seric /*
770586Seric **  MAKEFILE -- make filename of SCCS file
771586Seric **
772586Seric **	If the name passed is already the name of an SCCS file,
773586Seric **	just return it.  Otherwise, munge the name into the name
774586Seric **	of the actual SCCS file.
775586Seric **
776586Seric **	There are cases when it is not clear what you want to
777586Seric **	do.  For example, if SccsPath is an absolute pathname
778586Seric **	and the name given is also an absolute pathname, we go
779586Seric **	for SccsPath (& only use the last component of the name
780586Seric **	passed) -- this is important for security reasons (if
781586Seric **	sccs is being used as a setuid front end), but not
782586Seric **	particularly intuitive.
783586Seric **
784586Seric **	Parameters:
785586Seric **		name -- the file name to be munged.
786586Seric **
787586Seric **	Returns:
788586Seric **		The pathname of the sccs file.
789586Seric **		NULL on error.
790586Seric **
791586Seric **	Side Effects:
792586Seric **		none.
793586Seric */
794148Seric 
795148Seric char *
796148Seric makefile(name)
797148Seric 	char *name;
798148Seric {
799148Seric 	register char *p;
80010110Srrh 	char buf[3*FBUFSIZ];
801148Seric 	extern char *malloc();
802586Seric 	extern char *rindex();
803588Seric 	extern bool safepath();
804587Seric 	extern bool isdir();
805587Seric 	register char *q;
806148Seric 
807586Seric 	p = rindex(name, '/');
808586Seric 	if (p == NULL)
809586Seric 		p = name;
810586Seric 	else
811586Seric 		p++;
812586Seric 
813148Seric 	/*
814588Seric 	**  Check to see that the path is "safe", i.e., that we
815588Seric 	**  are not letting some nasty person use the setuid part
816588Seric 	**  of this program to look at or munge some presumably
817588Seric 	**  hidden files.
818148Seric 	*/
819148Seric 
820588Seric 	if (SccsDir[0] == '/' && !safepath(name))
821588Seric 		return (NULL);
822586Seric 
823586Seric 	/*
824588Seric 	**  Create the base pathname.
825586Seric 	*/
826586Seric 
8271438Seric 	/* first the directory part */
828588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
829148Seric 	{
83010110Srrh 		gstrcpy(buf, SccsDir, sizeof(buf));
83110110Srrh 		gstrcat(buf, "/", sizeof(buf));
832586Seric 	}
833586Seric 	else
83410110Srrh 		gstrcpy(buf, "", sizeof(buf));
8351438Seric 
8361438Seric 	/* then the head of the pathname */
83710110Srrh 	gstrncat(buf, name, p - name, sizeof(buf));
838587Seric 	q = &buf[strlen(buf)];
8391438Seric 
8401438Seric 	/* now copy the final part of the name, in case useful */
84110110Srrh 	gstrcpy(q, p, sizeof(buf));
8421438Seric 
8431438Seric 	/* so is it useful? */
844587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
845586Seric 	{
8461438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
84710110Srrh 		gstrcpy(q, SccsPath, sizeof(buf));
84810110Srrh 		gstrcat(buf, "/s.", sizeof(buf));
8491438Seric 
8501438Seric 		/* and now the end of the name */
85110110Srrh 		gstrcat(buf, p, sizeof(buf));
852586Seric 	}
853148Seric 
8541438Seric 	/* if i haven't changed it, why did I do all this? */
855588Seric 	if (strcmp(buf, name) == 0)
856588Seric 		p = name;
857588Seric 	else
858148Seric 	{
8591438Seric 		/* but if I have, squirrel it away */
860588Seric 		p = malloc(strlen(buf) + 1);
861588Seric 		if (p == NULL)
862588Seric 		{
863588Seric 			perror("Sccs: no mem");
864588Seric 			exit(EX_OSERR);
865588Seric 		}
866588Seric 		strcpy(p, buf);
867148Seric 	}
8681438Seric 
869148Seric 	return (p);
870148Seric }
8711432Seric 
8721432Seric /*
873587Seric **  ISDIR -- return true if the argument is a directory.
874587Seric **
875587Seric **	Parameters:
876587Seric **		name -- the pathname of the file to check.
877587Seric **
878587Seric **	Returns:
879587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
880587Seric **
881587Seric **	Side Effects:
882587Seric **		none.
883587Seric */
884587Seric 
885587Seric bool
886587Seric isdir(name)
887587Seric 	char *name;
888587Seric {
889587Seric 	struct stat stbuf;
890587Seric 
891587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
892587Seric }
8931432Seric 
8941432Seric /*
895586Seric **  SAFEPATH -- determine whether a pathname is "safe"
896586Seric **
897586Seric **	"Safe" pathnames only allow you to get deeper into the
898586Seric **	directory structure, i.e., full pathnames and ".." are
899586Seric **	not allowed.
900586Seric **
901586Seric **	Parameters:
902586Seric **		p -- the name to check.
903586Seric **
904586Seric **	Returns:
905586Seric **		TRUE -- if the path is safe.
906586Seric **		FALSE -- if the path is not safe.
907586Seric **
908586Seric **	Side Effects:
909586Seric **		Prints a message if the path is not safe.
910586Seric */
911586Seric 
912586Seric bool
913586Seric safepath(p)
914586Seric 	register char *p;
915586Seric {
916586Seric 	extern char *index();
917586Seric 
918586Seric 	if (*p != '/')
919586Seric 	{
920586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
921586Seric 		{
922586Seric 			p = index(p, '/');
923586Seric 			if (p == NULL)
924586Seric 				return (TRUE);
925586Seric 			p++;
926586Seric 		}
927586Seric 	}
928586Seric 
929586Seric 	printf("You may not use full pathnames or \"..\"\n");
930586Seric 	return (FALSE);
931586Seric }
9321432Seric 
9331432Seric /*
934261Seric **  CLEAN -- clean out recreatable files
935261Seric **
936261Seric **	Any file for which an "s." file exists but no "p." file
937261Seric **	exists in the current directory is purged.
938261Seric **
939261Seric **	Parameters:
9401822Seric **		mode -- tells whether this came from a "clean", "info", or
9411822Seric **			"check" command.
9421822Seric **		argv -- the rest of the argument vector.
943261Seric **
944261Seric **	Returns:
945261Seric **		none.
946261Seric **
947261Seric **	Side Effects:
948819Seric **		Removes files in the current directory.
949819Seric **		Prints information regarding files being edited.
950819Seric **		Exits if a "check" command.
951261Seric */
952261Seric 
9531822Seric clean(mode, argv)
954819Seric 	int mode;
9551822Seric 	char **argv;
956261Seric {
9576784Smckusick 	struct direct *dir;
95810110Srrh 	char buf[FBUFSIZ];
9592140Seric 	char *bufend;
96030466Smckusick 	register DIR *dirp;
961346Seric 	register char *basefile;
962351Seric 	bool gotedit;
9631822Seric 	bool gotpfent;
964394Seric 	FILE *pfp;
9651822Seric 	bool nobranch = FALSE;
9661822Seric 	extern struct pfile *getpfent();
9671822Seric 	register struct pfile *pf;
9681822Seric 	register char **ap;
9691864Seric 	extern char *username();
9701864Seric 	char *usernm = NULL;
9712140Seric 	char *subdir = NULL;
9722140Seric 	char *cmdname;
973261Seric 
9741438Seric 	/*
9751822Seric 	**  Process the argv
9761822Seric 	*/
9771822Seric 
9782140Seric 	cmdname = *argv;
9792140Seric 	for (ap = argv; *++ap != NULL; )
9801822Seric 	{
9811864Seric 		if (**ap == '-')
9821864Seric 		{
9831864Seric 			/* we have a flag */
9841864Seric 			switch ((*ap)[1])
9851864Seric 			{
9861864Seric 			  case 'b':
9871864Seric 				nobranch = TRUE;
9881864Seric 				break;
9891864Seric 
9901864Seric 			  case 'u':
9911864Seric 				if ((*ap)[2] != '\0')
9921864Seric 					usernm = &(*ap)[2];
9931864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9941864Seric 					usernm = *++ap;
9951864Seric 				else
9961864Seric 					usernm = username();
9971864Seric 				break;
9981864Seric 			}
9991864Seric 		}
10002140Seric 		else
10012140Seric 		{
10022140Seric 			if (subdir != NULL)
10032140Seric 				usrerr("too many args");
10042140Seric 			else
10052140Seric 				subdir = *ap;
10062140Seric 		}
10071822Seric 	}
10081822Seric 
10091822Seric 	/*
10101438Seric 	**  Find and open the SCCS directory.
10111438Seric 	*/
10121438Seric 
101310110Srrh 	gstrcpy(buf, SccsDir, sizeof(buf));
10141207Seric 	if (buf[0] != '\0')
101510110Srrh 		gstrcat(buf, "/", sizeof(buf));
10162140Seric 	if (subdir != NULL)
10172140Seric 	{
101810110Srrh 		gstrcat(buf, subdir, sizeof(buf));
101910110Srrh 		gstrcat(buf, "/", sizeof(buf));
10202140Seric 	}
102110110Srrh 	gstrcat(buf, SccsPath, sizeof(buf));
10222140Seric 	bufend = &buf[strlen(buf)];
10231438Seric 
102430466Smckusick 	dirp = opendir(buf);
102530466Smckusick 	if (dirp == NULL)
1026261Seric 	{
10271207Seric 		usrerr("cannot open %s", buf);
10281282Seric 		return (EX_NOINPUT);
1029261Seric 	}
1030261Seric 
1031261Seric 	/*
1032261Seric 	**  Scan the SCCS directory looking for s. files.
10331438Seric 	**	gotedit tells whether we have tried to clean any
10341438Seric 	**		files that are being edited.
1035261Seric 	*/
1036261Seric 
1037351Seric 	gotedit = FALSE;
103830466Smckusick 	while (dir = readdir(dirp)) {
10396784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
1040261Seric 			continue;
1041261Seric 
1042261Seric 		/* got an s. file -- see if the p. file exists */
104310110Srrh 		gstrcpy(bufend, "/p.", sizeof(buf));
10442140Seric 		basefile = bufend + 3;
104510110Srrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
10461822Seric 
10471822Seric 		/*
10481822Seric 		**  open and scan the p-file.
10491822Seric 		**	'gotpfent' tells if we have found a valid p-file
10501822Seric 		**		entry.
10511822Seric 		*/
10521822Seric 
1053394Seric 		pfp = fopen(buf, "r");
10541822Seric 		gotpfent = FALSE;
1055394Seric 		if (pfp != NULL)
1056346Seric 		{
10571438Seric 			/* the file exists -- report it's contents */
10581822Seric 			while ((pf = getpfent(pfp)) != NULL)
10591730Seric 			{
10601822Seric 				if (nobranch && isbranch(pf->p_nsid))
10611822Seric 					continue;
10621864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10631864Seric 					continue;
10641822Seric 				gotedit = TRUE;
10651822Seric 				gotpfent = TRUE;
10661822Seric 				if (mode == TELLC)
10671822Seric 				{
10681822Seric 					printf("%s\n", basefile);
10691822Seric 					break;
10701822Seric 				}
10712161Seric 				printf("%12s: being edited: ", basefile);
10722161Seric 				putpfent(pf, stdout);
10731730Seric 			}
1074394Seric 			fclose(pfp);
10751822Seric 		}
1076261Seric 
1077261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10781870Seric 		if (mode == CLEANC && !gotpfent)
1079346Seric 		{
108010110Srrh 			char	unlinkbuf[FBUFSIZ];
108110110Srrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
108210103Srrh 			unlink(unlinkbuf);
1083346Seric 		}
1084261Seric 	}
1085261Seric 
10861438Seric 	/* cleanup & report results */
108730466Smckusick 	closedir(dirp);
1088819Seric 	if (!gotedit && mode == INFOC)
10891864Seric 	{
10901864Seric 		printf("Nothing being edited");
10911864Seric 		if (nobranch)
10921864Seric 			printf(" (on trunk)");
10931864Seric 		if (usernm == NULL)
10941864Seric 			printf("\n");
10951864Seric 		else
10961864Seric 			printf(" by %s\n", usernm);
10971864Seric 	}
1098819Seric 	if (mode == CHECKC)
1099819Seric 		exit(gotedit);
11001282Seric 	return (EX_OK);
1101261Seric }
11021432Seric 
11031432Seric /*
11041822Seric **  ISBRANCH -- is the SID a branch?
11051822Seric **
11061822Seric **	Parameters:
11071822Seric **		sid -- the sid to check.
11081822Seric **
11091822Seric **	Returns:
11101822Seric **		TRUE if the sid represents a branch.
11111822Seric **		FALSE otherwise.
11121822Seric **
11131822Seric **	Side Effects:
11141822Seric **		none.
11151822Seric */
11161822Seric 
11171822Seric isbranch(sid)
11181822Seric 	char *sid;
11191822Seric {
11201822Seric 	register char *p;
11211822Seric 	int dots;
11221822Seric 
11231822Seric 	dots = 0;
11241822Seric 	for (p = sid; *p != '\0'; p++)
11251822Seric 	{
11261822Seric 		if (*p == '.')
11271822Seric 			dots++;
11281822Seric 		if (dots > 1)
11291822Seric 			return (TRUE);
11301822Seric 	}
11311822Seric 	return (FALSE);
11321822Seric }
11331822Seric 
11341822Seric /*
1135396Seric **  UNEDIT -- unedit a file
1136396Seric **
1137396Seric **	Checks to see that the current user is actually editting
1138396Seric **	the file and arranges that s/he is not editting it.
1139396Seric **
1140396Seric **	Parameters:
1141416Seric **		fn -- the name of the file to be unedited.
1142396Seric **
1143396Seric **	Returns:
1144585Seric **		TRUE -- if the file was successfully unedited.
1145585Seric **		FALSE -- if the file was not unedited for some
1146585Seric **			reason.
1147396Seric **
1148396Seric **	Side Effects:
1149396Seric **		fn is removed
1150396Seric **		entries are removed from pfile.
1151396Seric */
1152396Seric 
1153585Seric bool
1154396Seric unedit(fn)
1155396Seric 	char *fn;
1156396Seric {
1157396Seric 	register FILE *pfp;
115812153Ssam 	char *cp, *pfn;
1159*37921Sbostic 	static char tfn[] = _PATH_TMP;
1160396Seric 	FILE *tfp;
1161396Seric 	register char *q;
1162396Seric 	bool delete = FALSE;
1163396Seric 	bool others = FALSE;
1164396Seric 	char *myname;
11651864Seric 	extern char *username();
1166396Seric 	struct pfile *pent;
11671822Seric 	extern struct pfile *getpfent();
116810110Srrh 	char buf[PFILELG];
116933251Sbostic 	extern char *makefile(), *rindex(), *tail();
1170396Seric 
1171396Seric 	/* make "s." filename & find the trailing component */
1172396Seric 	pfn = makefile(fn);
1173586Seric 	if (pfn == NULL)
1174586Seric 		return (FALSE);
1175586Seric 	q = rindex(pfn, '/');
1176586Seric 	if (q == NULL)
1177586Seric 		q = &pfn[-1];
1178586Seric 	if (q[1] != 's' || q[2] != '.')
1179396Seric 	{
11801205Seric 		usrerr("bad file name \"%s\"", fn);
1181585Seric 		return (FALSE);
1182396Seric 	}
1183396Seric 
11841438Seric 	/* turn "s." into "p." & try to open it */
1185396Seric 	*++q = 'p';
1186396Seric 
1187396Seric 	pfp = fopen(pfn, "r");
1188396Seric 	if (pfp == NULL)
1189396Seric 	{
1190416Seric 		printf("%12s: not being edited\n", fn);
1191585Seric 		return (FALSE);
1192396Seric 	}
1193396Seric 
11941438Seric 	/* create temp file for editing p-file */
1195396Seric 	mktemp(tfn);
1196396Seric 	tfp = fopen(tfn, "w");
1197396Seric 	if (tfp == NULL)
1198396Seric 	{
11991205Seric 		usrerr("cannot create \"%s\"", tfn);
1200396Seric 		exit(EX_OSERR);
1201396Seric 	}
1202396Seric 
12031438Seric 	/* figure out who I am */
12041864Seric 	myname = username();
12051438Seric 
12061438Seric 	/*
12071438Seric 	**  Copy p-file to temp file, doing deletions as needed.
12081438Seric 	*/
12091438Seric 
12101822Seric 	while ((pent = getpfent(pfp)) != NULL)
1211396Seric 	{
1212396Seric 		if (strcmp(pent->p_user, myname) == 0)
1213396Seric 		{
1214396Seric 			/* a match */
1215396Seric 			delete++;
1216396Seric 		}
1217396Seric 		else
1218396Seric 		{
12191438Seric 			/* output it again */
12202161Seric 			putpfent(pent, tfp);
1221396Seric 			others++;
1222396Seric 		}
1223396Seric 	}
1224396Seric 
122512153Ssam 	/*
122612153Ssam 	 * Before changing anything, make sure we can remove
122712153Ssam 	 * the file in question (assuming it exists).
122812153Ssam 	 */
122912153Ssam 	if (delete) {
123012153Ssam 		extern int errno;
123112153Ssam 
123212153Ssam 		cp = tail(fn);
123312153Ssam 		errno = 0;
123412153Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
123512153Ssam 			goto bad;
123612153Ssam 		if (errno == 0)
123712153Ssam 			/*
123812153Ssam 			 * This is wrong, but the rest of the program
123912153Ssam 			 * has built in assumptions about "." as well,
124012153Ssam 			 * so why make unedit a special case?
124112153Ssam 			 */
124212153Ssam 			if (access(".", 2) < 0) {
124312153Ssam 	bad:
124412153Ssam 				printf("%12s: can't remove\n", cp);
124512153Ssam 				fclose(tfp);
124612153Ssam 				fclose(pfp);
124712153Ssam 				unlink(tfn);
124812153Ssam 				return (FALSE);
124912153Ssam 			}
125012153Ssam 	}
1251396Seric 	/* do final cleanup */
1252396Seric 	if (others)
1253396Seric 	{
12541438Seric 		/* copy it back (perhaps it should be linked?) */
1255396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1256396Seric 		{
12571205Seric 			syserr("cannot reopen \"%s\"", tfn);
1258396Seric 			exit(EX_OSERR);
1259396Seric 		}
1260396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1261396Seric 		{
12621205Seric 			usrerr("cannot create \"%s\"", pfn);
1263585Seric 			return (FALSE);
1264396Seric 		}
1265396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1266396Seric 			fputs(buf, pfp);
1267396Seric 	}
1268396Seric 	else
1269396Seric 	{
12701438Seric 		/* it's empty -- remove it */
1271396Seric 		unlink(pfn);
1272396Seric 	}
1273396Seric 	fclose(tfp);
1274396Seric 	fclose(pfp);
1275396Seric 	unlink(tfn);
1276396Seric 
12771438Seric 	/* actually remove the g-file */
1278396Seric 	if (delete)
1279396Seric 	{
128012153Ssam 		/*
128112153Ssam 		 * Since we've checked above, we can
128212153Ssam 		 * use the return from unlink to
128312153Ssam 		 * determine if the file existed or not.
128412153Ssam 		 */
128512153Ssam 		if (unlink(cp) >= 0)
128612153Ssam 			printf("%12s: removed\n", cp);
1287585Seric 		return (TRUE);
1288396Seric 	}
1289396Seric 	else
1290396Seric 	{
1291416Seric 		printf("%12s: not being edited by you\n", fn);
1292585Seric 		return (FALSE);
1293396Seric 	}
1294396Seric }
12951432Seric 
12961432Seric /*
12971433Seric **  DODIFF -- diff an s-file against a g-file
12981433Seric **
12991433Seric **	Parameters:
13001433Seric **		getv -- argv for the 'get' command.
13011433Seric **		gfile -- name of the g-file to diff against.
13021433Seric **
13031433Seric **	Returns:
13041433Seric **		Result of get.
13051433Seric **
13061433Seric **	Side Effects:
13071433Seric **		none.
13081433Seric */
13091433Seric 
13101433Seric dodiff(getv, gfile)
13111433Seric 	char **getv;
13121433Seric 	char *gfile;
13131433Seric {
13141433Seric 	int pipev[2];
13151433Seric 	int rval;
13161433Seric 	register int i;
13171433Seric 	register int pid;
13181433Seric 	auto int st;
13191433Seric 	extern int errno;
13201433Seric 	int (*osig)();
13211433Seric 
13221905Seric 	printf("\n------- %s -------\n", gfile);
13231871Seric 	fflush(stdout);
13241501Seric 
13251438Seric 	/* create context for diff to run in */
13261433Seric 	if (pipe(pipev) < 0)
13271433Seric 	{
13281433Seric 		syserr("dodiff: pipe failed");
13291433Seric 		exit(EX_OSERR);
13301433Seric 	}
13311433Seric 	if ((pid = fork()) < 0)
13321433Seric 	{
13331433Seric 		syserr("dodiff: fork failed");
13341433Seric 		exit(EX_OSERR);
13351433Seric 	}
13361433Seric 	else if (pid > 0)
13371433Seric 	{
13381433Seric 		/* in parent; run get */
13391433Seric 		OutFile = pipev[1];
13401433Seric 		close(pipev[0]);
13411871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
13421433Seric 		osig = signal(SIGINT, SIG_IGN);
13431433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
13441433Seric 			errno = 0;
13451433Seric 		signal(SIGINT, osig);
13461433Seric 		/* ignore result of diff */
13471433Seric 	}
13481433Seric 	else
13491433Seric 	{
13501433Seric 		/* in child, run diff */
13511433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
13521433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
13531433Seric 		{
13541433Seric 			syserr("dodiff: magic failed");
13551433Seric 			exit(EX_OSERR);
13561433Seric 		}
13571871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
13581433Seric 	}
13591433Seric 	return (rval);
13601433Seric }
13611433Seric 
13621433Seric /*
13631435Seric **  TAIL -- return tail of filename.
13641435Seric **
13651435Seric **	Parameters:
13661435Seric **		fn -- the filename.
13671435Seric **
13681435Seric **	Returns:
13691435Seric **		a pointer to the tail of the filename; e.g., given
13701435Seric **		"cmd/ls.c", "ls.c" is returned.
13711435Seric **
13721435Seric **	Side Effects:
13731435Seric **		none.
13741435Seric */
13751435Seric 
13761435Seric char *
13771435Seric tail(fn)
13781435Seric 	register char *fn;
13791435Seric {
13801435Seric 	register char *p;
13811435Seric 
13821435Seric 	for (p = fn; *p != 0; p++)
13831435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
13841435Seric 			fn = &p[1];
13851435Seric 	return (fn);
13861435Seric }
13871435Seric 
13881435Seric /*
13891822Seric **  GETPFENT -- get an entry from the p-file
1390396Seric **
1391396Seric **	Parameters:
1392396Seric **		pfp -- p-file file pointer
1393396Seric **
1394396Seric **	Returns:
1395396Seric **		pointer to p-file struct for next entry
1396396Seric **		NULL on EOF or error
1397396Seric **
1398396Seric **	Side Effects:
1399396Seric **		Each call wipes out results of previous call.
1400396Seric */
1401396Seric 
1402396Seric struct pfile *
14031822Seric getpfent(pfp)
1404396Seric 	FILE *pfp;
1405396Seric {
1406396Seric 	static struct pfile ent;
140710110Srrh 	static char buf[PFILELG];
1408396Seric 	register char *p;
1409396Seric 	extern char *nextfield();
1410396Seric 
1411396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1412396Seric 		return (NULL);
1413396Seric 
1414396Seric 	ent.p_osid = p = buf;
1415396Seric 	ent.p_nsid = p = nextfield(p);
1416396Seric 	ent.p_user = p = nextfield(p);
1417396Seric 	ent.p_date = p = nextfield(p);
1418396Seric 	ent.p_time = p = nextfield(p);
14192161Seric 	ent.p_aux = p = nextfield(p);
1420396Seric 
1421396Seric 	return (&ent);
1422396Seric }
1423396Seric 
1424396Seric 
1425396Seric char *
1426396Seric nextfield(p)
1427396Seric 	register char *p;
1428396Seric {
1429396Seric 	if (p == NULL || *p == '\0')
1430396Seric 		return (NULL);
1431396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1432396Seric 		p++;
1433396Seric 	if (*p == '\n' || *p == '\0')
1434396Seric 	{
1435396Seric 		*p = '\0';
1436396Seric 		return (NULL);
1437396Seric 	}
1438396Seric 	*p++ = '\0';
1439396Seric 	return (p);
1440396Seric }
14412161Seric /*
14422161Seric **  PUTPFENT -- output a p-file entry to a file
14432161Seric **
14442161Seric **	Parameters:
14452161Seric **		pf -- the p-file entry
14462161Seric **		f -- the file to put it on.
14472161Seric **
14482161Seric **	Returns:
14492161Seric **		none.
14502161Seric **
14512161Seric **	Side Effects:
14522161Seric **		pf is written onto file f.
14532161Seric */
14542161Seric 
14552161Seric putpfent(pf, f)
14562161Seric 	register struct pfile *pf;
14572161Seric 	register FILE *f;
14582161Seric {
14592161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
14602161Seric 		pf->p_user, pf->p_date, pf->p_time);
14612161Seric 	if (pf->p_aux != NULL)
14622161Seric 		fprintf(f, " %s", pf->p_aux);
14632161Seric 	else
14642161Seric 		fprintf(f, "\n");
14652161Seric }
14661432Seric 
14671432Seric /*
14681205Seric **  USRERR -- issue user-level error
14691205Seric **
14701205Seric **	Parameters:
14711205Seric **		f -- format string.
14721205Seric **		p1-p3 -- parameters to a printf.
14731205Seric **
14741205Seric **	Returns:
14751205Seric **		-1
14761205Seric **
14771205Seric **	Side Effects:
14781205Seric **		none.
14791205Seric */
14801205Seric 
14811738Seric /*VARARGS1*/
14821205Seric usrerr(f, p1, p2, p3)
14831205Seric 	char *f;
14841205Seric {
14851205Seric 	fprintf(stderr, "\n%s: ", MyName);
14861205Seric 	fprintf(stderr, f, p1, p2, p3);
14871205Seric 	fprintf(stderr, "\n");
14881205Seric 
14891205Seric 	return (-1);
14901205Seric }
14911432Seric 
14921432Seric /*
14931205Seric **  SYSERR -- print system-generated error.
14941205Seric **
14951205Seric **	Parameters:
14961205Seric **		f -- format string to a printf.
14971205Seric **		p1, p2, p3 -- parameters to f.
14981205Seric **
14991205Seric **	Returns:
15001205Seric **		never.
15011205Seric **
15021205Seric **	Side Effects:
15031205Seric **		none.
15041205Seric */
15051205Seric 
15061738Seric /*VARARGS1*/
15071205Seric syserr(f, p1, p2, p3)
15081205Seric 	char *f;
15091205Seric {
15101205Seric 	extern int errno;
15111205Seric 
15121205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
15131205Seric 	fprintf(stderr, f, p1, p2, p3);
15141205Seric 	fprintf(stderr, "\n");
15151205Seric 	if (errno == 0)
15161205Seric 		exit(EX_SOFTWARE);
15171205Seric 	else
15181205Seric 	{
15191738Seric 		perror(NULL);
15201205Seric 		exit(EX_OSERR);
15211205Seric 	}
15221205Seric }
15231864Seric /*
15241864Seric **  USERNAME -- return name of the current user
15251864Seric **
15261864Seric **	Parameters:
15271864Seric **		none
15281864Seric **
15291864Seric **	Returns:
15301864Seric **		name of current user
15311864Seric **
15321864Seric **	Side Effects:
15331864Seric **		none
15341864Seric */
15351864Seric 
15361864Seric char *
15371864Seric username()
15381864Seric {
15391864Seric # ifdef UIDUSER
15401864Seric 	extern struct passwd *getpwuid();
15411864Seric 	register struct passwd *pw;
15421864Seric 
15431864Seric 	pw = getpwuid(getuid());
15441864Seric 	if (pw == NULL)
15451864Seric 	{
15461864Seric 		syserr("who are you? (uid=%d)", getuid());
15471864Seric 		exit(EX_OSERR);
15481864Seric 	}
15491864Seric 	return (pw->pw_name);
15501864Seric # else
15511905Seric 	extern char *getlogin();
15526785Smckusick 	register char *p;
15531905Seric 
15546785Smckusick 	p = getenv("USER");
15556785Smckusick 	if (p == NULL || p[0] == '\0')
15566785Smckusick 		p = getlogin();
15576785Smckusick 	return (p);
15581864Seric # endif UIDUSER
15591864Seric }
156010110Srrh 
156110110Srrh /*
156210110Srrh **	Guarded string manipulation routines; the last argument
156310110Srrh **	is the length of the buffer into which the strcpy or strcat
156410110Srrh **	is to be done.
156510110Srrh */
156610110Srrh char *gstrcat(to, from, length)
156710110Srrh 	char	*to, *from;
156810110Srrh 	int	length;
156910110Srrh {
157010110Srrh 	if (strlen(from) + strlen(to) >= length) {
157110110Srrh 		gstrbotch(to, from);
157210110Srrh 	}
157310110Srrh 	return(strcat(to, from));
157410110Srrh }
157510110Srrh 
157610110Srrh char *gstrncat(to, from, n, length)
157710110Srrh 	char	*to, *from;
157810110Srrh 	int	n;
157910110Srrh 	int	length;
158010110Srrh {
158110110Srrh 	if (n + strlen(to) >= length) {
158210110Srrh 		gstrbotch(to, from);
158310110Srrh 	}
158410110Srrh 	return(strncat(to, from, n));
158510110Srrh }
158610110Srrh 
158710110Srrh char *gstrcpy(to, from, length)
158810110Srrh 	char	*to, *from;
158910110Srrh 	int	length;
159010110Srrh {
159110110Srrh 	if (strlen(from) >= length) {
159210110Srrh 		gstrbotch(from, (char *)0);
159310110Srrh 	}
159410110Srrh 	return(strcpy(to, from));
159510110Srrh }
159610110Srrh gstrbotch(str1, str2)
159710110Srrh 	char	*str1, *str2;
159810110Srrh {
159910110Srrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
160010110Srrh }
1601