xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 34911)
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
6*34911Sbostic  * provided that the above copyright notice and this paragraph are
7*34911Sbostic  * duplicated in all such forms and that any documentation,
8*34911Sbostic  * advertising materials, and other materials related to such
9*34911Sbostic  * distribution and use acknowledge that the software was developed
10*34911Sbostic  * by the University of California, Berkeley.  The name of the
11*34911Sbostic  * University may not be used to endorse or promote products derived
12*34911Sbostic  * from this software without specific prior written permission.
13*34911Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*34911Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*34911Sbostic  * 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*34911Sbostic static char sccsid[] = "@(#)sccs.c	5.6 (Berkeley) 06/29/88";
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>
36148Seric 
37828Seric /*
38828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
39828Seric **
40828Seric **	Without trying to add any functionality to speak of, this
41828Seric **	program tries to make SCCS a little more accessible to human
42828Seric **	types.  The main thing it does is automatically put the
43828Seric **	string "SCCS/s." on the front of names.  Also, it has a
44828Seric **	couple of things that are designed to shorten frequent
45828Seric **	combinations, e.g., "delget" which expands to a "delta"
46828Seric **	and a "get".
47828Seric **
48828Seric **	This program can also function as a setuid front end.
49828Seric **	To do this, you should copy the source, renaming it to
50828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
51828Seric **	in the program (e.g., syssccs might default -d to
52828Seric **	"/usr/src/sys").  Then recompile and put the result
53828Seric **	as setuid to whomever you want.  In this mode, sccs
54828Seric **	knows to not run setuid for certain programs in order
55828Seric **	to preserve security, and so forth.
56828Seric **
57828Seric **	Usage:
58828Seric **		sccs [flags] command [args]
59828Seric **
60828Seric **	Flags:
61828Seric **		-d<dir>		<dir> represents a directory to search
62828Seric **				out of.  It should be a full pathname
63828Seric **				for general usage.  E.g., if <dir> is
64828Seric **				"/usr/src/sys", then a reference to the
65828Seric **				file "dev/bio.c" becomes a reference to
66828Seric **				"/usr/src/sys/dev/bio.c".
67828Seric **		-p<path>	prepends <path> to the final component
68828Seric **				of the pathname.  By default, this is
69828Seric **				"SCCS".  For example, in the -d example
70828Seric **				above, the path then gets modified to
71828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
72828Seric **				more common usage (without the -d flag),
73828Seric **				"prog.c" would get modified to
74828Seric **				"SCCS/s.prog.c".  In both cases, the
75828Seric **				"s." gets automatically prepended.
76828Seric **		-r		run as the real user.
77828Seric **
78828Seric **	Commands:
79828Seric **		admin,
80828Seric **		get,
81828Seric **		delta,
82828Seric **		rmdel,
8330959Sbostic **		cdc,
84828Seric **		etc.		Straight out of SCCS; only difference
85828Seric **				is that pathnames get modified as
86828Seric **				described above.
8730959Sbostic **		enter		Front end doing "sccs admin -i<name> <name>"
8830959Sbostic **		create		Macro for "enter" followed by "get".
89828Seric **		edit		Macro for "get -e".
90828Seric **		unedit		Removes a file being edited, knowing
91828Seric **				about p-files, etc.
92828Seric **		delget		Macro for "delta" followed by "get".
93828Seric **		deledit		Macro for "delta" followed by "get -e".
9430959Sbostic **		branch		Macro for "get -b -e", followed by "delta
9530959Sbostic **				-s -n", followd by "get -e -t -g".
9630959Sbostic **		diffs		"diff" the specified version of files
9730959Sbostic **				and the checked-out version.
9830959Sbostic **		print		Macro for "prs -e" followed by "get -p -m".
9930959Sbostic **		tell		List what files are being edited.
10030959Sbostic **		info		Print information about files being edited.
101828Seric **		clean		Remove all files that can be
102828Seric **				regenerated from SCCS files.
1031205Seric **		check		Like info, but return exit status, for
104828Seric **				use in makefiles.
105828Seric **		fix		Remove a top delta & reedit, but save
106828Seric **				the previous changes in that delta.
107828Seric **
108828Seric **	Compilation Flags:
109828Seric **		UIDUSER -- determine who the user is by looking at the
110828Seric **			uid rather than the login name -- for machines
111828Seric **			where SCCS gets the user in this way.
1121270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
1131205Seric **			this value.  This is so that the setuid
1141205Seric **			aspects of this program cannot be abused.
1151270Seric **			This flag also disables the -p flag.
1161270Seric **		SCCSPATH -- the default for the -p flag.
1171437Seric **		MYNAME -- the title this program should print when it
1181437Seric **			gives error messages.
119828Seric **
120828Seric **	Compilation Instructions:
121828Seric **		cc -O -n -s sccs.c
1221437Seric **		The flags listed above can be -D defined to simplify
1231437Seric **			recompilation for variant versions.
124828Seric **
125828Seric **	Author:
126828Seric **		Eric Allman, UCB/INGRES
1271270Seric **		Copyright 1980 Regents of the University of California
128828Seric */
129155Seric 
1301432Seric 
1311270Seric /*******************  Configuration Information  ********************/
1321270Seric 
1331437Seric # ifndef SCCSPATH
1341432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1351437Seric # endif NOT SCCSPATH
136828Seric 
1371437Seric # ifndef MYNAME
1381437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1391437Seric # endif NOT MYNAME
1401270Seric 
1411432Seric # ifndef PROGPATH
1426784Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1431432Seric # endif PROGPATH
1441432Seric 
1451270Seric /****************  End of Configuration Information  ****************/
1461432Seric 
147157Seric typedef char	bool;
148200Seric # define TRUE	1
149200Seric # define FALSE	0
150157Seric 
1511438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1521438Seric 
153148Seric struct sccsprog
154148Seric {
155148Seric 	char	*sccsname;	/* name of SCCS routine */
156200Seric 	short	sccsoper;	/* opcode, see below */
157200Seric 	short	sccsflags;	/* flags, see below */
158148Seric 	char	*sccspath;	/* pathname of binary implementing */
159148Seric };
160148Seric 
161200Seric /* values for sccsoper */
162200Seric # define PROG		0	/* call a program */
163201Seric # define CMACRO		1	/* command substitution macro */
164226Seric # define FIX		2	/* fix a delta */
165261Seric # define CLEAN		3	/* clean out recreatable files */
166396Seric # define UNEDIT		4	/* unedit a file */
1671431Seric # define SHELL		5	/* call a shell file (like PROG) */
1681433Seric # define DIFFS		6	/* diff between sccs & file out */
1691871Seric # define DODIFF		7	/* internal call to diff program */
17010104Srrh # define ENTER		8	/* enter new files */
171200Seric 
172157Seric /* bits for sccsflags */
173200Seric # define NO_SDOT	0001	/* no s. on front of args */
174200Seric # define REALUSER	0002	/* protected (e.g., admin) */
175148Seric 
176819Seric /* modes for the "clean", "info", "check" ops */
177819Seric # define CLEANC		0	/* clean command */
178819Seric # define INFOC		1	/* info command */
179819Seric # define CHECKC		2	/* check command */
1801730Seric # define TELLC		3	/* give list of files being edited */
181819Seric 
1821432Seric /*
1831432Seric **  Description of commands known to this program.
1841432Seric **	First argument puts the command into a class.  Second arg is
1851432Seric **	info regarding treatment of this command.  Third arg is a
1861432Seric **	list of flags this command accepts from macros, etc.  Fourth
1871432Seric **	arg is the pathname of the implementing program, or the
1881432Seric **	macro definition, or the arg to a sub-algorithm.
1891432Seric */
190202Seric 
191148Seric struct sccsprog SccsProg[] =
192148Seric {
1931864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
19430959Sbostic 	"cdc",		PROG,	0,			PROGPATH(rmdel),
1951864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1961864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1971864Seric 	"get",		PROG,	0,			PROGPATH(get),
1981864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1992136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
2001864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
2011864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
2022136Seric 	"val",		PROG,	0,			PROGPATH(val),
2031864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
2041864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
2051864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
2061864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
2072138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
2081864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
2091864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
2101864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
2111864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
2121864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
2131864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
2141864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
2151871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
21630959Sbostic 	"print",	CMACRO,	0,			"prs -e/get -p -m -s",
2172226Seric 	"branch",	CMACRO,	NO_SDOT,
2182226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
21910104Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
22010104Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
2211864Seric 	NULL,		-1,	0,			NULL
222148Seric };
223148Seric 
2241432Seric /* one line from a p-file */
225396Seric struct pfile
226396Seric {
227396Seric 	char	*p_osid;	/* old SID */
228396Seric 	char	*p_nsid;	/* new SID */
229396Seric 	char	*p_user;	/* user who did edit */
230396Seric 	char	*p_date;	/* date of get */
231396Seric 	char	*p_time;	/* time of get */
2322161Seric 	char	*p_aux;		/* extra info at end */
233396Seric };
234396Seric 
2351270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2361270Seric # ifdef SCCSDIR
2371270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2381205Seric # else
2391270Seric char	*SccsDir = "";
2401205Seric # endif
2411437Seric char	MyName[] = MYNAME;	/* name used in messages */
2421433Seric int	OutFile = -1;		/* override output file for commands */
243157Seric bool	RealUser;		/* if set, running as real user */
244393Seric # ifdef DEBUG
245393Seric bool	Debug;			/* turn on tracing */
246393Seric # endif
2472139Seric # ifndef V6
2482139Seric extern char	*getenv();
2492139Seric # endif V6
25010110Srrh 
25130959Sbostic extern char	*sys_siglist[];
25230959Sbostic 
25310110Srrh char *gstrcat(), *strcat();
25410110Srrh char *gstrncat(), *strncat();
25510110Srrh char *gstrcpy(), *strcpy();
25610110Srrh #define	FBUFSIZ	BUFSIZ
25710110Srrh #define	PFILELG	120
2581432Seric 
259148Seric main(argc, argv)
260148Seric 	int argc;
261148Seric 	char **argv;
262148Seric {
263148Seric 	register char *p;
264262Seric 	extern struct sccsprog *lookup();
2651282Seric 	register int i;
2662139Seric # ifndef V6
2672139Seric # ifndef SCCSDIR
2682140Seric 	register struct passwd *pw;
2692140Seric 	extern struct passwd *getpwnam();
27010110Srrh 	char buf[FBUFSIZ];
2712140Seric 
2722139Seric 	/* pull "SccsDir" out of the environment (possibly) */
27310260Seric 	p = getenv("PROJECTDIR");
2742140Seric 	if (p != NULL && p[0] != '\0')
2752140Seric 	{
2762140Seric 		if (p[0] == '/')
2772140Seric 			SccsDir = p;
2782140Seric 		else
2792140Seric 		{
2802140Seric 			pw = getpwnam(p);
2812140Seric 			if (pw == NULL)
2822140Seric 			{
2832140Seric 				usrerr("user %s does not exist", p);
2842140Seric 				exit(EX_USAGE);
2852140Seric 			}
28610110Srrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
28710110Srrh 			gstrcat(buf, "/src", sizeof(buf));
2882140Seric 			if (access(buf, 0) < 0)
2892140Seric 			{
29010110Srrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
29110110Srrh 				gstrcat(buf, "/source", sizeof(buf));
2922140Seric 				if (access(buf, 0) < 0)
2932140Seric 				{
2942140Seric 					usrerr("project %s has no source!", p);
2952140Seric 					exit(EX_USAGE);
2962140Seric 				}
2972140Seric 			}
2982140Seric 			SccsDir = buf;
2992140Seric 		}
3002140Seric 	}
3012139Seric # endif SCCSDIR
3022139Seric # endif V6
3032139Seric 
304148Seric 	/*
305148Seric 	**  Detect and decode flags intended for this program.
306148Seric 	*/
307148Seric 
308200Seric 	if (argc < 2)
309148Seric 	{
3101205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
311200Seric 		exit(EX_USAGE);
312200Seric 	}
313200Seric 	argv[argc] = NULL;
314200Seric 
315262Seric 	if (lookup(argv[0]) == NULL)
316200Seric 	{
317262Seric 		while ((p = *++argv) != NULL)
318148Seric 		{
319262Seric 			if (*p != '-')
320262Seric 				break;
321262Seric 			switch (*++p)
322262Seric 			{
323262Seric 			  case 'r':		/* run as real user */
324262Seric 				setuid(getuid());
325262Seric 				RealUser++;
326262Seric 				break;
327148Seric 
3281270Seric # ifndef SCCSDIR
329262Seric 			  case 'p':		/* path of sccs files */
330262Seric 				SccsPath = ++p;
3312348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3322348Seric 					SccsPath = *++argv;
333262Seric 				break;
334148Seric 
335588Seric 			  case 'd':		/* directory to search from */
336588Seric 				SccsDir = ++p;
3372348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3382348Seric 					SccsDir = *++argv;
339588Seric 				break;
3401205Seric # endif
341588Seric 
342393Seric # ifdef DEBUG
343393Seric 			  case 'T':		/* trace */
344393Seric 				Debug++;
345393Seric 				break;
346393Seric # endif
347393Seric 
348262Seric 			  default:
3491205Seric 				usrerr("unknown option -%s", p);
350262Seric 				break;
351262Seric 			}
352148Seric 		}
353262Seric 		if (SccsPath[0] == '\0')
354262Seric 			SccsPath = ".";
355148Seric 	}
356148Seric 
3571737Seric 	i = command(argv, FALSE, "");
3581282Seric 	exit(i);
359200Seric }
3601432Seric 
3611432Seric /*
3621282Seric **  COMMAND -- look up and perform a command
3631282Seric **
3641282Seric **	This routine is the guts of this program.  Given an
3651282Seric **	argument vector, it looks up the "command" (argv[0])
3661282Seric **	in the configuration table and does the necessary stuff.
3671282Seric **
3681282Seric **	Parameters:
3691282Seric **		argv -- an argument vector to process.
3701282Seric **		forkflag -- if set, fork before executing the command.
3711316Seric **		editflag -- if set, only include flags listed in the
3721316Seric **			sccsklets field of the command descriptor.
3731316Seric **		arg0 -- a space-seperated list of arguments to insert
3741316Seric **			before argv.
3751282Seric **
3761282Seric **	Returns:
3771282Seric **		zero -- command executed ok.
3781282Seric **		else -- error status.
3791282Seric **
3801282Seric **	Side Effects:
3811282Seric **		none.
3821282Seric */
383157Seric 
3841737Seric command(argv, forkflag, arg0)
385200Seric 	char **argv;
386201Seric 	bool forkflag;
3871316Seric 	char *arg0;
388200Seric {
389200Seric 	register struct sccsprog *cmd;
390200Seric 	register char *p;
39110110Srrh 	char buf[FBUFSIZ];
392262Seric 	extern struct sccsprog *lookup();
3931316Seric 	char *nav[1000];
3941316Seric 	char **np;
3951431Seric 	register char **ap;
396585Seric 	register int i;
3971431Seric 	register char *q;
398585Seric 	extern bool unedit();
3991282Seric 	int rval = 0;
4001316Seric 	extern char *index();
4011316Seric 	extern char *makefile();
4021737Seric 	char *editchs;
4031435Seric 	extern char *tail();
404200Seric 
405393Seric # ifdef DEBUG
406393Seric 	if (Debug)
407393Seric 	{
4081316Seric 		printf("command:\n\t\"%s\"\n", arg0);
4091316Seric 		for (np = argv; *np != NULL; np++)
4101316Seric 			printf("\t\"%s\"\n", *np);
411393Seric 	}
412393Seric # endif
413393Seric 
414157Seric 	/*
4151316Seric 	**  Copy arguments.
4161438Seric 	**	Copy from arg0 & if necessary at most one arg
4171438Seric 	**	from argv[0].
4181316Seric 	*/
4191316Seric 
4201431Seric 	np = ap = &nav[1];
4211737Seric 	editchs = NULL;
4221821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
4231316Seric 	{
4241316Seric 		*np++ = q;
4251316Seric 		while (*p == ' ')
4261316Seric 			p++;
4271737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
4281316Seric 			*q++ = *p++;
4291316Seric 		*q++ = '\0';
4301737Seric 		if (*p == ':')
4311737Seric 		{
4321737Seric 			editchs = q;
4331821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4341737Seric 				*q++ = *p;
4351737Seric 			*q++ = '\0';
4361737Seric 		}
4371316Seric 	}
4381316Seric 	*np = NULL;
4391431Seric 	if (*ap == NULL)
4401316Seric 		*np++ = *argv++;
4411316Seric 
4421316Seric 	/*
443148Seric 	**  Look up command.
4441431Seric 	**	At this point, *ap is the command name.
445148Seric 	*/
446148Seric 
4471431Seric 	cmd = lookup(*ap);
448262Seric 	if (cmd == NULL)
449148Seric 	{
4501431Seric 		usrerr("Unknown command \"%s\"", *ap);
4511282Seric 		return (EX_USAGE);
452148Seric 	}
453148Seric 
454148Seric 	/*
4551316Seric 	**  Copy remaining arguments doing editing as appropriate.
4561316Seric 	*/
4571316Seric 
4581316Seric 	for (; *argv != NULL; argv++)
4591316Seric 	{
4601316Seric 		p = *argv;
4611316Seric 		if (*p == '-')
4621316Seric 		{
4631737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4641316Seric 				*np++ = p;
4651316Seric 		}
4661316Seric 		else
4671316Seric 		{
4681316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4691316Seric 				p = makefile(p);
4701316Seric 			if (p != NULL)
4711316Seric 				*np++ = p;
4721316Seric 		}
4731316Seric 	}
4741316Seric 	*np = NULL;
4751316Seric 
4761316Seric 	/*
477200Seric 	**  Interpret operation associated with this command.
478157Seric 	*/
479157Seric 
480200Seric 	switch (cmd->sccsoper)
481200Seric 	{
4821431Seric 	  case SHELL:		/* call a shell file */
4831431Seric 		*ap = cmd->sccspath;
4841431Seric 		*--ap = "sh";
4851431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4861431Seric 		break;
4871431Seric 
488200Seric 	  case PROG:		/* call an sccs prog */
4891431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
490201Seric 		break;
491201Seric 
492201Seric 	  case CMACRO:		/* command macro */
4931438Seric 		/* step through & execute each part of the macro */
494201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
495201Seric 		{
4961316Seric 			q = p;
4971316Seric 			while (*p != '\0' && *p != '/')
4981316Seric 				p++;
4991737Seric 			rval = command(&ap[1], *p != '\0', q);
5001282Seric 			if (rval != 0)
5011282Seric 				break;
502201Seric 		}
5031282Seric 		break;
504157Seric 
505226Seric 	  case FIX:		/* fix a delta */
50630959Sbostic 		if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
507226Seric 		{
5081205Seric 			usrerr("-r flag needed for fix command");
5091282Seric 			rval = EX_USAGE;
510226Seric 			break;
511226Seric 		}
5121438Seric 
5131438Seric 		/* get the version with all changes */
5141737Seric 		rval = command(&ap[1], TRUE, "get -k");
5151438Seric 
5161438Seric 		/* now remove that version from the s-file */
5171282Seric 		if (rval == 0)
5181737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
5191438Seric 
5201438Seric 		/* and edit the old version (but don't clobber new vers) */
5211282Seric 		if (rval == 0)
5221737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
5231282Seric 		break;
524226Seric 
525261Seric 	  case CLEAN:
5261822Seric 		rval = clean((int) cmd->sccspath, ap);
527261Seric 		break;
528261Seric 
529396Seric 	  case UNEDIT:
5301431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
531585Seric 		{
5321316Seric 			if (unedit(*argv))
5331316Seric 				*np++ = *argv;
534585Seric 		}
5351316Seric 		*np = NULL;
5361438Seric 
5371438Seric 		/* get all the files that we unedited successfully */
5381738Seric 		if (np > &ap[1])
5391737Seric 			rval = command(&ap[1], FALSE, "get");
540396Seric 		break;
541396Seric 
5421433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5431433Seric 		/* find the end of the flag arguments */
5441433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5451433Seric 			continue;
5461433Seric 		argv = np;
5471433Seric 
5481433Seric 		/* for each file, do the diff */
5491502Seric 		p = argv[1];
5501433Seric 		while (*np != NULL)
5511433Seric 		{
5521438Seric 			/* messy, but we need a null terminated argv */
5531433Seric 			*argv = *np++;
5541502Seric 			argv[1] = NULL;
5551435Seric 			i = dodiff(ap, tail(*argv));
5561433Seric 			if (rval == 0)
5571433Seric 				rval = i;
5581502Seric 			argv[1] = p;
5591433Seric 		}
5601433Seric 		break;
5611433Seric 
5621871Seric 	  case DODIFF:		/* internal diff call */
5631871Seric 		setuid(getuid());
5641871Seric 		for (np = ap; *np != NULL; np++)
5651871Seric 		{
5661871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5671871Seric 				(*np)[1] = 'c';
5681871Seric 		}
5691871Seric 
5701871Seric 		/* insert "-" argument */
5711871Seric 		np[1] = NULL;
5721871Seric 		np[0] = np[-1];
5731871Seric 		np[-1] = "-";
5741871Seric 
5751871Seric 		/* execute the diff program of choice */
5761871Seric # ifndef V6
5771871Seric 		execvp("diff", ap);
5781871Seric # endif
5791871Seric 		execv(cmd->sccspath, argv);
5801871Seric 		syserr("cannot exec %s", cmd->sccspath);
5811871Seric 		exit(EX_OSERR);
5821871Seric 
58310104Srrh 	  case ENTER:		/* enter new sccs files */
5846785Smckusick 		/* skip over flag arguments */
5856785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5866785Smckusick 			continue;
5876785Smckusick 		argv = np;
5886785Smckusick 
5896785Smckusick 		/* do an admin for each file */
5906785Smckusick 		p = argv[1];
5916785Smckusick 		while (*np != NULL)
5926785Smckusick 		{
5936785Smckusick 			printf("\n%s:\n", *np);
59410110Srrh 			strcpy(buf, "-i");
59510110Srrh 			gstrcat(buf, *np, sizeof(buf));
5966785Smckusick 			ap[0] = buf;
5976785Smckusick 			argv[0] = tail(*np);
5986785Smckusick 			argv[1] = NULL;
5996785Smckusick 			rval = command(ap, TRUE, "admin");
6006785Smckusick 			argv[1] = p;
6016785Smckusick 			if (rval == 0)
6026785Smckusick 			{
60310110Srrh 				strcpy(buf, ",");
60410110Srrh 				gstrcat(buf, tail(*np), sizeof(buf));
6056785Smckusick 				if (link(*np, buf) >= 0)
6066785Smckusick 					unlink(*np);
6076785Smckusick 			}
6086785Smckusick 			np++;
6096785Smckusick 		}
6106785Smckusick 		break;
6116785Smckusick 
612200Seric 	  default:
6131205Seric 		syserr("oper %d", cmd->sccsoper);
614200Seric 		exit(EX_SOFTWARE);
615200Seric 	}
6161282Seric # ifdef DEBUG
6171282Seric 	if (Debug)
6181282Seric 		printf("command: rval=%d\n", rval);
6191282Seric # endif
6201282Seric 	return (rval);
621200Seric }
6221432Seric 
6231432Seric /*
624262Seric **  LOOKUP -- look up an SCCS command name.
625262Seric **
626262Seric **	Parameters:
627262Seric **		name -- the name of the command to look up.
628262Seric **
629262Seric **	Returns:
630262Seric **		ptr to command descriptor for this command.
631262Seric **		NULL if no such entry.
632262Seric **
633262Seric **	Side Effects:
634262Seric **		none.
635262Seric */
636200Seric 
637262Seric struct sccsprog *
638262Seric lookup(name)
639262Seric 	char *name;
640262Seric {
641262Seric 	register struct sccsprog *cmd;
642226Seric 
643262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
644262Seric 	{
645262Seric 		if (strcmp(cmd->sccsname, name) == 0)
646262Seric 			return (cmd);
647262Seric 	}
648262Seric 	return (NULL);
649262Seric }
6501432Seric 
6511432Seric /*
6521282Seric **  CALLPROG -- call a program
6531282Seric **
6541316Seric **	Used to call the SCCS programs.
6551282Seric **
6561282Seric **	Parameters:
6571282Seric **		progpath -- pathname of the program to call.
6581282Seric **		flags -- status flags from the command descriptors.
6591282Seric **		argv -- an argument vector to pass to the program.
6601282Seric **		forkflag -- if true, fork before calling, else just
6611282Seric **			exec.
6621282Seric **
6631282Seric **	Returns:
6641282Seric **		The exit status of the program.
6651282Seric **		Nothing if forkflag == FALSE.
6661282Seric **
6671282Seric **	Side Effects:
6681282Seric **		Can exit if forkflag == FALSE.
6691282Seric */
670226Seric 
671200Seric callprog(progpath, flags, argv, forkflag)
672200Seric 	char *progpath;
673200Seric 	short flags;
674200Seric 	char **argv;
675200Seric 	bool forkflag;
676200Seric {
677200Seric 	register int i;
67830959Sbostic 	register int wpid;
679201Seric 	auto int st;
68030959Sbostic 	register int sigcode;
68130959Sbostic 	register int coredumped;
68230959Sbostic 	register char *sigmsg;
68330959Sbostic 	auto char sigmsgbuf[10+1];	/* "Signal 127" + terminating '\0' */
684200Seric 
6851316Seric # ifdef DEBUG
6861316Seric 	if (Debug)
6871316Seric 	{
6881316Seric 		printf("callprog:\n");
6891316Seric 		for (i = 0; argv[i] != NULL; i++)
6901316Seric 			printf("\t\"%s\"\n", argv[i]);
6911316Seric 	}
6921316Seric # endif
6931316Seric 
694200Seric 	if (*argv == NULL)
695200Seric 		return (-1);
696200Seric 
697157Seric 	/*
698226Seric 	**  Fork if appropriate.
699148Seric 	*/
700148Seric 
701200Seric 	if (forkflag)
702200Seric 	{
703393Seric # ifdef DEBUG
704393Seric 		if (Debug)
705393Seric 			printf("Forking\n");
706393Seric # endif
707200Seric 		i = fork();
708200Seric 		if (i < 0)
709200Seric 		{
7101205Seric 			syserr("cannot fork");
711200Seric 			exit(EX_OSERR);
712200Seric 		}
713200Seric 		else if (i > 0)
714201Seric 		{
71530959Sbostic 			while ((wpid = wait(&st)) != -1 && wpid != i)
71630959Sbostic 				;
71730959Sbostic 			if ((sigcode = st & 0377) == 0)
7181282Seric 				st = (st >> 8) & 0377;
71930959Sbostic 			else
72030959Sbostic 			{
72130959Sbostic 				coredumped = sigcode & 0200;
72230959Sbostic 				sigcode &= 0177;
72330959Sbostic 				if (sigcode != SIGINT && sigcode != SIGPIPE)
72430959Sbostic 				{
72530959Sbostic 					if (sigcode < NSIG)
72630959Sbostic 						sigmsg = sys_siglist[sigcode];
72730959Sbostic 					else
72830959Sbostic 					{
72930959Sbostic 						sprintf(sigmsgbuf, "Signal %d",
73030959Sbostic 						    sigcode);
73130959Sbostic 						sigmsg = sigmsgbuf;
73230959Sbostic 					}
73330959Sbostic 					fprintf(stderr, "sccs: %s: %s%s", argv[0],
73430959Sbostic 					    sigmsg,
73530959Sbostic 					    coredumped ? " - core dumped": "");
73630959Sbostic 				}
73730959Sbostic 				st = EX_SOFTWARE;
73830959Sbostic 			}
7391433Seric 			if (OutFile >= 0)
7401433Seric 			{
7411433Seric 				close(OutFile);
7421433Seric 				OutFile = -1;
7431433Seric 			}
744201Seric 			return (st);
745201Seric 		}
746200Seric 	}
7471433Seric 	else if (OutFile >= 0)
7481433Seric 	{
7491433Seric 		syserr("callprog: setting stdout w/o forking");
7501433Seric 		exit(EX_SOFTWARE);
7511433Seric 	}
752200Seric 
7531433Seric 	/* set protection as appropriate */
754200Seric 	if (bitset(REALUSER, flags))
755200Seric 		setuid(getuid());
7561433Seric 
7571433Seric 	/* change standard input & output if needed */
7581433Seric 	if (OutFile >= 0)
7591433Seric 	{
7601433Seric 		close(1);
7611433Seric 		dup(OutFile);
7621433Seric 		close(OutFile);
7631433Seric 	}
764226Seric 
7651433Seric 	/* call real SCCS program */
766226Seric 	execv(progpath, argv);
7671205Seric 	syserr("cannot execute %s", progpath);
768148Seric 	exit(EX_UNAVAILABLE);
7691738Seric 	/*NOTREACHED*/
770148Seric }
7711432Seric 
7721432Seric /*
773586Seric **  MAKEFILE -- make filename of SCCS file
774586Seric **
775586Seric **	If the name passed is already the name of an SCCS file,
776586Seric **	just return it.  Otherwise, munge the name into the name
777586Seric **	of the actual SCCS file.
778586Seric **
779586Seric **	There are cases when it is not clear what you want to
780586Seric **	do.  For example, if SccsPath is an absolute pathname
781586Seric **	and the name given is also an absolute pathname, we go
782586Seric **	for SccsPath (& only use the last component of the name
783586Seric **	passed) -- this is important for security reasons (if
784586Seric **	sccs is being used as a setuid front end), but not
785586Seric **	particularly intuitive.
786586Seric **
787586Seric **	Parameters:
788586Seric **		name -- the file name to be munged.
789586Seric **
790586Seric **	Returns:
791586Seric **		The pathname of the sccs file.
792586Seric **		NULL on error.
793586Seric **
794586Seric **	Side Effects:
795586Seric **		none.
796586Seric */
797148Seric 
798148Seric char *
799148Seric makefile(name)
800148Seric 	char *name;
801148Seric {
802148Seric 	register char *p;
80310110Srrh 	char buf[3*FBUFSIZ];
804148Seric 	extern char *malloc();
805586Seric 	extern char *rindex();
806588Seric 	extern bool safepath();
807587Seric 	extern bool isdir();
808587Seric 	register char *q;
809148Seric 
810586Seric 	p = rindex(name, '/');
811586Seric 	if (p == NULL)
812586Seric 		p = name;
813586Seric 	else
814586Seric 		p++;
815586Seric 
816148Seric 	/*
817588Seric 	**  Check to see that the path is "safe", i.e., that we
818588Seric 	**  are not letting some nasty person use the setuid part
819588Seric 	**  of this program to look at or munge some presumably
820588Seric 	**  hidden files.
821148Seric 	*/
822148Seric 
823588Seric 	if (SccsDir[0] == '/' && !safepath(name))
824588Seric 		return (NULL);
825586Seric 
826586Seric 	/*
827588Seric 	**  Create the base pathname.
828586Seric 	*/
829586Seric 
8301438Seric 	/* first the directory part */
831588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
832148Seric 	{
83310110Srrh 		gstrcpy(buf, SccsDir, sizeof(buf));
83410110Srrh 		gstrcat(buf, "/", sizeof(buf));
835586Seric 	}
836586Seric 	else
83710110Srrh 		gstrcpy(buf, "", sizeof(buf));
8381438Seric 
8391438Seric 	/* then the head of the pathname */
84010110Srrh 	gstrncat(buf, name, p - name, sizeof(buf));
841587Seric 	q = &buf[strlen(buf)];
8421438Seric 
8431438Seric 	/* now copy the final part of the name, in case useful */
84410110Srrh 	gstrcpy(q, p, sizeof(buf));
8451438Seric 
8461438Seric 	/* so is it useful? */
847587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
848586Seric 	{
8491438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
85010110Srrh 		gstrcpy(q, SccsPath, sizeof(buf));
85110110Srrh 		gstrcat(buf, "/s.", sizeof(buf));
8521438Seric 
8531438Seric 		/* and now the end of the name */
85410110Srrh 		gstrcat(buf, p, sizeof(buf));
855586Seric 	}
856148Seric 
8571438Seric 	/* if i haven't changed it, why did I do all this? */
858588Seric 	if (strcmp(buf, name) == 0)
859588Seric 		p = name;
860588Seric 	else
861148Seric 	{
8621438Seric 		/* but if I have, squirrel it away */
863588Seric 		p = malloc(strlen(buf) + 1);
864588Seric 		if (p == NULL)
865588Seric 		{
866588Seric 			perror("Sccs: no mem");
867588Seric 			exit(EX_OSERR);
868588Seric 		}
869588Seric 		strcpy(p, buf);
870148Seric 	}
8711438Seric 
872148Seric 	return (p);
873148Seric }
8741432Seric 
8751432Seric /*
876587Seric **  ISDIR -- return true if the argument is a directory.
877587Seric **
878587Seric **	Parameters:
879587Seric **		name -- the pathname of the file to check.
880587Seric **
881587Seric **	Returns:
882587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
883587Seric **
884587Seric **	Side Effects:
885587Seric **		none.
886587Seric */
887587Seric 
888587Seric bool
889587Seric isdir(name)
890587Seric 	char *name;
891587Seric {
892587Seric 	struct stat stbuf;
893587Seric 
894587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
895587Seric }
8961432Seric 
8971432Seric /*
898586Seric **  SAFEPATH -- determine whether a pathname is "safe"
899586Seric **
900586Seric **	"Safe" pathnames only allow you to get deeper into the
901586Seric **	directory structure, i.e., full pathnames and ".." are
902586Seric **	not allowed.
903586Seric **
904586Seric **	Parameters:
905586Seric **		p -- the name to check.
906586Seric **
907586Seric **	Returns:
908586Seric **		TRUE -- if the path is safe.
909586Seric **		FALSE -- if the path is not safe.
910586Seric **
911586Seric **	Side Effects:
912586Seric **		Prints a message if the path is not safe.
913586Seric */
914586Seric 
915586Seric bool
916586Seric safepath(p)
917586Seric 	register char *p;
918586Seric {
919586Seric 	extern char *index();
920586Seric 
921586Seric 	if (*p != '/')
922586Seric 	{
923586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
924586Seric 		{
925586Seric 			p = index(p, '/');
926586Seric 			if (p == NULL)
927586Seric 				return (TRUE);
928586Seric 			p++;
929586Seric 		}
930586Seric 	}
931586Seric 
932586Seric 	printf("You may not use full pathnames or \"..\"\n");
933586Seric 	return (FALSE);
934586Seric }
9351432Seric 
9361432Seric /*
937261Seric **  CLEAN -- clean out recreatable files
938261Seric **
939261Seric **	Any file for which an "s." file exists but no "p." file
940261Seric **	exists in the current directory is purged.
941261Seric **
942261Seric **	Parameters:
9431822Seric **		mode -- tells whether this came from a "clean", "info", or
9441822Seric **			"check" command.
9451822Seric **		argv -- the rest of the argument vector.
946261Seric **
947261Seric **	Returns:
948261Seric **		none.
949261Seric **
950261Seric **	Side Effects:
951819Seric **		Removes files in the current directory.
952819Seric **		Prints information regarding files being edited.
953819Seric **		Exits if a "check" command.
954261Seric */
955261Seric 
9561822Seric clean(mode, argv)
957819Seric 	int mode;
9581822Seric 	char **argv;
959261Seric {
9606784Smckusick 	struct direct *dir;
96110110Srrh 	char buf[FBUFSIZ];
9622140Seric 	char *bufend;
96330466Smckusick 	register DIR *dirp;
964346Seric 	register char *basefile;
965351Seric 	bool gotedit;
9661822Seric 	bool gotpfent;
967394Seric 	FILE *pfp;
9681822Seric 	bool nobranch = FALSE;
9691822Seric 	extern struct pfile *getpfent();
9701822Seric 	register struct pfile *pf;
9711822Seric 	register char **ap;
9721864Seric 	extern char *username();
9731864Seric 	char *usernm = NULL;
9742140Seric 	char *subdir = NULL;
9752140Seric 	char *cmdname;
976261Seric 
9771438Seric 	/*
9781822Seric 	**  Process the argv
9791822Seric 	*/
9801822Seric 
9812140Seric 	cmdname = *argv;
9822140Seric 	for (ap = argv; *++ap != NULL; )
9831822Seric 	{
9841864Seric 		if (**ap == '-')
9851864Seric 		{
9861864Seric 			/* we have a flag */
9871864Seric 			switch ((*ap)[1])
9881864Seric 			{
9891864Seric 			  case 'b':
9901864Seric 				nobranch = TRUE;
9911864Seric 				break;
9921864Seric 
9931864Seric 			  case 'u':
9941864Seric 				if ((*ap)[2] != '\0')
9951864Seric 					usernm = &(*ap)[2];
9961864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9971864Seric 					usernm = *++ap;
9981864Seric 				else
9991864Seric 					usernm = username();
10001864Seric 				break;
10011864Seric 			}
10021864Seric 		}
10032140Seric 		else
10042140Seric 		{
10052140Seric 			if (subdir != NULL)
10062140Seric 				usrerr("too many args");
10072140Seric 			else
10082140Seric 				subdir = *ap;
10092140Seric 		}
10101822Seric 	}
10111822Seric 
10121822Seric 	/*
10131438Seric 	**  Find and open the SCCS directory.
10141438Seric 	*/
10151438Seric 
101610110Srrh 	gstrcpy(buf, SccsDir, sizeof(buf));
10171207Seric 	if (buf[0] != '\0')
101810110Srrh 		gstrcat(buf, "/", sizeof(buf));
10192140Seric 	if (subdir != NULL)
10202140Seric 	{
102110110Srrh 		gstrcat(buf, subdir, sizeof(buf));
102210110Srrh 		gstrcat(buf, "/", sizeof(buf));
10232140Seric 	}
102410110Srrh 	gstrcat(buf, SccsPath, sizeof(buf));
10252140Seric 	bufend = &buf[strlen(buf)];
10261438Seric 
102730466Smckusick 	dirp = opendir(buf);
102830466Smckusick 	if (dirp == NULL)
1029261Seric 	{
10301207Seric 		usrerr("cannot open %s", buf);
10311282Seric 		return (EX_NOINPUT);
1032261Seric 	}
1033261Seric 
1034261Seric 	/*
1035261Seric 	**  Scan the SCCS directory looking for s. files.
10361438Seric 	**	gotedit tells whether we have tried to clean any
10371438Seric 	**		files that are being edited.
1038261Seric 	*/
1039261Seric 
1040351Seric 	gotedit = FALSE;
104130466Smckusick 	while (dir = readdir(dirp)) {
10426784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
1043261Seric 			continue;
1044261Seric 
1045261Seric 		/* got an s. file -- see if the p. file exists */
104610110Srrh 		gstrcpy(bufend, "/p.", sizeof(buf));
10472140Seric 		basefile = bufend + 3;
104810110Srrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
10491822Seric 
10501822Seric 		/*
10511822Seric 		**  open and scan the p-file.
10521822Seric 		**	'gotpfent' tells if we have found a valid p-file
10531822Seric 		**		entry.
10541822Seric 		*/
10551822Seric 
1056394Seric 		pfp = fopen(buf, "r");
10571822Seric 		gotpfent = FALSE;
1058394Seric 		if (pfp != NULL)
1059346Seric 		{
10601438Seric 			/* the file exists -- report it's contents */
10611822Seric 			while ((pf = getpfent(pfp)) != NULL)
10621730Seric 			{
10631822Seric 				if (nobranch && isbranch(pf->p_nsid))
10641822Seric 					continue;
10651864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10661864Seric 					continue;
10671822Seric 				gotedit = TRUE;
10681822Seric 				gotpfent = TRUE;
10691822Seric 				if (mode == TELLC)
10701822Seric 				{
10711822Seric 					printf("%s\n", basefile);
10721822Seric 					break;
10731822Seric 				}
10742161Seric 				printf("%12s: being edited: ", basefile);
10752161Seric 				putpfent(pf, stdout);
10761730Seric 			}
1077394Seric 			fclose(pfp);
10781822Seric 		}
1079261Seric 
1080261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10811870Seric 		if (mode == CLEANC && !gotpfent)
1082346Seric 		{
108310110Srrh 			char	unlinkbuf[FBUFSIZ];
108410110Srrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
108510103Srrh 			unlink(unlinkbuf);
1086346Seric 		}
1087261Seric 	}
1088261Seric 
10891438Seric 	/* cleanup & report results */
109030466Smckusick 	closedir(dirp);
1091819Seric 	if (!gotedit && mode == INFOC)
10921864Seric 	{
10931864Seric 		printf("Nothing being edited");
10941864Seric 		if (nobranch)
10951864Seric 			printf(" (on trunk)");
10961864Seric 		if (usernm == NULL)
10971864Seric 			printf("\n");
10981864Seric 		else
10991864Seric 			printf(" by %s\n", usernm);
11001864Seric 	}
1101819Seric 	if (mode == CHECKC)
1102819Seric 		exit(gotedit);
11031282Seric 	return (EX_OK);
1104261Seric }
11051432Seric 
11061432Seric /*
11071822Seric **  ISBRANCH -- is the SID a branch?
11081822Seric **
11091822Seric **	Parameters:
11101822Seric **		sid -- the sid to check.
11111822Seric **
11121822Seric **	Returns:
11131822Seric **		TRUE if the sid represents a branch.
11141822Seric **		FALSE otherwise.
11151822Seric **
11161822Seric **	Side Effects:
11171822Seric **		none.
11181822Seric */
11191822Seric 
11201822Seric isbranch(sid)
11211822Seric 	char *sid;
11221822Seric {
11231822Seric 	register char *p;
11241822Seric 	int dots;
11251822Seric 
11261822Seric 	dots = 0;
11271822Seric 	for (p = sid; *p != '\0'; p++)
11281822Seric 	{
11291822Seric 		if (*p == '.')
11301822Seric 			dots++;
11311822Seric 		if (dots > 1)
11321822Seric 			return (TRUE);
11331822Seric 	}
11341822Seric 	return (FALSE);
11351822Seric }
11361822Seric 
11371822Seric /*
1138396Seric **  UNEDIT -- unedit a file
1139396Seric **
1140396Seric **	Checks to see that the current user is actually editting
1141396Seric **	the file and arranges that s/he is not editting it.
1142396Seric **
1143396Seric **	Parameters:
1144416Seric **		fn -- the name of the file to be unedited.
1145396Seric **
1146396Seric **	Returns:
1147585Seric **		TRUE -- if the file was successfully unedited.
1148585Seric **		FALSE -- if the file was not unedited for some
1149585Seric **			reason.
1150396Seric **
1151396Seric **	Side Effects:
1152396Seric **		fn is removed
1153396Seric **		entries are removed from pfile.
1154396Seric */
1155396Seric 
1156585Seric bool
1157396Seric unedit(fn)
1158396Seric 	char *fn;
1159396Seric {
1160396Seric 	register FILE *pfp;
116112153Ssam 	char *cp, *pfn;
1162396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1163396Seric 	FILE *tfp;
1164396Seric 	register char *q;
1165396Seric 	bool delete = FALSE;
1166396Seric 	bool others = FALSE;
1167396Seric 	char *myname;
11681864Seric 	extern char *username();
1169396Seric 	struct pfile *pent;
11701822Seric 	extern struct pfile *getpfent();
117110110Srrh 	char buf[PFILELG];
117233251Sbostic 	extern char *makefile(), *rindex(), *tail();
1173396Seric 
1174396Seric 	/* make "s." filename & find the trailing component */
1175396Seric 	pfn = makefile(fn);
1176586Seric 	if (pfn == NULL)
1177586Seric 		return (FALSE);
1178586Seric 	q = rindex(pfn, '/');
1179586Seric 	if (q == NULL)
1180586Seric 		q = &pfn[-1];
1181586Seric 	if (q[1] != 's' || q[2] != '.')
1182396Seric 	{
11831205Seric 		usrerr("bad file name \"%s\"", fn);
1184585Seric 		return (FALSE);
1185396Seric 	}
1186396Seric 
11871438Seric 	/* turn "s." into "p." & try to open it */
1188396Seric 	*++q = 'p';
1189396Seric 
1190396Seric 	pfp = fopen(pfn, "r");
1191396Seric 	if (pfp == NULL)
1192396Seric 	{
1193416Seric 		printf("%12s: not being edited\n", fn);
1194585Seric 		return (FALSE);
1195396Seric 	}
1196396Seric 
11971438Seric 	/* create temp file for editing p-file */
1198396Seric 	mktemp(tfn);
1199396Seric 	tfp = fopen(tfn, "w");
1200396Seric 	if (tfp == NULL)
1201396Seric 	{
12021205Seric 		usrerr("cannot create \"%s\"", tfn);
1203396Seric 		exit(EX_OSERR);
1204396Seric 	}
1205396Seric 
12061438Seric 	/* figure out who I am */
12071864Seric 	myname = username();
12081438Seric 
12091438Seric 	/*
12101438Seric 	**  Copy p-file to temp file, doing deletions as needed.
12111438Seric 	*/
12121438Seric 
12131822Seric 	while ((pent = getpfent(pfp)) != NULL)
1214396Seric 	{
1215396Seric 		if (strcmp(pent->p_user, myname) == 0)
1216396Seric 		{
1217396Seric 			/* a match */
1218396Seric 			delete++;
1219396Seric 		}
1220396Seric 		else
1221396Seric 		{
12221438Seric 			/* output it again */
12232161Seric 			putpfent(pent, tfp);
1224396Seric 			others++;
1225396Seric 		}
1226396Seric 	}
1227396Seric 
122812153Ssam 	/*
122912153Ssam 	 * Before changing anything, make sure we can remove
123012153Ssam 	 * the file in question (assuming it exists).
123112153Ssam 	 */
123212153Ssam 	if (delete) {
123312153Ssam 		extern int errno;
123412153Ssam 
123512153Ssam 		cp = tail(fn);
123612153Ssam 		errno = 0;
123712153Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
123812153Ssam 			goto bad;
123912153Ssam 		if (errno == 0)
124012153Ssam 			/*
124112153Ssam 			 * This is wrong, but the rest of the program
124212153Ssam 			 * has built in assumptions about "." as well,
124312153Ssam 			 * so why make unedit a special case?
124412153Ssam 			 */
124512153Ssam 			if (access(".", 2) < 0) {
124612153Ssam 	bad:
124712153Ssam 				printf("%12s: can't remove\n", cp);
124812153Ssam 				fclose(tfp);
124912153Ssam 				fclose(pfp);
125012153Ssam 				unlink(tfn);
125112153Ssam 				return (FALSE);
125212153Ssam 			}
125312153Ssam 	}
1254396Seric 	/* do final cleanup */
1255396Seric 	if (others)
1256396Seric 	{
12571438Seric 		/* copy it back (perhaps it should be linked?) */
1258396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1259396Seric 		{
12601205Seric 			syserr("cannot reopen \"%s\"", tfn);
1261396Seric 			exit(EX_OSERR);
1262396Seric 		}
1263396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1264396Seric 		{
12651205Seric 			usrerr("cannot create \"%s\"", pfn);
1266585Seric 			return (FALSE);
1267396Seric 		}
1268396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1269396Seric 			fputs(buf, pfp);
1270396Seric 	}
1271396Seric 	else
1272396Seric 	{
12731438Seric 		/* it's empty -- remove it */
1274396Seric 		unlink(pfn);
1275396Seric 	}
1276396Seric 	fclose(tfp);
1277396Seric 	fclose(pfp);
1278396Seric 	unlink(tfn);
1279396Seric 
12801438Seric 	/* actually remove the g-file */
1281396Seric 	if (delete)
1282396Seric 	{
128312153Ssam 		/*
128412153Ssam 		 * Since we've checked above, we can
128512153Ssam 		 * use the return from unlink to
128612153Ssam 		 * determine if the file existed or not.
128712153Ssam 		 */
128812153Ssam 		if (unlink(cp) >= 0)
128912153Ssam 			printf("%12s: removed\n", cp);
1290585Seric 		return (TRUE);
1291396Seric 	}
1292396Seric 	else
1293396Seric 	{
1294416Seric 		printf("%12s: not being edited by you\n", fn);
1295585Seric 		return (FALSE);
1296396Seric 	}
1297396Seric }
12981432Seric 
12991432Seric /*
13001433Seric **  DODIFF -- diff an s-file against a g-file
13011433Seric **
13021433Seric **	Parameters:
13031433Seric **		getv -- argv for the 'get' command.
13041433Seric **		gfile -- name of the g-file to diff against.
13051433Seric **
13061433Seric **	Returns:
13071433Seric **		Result of get.
13081433Seric **
13091433Seric **	Side Effects:
13101433Seric **		none.
13111433Seric */
13121433Seric 
13131433Seric dodiff(getv, gfile)
13141433Seric 	char **getv;
13151433Seric 	char *gfile;
13161433Seric {
13171433Seric 	int pipev[2];
13181433Seric 	int rval;
13191433Seric 	register int i;
13201433Seric 	register int pid;
13211433Seric 	auto int st;
13221433Seric 	extern int errno;
13231433Seric 	int (*osig)();
13241433Seric 
13251905Seric 	printf("\n------- %s -------\n", gfile);
13261871Seric 	fflush(stdout);
13271501Seric 
13281438Seric 	/* create context for diff to run in */
13291433Seric 	if (pipe(pipev) < 0)
13301433Seric 	{
13311433Seric 		syserr("dodiff: pipe failed");
13321433Seric 		exit(EX_OSERR);
13331433Seric 	}
13341433Seric 	if ((pid = fork()) < 0)
13351433Seric 	{
13361433Seric 		syserr("dodiff: fork failed");
13371433Seric 		exit(EX_OSERR);
13381433Seric 	}
13391433Seric 	else if (pid > 0)
13401433Seric 	{
13411433Seric 		/* in parent; run get */
13421433Seric 		OutFile = pipev[1];
13431433Seric 		close(pipev[0]);
13441871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
13451433Seric 		osig = signal(SIGINT, SIG_IGN);
13461433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
13471433Seric 			errno = 0;
13481433Seric 		signal(SIGINT, osig);
13491433Seric 		/* ignore result of diff */
13501433Seric 	}
13511433Seric 	else
13521433Seric 	{
13531433Seric 		/* in child, run diff */
13541433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
13551433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
13561433Seric 		{
13571433Seric 			syserr("dodiff: magic failed");
13581433Seric 			exit(EX_OSERR);
13591433Seric 		}
13601871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
13611433Seric 	}
13621433Seric 	return (rval);
13631433Seric }
13641433Seric 
13651433Seric /*
13661435Seric **  TAIL -- return tail of filename.
13671435Seric **
13681435Seric **	Parameters:
13691435Seric **		fn -- the filename.
13701435Seric **
13711435Seric **	Returns:
13721435Seric **		a pointer to the tail of the filename; e.g., given
13731435Seric **		"cmd/ls.c", "ls.c" is returned.
13741435Seric **
13751435Seric **	Side Effects:
13761435Seric **		none.
13771435Seric */
13781435Seric 
13791435Seric char *
13801435Seric tail(fn)
13811435Seric 	register char *fn;
13821435Seric {
13831435Seric 	register char *p;
13841435Seric 
13851435Seric 	for (p = fn; *p != 0; p++)
13861435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
13871435Seric 			fn = &p[1];
13881435Seric 	return (fn);
13891435Seric }
13901435Seric 
13911435Seric /*
13921822Seric **  GETPFENT -- get an entry from the p-file
1393396Seric **
1394396Seric **	Parameters:
1395396Seric **		pfp -- p-file file pointer
1396396Seric **
1397396Seric **	Returns:
1398396Seric **		pointer to p-file struct for next entry
1399396Seric **		NULL on EOF or error
1400396Seric **
1401396Seric **	Side Effects:
1402396Seric **		Each call wipes out results of previous call.
1403396Seric */
1404396Seric 
1405396Seric struct pfile *
14061822Seric getpfent(pfp)
1407396Seric 	FILE *pfp;
1408396Seric {
1409396Seric 	static struct pfile ent;
141010110Srrh 	static char buf[PFILELG];
1411396Seric 	register char *p;
1412396Seric 	extern char *nextfield();
1413396Seric 
1414396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1415396Seric 		return (NULL);
1416396Seric 
1417396Seric 	ent.p_osid = p = buf;
1418396Seric 	ent.p_nsid = p = nextfield(p);
1419396Seric 	ent.p_user = p = nextfield(p);
1420396Seric 	ent.p_date = p = nextfield(p);
1421396Seric 	ent.p_time = p = nextfield(p);
14222161Seric 	ent.p_aux = p = nextfield(p);
1423396Seric 
1424396Seric 	return (&ent);
1425396Seric }
1426396Seric 
1427396Seric 
1428396Seric char *
1429396Seric nextfield(p)
1430396Seric 	register char *p;
1431396Seric {
1432396Seric 	if (p == NULL || *p == '\0')
1433396Seric 		return (NULL);
1434396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1435396Seric 		p++;
1436396Seric 	if (*p == '\n' || *p == '\0')
1437396Seric 	{
1438396Seric 		*p = '\0';
1439396Seric 		return (NULL);
1440396Seric 	}
1441396Seric 	*p++ = '\0';
1442396Seric 	return (p);
1443396Seric }
14442161Seric /*
14452161Seric **  PUTPFENT -- output a p-file entry to a file
14462161Seric **
14472161Seric **	Parameters:
14482161Seric **		pf -- the p-file entry
14492161Seric **		f -- the file to put it on.
14502161Seric **
14512161Seric **	Returns:
14522161Seric **		none.
14532161Seric **
14542161Seric **	Side Effects:
14552161Seric **		pf is written onto file f.
14562161Seric */
14572161Seric 
14582161Seric putpfent(pf, f)
14592161Seric 	register struct pfile *pf;
14602161Seric 	register FILE *f;
14612161Seric {
14622161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
14632161Seric 		pf->p_user, pf->p_date, pf->p_time);
14642161Seric 	if (pf->p_aux != NULL)
14652161Seric 		fprintf(f, " %s", pf->p_aux);
14662161Seric 	else
14672161Seric 		fprintf(f, "\n");
14682161Seric }
14691432Seric 
14701432Seric /*
14711205Seric **  USRERR -- issue user-level error
14721205Seric **
14731205Seric **	Parameters:
14741205Seric **		f -- format string.
14751205Seric **		p1-p3 -- parameters to a printf.
14761205Seric **
14771205Seric **	Returns:
14781205Seric **		-1
14791205Seric **
14801205Seric **	Side Effects:
14811205Seric **		none.
14821205Seric */
14831205Seric 
14841738Seric /*VARARGS1*/
14851205Seric usrerr(f, p1, p2, p3)
14861205Seric 	char *f;
14871205Seric {
14881205Seric 	fprintf(stderr, "\n%s: ", MyName);
14891205Seric 	fprintf(stderr, f, p1, p2, p3);
14901205Seric 	fprintf(stderr, "\n");
14911205Seric 
14921205Seric 	return (-1);
14931205Seric }
14941432Seric 
14951432Seric /*
14961205Seric **  SYSERR -- print system-generated error.
14971205Seric **
14981205Seric **	Parameters:
14991205Seric **		f -- format string to a printf.
15001205Seric **		p1, p2, p3 -- parameters to f.
15011205Seric **
15021205Seric **	Returns:
15031205Seric **		never.
15041205Seric **
15051205Seric **	Side Effects:
15061205Seric **		none.
15071205Seric */
15081205Seric 
15091738Seric /*VARARGS1*/
15101205Seric syserr(f, p1, p2, p3)
15111205Seric 	char *f;
15121205Seric {
15131205Seric 	extern int errno;
15141205Seric 
15151205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
15161205Seric 	fprintf(stderr, f, p1, p2, p3);
15171205Seric 	fprintf(stderr, "\n");
15181205Seric 	if (errno == 0)
15191205Seric 		exit(EX_SOFTWARE);
15201205Seric 	else
15211205Seric 	{
15221738Seric 		perror(NULL);
15231205Seric 		exit(EX_OSERR);
15241205Seric 	}
15251205Seric }
15261864Seric /*
15271864Seric **  USERNAME -- return name of the current user
15281864Seric **
15291864Seric **	Parameters:
15301864Seric **		none
15311864Seric **
15321864Seric **	Returns:
15331864Seric **		name of current user
15341864Seric **
15351864Seric **	Side Effects:
15361864Seric **		none
15371864Seric */
15381864Seric 
15391864Seric char *
15401864Seric username()
15411864Seric {
15421864Seric # ifdef UIDUSER
15431864Seric 	extern struct passwd *getpwuid();
15441864Seric 	register struct passwd *pw;
15451864Seric 
15461864Seric 	pw = getpwuid(getuid());
15471864Seric 	if (pw == NULL)
15481864Seric 	{
15491864Seric 		syserr("who are you? (uid=%d)", getuid());
15501864Seric 		exit(EX_OSERR);
15511864Seric 	}
15521864Seric 	return (pw->pw_name);
15531864Seric # else
15541905Seric 	extern char *getlogin();
15556785Smckusick 	register char *p;
15561905Seric 
15576785Smckusick 	p = getenv("USER");
15586785Smckusick 	if (p == NULL || p[0] == '\0')
15596785Smckusick 		p = getlogin();
15606785Smckusick 	return (p);
15611864Seric # endif UIDUSER
15621864Seric }
156310110Srrh 
156410110Srrh /*
156510110Srrh **	Guarded string manipulation routines; the last argument
156610110Srrh **	is the length of the buffer into which the strcpy or strcat
156710110Srrh **	is to be done.
156810110Srrh */
156910110Srrh char *gstrcat(to, from, length)
157010110Srrh 	char	*to, *from;
157110110Srrh 	int	length;
157210110Srrh {
157310110Srrh 	if (strlen(from) + strlen(to) >= length) {
157410110Srrh 		gstrbotch(to, from);
157510110Srrh 	}
157610110Srrh 	return(strcat(to, from));
157710110Srrh }
157810110Srrh 
157910110Srrh char *gstrncat(to, from, n, length)
158010110Srrh 	char	*to, *from;
158110110Srrh 	int	n;
158210110Srrh 	int	length;
158310110Srrh {
158410110Srrh 	if (n + strlen(to) >= length) {
158510110Srrh 		gstrbotch(to, from);
158610110Srrh 	}
158710110Srrh 	return(strncat(to, from, n));
158810110Srrh }
158910110Srrh 
159010110Srrh char *gstrcpy(to, from, length)
159110110Srrh 	char	*to, *from;
159210110Srrh 	int	length;
159310110Srrh {
159410110Srrh 	if (strlen(from) >= length) {
159510110Srrh 		gstrbotch(from, (char *)0);
159610110Srrh 	}
159710110Srrh 	return(strcpy(to, from));
159810110Srrh }
159910110Srrh gstrbotch(str1, str2)
160010110Srrh 	char	*str1, *str2;
160110110Srrh {
160210110Srrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
160310110Srrh }
1604