xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 33251)
121574Sdist /*
221574Sdist  * Copyright (c) 1980 Regents of the University of California.
321574Sdist  * All rights reserved.  The Berkeley software License Agreement
421574Sdist  * specifies the terms and conditions for redistribution.
521574Sdist  */
621574Sdist 
713622Ssam #ifndef lint
821574Sdist char copyright[] =
921574Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1021574Sdist  All rights reserved.\n";
1121574Sdist #endif not lint
1213622Ssam 
1321574Sdist #ifndef lint
14*33251Sbostic static char sccsid[] = "@(#)sccs.c	5.4 (Berkeley) 01/03/88";
1521574Sdist #endif not lint
1621574Sdist 
17148Seric # include <stdio.h>
186784Smckusick # include <sys/param.h>
19148Seric # include <sys/stat.h>
2013622Ssam # include <sys/dir.h>
211433Seric # include <errno.h>
221433Seric # include <signal.h>
23148Seric # include <sysexits.h>
242140Seric # include <pwd.h>
25148Seric 
26828Seric /*
27828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
28828Seric **
29828Seric **	Without trying to add any functionality to speak of, this
30828Seric **	program tries to make SCCS a little more accessible to human
31828Seric **	types.  The main thing it does is automatically put the
32828Seric **	string "SCCS/s." on the front of names.  Also, it has a
33828Seric **	couple of things that are designed to shorten frequent
34828Seric **	combinations, e.g., "delget" which expands to a "delta"
35828Seric **	and a "get".
36828Seric **
37828Seric **	This program can also function as a setuid front end.
38828Seric **	To do this, you should copy the source, renaming it to
39828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
40828Seric **	in the program (e.g., syssccs might default -d to
41828Seric **	"/usr/src/sys").  Then recompile and put the result
42828Seric **	as setuid to whomever you want.  In this mode, sccs
43828Seric **	knows to not run setuid for certain programs in order
44828Seric **	to preserve security, and so forth.
45828Seric **
46828Seric **	Usage:
47828Seric **		sccs [flags] command [args]
48828Seric **
49828Seric **	Flags:
50828Seric **		-d<dir>		<dir> represents a directory to search
51828Seric **				out of.  It should be a full pathname
52828Seric **				for general usage.  E.g., if <dir> is
53828Seric **				"/usr/src/sys", then a reference to the
54828Seric **				file "dev/bio.c" becomes a reference to
55828Seric **				"/usr/src/sys/dev/bio.c".
56828Seric **		-p<path>	prepends <path> to the final component
57828Seric **				of the pathname.  By default, this is
58828Seric **				"SCCS".  For example, in the -d example
59828Seric **				above, the path then gets modified to
60828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
61828Seric **				more common usage (without the -d flag),
62828Seric **				"prog.c" would get modified to
63828Seric **				"SCCS/s.prog.c".  In both cases, the
64828Seric **				"s." gets automatically prepended.
65828Seric **		-r		run as the real user.
66828Seric **
67828Seric **	Commands:
68828Seric **		admin,
69828Seric **		get,
70828Seric **		delta,
71828Seric **		rmdel,
7230959Sbostic **		cdc,
73828Seric **		etc.		Straight out of SCCS; only difference
74828Seric **				is that pathnames get modified as
75828Seric **				described above.
7630959Sbostic **		enter		Front end doing "sccs admin -i<name> <name>"
7730959Sbostic **		create		Macro for "enter" followed by "get".
78828Seric **		edit		Macro for "get -e".
79828Seric **		unedit		Removes a file being edited, knowing
80828Seric **				about p-files, etc.
81828Seric **		delget		Macro for "delta" followed by "get".
82828Seric **		deledit		Macro for "delta" followed by "get -e".
8330959Sbostic **		branch		Macro for "get -b -e", followed by "delta
8430959Sbostic **				-s -n", followd by "get -e -t -g".
8530959Sbostic **		diffs		"diff" the specified version of files
8630959Sbostic **				and the checked-out version.
8730959Sbostic **		print		Macro for "prs -e" followed by "get -p -m".
8830959Sbostic **		tell		List what files are being edited.
8930959Sbostic **		info		Print information about files being edited.
90828Seric **		clean		Remove all files that can be
91828Seric **				regenerated from SCCS files.
921205Seric **		check		Like info, but return exit status, for
93828Seric **				use in makefiles.
94828Seric **		fix		Remove a top delta & reedit, but save
95828Seric **				the previous changes in that delta.
96828Seric **
97828Seric **	Compilation Flags:
98828Seric **		UIDUSER -- determine who the user is by looking at the
99828Seric **			uid rather than the login name -- for machines
100828Seric **			where SCCS gets the user in this way.
1011270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
1021205Seric **			this value.  This is so that the setuid
1031205Seric **			aspects of this program cannot be abused.
1041270Seric **			This flag also disables the -p flag.
1051270Seric **		SCCSPATH -- the default for the -p flag.
1061437Seric **		MYNAME -- the title this program should print when it
1071437Seric **			gives error messages.
108828Seric **
109828Seric **	Compilation Instructions:
110828Seric **		cc -O -n -s sccs.c
1111437Seric **		The flags listed above can be -D defined to simplify
1121437Seric **			recompilation for variant versions.
113828Seric **
114828Seric **	Author:
115828Seric **		Eric Allman, UCB/INGRES
1161270Seric **		Copyright 1980 Regents of the University of California
117828Seric */
118155Seric 
1191432Seric 
1201270Seric /*******************  Configuration Information  ********************/
1211270Seric 
1221437Seric # ifndef SCCSPATH
1231432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1241437Seric # endif NOT SCCSPATH
125828Seric 
1261437Seric # ifndef MYNAME
1271437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1281437Seric # endif NOT MYNAME
1291270Seric 
1301432Seric # ifndef PROGPATH
1316784Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1321432Seric # endif PROGPATH
1331432Seric 
1341270Seric /****************  End of Configuration Information  ****************/
1351432Seric 
136157Seric typedef char	bool;
137200Seric # define TRUE	1
138200Seric # define FALSE	0
139157Seric 
1401438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1411438Seric 
142148Seric struct sccsprog
143148Seric {
144148Seric 	char	*sccsname;	/* name of SCCS routine */
145200Seric 	short	sccsoper;	/* opcode, see below */
146200Seric 	short	sccsflags;	/* flags, see below */
147148Seric 	char	*sccspath;	/* pathname of binary implementing */
148148Seric };
149148Seric 
150200Seric /* values for sccsoper */
151200Seric # define PROG		0	/* call a program */
152201Seric # define CMACRO		1	/* command substitution macro */
153226Seric # define FIX		2	/* fix a delta */
154261Seric # define CLEAN		3	/* clean out recreatable files */
155396Seric # define UNEDIT		4	/* unedit a file */
1561431Seric # define SHELL		5	/* call a shell file (like PROG) */
1571433Seric # define DIFFS		6	/* diff between sccs & file out */
1581871Seric # define DODIFF		7	/* internal call to diff program */
15910104Srrh # define ENTER		8	/* enter new files */
160200Seric 
161157Seric /* bits for sccsflags */
162200Seric # define NO_SDOT	0001	/* no s. on front of args */
163200Seric # define REALUSER	0002	/* protected (e.g., admin) */
164148Seric 
165819Seric /* modes for the "clean", "info", "check" ops */
166819Seric # define CLEANC		0	/* clean command */
167819Seric # define INFOC		1	/* info command */
168819Seric # define CHECKC		2	/* check command */
1691730Seric # define TELLC		3	/* give list of files being edited */
170819Seric 
1711432Seric /*
1721432Seric **  Description of commands known to this program.
1731432Seric **	First argument puts the command into a class.  Second arg is
1741432Seric **	info regarding treatment of this command.  Third arg is a
1751432Seric **	list of flags this command accepts from macros, etc.  Fourth
1761432Seric **	arg is the pathname of the implementing program, or the
1771432Seric **	macro definition, or the arg to a sub-algorithm.
1781432Seric */
179202Seric 
180148Seric struct sccsprog SccsProg[] =
181148Seric {
1821864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
18330959Sbostic 	"cdc",		PROG,	0,			PROGPATH(rmdel),
1841864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1851864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1861864Seric 	"get",		PROG,	0,			PROGPATH(get),
1871864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1882136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1891864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1901864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1912136Seric 	"val",		PROG,	0,			PROGPATH(val),
1921864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1931864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1941864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1951864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1962138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1971864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1981864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1991864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
2001864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
2011864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
2021864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
2031864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
2041871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
20530959Sbostic 	"print",	CMACRO,	0,			"prs -e/get -p -m -s",
2062226Seric 	"branch",	CMACRO,	NO_SDOT,
2072226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
20810104Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
20910104Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
2101864Seric 	NULL,		-1,	0,			NULL
211148Seric };
212148Seric 
2131432Seric /* one line from a p-file */
214396Seric struct pfile
215396Seric {
216396Seric 	char	*p_osid;	/* old SID */
217396Seric 	char	*p_nsid;	/* new SID */
218396Seric 	char	*p_user;	/* user who did edit */
219396Seric 	char	*p_date;	/* date of get */
220396Seric 	char	*p_time;	/* time of get */
2212161Seric 	char	*p_aux;		/* extra info at end */
222396Seric };
223396Seric 
2241270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2251270Seric # ifdef SCCSDIR
2261270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2271205Seric # else
2281270Seric char	*SccsDir = "";
2291205Seric # endif
2301437Seric char	MyName[] = MYNAME;	/* name used in messages */
2311433Seric int	OutFile = -1;		/* override output file for commands */
232157Seric bool	RealUser;		/* if set, running as real user */
233393Seric # ifdef DEBUG
234393Seric bool	Debug;			/* turn on tracing */
235393Seric # endif
2362139Seric # ifndef V6
2372139Seric extern char	*getenv();
2382139Seric # endif V6
23910110Srrh 
24030959Sbostic extern char	*sys_siglist[];
24130959Sbostic 
24210110Srrh char *gstrcat(), *strcat();
24310110Srrh char *gstrncat(), *strncat();
24410110Srrh char *gstrcpy(), *strcpy();
24510110Srrh #define	FBUFSIZ	BUFSIZ
24610110Srrh #define	PFILELG	120
2471432Seric 
248148Seric main(argc, argv)
249148Seric 	int argc;
250148Seric 	char **argv;
251148Seric {
252148Seric 	register char *p;
253262Seric 	extern struct sccsprog *lookup();
2541282Seric 	register int i;
2552139Seric # ifndef V6
2562139Seric # ifndef SCCSDIR
2572140Seric 	register struct passwd *pw;
2582140Seric 	extern struct passwd *getpwnam();
25910110Srrh 	char buf[FBUFSIZ];
2602140Seric 
2612139Seric 	/* pull "SccsDir" out of the environment (possibly) */
26210260Seric 	p = getenv("PROJECTDIR");
2632140Seric 	if (p != NULL && p[0] != '\0')
2642140Seric 	{
2652140Seric 		if (p[0] == '/')
2662140Seric 			SccsDir = p;
2672140Seric 		else
2682140Seric 		{
2692140Seric 			pw = getpwnam(p);
2702140Seric 			if (pw == NULL)
2712140Seric 			{
2722140Seric 				usrerr("user %s does not exist", p);
2732140Seric 				exit(EX_USAGE);
2742140Seric 			}
27510110Srrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
27610110Srrh 			gstrcat(buf, "/src", sizeof(buf));
2772140Seric 			if (access(buf, 0) < 0)
2782140Seric 			{
27910110Srrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
28010110Srrh 				gstrcat(buf, "/source", sizeof(buf));
2812140Seric 				if (access(buf, 0) < 0)
2822140Seric 				{
2832140Seric 					usrerr("project %s has no source!", p);
2842140Seric 					exit(EX_USAGE);
2852140Seric 				}
2862140Seric 			}
2872140Seric 			SccsDir = buf;
2882140Seric 		}
2892140Seric 	}
2902139Seric # endif SCCSDIR
2912139Seric # endif V6
2922139Seric 
293148Seric 	/*
294148Seric 	**  Detect and decode flags intended for this program.
295148Seric 	*/
296148Seric 
297200Seric 	if (argc < 2)
298148Seric 	{
2991205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
300200Seric 		exit(EX_USAGE);
301200Seric 	}
302200Seric 	argv[argc] = NULL;
303200Seric 
304262Seric 	if (lookup(argv[0]) == NULL)
305200Seric 	{
306262Seric 		while ((p = *++argv) != NULL)
307148Seric 		{
308262Seric 			if (*p != '-')
309262Seric 				break;
310262Seric 			switch (*++p)
311262Seric 			{
312262Seric 			  case 'r':		/* run as real user */
313262Seric 				setuid(getuid());
314262Seric 				RealUser++;
315262Seric 				break;
316148Seric 
3171270Seric # ifndef SCCSDIR
318262Seric 			  case 'p':		/* path of sccs files */
319262Seric 				SccsPath = ++p;
3202348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3212348Seric 					SccsPath = *++argv;
322262Seric 				break;
323148Seric 
324588Seric 			  case 'd':		/* directory to search from */
325588Seric 				SccsDir = ++p;
3262348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3272348Seric 					SccsDir = *++argv;
328588Seric 				break;
3291205Seric # endif
330588Seric 
331393Seric # ifdef DEBUG
332393Seric 			  case 'T':		/* trace */
333393Seric 				Debug++;
334393Seric 				break;
335393Seric # endif
336393Seric 
337262Seric 			  default:
3381205Seric 				usrerr("unknown option -%s", p);
339262Seric 				break;
340262Seric 			}
341148Seric 		}
342262Seric 		if (SccsPath[0] == '\0')
343262Seric 			SccsPath = ".";
344148Seric 	}
345148Seric 
3461737Seric 	i = command(argv, FALSE, "");
3471282Seric 	exit(i);
348200Seric }
3491432Seric 
3501432Seric /*
3511282Seric **  COMMAND -- look up and perform a command
3521282Seric **
3531282Seric **	This routine is the guts of this program.  Given an
3541282Seric **	argument vector, it looks up the "command" (argv[0])
3551282Seric **	in the configuration table and does the necessary stuff.
3561282Seric **
3571282Seric **	Parameters:
3581282Seric **		argv -- an argument vector to process.
3591282Seric **		forkflag -- if set, fork before executing the command.
3601316Seric **		editflag -- if set, only include flags listed in the
3611316Seric **			sccsklets field of the command descriptor.
3621316Seric **		arg0 -- a space-seperated list of arguments to insert
3631316Seric **			before argv.
3641282Seric **
3651282Seric **	Returns:
3661282Seric **		zero -- command executed ok.
3671282Seric **		else -- error status.
3681282Seric **
3691282Seric **	Side Effects:
3701282Seric **		none.
3711282Seric */
372157Seric 
3731737Seric command(argv, forkflag, arg0)
374200Seric 	char **argv;
375201Seric 	bool forkflag;
3761316Seric 	char *arg0;
377200Seric {
378200Seric 	register struct sccsprog *cmd;
379200Seric 	register char *p;
38010110Srrh 	char buf[FBUFSIZ];
381262Seric 	extern struct sccsprog *lookup();
3821316Seric 	char *nav[1000];
3831316Seric 	char **np;
3841431Seric 	register char **ap;
385585Seric 	register int i;
3861431Seric 	register char *q;
387585Seric 	extern bool unedit();
3881282Seric 	int rval = 0;
3891316Seric 	extern char *index();
3901316Seric 	extern char *makefile();
3911737Seric 	char *editchs;
3921435Seric 	extern char *tail();
393200Seric 
394393Seric # ifdef DEBUG
395393Seric 	if (Debug)
396393Seric 	{
3971316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3981316Seric 		for (np = argv; *np != NULL; np++)
3991316Seric 			printf("\t\"%s\"\n", *np);
400393Seric 	}
401393Seric # endif
402393Seric 
403157Seric 	/*
4041316Seric 	**  Copy arguments.
4051438Seric 	**	Copy from arg0 & if necessary at most one arg
4061438Seric 	**	from argv[0].
4071316Seric 	*/
4081316Seric 
4091431Seric 	np = ap = &nav[1];
4101737Seric 	editchs = NULL;
4111821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
4121316Seric 	{
4131316Seric 		*np++ = q;
4141316Seric 		while (*p == ' ')
4151316Seric 			p++;
4161737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
4171316Seric 			*q++ = *p++;
4181316Seric 		*q++ = '\0';
4191737Seric 		if (*p == ':')
4201737Seric 		{
4211737Seric 			editchs = q;
4221821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4231737Seric 				*q++ = *p;
4241737Seric 			*q++ = '\0';
4251737Seric 		}
4261316Seric 	}
4271316Seric 	*np = NULL;
4281431Seric 	if (*ap == NULL)
4291316Seric 		*np++ = *argv++;
4301316Seric 
4311316Seric 	/*
432148Seric 	**  Look up command.
4331431Seric 	**	At this point, *ap is the command name.
434148Seric 	*/
435148Seric 
4361431Seric 	cmd = lookup(*ap);
437262Seric 	if (cmd == NULL)
438148Seric 	{
4391431Seric 		usrerr("Unknown command \"%s\"", *ap);
4401282Seric 		return (EX_USAGE);
441148Seric 	}
442148Seric 
443148Seric 	/*
4441316Seric 	**  Copy remaining arguments doing editing as appropriate.
4451316Seric 	*/
4461316Seric 
4471316Seric 	for (; *argv != NULL; argv++)
4481316Seric 	{
4491316Seric 		p = *argv;
4501316Seric 		if (*p == '-')
4511316Seric 		{
4521737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4531316Seric 				*np++ = p;
4541316Seric 		}
4551316Seric 		else
4561316Seric 		{
4571316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4581316Seric 				p = makefile(p);
4591316Seric 			if (p != NULL)
4601316Seric 				*np++ = p;
4611316Seric 		}
4621316Seric 	}
4631316Seric 	*np = NULL;
4641316Seric 
4651316Seric 	/*
466200Seric 	**  Interpret operation associated with this command.
467157Seric 	*/
468157Seric 
469200Seric 	switch (cmd->sccsoper)
470200Seric 	{
4711431Seric 	  case SHELL:		/* call a shell file */
4721431Seric 		*ap = cmd->sccspath;
4731431Seric 		*--ap = "sh";
4741431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4751431Seric 		break;
4761431Seric 
477200Seric 	  case PROG:		/* call an sccs prog */
4781431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
479201Seric 		break;
480201Seric 
481201Seric 	  case CMACRO:		/* command macro */
4821438Seric 		/* step through & execute each part of the macro */
483201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
484201Seric 		{
4851316Seric 			q = p;
4861316Seric 			while (*p != '\0' && *p != '/')
4871316Seric 				p++;
4881737Seric 			rval = command(&ap[1], *p != '\0', q);
4891282Seric 			if (rval != 0)
4901282Seric 				break;
491201Seric 		}
4921282Seric 		break;
493157Seric 
494226Seric 	  case FIX:		/* fix a delta */
49530959Sbostic 		if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
496226Seric 		{
4971205Seric 			usrerr("-r flag needed for fix command");
4981282Seric 			rval = EX_USAGE;
499226Seric 			break;
500226Seric 		}
5011438Seric 
5021438Seric 		/* get the version with all changes */
5031737Seric 		rval = command(&ap[1], TRUE, "get -k");
5041438Seric 
5051438Seric 		/* now remove that version from the s-file */
5061282Seric 		if (rval == 0)
5071737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
5081438Seric 
5091438Seric 		/* and edit the old version (but don't clobber new vers) */
5101282Seric 		if (rval == 0)
5111737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
5121282Seric 		break;
513226Seric 
514261Seric 	  case CLEAN:
5151822Seric 		rval = clean((int) cmd->sccspath, ap);
516261Seric 		break;
517261Seric 
518396Seric 	  case UNEDIT:
5191431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
520585Seric 		{
5211316Seric 			if (unedit(*argv))
5221316Seric 				*np++ = *argv;
523585Seric 		}
5241316Seric 		*np = NULL;
5251438Seric 
5261438Seric 		/* get all the files that we unedited successfully */
5271738Seric 		if (np > &ap[1])
5281737Seric 			rval = command(&ap[1], FALSE, "get");
529396Seric 		break;
530396Seric 
5311433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5321433Seric 		/* find the end of the flag arguments */
5331433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5341433Seric 			continue;
5351433Seric 		argv = np;
5361433Seric 
5371433Seric 		/* for each file, do the diff */
5381502Seric 		p = argv[1];
5391433Seric 		while (*np != NULL)
5401433Seric 		{
5411438Seric 			/* messy, but we need a null terminated argv */
5421433Seric 			*argv = *np++;
5431502Seric 			argv[1] = NULL;
5441435Seric 			i = dodiff(ap, tail(*argv));
5451433Seric 			if (rval == 0)
5461433Seric 				rval = i;
5471502Seric 			argv[1] = p;
5481433Seric 		}
5491433Seric 		break;
5501433Seric 
5511871Seric 	  case DODIFF:		/* internal diff call */
5521871Seric 		setuid(getuid());
5531871Seric 		for (np = ap; *np != NULL; np++)
5541871Seric 		{
5551871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5561871Seric 				(*np)[1] = 'c';
5571871Seric 		}
5581871Seric 
5591871Seric 		/* insert "-" argument */
5601871Seric 		np[1] = NULL;
5611871Seric 		np[0] = np[-1];
5621871Seric 		np[-1] = "-";
5631871Seric 
5641871Seric 		/* execute the diff program of choice */
5651871Seric # ifndef V6
5661871Seric 		execvp("diff", ap);
5671871Seric # endif
5681871Seric 		execv(cmd->sccspath, argv);
5691871Seric 		syserr("cannot exec %s", cmd->sccspath);
5701871Seric 		exit(EX_OSERR);
5711871Seric 
57210104Srrh 	  case ENTER:		/* enter new sccs files */
5736785Smckusick 		/* skip over flag arguments */
5746785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5756785Smckusick 			continue;
5766785Smckusick 		argv = np;
5776785Smckusick 
5786785Smckusick 		/* do an admin for each file */
5796785Smckusick 		p = argv[1];
5806785Smckusick 		while (*np != NULL)
5816785Smckusick 		{
5826785Smckusick 			printf("\n%s:\n", *np);
58310110Srrh 			strcpy(buf, "-i");
58410110Srrh 			gstrcat(buf, *np, sizeof(buf));
5856785Smckusick 			ap[0] = buf;
5866785Smckusick 			argv[0] = tail(*np);
5876785Smckusick 			argv[1] = NULL;
5886785Smckusick 			rval = command(ap, TRUE, "admin");
5896785Smckusick 			argv[1] = p;
5906785Smckusick 			if (rval == 0)
5916785Smckusick 			{
59210110Srrh 				strcpy(buf, ",");
59310110Srrh 				gstrcat(buf, tail(*np), sizeof(buf));
5946785Smckusick 				if (link(*np, buf) >= 0)
5956785Smckusick 					unlink(*np);
5966785Smckusick 			}
5976785Smckusick 			np++;
5986785Smckusick 		}
5996785Smckusick 		break;
6006785Smckusick 
601200Seric 	  default:
6021205Seric 		syserr("oper %d", cmd->sccsoper);
603200Seric 		exit(EX_SOFTWARE);
604200Seric 	}
6051282Seric # ifdef DEBUG
6061282Seric 	if (Debug)
6071282Seric 		printf("command: rval=%d\n", rval);
6081282Seric # endif
6091282Seric 	return (rval);
610200Seric }
6111432Seric 
6121432Seric /*
613262Seric **  LOOKUP -- look up an SCCS command name.
614262Seric **
615262Seric **	Parameters:
616262Seric **		name -- the name of the command to look up.
617262Seric **
618262Seric **	Returns:
619262Seric **		ptr to command descriptor for this command.
620262Seric **		NULL if no such entry.
621262Seric **
622262Seric **	Side Effects:
623262Seric **		none.
624262Seric */
625200Seric 
626262Seric struct sccsprog *
627262Seric lookup(name)
628262Seric 	char *name;
629262Seric {
630262Seric 	register struct sccsprog *cmd;
631226Seric 
632262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
633262Seric 	{
634262Seric 		if (strcmp(cmd->sccsname, name) == 0)
635262Seric 			return (cmd);
636262Seric 	}
637262Seric 	return (NULL);
638262Seric }
6391432Seric 
6401432Seric /*
6411282Seric **  CALLPROG -- call a program
6421282Seric **
6431316Seric **	Used to call the SCCS programs.
6441282Seric **
6451282Seric **	Parameters:
6461282Seric **		progpath -- pathname of the program to call.
6471282Seric **		flags -- status flags from the command descriptors.
6481282Seric **		argv -- an argument vector to pass to the program.
6491282Seric **		forkflag -- if true, fork before calling, else just
6501282Seric **			exec.
6511282Seric **
6521282Seric **	Returns:
6531282Seric **		The exit status of the program.
6541282Seric **		Nothing if forkflag == FALSE.
6551282Seric **
6561282Seric **	Side Effects:
6571282Seric **		Can exit if forkflag == FALSE.
6581282Seric */
659226Seric 
660200Seric callprog(progpath, flags, argv, forkflag)
661200Seric 	char *progpath;
662200Seric 	short flags;
663200Seric 	char **argv;
664200Seric 	bool forkflag;
665200Seric {
666200Seric 	register int i;
66730959Sbostic 	register int wpid;
668201Seric 	auto int st;
66930959Sbostic 	register int sigcode;
67030959Sbostic 	register int coredumped;
67130959Sbostic 	register char *sigmsg;
67230959Sbostic 	auto char sigmsgbuf[10+1];	/* "Signal 127" + terminating '\0' */
673200Seric 
6741316Seric # ifdef DEBUG
6751316Seric 	if (Debug)
6761316Seric 	{
6771316Seric 		printf("callprog:\n");
6781316Seric 		for (i = 0; argv[i] != NULL; i++)
6791316Seric 			printf("\t\"%s\"\n", argv[i]);
6801316Seric 	}
6811316Seric # endif
6821316Seric 
683200Seric 	if (*argv == NULL)
684200Seric 		return (-1);
685200Seric 
686157Seric 	/*
687226Seric 	**  Fork if appropriate.
688148Seric 	*/
689148Seric 
690200Seric 	if (forkflag)
691200Seric 	{
692393Seric # ifdef DEBUG
693393Seric 		if (Debug)
694393Seric 			printf("Forking\n");
695393Seric # endif
696200Seric 		i = fork();
697200Seric 		if (i < 0)
698200Seric 		{
6991205Seric 			syserr("cannot fork");
700200Seric 			exit(EX_OSERR);
701200Seric 		}
702200Seric 		else if (i > 0)
703201Seric 		{
70430959Sbostic 			while ((wpid = wait(&st)) != -1 && wpid != i)
70530959Sbostic 				;
70630959Sbostic 			if ((sigcode = st & 0377) == 0)
7071282Seric 				st = (st >> 8) & 0377;
70830959Sbostic 			else
70930959Sbostic 			{
71030959Sbostic 				coredumped = sigcode & 0200;
71130959Sbostic 				sigcode &= 0177;
71230959Sbostic 				if (sigcode != SIGINT && sigcode != SIGPIPE)
71330959Sbostic 				{
71430959Sbostic 					if (sigcode < NSIG)
71530959Sbostic 						sigmsg = sys_siglist[sigcode];
71630959Sbostic 					else
71730959Sbostic 					{
71830959Sbostic 						sprintf(sigmsgbuf, "Signal %d",
71930959Sbostic 						    sigcode);
72030959Sbostic 						sigmsg = sigmsgbuf;
72130959Sbostic 					}
72230959Sbostic 					fprintf(stderr, "sccs: %s: %s%s", argv[0],
72330959Sbostic 					    sigmsg,
72430959Sbostic 					    coredumped ? " - core dumped": "");
72530959Sbostic 				}
72630959Sbostic 				st = EX_SOFTWARE;
72730959Sbostic 			}
7281433Seric 			if (OutFile >= 0)
7291433Seric 			{
7301433Seric 				close(OutFile);
7311433Seric 				OutFile = -1;
7321433Seric 			}
733201Seric 			return (st);
734201Seric 		}
735200Seric 	}
7361433Seric 	else if (OutFile >= 0)
7371433Seric 	{
7381433Seric 		syserr("callprog: setting stdout w/o forking");
7391433Seric 		exit(EX_SOFTWARE);
7401433Seric 	}
741200Seric 
7421433Seric 	/* set protection as appropriate */
743200Seric 	if (bitset(REALUSER, flags))
744200Seric 		setuid(getuid());
7451433Seric 
7461433Seric 	/* change standard input & output if needed */
7471433Seric 	if (OutFile >= 0)
7481433Seric 	{
7491433Seric 		close(1);
7501433Seric 		dup(OutFile);
7511433Seric 		close(OutFile);
7521433Seric 	}
753226Seric 
7541433Seric 	/* call real SCCS program */
755226Seric 	execv(progpath, argv);
7561205Seric 	syserr("cannot execute %s", progpath);
757148Seric 	exit(EX_UNAVAILABLE);
7581738Seric 	/*NOTREACHED*/
759148Seric }
7601432Seric 
7611432Seric /*
762586Seric **  MAKEFILE -- make filename of SCCS file
763586Seric **
764586Seric **	If the name passed is already the name of an SCCS file,
765586Seric **	just return it.  Otherwise, munge the name into the name
766586Seric **	of the actual SCCS file.
767586Seric **
768586Seric **	There are cases when it is not clear what you want to
769586Seric **	do.  For example, if SccsPath is an absolute pathname
770586Seric **	and the name given is also an absolute pathname, we go
771586Seric **	for SccsPath (& only use the last component of the name
772586Seric **	passed) -- this is important for security reasons (if
773586Seric **	sccs is being used as a setuid front end), but not
774586Seric **	particularly intuitive.
775586Seric **
776586Seric **	Parameters:
777586Seric **		name -- the file name to be munged.
778586Seric **
779586Seric **	Returns:
780586Seric **		The pathname of the sccs file.
781586Seric **		NULL on error.
782586Seric **
783586Seric **	Side Effects:
784586Seric **		none.
785586Seric */
786148Seric 
787148Seric char *
788148Seric makefile(name)
789148Seric 	char *name;
790148Seric {
791148Seric 	register char *p;
79210110Srrh 	char buf[3*FBUFSIZ];
793148Seric 	extern char *malloc();
794586Seric 	extern char *rindex();
795588Seric 	extern bool safepath();
796587Seric 	extern bool isdir();
797587Seric 	register char *q;
798148Seric 
799586Seric 	p = rindex(name, '/');
800586Seric 	if (p == NULL)
801586Seric 		p = name;
802586Seric 	else
803586Seric 		p++;
804586Seric 
805148Seric 	/*
806588Seric 	**  Check to see that the path is "safe", i.e., that we
807588Seric 	**  are not letting some nasty person use the setuid part
808588Seric 	**  of this program to look at or munge some presumably
809588Seric 	**  hidden files.
810148Seric 	*/
811148Seric 
812588Seric 	if (SccsDir[0] == '/' && !safepath(name))
813588Seric 		return (NULL);
814586Seric 
815586Seric 	/*
816588Seric 	**  Create the base pathname.
817586Seric 	*/
818586Seric 
8191438Seric 	/* first the directory part */
820588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
821148Seric 	{
82210110Srrh 		gstrcpy(buf, SccsDir, sizeof(buf));
82310110Srrh 		gstrcat(buf, "/", sizeof(buf));
824586Seric 	}
825586Seric 	else
82610110Srrh 		gstrcpy(buf, "", sizeof(buf));
8271438Seric 
8281438Seric 	/* then the head of the pathname */
82910110Srrh 	gstrncat(buf, name, p - name, sizeof(buf));
830587Seric 	q = &buf[strlen(buf)];
8311438Seric 
8321438Seric 	/* now copy the final part of the name, in case useful */
83310110Srrh 	gstrcpy(q, p, sizeof(buf));
8341438Seric 
8351438Seric 	/* so is it useful? */
836587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
837586Seric 	{
8381438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
83910110Srrh 		gstrcpy(q, SccsPath, sizeof(buf));
84010110Srrh 		gstrcat(buf, "/s.", sizeof(buf));
8411438Seric 
8421438Seric 		/* and now the end of the name */
84310110Srrh 		gstrcat(buf, p, sizeof(buf));
844586Seric 	}
845148Seric 
8461438Seric 	/* if i haven't changed it, why did I do all this? */
847588Seric 	if (strcmp(buf, name) == 0)
848588Seric 		p = name;
849588Seric 	else
850148Seric 	{
8511438Seric 		/* but if I have, squirrel it away */
852588Seric 		p = malloc(strlen(buf) + 1);
853588Seric 		if (p == NULL)
854588Seric 		{
855588Seric 			perror("Sccs: no mem");
856588Seric 			exit(EX_OSERR);
857588Seric 		}
858588Seric 		strcpy(p, buf);
859148Seric 	}
8601438Seric 
861148Seric 	return (p);
862148Seric }
8631432Seric 
8641432Seric /*
865587Seric **  ISDIR -- return true if the argument is a directory.
866587Seric **
867587Seric **	Parameters:
868587Seric **		name -- the pathname of the file to check.
869587Seric **
870587Seric **	Returns:
871587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
872587Seric **
873587Seric **	Side Effects:
874587Seric **		none.
875587Seric */
876587Seric 
877587Seric bool
878587Seric isdir(name)
879587Seric 	char *name;
880587Seric {
881587Seric 	struct stat stbuf;
882587Seric 
883587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
884587Seric }
8851432Seric 
8861432Seric /*
887586Seric **  SAFEPATH -- determine whether a pathname is "safe"
888586Seric **
889586Seric **	"Safe" pathnames only allow you to get deeper into the
890586Seric **	directory structure, i.e., full pathnames and ".." are
891586Seric **	not allowed.
892586Seric **
893586Seric **	Parameters:
894586Seric **		p -- the name to check.
895586Seric **
896586Seric **	Returns:
897586Seric **		TRUE -- if the path is safe.
898586Seric **		FALSE -- if the path is not safe.
899586Seric **
900586Seric **	Side Effects:
901586Seric **		Prints a message if the path is not safe.
902586Seric */
903586Seric 
904586Seric bool
905586Seric safepath(p)
906586Seric 	register char *p;
907586Seric {
908586Seric 	extern char *index();
909586Seric 
910586Seric 	if (*p != '/')
911586Seric 	{
912586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
913586Seric 		{
914586Seric 			p = index(p, '/');
915586Seric 			if (p == NULL)
916586Seric 				return (TRUE);
917586Seric 			p++;
918586Seric 		}
919586Seric 	}
920586Seric 
921586Seric 	printf("You may not use full pathnames or \"..\"\n");
922586Seric 	return (FALSE);
923586Seric }
9241432Seric 
9251432Seric /*
926261Seric **  CLEAN -- clean out recreatable files
927261Seric **
928261Seric **	Any file for which an "s." file exists but no "p." file
929261Seric **	exists in the current directory is purged.
930261Seric **
931261Seric **	Parameters:
9321822Seric **		mode -- tells whether this came from a "clean", "info", or
9331822Seric **			"check" command.
9341822Seric **		argv -- the rest of the argument vector.
935261Seric **
936261Seric **	Returns:
937261Seric **		none.
938261Seric **
939261Seric **	Side Effects:
940819Seric **		Removes files in the current directory.
941819Seric **		Prints information regarding files being edited.
942819Seric **		Exits if a "check" command.
943261Seric */
944261Seric 
9451822Seric clean(mode, argv)
946819Seric 	int mode;
9471822Seric 	char **argv;
948261Seric {
9496784Smckusick 	struct direct *dir;
95010110Srrh 	char buf[FBUFSIZ];
9512140Seric 	char *bufend;
95230466Smckusick 	register DIR *dirp;
953346Seric 	register char *basefile;
954351Seric 	bool gotedit;
9551822Seric 	bool gotpfent;
956394Seric 	FILE *pfp;
9571822Seric 	bool nobranch = FALSE;
9581822Seric 	extern struct pfile *getpfent();
9591822Seric 	register struct pfile *pf;
9601822Seric 	register char **ap;
9611864Seric 	extern char *username();
9621864Seric 	char *usernm = NULL;
9632140Seric 	char *subdir = NULL;
9642140Seric 	char *cmdname;
965261Seric 
9661438Seric 	/*
9671822Seric 	**  Process the argv
9681822Seric 	*/
9691822Seric 
9702140Seric 	cmdname = *argv;
9712140Seric 	for (ap = argv; *++ap != NULL; )
9721822Seric 	{
9731864Seric 		if (**ap == '-')
9741864Seric 		{
9751864Seric 			/* we have a flag */
9761864Seric 			switch ((*ap)[1])
9771864Seric 			{
9781864Seric 			  case 'b':
9791864Seric 				nobranch = TRUE;
9801864Seric 				break;
9811864Seric 
9821864Seric 			  case 'u':
9831864Seric 				if ((*ap)[2] != '\0')
9841864Seric 					usernm = &(*ap)[2];
9851864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9861864Seric 					usernm = *++ap;
9871864Seric 				else
9881864Seric 					usernm = username();
9891864Seric 				break;
9901864Seric 			}
9911864Seric 		}
9922140Seric 		else
9932140Seric 		{
9942140Seric 			if (subdir != NULL)
9952140Seric 				usrerr("too many args");
9962140Seric 			else
9972140Seric 				subdir = *ap;
9982140Seric 		}
9991822Seric 	}
10001822Seric 
10011822Seric 	/*
10021438Seric 	**  Find and open the SCCS directory.
10031438Seric 	*/
10041438Seric 
100510110Srrh 	gstrcpy(buf, SccsDir, sizeof(buf));
10061207Seric 	if (buf[0] != '\0')
100710110Srrh 		gstrcat(buf, "/", sizeof(buf));
10082140Seric 	if (subdir != NULL)
10092140Seric 	{
101010110Srrh 		gstrcat(buf, subdir, sizeof(buf));
101110110Srrh 		gstrcat(buf, "/", sizeof(buf));
10122140Seric 	}
101310110Srrh 	gstrcat(buf, SccsPath, sizeof(buf));
10142140Seric 	bufend = &buf[strlen(buf)];
10151438Seric 
101630466Smckusick 	dirp = opendir(buf);
101730466Smckusick 	if (dirp == NULL)
1018261Seric 	{
10191207Seric 		usrerr("cannot open %s", buf);
10201282Seric 		return (EX_NOINPUT);
1021261Seric 	}
1022261Seric 
1023261Seric 	/*
1024261Seric 	**  Scan the SCCS directory looking for s. files.
10251438Seric 	**	gotedit tells whether we have tried to clean any
10261438Seric 	**		files that are being edited.
1027261Seric 	*/
1028261Seric 
1029351Seric 	gotedit = FALSE;
103030466Smckusick 	while (dir = readdir(dirp)) {
10316784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
1032261Seric 			continue;
1033261Seric 
1034261Seric 		/* got an s. file -- see if the p. file exists */
103510110Srrh 		gstrcpy(bufend, "/p.", sizeof(buf));
10362140Seric 		basefile = bufend + 3;
103710110Srrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
10381822Seric 
10391822Seric 		/*
10401822Seric 		**  open and scan the p-file.
10411822Seric 		**	'gotpfent' tells if we have found a valid p-file
10421822Seric 		**		entry.
10431822Seric 		*/
10441822Seric 
1045394Seric 		pfp = fopen(buf, "r");
10461822Seric 		gotpfent = FALSE;
1047394Seric 		if (pfp != NULL)
1048346Seric 		{
10491438Seric 			/* the file exists -- report it's contents */
10501822Seric 			while ((pf = getpfent(pfp)) != NULL)
10511730Seric 			{
10521822Seric 				if (nobranch && isbranch(pf->p_nsid))
10531822Seric 					continue;
10541864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10551864Seric 					continue;
10561822Seric 				gotedit = TRUE;
10571822Seric 				gotpfent = TRUE;
10581822Seric 				if (mode == TELLC)
10591822Seric 				{
10601822Seric 					printf("%s\n", basefile);
10611822Seric 					break;
10621822Seric 				}
10632161Seric 				printf("%12s: being edited: ", basefile);
10642161Seric 				putpfent(pf, stdout);
10651730Seric 			}
1066394Seric 			fclose(pfp);
10671822Seric 		}
1068261Seric 
1069261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10701870Seric 		if (mode == CLEANC && !gotpfent)
1071346Seric 		{
107210110Srrh 			char	unlinkbuf[FBUFSIZ];
107310110Srrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
107410103Srrh 			unlink(unlinkbuf);
1075346Seric 		}
1076261Seric 	}
1077261Seric 
10781438Seric 	/* cleanup & report results */
107930466Smckusick 	closedir(dirp);
1080819Seric 	if (!gotedit && mode == INFOC)
10811864Seric 	{
10821864Seric 		printf("Nothing being edited");
10831864Seric 		if (nobranch)
10841864Seric 			printf(" (on trunk)");
10851864Seric 		if (usernm == NULL)
10861864Seric 			printf("\n");
10871864Seric 		else
10881864Seric 			printf(" by %s\n", usernm);
10891864Seric 	}
1090819Seric 	if (mode == CHECKC)
1091819Seric 		exit(gotedit);
10921282Seric 	return (EX_OK);
1093261Seric }
10941432Seric 
10951432Seric /*
10961822Seric **  ISBRANCH -- is the SID a branch?
10971822Seric **
10981822Seric **	Parameters:
10991822Seric **		sid -- the sid to check.
11001822Seric **
11011822Seric **	Returns:
11021822Seric **		TRUE if the sid represents a branch.
11031822Seric **		FALSE otherwise.
11041822Seric **
11051822Seric **	Side Effects:
11061822Seric **		none.
11071822Seric */
11081822Seric 
11091822Seric isbranch(sid)
11101822Seric 	char *sid;
11111822Seric {
11121822Seric 	register char *p;
11131822Seric 	int dots;
11141822Seric 
11151822Seric 	dots = 0;
11161822Seric 	for (p = sid; *p != '\0'; p++)
11171822Seric 	{
11181822Seric 		if (*p == '.')
11191822Seric 			dots++;
11201822Seric 		if (dots > 1)
11211822Seric 			return (TRUE);
11221822Seric 	}
11231822Seric 	return (FALSE);
11241822Seric }
11251822Seric 
11261822Seric /*
1127396Seric **  UNEDIT -- unedit a file
1128396Seric **
1129396Seric **	Checks to see that the current user is actually editting
1130396Seric **	the file and arranges that s/he is not editting it.
1131396Seric **
1132396Seric **	Parameters:
1133416Seric **		fn -- the name of the file to be unedited.
1134396Seric **
1135396Seric **	Returns:
1136585Seric **		TRUE -- if the file was successfully unedited.
1137585Seric **		FALSE -- if the file was not unedited for some
1138585Seric **			reason.
1139396Seric **
1140396Seric **	Side Effects:
1141396Seric **		fn is removed
1142396Seric **		entries are removed from pfile.
1143396Seric */
1144396Seric 
1145585Seric bool
1146396Seric unedit(fn)
1147396Seric 	char *fn;
1148396Seric {
1149396Seric 	register FILE *pfp;
115012153Ssam 	char *cp, *pfn;
1151396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1152396Seric 	FILE *tfp;
1153396Seric 	register char *q;
1154396Seric 	bool delete = FALSE;
1155396Seric 	bool others = FALSE;
1156396Seric 	char *myname;
11571864Seric 	extern char *username();
1158396Seric 	struct pfile *pent;
11591822Seric 	extern struct pfile *getpfent();
116010110Srrh 	char buf[PFILELG];
1161*33251Sbostic 	extern char *makefile(), *rindex(), *tail();
1162396Seric 
1163396Seric 	/* make "s." filename & find the trailing component */
1164396Seric 	pfn = makefile(fn);
1165586Seric 	if (pfn == NULL)
1166586Seric 		return (FALSE);
1167586Seric 	q = rindex(pfn, '/');
1168586Seric 	if (q == NULL)
1169586Seric 		q = &pfn[-1];
1170586Seric 	if (q[1] != 's' || q[2] != '.')
1171396Seric 	{
11721205Seric 		usrerr("bad file name \"%s\"", fn);
1173585Seric 		return (FALSE);
1174396Seric 	}
1175396Seric 
11761438Seric 	/* turn "s." into "p." & try to open it */
1177396Seric 	*++q = 'p';
1178396Seric 
1179396Seric 	pfp = fopen(pfn, "r");
1180396Seric 	if (pfp == NULL)
1181396Seric 	{
1182416Seric 		printf("%12s: not being edited\n", fn);
1183585Seric 		return (FALSE);
1184396Seric 	}
1185396Seric 
11861438Seric 	/* create temp file for editing p-file */
1187396Seric 	mktemp(tfn);
1188396Seric 	tfp = fopen(tfn, "w");
1189396Seric 	if (tfp == NULL)
1190396Seric 	{
11911205Seric 		usrerr("cannot create \"%s\"", tfn);
1192396Seric 		exit(EX_OSERR);
1193396Seric 	}
1194396Seric 
11951438Seric 	/* figure out who I am */
11961864Seric 	myname = username();
11971438Seric 
11981438Seric 	/*
11991438Seric 	**  Copy p-file to temp file, doing deletions as needed.
12001438Seric 	*/
12011438Seric 
12021822Seric 	while ((pent = getpfent(pfp)) != NULL)
1203396Seric 	{
1204396Seric 		if (strcmp(pent->p_user, myname) == 0)
1205396Seric 		{
1206396Seric 			/* a match */
1207396Seric 			delete++;
1208396Seric 		}
1209396Seric 		else
1210396Seric 		{
12111438Seric 			/* output it again */
12122161Seric 			putpfent(pent, tfp);
1213396Seric 			others++;
1214396Seric 		}
1215396Seric 	}
1216396Seric 
121712153Ssam 	/*
121812153Ssam 	 * Before changing anything, make sure we can remove
121912153Ssam 	 * the file in question (assuming it exists).
122012153Ssam 	 */
122112153Ssam 	if (delete) {
122212153Ssam 		extern int errno;
122312153Ssam 
122412153Ssam 		cp = tail(fn);
122512153Ssam 		errno = 0;
122612153Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
122712153Ssam 			goto bad;
122812153Ssam 		if (errno == 0)
122912153Ssam 			/*
123012153Ssam 			 * This is wrong, but the rest of the program
123112153Ssam 			 * has built in assumptions about "." as well,
123212153Ssam 			 * so why make unedit a special case?
123312153Ssam 			 */
123412153Ssam 			if (access(".", 2) < 0) {
123512153Ssam 	bad:
123612153Ssam 				printf("%12s: can't remove\n", cp);
123712153Ssam 				fclose(tfp);
123812153Ssam 				fclose(pfp);
123912153Ssam 				unlink(tfn);
124012153Ssam 				return (FALSE);
124112153Ssam 			}
124212153Ssam 	}
1243396Seric 	/* do final cleanup */
1244396Seric 	if (others)
1245396Seric 	{
12461438Seric 		/* copy it back (perhaps it should be linked?) */
1247396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1248396Seric 		{
12491205Seric 			syserr("cannot reopen \"%s\"", tfn);
1250396Seric 			exit(EX_OSERR);
1251396Seric 		}
1252396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1253396Seric 		{
12541205Seric 			usrerr("cannot create \"%s\"", pfn);
1255585Seric 			return (FALSE);
1256396Seric 		}
1257396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1258396Seric 			fputs(buf, pfp);
1259396Seric 	}
1260396Seric 	else
1261396Seric 	{
12621438Seric 		/* it's empty -- remove it */
1263396Seric 		unlink(pfn);
1264396Seric 	}
1265396Seric 	fclose(tfp);
1266396Seric 	fclose(pfp);
1267396Seric 	unlink(tfn);
1268396Seric 
12691438Seric 	/* actually remove the g-file */
1270396Seric 	if (delete)
1271396Seric 	{
127212153Ssam 		/*
127312153Ssam 		 * Since we've checked above, we can
127412153Ssam 		 * use the return from unlink to
127512153Ssam 		 * determine if the file existed or not.
127612153Ssam 		 */
127712153Ssam 		if (unlink(cp) >= 0)
127812153Ssam 			printf("%12s: removed\n", cp);
1279585Seric 		return (TRUE);
1280396Seric 	}
1281396Seric 	else
1282396Seric 	{
1283416Seric 		printf("%12s: not being edited by you\n", fn);
1284585Seric 		return (FALSE);
1285396Seric 	}
1286396Seric }
12871432Seric 
12881432Seric /*
12891433Seric **  DODIFF -- diff an s-file against a g-file
12901433Seric **
12911433Seric **	Parameters:
12921433Seric **		getv -- argv for the 'get' command.
12931433Seric **		gfile -- name of the g-file to diff against.
12941433Seric **
12951433Seric **	Returns:
12961433Seric **		Result of get.
12971433Seric **
12981433Seric **	Side Effects:
12991433Seric **		none.
13001433Seric */
13011433Seric 
13021433Seric dodiff(getv, gfile)
13031433Seric 	char **getv;
13041433Seric 	char *gfile;
13051433Seric {
13061433Seric 	int pipev[2];
13071433Seric 	int rval;
13081433Seric 	register int i;
13091433Seric 	register int pid;
13101433Seric 	auto int st;
13111433Seric 	extern int errno;
13121433Seric 	int (*osig)();
13131433Seric 
13141905Seric 	printf("\n------- %s -------\n", gfile);
13151871Seric 	fflush(stdout);
13161501Seric 
13171438Seric 	/* create context for diff to run in */
13181433Seric 	if (pipe(pipev) < 0)
13191433Seric 	{
13201433Seric 		syserr("dodiff: pipe failed");
13211433Seric 		exit(EX_OSERR);
13221433Seric 	}
13231433Seric 	if ((pid = fork()) < 0)
13241433Seric 	{
13251433Seric 		syserr("dodiff: fork failed");
13261433Seric 		exit(EX_OSERR);
13271433Seric 	}
13281433Seric 	else if (pid > 0)
13291433Seric 	{
13301433Seric 		/* in parent; run get */
13311433Seric 		OutFile = pipev[1];
13321433Seric 		close(pipev[0]);
13331871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
13341433Seric 		osig = signal(SIGINT, SIG_IGN);
13351433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
13361433Seric 			errno = 0;
13371433Seric 		signal(SIGINT, osig);
13381433Seric 		/* ignore result of diff */
13391433Seric 	}
13401433Seric 	else
13411433Seric 	{
13421433Seric 		/* in child, run diff */
13431433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
13441433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
13451433Seric 		{
13461433Seric 			syserr("dodiff: magic failed");
13471433Seric 			exit(EX_OSERR);
13481433Seric 		}
13491871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
13501433Seric 	}
13511433Seric 	return (rval);
13521433Seric }
13531433Seric 
13541433Seric /*
13551435Seric **  TAIL -- return tail of filename.
13561435Seric **
13571435Seric **	Parameters:
13581435Seric **		fn -- the filename.
13591435Seric **
13601435Seric **	Returns:
13611435Seric **		a pointer to the tail of the filename; e.g., given
13621435Seric **		"cmd/ls.c", "ls.c" is returned.
13631435Seric **
13641435Seric **	Side Effects:
13651435Seric **		none.
13661435Seric */
13671435Seric 
13681435Seric char *
13691435Seric tail(fn)
13701435Seric 	register char *fn;
13711435Seric {
13721435Seric 	register char *p;
13731435Seric 
13741435Seric 	for (p = fn; *p != 0; p++)
13751435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
13761435Seric 			fn = &p[1];
13771435Seric 	return (fn);
13781435Seric }
13791435Seric 
13801435Seric /*
13811822Seric **  GETPFENT -- get an entry from the p-file
1382396Seric **
1383396Seric **	Parameters:
1384396Seric **		pfp -- p-file file pointer
1385396Seric **
1386396Seric **	Returns:
1387396Seric **		pointer to p-file struct for next entry
1388396Seric **		NULL on EOF or error
1389396Seric **
1390396Seric **	Side Effects:
1391396Seric **		Each call wipes out results of previous call.
1392396Seric */
1393396Seric 
1394396Seric struct pfile *
13951822Seric getpfent(pfp)
1396396Seric 	FILE *pfp;
1397396Seric {
1398396Seric 	static struct pfile ent;
139910110Srrh 	static char buf[PFILELG];
1400396Seric 	register char *p;
1401396Seric 	extern char *nextfield();
1402396Seric 
1403396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1404396Seric 		return (NULL);
1405396Seric 
1406396Seric 	ent.p_osid = p = buf;
1407396Seric 	ent.p_nsid = p = nextfield(p);
1408396Seric 	ent.p_user = p = nextfield(p);
1409396Seric 	ent.p_date = p = nextfield(p);
1410396Seric 	ent.p_time = p = nextfield(p);
14112161Seric 	ent.p_aux = p = nextfield(p);
1412396Seric 
1413396Seric 	return (&ent);
1414396Seric }
1415396Seric 
1416396Seric 
1417396Seric char *
1418396Seric nextfield(p)
1419396Seric 	register char *p;
1420396Seric {
1421396Seric 	if (p == NULL || *p == '\0')
1422396Seric 		return (NULL);
1423396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1424396Seric 		p++;
1425396Seric 	if (*p == '\n' || *p == '\0')
1426396Seric 	{
1427396Seric 		*p = '\0';
1428396Seric 		return (NULL);
1429396Seric 	}
1430396Seric 	*p++ = '\0';
1431396Seric 	return (p);
1432396Seric }
14332161Seric /*
14342161Seric **  PUTPFENT -- output a p-file entry to a file
14352161Seric **
14362161Seric **	Parameters:
14372161Seric **		pf -- the p-file entry
14382161Seric **		f -- the file to put it on.
14392161Seric **
14402161Seric **	Returns:
14412161Seric **		none.
14422161Seric **
14432161Seric **	Side Effects:
14442161Seric **		pf is written onto file f.
14452161Seric */
14462161Seric 
14472161Seric putpfent(pf, f)
14482161Seric 	register struct pfile *pf;
14492161Seric 	register FILE *f;
14502161Seric {
14512161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
14522161Seric 		pf->p_user, pf->p_date, pf->p_time);
14532161Seric 	if (pf->p_aux != NULL)
14542161Seric 		fprintf(f, " %s", pf->p_aux);
14552161Seric 	else
14562161Seric 		fprintf(f, "\n");
14572161Seric }
14581432Seric 
14591432Seric /*
14601205Seric **  USRERR -- issue user-level error
14611205Seric **
14621205Seric **	Parameters:
14631205Seric **		f -- format string.
14641205Seric **		p1-p3 -- parameters to a printf.
14651205Seric **
14661205Seric **	Returns:
14671205Seric **		-1
14681205Seric **
14691205Seric **	Side Effects:
14701205Seric **		none.
14711205Seric */
14721205Seric 
14731738Seric /*VARARGS1*/
14741205Seric usrerr(f, p1, p2, p3)
14751205Seric 	char *f;
14761205Seric {
14771205Seric 	fprintf(stderr, "\n%s: ", MyName);
14781205Seric 	fprintf(stderr, f, p1, p2, p3);
14791205Seric 	fprintf(stderr, "\n");
14801205Seric 
14811205Seric 	return (-1);
14821205Seric }
14831432Seric 
14841432Seric /*
14851205Seric **  SYSERR -- print system-generated error.
14861205Seric **
14871205Seric **	Parameters:
14881205Seric **		f -- format string to a printf.
14891205Seric **		p1, p2, p3 -- parameters to f.
14901205Seric **
14911205Seric **	Returns:
14921205Seric **		never.
14931205Seric **
14941205Seric **	Side Effects:
14951205Seric **		none.
14961205Seric */
14971205Seric 
14981738Seric /*VARARGS1*/
14991205Seric syserr(f, p1, p2, p3)
15001205Seric 	char *f;
15011205Seric {
15021205Seric 	extern int errno;
15031205Seric 
15041205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
15051205Seric 	fprintf(stderr, f, p1, p2, p3);
15061205Seric 	fprintf(stderr, "\n");
15071205Seric 	if (errno == 0)
15081205Seric 		exit(EX_SOFTWARE);
15091205Seric 	else
15101205Seric 	{
15111738Seric 		perror(NULL);
15121205Seric 		exit(EX_OSERR);
15131205Seric 	}
15141205Seric }
15151864Seric /*
15161864Seric **  USERNAME -- return name of the current user
15171864Seric **
15181864Seric **	Parameters:
15191864Seric **		none
15201864Seric **
15211864Seric **	Returns:
15221864Seric **		name of current user
15231864Seric **
15241864Seric **	Side Effects:
15251864Seric **		none
15261864Seric */
15271864Seric 
15281864Seric char *
15291864Seric username()
15301864Seric {
15311864Seric # ifdef UIDUSER
15321864Seric 	extern struct passwd *getpwuid();
15331864Seric 	register struct passwd *pw;
15341864Seric 
15351864Seric 	pw = getpwuid(getuid());
15361864Seric 	if (pw == NULL)
15371864Seric 	{
15381864Seric 		syserr("who are you? (uid=%d)", getuid());
15391864Seric 		exit(EX_OSERR);
15401864Seric 	}
15411864Seric 	return (pw->pw_name);
15421864Seric # else
15431905Seric 	extern char *getlogin();
15446785Smckusick 	register char *p;
15451905Seric 
15466785Smckusick 	p = getenv("USER");
15476785Smckusick 	if (p == NULL || p[0] == '\0')
15486785Smckusick 		p = getlogin();
15496785Smckusick 	return (p);
15501864Seric # endif UIDUSER
15511864Seric }
155210110Srrh 
155310110Srrh /*
155410110Srrh **	Guarded string manipulation routines; the last argument
155510110Srrh **	is the length of the buffer into which the strcpy or strcat
155610110Srrh **	is to be done.
155710110Srrh */
155810110Srrh char *gstrcat(to, from, length)
155910110Srrh 	char	*to, *from;
156010110Srrh 	int	length;
156110110Srrh {
156210110Srrh 	if (strlen(from) + strlen(to) >= length) {
156310110Srrh 		gstrbotch(to, from);
156410110Srrh 	}
156510110Srrh 	return(strcat(to, from));
156610110Srrh }
156710110Srrh 
156810110Srrh char *gstrncat(to, from, n, length)
156910110Srrh 	char	*to, *from;
157010110Srrh 	int	n;
157110110Srrh 	int	length;
157210110Srrh {
157310110Srrh 	if (n + strlen(to) >= length) {
157410110Srrh 		gstrbotch(to, from);
157510110Srrh 	}
157610110Srrh 	return(strncat(to, from, n));
157710110Srrh }
157810110Srrh 
157910110Srrh char *gstrcpy(to, from, length)
158010110Srrh 	char	*to, *from;
158110110Srrh 	int	length;
158210110Srrh {
158310110Srrh 	if (strlen(from) >= length) {
158410110Srrh 		gstrbotch(from, (char *)0);
158510110Srrh 	}
158610110Srrh 	return(strcpy(to, from));
158710110Srrh }
158810110Srrh gstrbotch(str1, str2)
158910110Srrh 	char	*str1, *str2;
159010110Srrh {
159110110Srrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
159210110Srrh }
1593