xref: /original-bsd/usr.bin/sccs/sccs.c (revision 98a2bcfb)
13bbbf6e6Sdist /*
2*98a2bcfbSbostic  * Copyright (c) 1980, 1993
3*98a2bcfbSbostic  *	The Regents of the University of California.  All rights reserved.
468dc9f68Sbostic  *
5aa05f8e7Sbostic  * %sccs.include.redist.c%
63bbbf6e6Sdist  */
73bbbf6e6Sdist 
86d5ea7f2Ssam #ifndef lint
9*98a2bcfbSbostic static char copyright[] =
10*98a2bcfbSbostic "@(#) Copyright (c) 1980, 1993\n\
11*98a2bcfbSbostic 	The Regents of the University of California.  All rights reserved.\n";
1268dc9f68Sbostic #endif /* not lint */
133bbbf6e6Sdist 
143bbbf6e6Sdist #ifndef lint
15*98a2bcfbSbostic static char sccsid[] = "@(#)sccs.c	8.1 (Berkeley) 06/06/93";
1668dc9f68Sbostic #endif /* not lint */
176d5ea7f2Ssam 
18b421300eSbostic #include <sys/cdefs.h>
19683375a0Smckusick #include <sys/param.h>
20adf8f7d4Seric #include <sys/stat.h>
216d5ea7f2Ssam #include <sys/dir.h>
2278d41c6aSeric #include <signal.h>
23adf8f7d4Seric #include <sysexits.h>
24b421300eSbostic #include <errno.h>
25393e27f2Seric #include <pwd.h>
26b421300eSbostic #include <stdio.h>
277a8071bfSbostic #include "pathnames.h"
28adf8f7d4Seric 
2910cd16efSeric /*
3010cd16efSeric **  SCCS.C -- human-oriented front end to the SCCS system.
3110cd16efSeric **
3210cd16efSeric **	Without trying to add any functionality to speak of, this
3310cd16efSeric **	program tries to make SCCS a little more accessible to human
3410cd16efSeric **	types.  The main thing it does is automatically put the
3510cd16efSeric **	string "SCCS/s." on the front of names.  Also, it has a
3610cd16efSeric **	couple of things that are designed to shorten frequent
3710cd16efSeric **	combinations, e.g., "delget" which expands to a "delta"
3810cd16efSeric **	and a "get".
3910cd16efSeric **
4010cd16efSeric **	This program can also function as a setuid front end.
4110cd16efSeric **	To do this, you should copy the source, renaming it to
4210cd16efSeric **	whatever you want, e.g., "syssccs".  Change any defaults
4310cd16efSeric **	in the program (e.g., syssccs might default -d to
4410cd16efSeric **	"/usr/src/sys").  Then recompile and put the result
4510cd16efSeric **	as setuid to whomever you want.  In this mode, sccs
4610cd16efSeric **	knows to not run setuid for certain programs in order
4710cd16efSeric **	to preserve security, and so forth.
4810cd16efSeric **
4910cd16efSeric **	Usage:
5010cd16efSeric **		sccs [flags] command [args]
5110cd16efSeric **
5210cd16efSeric **	Flags:
5310cd16efSeric **		-d<dir>		<dir> represents a directory to search
5410cd16efSeric **				out of.  It should be a full pathname
5510cd16efSeric **				for general usage.  E.g., if <dir> is
5610cd16efSeric **				"/usr/src/sys", then a reference to the
5710cd16efSeric **				file "dev/bio.c" becomes a reference to
5810cd16efSeric **				"/usr/src/sys/dev/bio.c".
5910cd16efSeric **		-p<path>	prepends <path> to the final component
6010cd16efSeric **				of the pathname.  By default, this is
6110cd16efSeric **				"SCCS".  For example, in the -d example
6210cd16efSeric **				above, the path then gets modified to
6310cd16efSeric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
6410cd16efSeric **				more common usage (without the -d flag),
6510cd16efSeric **				"prog.c" would get modified to
6610cd16efSeric **				"SCCS/s.prog.c".  In both cases, the
6710cd16efSeric **				"s." gets automatically prepended.
6810cd16efSeric **		-r		run as the real user.
6910cd16efSeric **
7010cd16efSeric **	Commands:
7110cd16efSeric **		admin,
7210cd16efSeric **		get,
7310cd16efSeric **		delta,
7410cd16efSeric **		rmdel,
756e36cbf6Sbostic **		cdc,
7610cd16efSeric **		etc.		Straight out of SCCS; only difference
7710cd16efSeric **				is that pathnames get modified as
7810cd16efSeric **				described above.
796e36cbf6Sbostic **		enter		Front end doing "sccs admin -i<name> <name>"
806e36cbf6Sbostic **		create		Macro for "enter" followed by "get".
8110cd16efSeric **		edit		Macro for "get -e".
8210cd16efSeric **		unedit		Removes a file being edited, knowing
8310cd16efSeric **				about p-files, etc.
8410cd16efSeric **		delget		Macro for "delta" followed by "get".
8510cd16efSeric **		deledit		Macro for "delta" followed by "get -e".
866e36cbf6Sbostic **		branch		Macro for "get -b -e", followed by "delta
876e36cbf6Sbostic **				-s -n", followd by "get -e -t -g".
886e36cbf6Sbostic **		diffs		"diff" the specified version of files
896e36cbf6Sbostic **				and the checked-out version.
906e36cbf6Sbostic **		print		Macro for "prs -e" followed by "get -p -m".
916e36cbf6Sbostic **		tell		List what files are being edited.
926e36cbf6Sbostic **		info		Print information about files being edited.
9310cd16efSeric **		clean		Remove all files that can be
9410cd16efSeric **				regenerated from SCCS files.
95095915d3Seric **		check		Like info, but return exit status, for
9610cd16efSeric **				use in makefiles.
9710cd16efSeric **		fix		Remove a top delta & reedit, but save
9810cd16efSeric **				the previous changes in that delta.
9910cd16efSeric **
10010cd16efSeric **	Compilation Flags:
10110cd16efSeric **		UIDUSER -- determine who the user is by looking at the
10210cd16efSeric **			uid rather than the login name -- for machines
10310cd16efSeric **			where SCCS gets the user in this way.
1047abec564Seric **		SCCSDIR -- if defined, forces the -d flag to take on
105095915d3Seric **			this value.  This is so that the setuid
106095915d3Seric **			aspects of this program cannot be abused.
1077abec564Seric **			This flag also disables the -p flag.
1087abec564Seric **		SCCSPATH -- the default for the -p flag.
109c4ad8825Seric **		MYNAME -- the title this program should print when it
110c4ad8825Seric **			gives error messages.
11110cd16efSeric **
11210cd16efSeric **	Compilation Instructions:
11310cd16efSeric **		cc -O -n -s sccs.c
114c4ad8825Seric **		The flags listed above can be -D defined to simplify
115c4ad8825Seric **			recompilation for variant versions.
11610cd16efSeric **
11710cd16efSeric **	Author:
11810cd16efSeric **		Eric Allman, UCB/INGRES
1197abec564Seric **		Copyright 1980 Regents of the University of California
12010cd16efSeric */
12110cd16efSeric 
1220faf63f9Seric 
1237abec564Seric /*******************  Configuration Information  ********************/
1247abec564Seric 
125c4ad8825Seric # ifndef SCCSPATH
126c4ad8825Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
127c4ad8825Seric # endif NOT SCCSPATH
128c4ad8825Seric 
129c4ad8825Seric # ifndef MYNAME
130c4ad8825Seric # define MYNAME		"sccs"	/* name used for printing errors */
131c4ad8825Seric # endif NOT MYNAME
1327abec564Seric 
1337abec564Seric /****************  End of Configuration Information  ****************/
1340faf63f9Seric 
135c4432be4Seric typedef char	bool;
1367de81dc7Seric # define TRUE	1
1377de81dc7Seric # define FALSE	0
138d02a4f42Seric 
139ff038098Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
140ff038098Seric 
141adf8f7d4Seric struct sccsprog
142adf8f7d4Seric {
143adf8f7d4Seric 	char	*sccsname;	/* name of SCCS routine */
1447de81dc7Seric 	short	sccsoper;	/* opcode, see below */
1457de81dc7Seric 	short	sccsflags;	/* flags, see below */
146adf8f7d4Seric 	char	*sccspath;	/* pathname of binary implementing */
147adf8f7d4Seric };
148adf8f7d4Seric 
1497de81dc7Seric /* values for sccsoper */
1507de81dc7Seric # define PROG		0	/* call a program */
1511777fbcbSeric # define CMACRO		1	/* command substitution macro */
152172147faSeric # define FIX		2	/* fix a delta */
153b2538d76Seric # define CLEAN		3	/* clean out recreatable files */
154e39a5722Seric # define UNEDIT		4	/* unedit a file */
155419708b0Seric # define SHELL		5	/* call a shell file (like PROG) */
15678d41c6aSeric # define DIFFS		6	/* diff between sccs & file out */
1572569d37aSeric # define DODIFF		7	/* internal call to diff program */
15844c626a5Srrh # define ENTER		8	/* enter new files */
1597de81dc7Seric 
160c4432be4Seric /* bits for sccsflags */
1617de81dc7Seric # define NO_SDOT	0001	/* no s. on front of args */
1627de81dc7Seric # define REALUSER	0002	/* protected (e.g., admin) */
163adf8f7d4Seric 
16448ff0e7eSeric /* modes for the "clean", "info", "check" ops */
16548ff0e7eSeric # define CLEANC		0	/* clean command */
16648ff0e7eSeric # define INFOC		1	/* info command */
16748ff0e7eSeric # define CHECKC		2	/* check command */
1682734e237Seric # define TELLC		3	/* give list of files being edited */
16948ff0e7eSeric 
1700faf63f9Seric /*
1710faf63f9Seric **  Description of commands known to this program.
1720faf63f9Seric **	First argument puts the command into a class.  Second arg is
1730faf63f9Seric **	info regarding treatment of this command.  Third arg is a
1740faf63f9Seric **	list of flags this command accepts from macros, etc.  Fourth
1750faf63f9Seric **	arg is the pathname of the implementing program, or the
1760faf63f9Seric **	macro definition, or the arg to a sub-algorithm.
1770faf63f9Seric */
178b5d4f080Seric 
179e0af868fSbostic struct sccsprog SccsProg[] = {
180b421300eSbostic 	"admin",	PROG,	REALUSER,	_PATH_SCCSADMIN,
181b421300eSbostic 	"cdc",		PROG,	0,		_PATH_SCCSRMDEL,
182b421300eSbostic 	"comb",		PROG,	0,		_PATH_SCCSCOMB,
183b421300eSbostic 	"delta",	PROG,	0,		_PATH_SCCSDELTA,
184b421300eSbostic 	"get",		PROG,	0,		_PATH_SCCSGET,
185b421300eSbostic 	"help",		PROG,	NO_SDOT,	_PATH_SCCSHELP,
186b421300eSbostic 	"prs",		PROG,	0,		_PATH_SCCSPRS,
187b421300eSbostic 	"prt",		PROG,	0,		_PATH_SCCSPRT,
188b421300eSbostic 	"rmdel",	PROG,	REALUSER,	_PATH_SCCSRMDEL,
189b421300eSbostic 	"val",		PROG,	0,		_PATH_SCCSVAL,
190b421300eSbostic 	"what",		PROG,	NO_SDOT,	_PATH_SCCSWHAT,
191b421300eSbostic 	"sccsdiff",	SHELL,	REALUSER,	_PATH_SCCSDIFF,
1923642a8d2Seric 	"edit",		CMACRO,	NO_SDOT,	"get -e",
19359e81a43Seric 	"delget",	CMACRO,	NO_SDOT,	"delta:mysrp/get:ixbeskcl -t",
194e0af868fSbostic 	"deledit",	CMACRO,	NO_SDOT,
195e0af868fSbostic 					"delta:mysrp -n/get:ixbskcl -e -t -g",
1963642a8d2Seric 	"fix",		FIX,	NO_SDOT,	NULL,
197e0af868fSbostic 	"clean",	CLEAN,	REALUSER|NO_SDOT,
198e0af868fSbostic 						(char *) CLEANC,
199e0af868fSbostic 	"info",		CLEAN,	REALUSER|NO_SDOT,
200e0af868fSbostic 						(char *) INFOC,
201e0af868fSbostic 	"check",	CLEAN,	REALUSER|NO_SDOT,
202e0af868fSbostic 						(char *) CHECKC,
203e0af868fSbostic 	"tell",		CLEAN,	REALUSER|NO_SDOT,
204e0af868fSbostic 						(char *) TELLC,
2053642a8d2Seric 	"unedit",	UNEDIT,	NO_SDOT,	NULL,
206e0af868fSbostic 	"diffs",	DIFFS,	NO_SDOT|REALUSER,
207e0af868fSbostic 						NULL,
208e0af868fSbostic 	"-diff",	DODIFF,	NO_SDOT|REALUSER,
209b421300eSbostic 						_PATH_SCCSBDIFF,
2106e36cbf6Sbostic 	"print",	CMACRO,	0,		"prs -e/get -p -m -s",
211a4f81e73Seric 	"branch",	CMACRO,	NO_SDOT,
212a4f81e73Seric 	    "get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
21344c626a5Srrh 	"enter",	ENTER,	NO_SDOT,	NULL,
21444c626a5Srrh 	"create",	CMACRO,	NO_SDOT,	"enter/get:ixbeskcl -t",
2153642a8d2Seric 	NULL,		-1,	0,		NULL
216adf8f7d4Seric };
217adf8f7d4Seric 
2180faf63f9Seric /* one line from a p-file */
219e39a5722Seric struct pfile
220e39a5722Seric {
221e39a5722Seric 	char	*p_osid;	/* old SID */
222e39a5722Seric 	char	*p_nsid;	/* new SID */
223e39a5722Seric 	char	*p_user;	/* user who did edit */
224e39a5722Seric 	char	*p_date;	/* date of get */
225e39a5722Seric 	char	*p_time;	/* time of get */
22677a8a0dfSeric 	char	*p_aux;		/* extra info at end */
227e39a5722Seric };
228e39a5722Seric 
2297abec564Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2307abec564Seric # ifdef SCCSDIR
2317abec564Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
232095915d3Seric # else
2337abec564Seric char	*SccsDir = "";
234095915d3Seric # endif
235c4ad8825Seric char	MyName[] = MYNAME;	/* name used in messages */
23678d41c6aSeric int	OutFile = -1;		/* override output file for commands */
237c4432be4Seric bool	RealUser;		/* if set, running as real user */
238f0cc3246Seric # ifdef DEBUG
239f0cc3246Seric bool	Debug;			/* turn on tracing */
240f0cc3246Seric # endif
24192fabd68Seric # ifndef V6
24292fabd68Seric extern char	*getenv();
24392fabd68Seric # endif V6
244f76dd34fSrrh 
245f76dd34fSrrh char *gstrcat(), *strcat();
246f76dd34fSrrh char *gstrncat(), *strncat();
247f76dd34fSrrh char *gstrcpy(), *strcpy();
248f76dd34fSrrh #define	FBUFSIZ	BUFSIZ
249f76dd34fSrrh #define	PFILELG	120
2500faf63f9Seric 
main(argc,argv)251adf8f7d4Seric main(argc, argv)
252adf8f7d4Seric 	int argc;
253adf8f7d4Seric 	char **argv;
254adf8f7d4Seric {
255adf8f7d4Seric 	register char *p;
256b1ed8a43Seric 	extern struct sccsprog *lookup();
25741290c52Seric 	register int i;
25892fabd68Seric # ifndef V6
25992fabd68Seric # ifndef SCCSDIR
260393e27f2Seric 	register struct passwd *pw;
261393e27f2Seric 	extern struct passwd *getpwnam();
262f76dd34fSrrh 	char buf[FBUFSIZ];
263393e27f2Seric 
26492fabd68Seric 	/* pull "SccsDir" out of the environment (possibly) */
265250566abSeric 	p = getenv("PROJECTDIR");
266393e27f2Seric 	if (p != NULL && p[0] != '\0')
267393e27f2Seric 	{
26892fabd68Seric 		if (p[0] == '/')
26992fabd68Seric 			SccsDir = p;
27092fabd68Seric 		else
271393e27f2Seric 		{
272393e27f2Seric 			pw = getpwnam(p);
273393e27f2Seric 			if (pw == NULL)
274393e27f2Seric 			{
275393e27f2Seric 				usrerr("user %s does not exist", p);
276393e27f2Seric 				exit(EX_USAGE);
277393e27f2Seric 			}
278f76dd34fSrrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
279f76dd34fSrrh 			gstrcat(buf, "/src", sizeof(buf));
280393e27f2Seric 			if (access(buf, 0) < 0)
281393e27f2Seric 			{
282f76dd34fSrrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
283f76dd34fSrrh 				gstrcat(buf, "/source", sizeof(buf));
284393e27f2Seric 				if (access(buf, 0) < 0)
285393e27f2Seric 				{
286393e27f2Seric 					usrerr("project %s has no source!", p);
287393e27f2Seric 					exit(EX_USAGE);
288393e27f2Seric 				}
289393e27f2Seric 			}
290393e27f2Seric 			SccsDir = buf;
291393e27f2Seric 		}
292393e27f2Seric 	}
29392fabd68Seric # endif SCCSDIR
29492fabd68Seric # endif V6
29592fabd68Seric 
296adf8f7d4Seric 	/*
297adf8f7d4Seric 	**  Detect and decode flags intended for this program.
298adf8f7d4Seric 	*/
299adf8f7d4Seric 
3007de81dc7Seric 	if (argc < 2)
301adf8f7d4Seric 	{
302095915d3Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
3037de81dc7Seric 		exit(EX_USAGE);
3047de81dc7Seric 	}
3057de81dc7Seric 	argv[argc] = NULL;
3067de81dc7Seric 
307b1ed8a43Seric 	if (lookup(argv[0]) == NULL)
308b1ed8a43Seric 	{
3097de81dc7Seric 		while ((p = *++argv) != NULL)
3107de81dc7Seric 		{
311adf8f7d4Seric 			if (*p != '-')
312adf8f7d4Seric 				break;
313adf8f7d4Seric 			switch (*++p)
314adf8f7d4Seric 			{
315adf8f7d4Seric 			  case 'r':		/* run as real user */
316adf8f7d4Seric 				setuid(getuid());
317c4432be4Seric 				RealUser++;
318adf8f7d4Seric 				break;
319adf8f7d4Seric 
3207abec564Seric # ifndef SCCSDIR
321adf8f7d4Seric 			  case 'p':		/* path of sccs files */
322adf8f7d4Seric 				SccsPath = ++p;
3236c7454d2Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3246c7454d2Seric 					SccsPath = *++argv;
325adf8f7d4Seric 				break;
326adf8f7d4Seric 
3270a8b9ab0Seric 			  case 'd':		/* directory to search from */
3280a8b9ab0Seric 				SccsDir = ++p;
3296c7454d2Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3306c7454d2Seric 					SccsDir = *++argv;
3310a8b9ab0Seric 				break;
332095915d3Seric # endif
3330a8b9ab0Seric 
334f0cc3246Seric # ifdef DEBUG
335f0cc3246Seric 			  case 'T':		/* trace */
336f0cc3246Seric 				Debug++;
337f0cc3246Seric 				break;
338f0cc3246Seric # endif
339f0cc3246Seric 
340adf8f7d4Seric 			  default:
341095915d3Seric 				usrerr("unknown option -%s", p);
342adf8f7d4Seric 				break;
343adf8f7d4Seric 			}
344adf8f7d4Seric 		}
3455cabffd9Seric 		if (SccsPath[0] == '\0')
3465cabffd9Seric 			SccsPath = ".";
347b1ed8a43Seric 	}
348adf8f7d4Seric 
3493642a8d2Seric 	i = command(argv, FALSE, "");
35041290c52Seric 	exit(i);
3517de81dc7Seric }
3520faf63f9Seric 
3530faf63f9Seric /*
35441290c52Seric **  COMMAND -- look up and perform a command
35541290c52Seric **
35641290c52Seric **	This routine is the guts of this program.  Given an
35741290c52Seric **	argument vector, it looks up the "command" (argv[0])
35841290c52Seric **	in the configuration table and does the necessary stuff.
35941290c52Seric **
36041290c52Seric **	Parameters:
36141290c52Seric **		argv -- an argument vector to process.
36241290c52Seric **		forkflag -- if set, fork before executing the command.
363108d6082Seric **		editflag -- if set, only include flags listed in the
364108d6082Seric **			sccsklets field of the command descriptor.
365108d6082Seric **		arg0 -- a space-seperated list of arguments to insert
366108d6082Seric **			before argv.
36741290c52Seric **
36841290c52Seric **	Returns:
36941290c52Seric **		zero -- command executed ok.
37041290c52Seric **		else -- error status.
37141290c52Seric **
37241290c52Seric **	Side Effects:
37341290c52Seric **		none.
37441290c52Seric */
3757de81dc7Seric 
command(argv,forkflag,arg0)3763642a8d2Seric command(argv, forkflag, arg0)
3777de81dc7Seric 	char **argv;
3781777fbcbSeric 	bool forkflag;
379108d6082Seric 	char *arg0;
3807de81dc7Seric {
3817de81dc7Seric 	register struct sccsprog *cmd;
3827de81dc7Seric 	register char *p;
383f76dd34fSrrh 	char buf[FBUFSIZ];
384b1ed8a43Seric 	extern struct sccsprog *lookup();
385108d6082Seric 	char *nav[1000];
386108d6082Seric 	char **np;
387419708b0Seric 	register char **ap;
388d51cd7e5Seric 	register int i;
389419708b0Seric 	register char *q;
390d51cd7e5Seric 	extern bool unedit();
39141290c52Seric 	int rval = 0;
392108d6082Seric 	extern char *index();
393108d6082Seric 	extern char *makefile();
3943642a8d2Seric 	char *editchs;
395287bbaa2Seric 	extern char *tail();
396f0cc3246Seric 
397f0cc3246Seric # ifdef DEBUG
398f0cc3246Seric 	if (Debug)
399f0cc3246Seric 	{
400108d6082Seric 		printf("command:\n\t\"%s\"\n", arg0);
401108d6082Seric 		for (np = argv; *np != NULL; np++)
402108d6082Seric 			printf("\t\"%s\"\n", *np);
403f0cc3246Seric 	}
404f0cc3246Seric # endif
405c4432be4Seric 
406c4432be4Seric 	/*
407108d6082Seric 	**  Copy arguments.
408ff038098Seric 	**	Copy from arg0 & if necessary at most one arg
409ff038098Seric 	**	from argv[0].
410adf8f7d4Seric 	*/
411adf8f7d4Seric 
412419708b0Seric 	np = ap = &nav[1];
4133642a8d2Seric 	editchs = NULL;
41459e81a43Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
415108d6082Seric 	{
416108d6082Seric 		*np++ = q;
417108d6082Seric 		while (*p == ' ')
418108d6082Seric 			p++;
4193642a8d2Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
420108d6082Seric 			*q++ = *p++;
421108d6082Seric 		*q++ = '\0';
4223642a8d2Seric 		if (*p == ':')
4233642a8d2Seric 		{
4243642a8d2Seric 			editchs = q;
42559e81a43Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4263642a8d2Seric 				*q++ = *p;
4273642a8d2Seric 			*q++ = '\0';
4283642a8d2Seric 		}
429108d6082Seric 	}
430108d6082Seric 	*np = NULL;
431419708b0Seric 	if (*ap == NULL)
432108d6082Seric 		*np++ = *argv++;
433108d6082Seric 
434108d6082Seric 	/*
435108d6082Seric 	**  Look up command.
436419708b0Seric 	**	At this point, *ap is the command name.
437108d6082Seric 	*/
438108d6082Seric 
439419708b0Seric 	cmd = lookup(*ap);
440b1ed8a43Seric 	if (cmd == NULL)
441adf8f7d4Seric 	{
442419708b0Seric 		usrerr("Unknown command \"%s\"", *ap);
44341290c52Seric 		return (EX_USAGE);
444adf8f7d4Seric 	}
445adf8f7d4Seric 
446adf8f7d4Seric 	/*
447108d6082Seric 	**  Copy remaining arguments doing editing as appropriate.
448108d6082Seric 	*/
449108d6082Seric 
450108d6082Seric 	for (; *argv != NULL; argv++)
451108d6082Seric 	{
452108d6082Seric 		p = *argv;
453108d6082Seric 		if (*p == '-')
454108d6082Seric 		{
4553642a8d2Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
456108d6082Seric 				*np++ = p;
457108d6082Seric 		}
458108d6082Seric 		else
459108d6082Seric 		{
460108d6082Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
461108d6082Seric 				p = makefile(p);
462108d6082Seric 			if (p != NULL)
463108d6082Seric 				*np++ = p;
464108d6082Seric 		}
465108d6082Seric 	}
466108d6082Seric 	*np = NULL;
467108d6082Seric 
468108d6082Seric 	/*
4697de81dc7Seric 	**  Interpret operation associated with this command.
470c4432be4Seric 	*/
471c4432be4Seric 
4727de81dc7Seric 	switch (cmd->sccsoper)
4737de81dc7Seric 	{
474419708b0Seric 	  case SHELL:		/* call a shell file */
475419708b0Seric 		*ap = cmd->sccspath;
476419708b0Seric 		*--ap = "sh";
4777a8071bfSbostic 		rval = callprog(_PATH_BSHELL, cmd->sccsflags, ap, forkflag);
478419708b0Seric 		break;
479419708b0Seric 
4807de81dc7Seric 	  case PROG:		/* call an sccs prog */
481419708b0Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
4821777fbcbSeric 		break;
4831777fbcbSeric 
4841777fbcbSeric 	  case CMACRO:		/* command macro */
485ff038098Seric 		/* step through & execute each part of the macro */
4861777fbcbSeric 		for (p = cmd->sccspath; *p != '\0'; p++)
4871777fbcbSeric 		{
488108d6082Seric 			q = p;
489108d6082Seric 			while (*p != '\0' && *p != '/')
490108d6082Seric 				p++;
4913642a8d2Seric 			rval = command(&ap[1], *p != '\0', q);
49241290c52Seric 			if (rval != 0)
49341290c52Seric 				break;
4941777fbcbSeric 		}
49541290c52Seric 		break;
4967de81dc7Seric 
497172147faSeric 	  case FIX:		/* fix a delta */
4986e36cbf6Sbostic 		if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
499172147faSeric 		{
500095915d3Seric 			usrerr("-r flag needed for fix command");
50141290c52Seric 			rval = EX_USAGE;
502172147faSeric 			break;
503172147faSeric 		}
504ff038098Seric 
505ff038098Seric 		/* get the version with all changes */
5063642a8d2Seric 		rval = command(&ap[1], TRUE, "get -k");
507ff038098Seric 
508ff038098Seric 		/* now remove that version from the s-file */
50941290c52Seric 		if (rval == 0)
5103642a8d2Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
511ff038098Seric 
512ff038098Seric 		/* and edit the old version (but don't clobber new vers) */
51341290c52Seric 		if (rval == 0)
5143642a8d2Seric 			rval = command(&ap[2], FALSE, "get -e -g");
51541290c52Seric 		break;
516172147faSeric 
517b2538d76Seric 	  case CLEAN:
518ec4190ddSeric 		rval = clean((int) cmd->sccspath, ap);
519b2538d76Seric 		break;
520b2538d76Seric 
521e39a5722Seric 	  case UNEDIT:
522419708b0Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
523d51cd7e5Seric 		{
524108d6082Seric 			if (unedit(*argv))
525108d6082Seric 				*np++ = *argv;
526d51cd7e5Seric 		}
527108d6082Seric 		*np = NULL;
528ff038098Seric 
529ff038098Seric 		/* get all the files that we unedited successfully */
530fe7b004aSeric 		if (np > &ap[1])
5313642a8d2Seric 			rval = command(&ap[1], FALSE, "get");
532e39a5722Seric 		break;
533e39a5722Seric 
53478d41c6aSeric 	  case DIFFS:		/* diff between s-file & edit file */
53578d41c6aSeric 		/* find the end of the flag arguments */
53678d41c6aSeric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
53778d41c6aSeric 			continue;
53878d41c6aSeric 		argv = np;
53978d41c6aSeric 
54078d41c6aSeric 		/* for each file, do the diff */
54196370756Seric 		p = argv[1];
54278d41c6aSeric 		while (*np != NULL)
54378d41c6aSeric 		{
544ff038098Seric 			/* messy, but we need a null terminated argv */
54578d41c6aSeric 			*argv = *np++;
54696370756Seric 			argv[1] = NULL;
547287bbaa2Seric 			i = dodiff(ap, tail(*argv));
54878d41c6aSeric 			if (rval == 0)
54978d41c6aSeric 				rval = i;
55096370756Seric 			argv[1] = p;
55178d41c6aSeric 		}
55278d41c6aSeric 		break;
55378d41c6aSeric 
5542569d37aSeric 	  case DODIFF:		/* internal diff call */
5552569d37aSeric 		setuid(getuid());
5562569d37aSeric 		for (np = ap; *np != NULL; np++)
5572569d37aSeric 		{
5582569d37aSeric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5592569d37aSeric 				(*np)[1] = 'c';
5602569d37aSeric 		}
5612569d37aSeric 
5622569d37aSeric 		/* insert "-" argument */
5632569d37aSeric 		np[1] = NULL;
5642569d37aSeric 		np[0] = np[-1];
5652569d37aSeric 		np[-1] = "-";
5662569d37aSeric 
5672569d37aSeric 		/* execute the diff program of choice */
5682569d37aSeric # ifndef V6
5692569d37aSeric 		execvp("diff", ap);
5702569d37aSeric # endif
5712569d37aSeric 		execv(cmd->sccspath, argv);
5722569d37aSeric 		syserr("cannot exec %s", cmd->sccspath);
5732569d37aSeric 		exit(EX_OSERR);
5742569d37aSeric 
57544c626a5Srrh 	  case ENTER:		/* enter new sccs files */
576bb5509cbSmckusick 		/* skip over flag arguments */
577bb5509cbSmckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
578bb5509cbSmckusick 			continue;
579bb5509cbSmckusick 		argv = np;
580bb5509cbSmckusick 
581bb5509cbSmckusick 		/* do an admin for each file */
582bb5509cbSmckusick 		p = argv[1];
583bb5509cbSmckusick 		while (*np != NULL)
584bb5509cbSmckusick 		{
585bb5509cbSmckusick 			printf("\n%s:\n", *np);
586f76dd34fSrrh 			strcpy(buf, "-i");
587f76dd34fSrrh 			gstrcat(buf, *np, sizeof(buf));
588bb5509cbSmckusick 			ap[0] = buf;
589bb5509cbSmckusick 			argv[0] = tail(*np);
590bb5509cbSmckusick 			argv[1] = NULL;
591bb5509cbSmckusick 			rval = command(ap, TRUE, "admin");
592bb5509cbSmckusick 			argv[1] = p;
593bb5509cbSmckusick 			if (rval == 0)
594bb5509cbSmckusick 			{
595f76dd34fSrrh 				strcpy(buf, ",");
596f76dd34fSrrh 				gstrcat(buf, tail(*np), sizeof(buf));
597bb5509cbSmckusick 				if (link(*np, buf) >= 0)
598bb5509cbSmckusick 					unlink(*np);
599bb5509cbSmckusick 			}
600bb5509cbSmckusick 			np++;
601bb5509cbSmckusick 		}
602bb5509cbSmckusick 		break;
603bb5509cbSmckusick 
6047de81dc7Seric 	  default:
605095915d3Seric 		syserr("oper %d", cmd->sccsoper);
6067de81dc7Seric 		exit(EX_SOFTWARE);
6077de81dc7Seric 	}
60841290c52Seric # ifdef DEBUG
60941290c52Seric 	if (Debug)
61041290c52Seric 		printf("command: rval=%d\n", rval);
61141290c52Seric # endif
61241290c52Seric 	return (rval);
6137de81dc7Seric }
6140faf63f9Seric 
6150faf63f9Seric /*
616b1ed8a43Seric **  LOOKUP -- look up an SCCS command name.
617b1ed8a43Seric **
618b1ed8a43Seric **	Parameters:
619b1ed8a43Seric **		name -- the name of the command to look up.
620b1ed8a43Seric **
621b1ed8a43Seric **	Returns:
622b1ed8a43Seric **		ptr to command descriptor for this command.
623b1ed8a43Seric **		NULL if no such entry.
624b1ed8a43Seric **
625b1ed8a43Seric **	Side Effects:
626b1ed8a43Seric **		none.
627b1ed8a43Seric */
628b1ed8a43Seric 
629b1ed8a43Seric struct sccsprog *
lookup(name)630b1ed8a43Seric lookup(name)
631b1ed8a43Seric 	char *name;
632b1ed8a43Seric {
633b1ed8a43Seric 	register struct sccsprog *cmd;
634b1ed8a43Seric 
635b1ed8a43Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
636b1ed8a43Seric 	{
637b1ed8a43Seric 		if (strcmp(cmd->sccsname, name) == 0)
638b1ed8a43Seric 			return (cmd);
639b1ed8a43Seric 	}
640b1ed8a43Seric 	return (NULL);
641b1ed8a43Seric }
6420faf63f9Seric 
6430faf63f9Seric /*
64441290c52Seric **  CALLPROG -- call a program
64541290c52Seric **
646108d6082Seric **	Used to call the SCCS programs.
64741290c52Seric **
64841290c52Seric **	Parameters:
64941290c52Seric **		progpath -- pathname of the program to call.
65041290c52Seric **		flags -- status flags from the command descriptors.
65141290c52Seric **		argv -- an argument vector to pass to the program.
65241290c52Seric **		forkflag -- if true, fork before calling, else just
65341290c52Seric **			exec.
65441290c52Seric **
65541290c52Seric **	Returns:
65641290c52Seric **		The exit status of the program.
65741290c52Seric **		Nothing if forkflag == FALSE.
65841290c52Seric **
65941290c52Seric **	Side Effects:
66041290c52Seric **		Can exit if forkflag == FALSE.
66141290c52Seric */
662172147faSeric 
callprog(progpath,flags,argv,forkflag)6637de81dc7Seric callprog(progpath, flags, argv, forkflag)
6647de81dc7Seric 	char *progpath;
6657de81dc7Seric 	short flags;
6667de81dc7Seric 	char **argv;
6677de81dc7Seric 	bool forkflag;
6687de81dc7Seric {
6697de81dc7Seric 	register int i;
6706e36cbf6Sbostic 	register int wpid;
6711777fbcbSeric 	auto int st;
6726e36cbf6Sbostic 	register int sigcode;
6736e36cbf6Sbostic 	register int coredumped;
67484c8db45Sbostic 	register const char *sigmsg;
67584c8db45Sbostic 	char sigmsgbuf[10+1];	/* "Signal 127" + terminating '\0' */
676108d6082Seric 
677108d6082Seric # ifdef DEBUG
678108d6082Seric 	if (Debug)
679108d6082Seric 	{
680108d6082Seric 		printf("callprog:\n");
681108d6082Seric 		for (i = 0; argv[i] != NULL; i++)
682108d6082Seric 			printf("\t\"%s\"\n", argv[i]);
683108d6082Seric 	}
684108d6082Seric # endif
6857de81dc7Seric 
6867de81dc7Seric 	if (*argv == NULL)
6877de81dc7Seric 		return (-1);
688c4432be4Seric 
689c4432be4Seric 	/*
690172147faSeric 	**  Fork if appropriate.
691adf8f7d4Seric 	*/
692adf8f7d4Seric 
6937de81dc7Seric 	if (forkflag)
6947de81dc7Seric 	{
695f0cc3246Seric # ifdef DEBUG
696f0cc3246Seric 		if (Debug)
697f0cc3246Seric 			printf("Forking\n");
698f0cc3246Seric # endif
6997de81dc7Seric 		i = fork();
7007de81dc7Seric 		if (i < 0)
7017de81dc7Seric 		{
702095915d3Seric 			syserr("cannot fork");
7037de81dc7Seric 			exit(EX_OSERR);
7047de81dc7Seric 		}
7057de81dc7Seric 		else if (i > 0)
7061777fbcbSeric 		{
7076e36cbf6Sbostic 			while ((wpid = wait(&st)) != -1 && wpid != i)
7086e36cbf6Sbostic 				;
7096e36cbf6Sbostic 			if ((sigcode = st & 0377) == 0)
71041290c52Seric 				st = (st >> 8) & 0377;
7116e36cbf6Sbostic 			else
7126e36cbf6Sbostic 			{
7136e36cbf6Sbostic 				coredumped = sigcode & 0200;
7146e36cbf6Sbostic 				sigcode &= 0177;
7156e36cbf6Sbostic 				if (sigcode != SIGINT && sigcode != SIGPIPE)
7166e36cbf6Sbostic 				{
7176e36cbf6Sbostic 					if (sigcode < NSIG)
7186e36cbf6Sbostic 						sigmsg = sys_siglist[sigcode];
7196e36cbf6Sbostic 					else
7206e36cbf6Sbostic 					{
7216e36cbf6Sbostic 						sprintf(sigmsgbuf, "Signal %d",
7226e36cbf6Sbostic 						    sigcode);
7236e36cbf6Sbostic 						sigmsg = sigmsgbuf;
7246e36cbf6Sbostic 					}
7256e36cbf6Sbostic 					fprintf(stderr, "sccs: %s: %s%s", argv[0],
7266e36cbf6Sbostic 					    sigmsg,
7276e36cbf6Sbostic 					    coredumped ? " - core dumped": "");
7286e36cbf6Sbostic 				}
7296e36cbf6Sbostic 				st = EX_SOFTWARE;
7306e36cbf6Sbostic 			}
73178d41c6aSeric 			if (OutFile >= 0)
73278d41c6aSeric 			{
73378d41c6aSeric 				close(OutFile);
73478d41c6aSeric 				OutFile = -1;
73578d41c6aSeric 			}
7361777fbcbSeric 			return (st);
7371777fbcbSeric 		}
7387de81dc7Seric 	}
73978d41c6aSeric 	else if (OutFile >= 0)
74078d41c6aSeric 	{
74178d41c6aSeric 		syserr("callprog: setting stdout w/o forking");
74278d41c6aSeric 		exit(EX_SOFTWARE);
74378d41c6aSeric 	}
7447de81dc7Seric 
74578d41c6aSeric 	/* set protection as appropriate */
7467de81dc7Seric 	if (bitset(REALUSER, flags))
7477de81dc7Seric 		setuid(getuid());
7487de81dc7Seric 
74978d41c6aSeric 	/* change standard input & output if needed */
75078d41c6aSeric 	if (OutFile >= 0)
75178d41c6aSeric 	{
75278d41c6aSeric 		close(1);
75378d41c6aSeric 		dup(OutFile);
75478d41c6aSeric 		close(OutFile);
75578d41c6aSeric 	}
7567de81dc7Seric 
75778d41c6aSeric 	/* call real SCCS program */
758172147faSeric 	execv(progpath, argv);
759095915d3Seric 	syserr("cannot execute %s", progpath);
760adf8f7d4Seric 	exit(EX_UNAVAILABLE);
761fe7b004aSeric 	/*NOTREACHED*/
762adf8f7d4Seric }
7630faf63f9Seric 
7640faf63f9Seric /*
765cdc1aa65Seric **  MAKEFILE -- make filename of SCCS file
766cdc1aa65Seric **
767cdc1aa65Seric **	If the name passed is already the name of an SCCS file,
768cdc1aa65Seric **	just return it.  Otherwise, munge the name into the name
769cdc1aa65Seric **	of the actual SCCS file.
770cdc1aa65Seric **
771cdc1aa65Seric **	There are cases when it is not clear what you want to
772cdc1aa65Seric **	do.  For example, if SccsPath is an absolute pathname
773cdc1aa65Seric **	and the name given is also an absolute pathname, we go
774cdc1aa65Seric **	for SccsPath (& only use the last component of the name
775cdc1aa65Seric **	passed) -- this is important for security reasons (if
776cdc1aa65Seric **	sccs is being used as a setuid front end), but not
777cdc1aa65Seric **	particularly intuitive.
778cdc1aa65Seric **
779cdc1aa65Seric **	Parameters:
780cdc1aa65Seric **		name -- the file name to be munged.
781cdc1aa65Seric **
782cdc1aa65Seric **	Returns:
783cdc1aa65Seric **		The pathname of the sccs file.
784cdc1aa65Seric **		NULL on error.
785cdc1aa65Seric **
786cdc1aa65Seric **	Side Effects:
787cdc1aa65Seric **		none.
788cdc1aa65Seric */
789adf8f7d4Seric 
790adf8f7d4Seric char *
makefile(name)791adf8f7d4Seric makefile(name)
792adf8f7d4Seric 	char *name;
793adf8f7d4Seric {
794adf8f7d4Seric 	register char *p;
795f76dd34fSrrh 	char buf[3*FBUFSIZ];
796adf8f7d4Seric 	extern char *malloc();
797cdc1aa65Seric 	extern char *rindex();
7980a8b9ab0Seric 	extern bool safepath();
7997d70f5d8Seric 	extern bool isdir();
8007d70f5d8Seric 	register char *q;
801cdc1aa65Seric 
802cdc1aa65Seric 	p = rindex(name, '/');
803cdc1aa65Seric 	if (p == NULL)
804cdc1aa65Seric 		p = name;
805cdc1aa65Seric 	else
806cdc1aa65Seric 		p++;
807adf8f7d4Seric 
808adf8f7d4Seric 	/*
8090a8b9ab0Seric 	**  Check to see that the path is "safe", i.e., that we
8100a8b9ab0Seric 	**  are not letting some nasty person use the setuid part
8110a8b9ab0Seric 	**  of this program to look at or munge some presumably
8120a8b9ab0Seric 	**  hidden files.
813adf8f7d4Seric 	*/
814adf8f7d4Seric 
8150a8b9ab0Seric 	if (SccsDir[0] == '/' && !safepath(name))
8160a8b9ab0Seric 		return (NULL);
817cdc1aa65Seric 
818cdc1aa65Seric 	/*
8190a8b9ab0Seric 	**  Create the base pathname.
820cdc1aa65Seric 	*/
821cdc1aa65Seric 
822ff038098Seric 	/* first the directory part */
8230a8b9ab0Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
824adf8f7d4Seric 	{
825f76dd34fSrrh 		gstrcpy(buf, SccsDir, sizeof(buf));
826f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
827cdc1aa65Seric 	}
828cdc1aa65Seric 	else
829f76dd34fSrrh 		gstrcpy(buf, "", sizeof(buf));
830ff038098Seric 
831ff038098Seric 	/* then the head of the pathname */
832f76dd34fSrrh 	gstrncat(buf, name, p - name, sizeof(buf));
8337d70f5d8Seric 	q = &buf[strlen(buf)];
834ff038098Seric 
835ff038098Seric 	/* now copy the final part of the name, in case useful */
836f76dd34fSrrh 	gstrcpy(q, p, sizeof(buf));
837ff038098Seric 
838ff038098Seric 	/* so is it useful? */
8397d70f5d8Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
8407d70f5d8Seric 	{
841ff038098Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
842f76dd34fSrrh 		gstrcpy(q, SccsPath, sizeof(buf));
843f76dd34fSrrh 		gstrcat(buf, "/s.", sizeof(buf));
844ff038098Seric 
845ff038098Seric 		/* and now the end of the name */
846f76dd34fSrrh 		gstrcat(buf, p, sizeof(buf));
847cdc1aa65Seric 	}
848adf8f7d4Seric 
849ff038098Seric 	/* if i haven't changed it, why did I do all this? */
8500a8b9ab0Seric 	if (strcmp(buf, name) == 0)
8510a8b9ab0Seric 		p = name;
8520a8b9ab0Seric 	else
8530a8b9ab0Seric 	{
854ff038098Seric 		/* but if I have, squirrel it away */
855adf8f7d4Seric 		p = malloc(strlen(buf) + 1);
856adf8f7d4Seric 		if (p == NULL)
857adf8f7d4Seric 		{
858adf8f7d4Seric 			perror("Sccs: no mem");
859adf8f7d4Seric 			exit(EX_OSERR);
860adf8f7d4Seric 		}
861adf8f7d4Seric 		strcpy(p, buf);
8620a8b9ab0Seric 	}
863ff038098Seric 
864adf8f7d4Seric 	return (p);
865adf8f7d4Seric }
8660faf63f9Seric 
8670faf63f9Seric /*
8687d70f5d8Seric **  ISDIR -- return true if the argument is a directory.
8697d70f5d8Seric **
8707d70f5d8Seric **	Parameters:
8717d70f5d8Seric **		name -- the pathname of the file to check.
8727d70f5d8Seric **
8737d70f5d8Seric **	Returns:
8747d70f5d8Seric **		TRUE if 'name' is a directory, FALSE otherwise.
8757d70f5d8Seric **
8767d70f5d8Seric **	Side Effects:
8777d70f5d8Seric **		none.
8787d70f5d8Seric */
8797d70f5d8Seric 
8807d70f5d8Seric bool
isdir(name)8817d70f5d8Seric isdir(name)
8827d70f5d8Seric 	char *name;
8837d70f5d8Seric {
8847d70f5d8Seric 	struct stat stbuf;
8857d70f5d8Seric 
8867d70f5d8Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
8877d70f5d8Seric }
8880faf63f9Seric 
8890faf63f9Seric /*
890cdc1aa65Seric **  SAFEPATH -- determine whether a pathname is "safe"
891cdc1aa65Seric **
892cdc1aa65Seric **	"Safe" pathnames only allow you to get deeper into the
893cdc1aa65Seric **	directory structure, i.e., full pathnames and ".." are
894cdc1aa65Seric **	not allowed.
895cdc1aa65Seric **
896cdc1aa65Seric **	Parameters:
897cdc1aa65Seric **		p -- the name to check.
898cdc1aa65Seric **
899cdc1aa65Seric **	Returns:
900cdc1aa65Seric **		TRUE -- if the path is safe.
901cdc1aa65Seric **		FALSE -- if the path is not safe.
902cdc1aa65Seric **
903cdc1aa65Seric **	Side Effects:
904cdc1aa65Seric **		Prints a message if the path is not safe.
905cdc1aa65Seric */
906cdc1aa65Seric 
907cdc1aa65Seric bool
safepath(p)908cdc1aa65Seric safepath(p)
909cdc1aa65Seric 	register char *p;
910cdc1aa65Seric {
911cdc1aa65Seric 	extern char *index();
912cdc1aa65Seric 
913cdc1aa65Seric 	if (*p != '/')
914cdc1aa65Seric 	{
915cdc1aa65Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
916cdc1aa65Seric 		{
917cdc1aa65Seric 			p = index(p, '/');
918cdc1aa65Seric 			if (p == NULL)
919cdc1aa65Seric 				return (TRUE);
920cdc1aa65Seric 			p++;
921cdc1aa65Seric 		}
922cdc1aa65Seric 	}
923cdc1aa65Seric 
924cdc1aa65Seric 	printf("You may not use full pathnames or \"..\"\n");
925cdc1aa65Seric 	return (FALSE);
926cdc1aa65Seric }
9270faf63f9Seric 
9280faf63f9Seric /*
929b2538d76Seric **  CLEAN -- clean out recreatable files
930b2538d76Seric **
931b2538d76Seric **	Any file for which an "s." file exists but no "p." file
932b2538d76Seric **	exists in the current directory is purged.
933b2538d76Seric **
934b2538d76Seric **	Parameters:
935ec4190ddSeric **		mode -- tells whether this came from a "clean", "info", or
93648ff0e7eSeric **			"check" command.
937ec4190ddSeric **		argv -- the rest of the argument vector.
938b2538d76Seric **
939b2538d76Seric **	Returns:
940b2538d76Seric **		none.
941b2538d76Seric **
942b2538d76Seric **	Side Effects:
94348ff0e7eSeric **		Removes files in the current directory.
94448ff0e7eSeric **		Prints information regarding files being edited.
94548ff0e7eSeric **		Exits if a "check" command.
946b2538d76Seric */
947b2538d76Seric 
clean(mode,argv)948ec4190ddSeric clean(mode, argv)
94948ff0e7eSeric 	int mode;
950ec4190ddSeric 	char **argv;
951b2538d76Seric {
952683375a0Smckusick 	struct direct *dir;
953f76dd34fSrrh 	char buf[FBUFSIZ];
954393e27f2Seric 	char *bufend;
955bb638ad5Smckusick 	register DIR *dirp;
9563a208c77Seric 	register char *basefile;
9572caac8fdSeric 	bool gotedit;
958ec4190ddSeric 	bool gotpfent;
959bf9c1e66Seric 	FILE *pfp;
960ec4190ddSeric 	bool nobranch = FALSE;
961ec4190ddSeric 	extern struct pfile *getpfent();
962ec4190ddSeric 	register struct pfile *pf;
963ec4190ddSeric 	register char **ap;
9641e0924d0Seric 	extern char *username();
9651e0924d0Seric 	char *usernm = NULL;
966393e27f2Seric 	char *subdir = NULL;
967393e27f2Seric 	char *cmdname;
968ec4190ddSeric 
969ec4190ddSeric 	/*
970ec4190ddSeric 	**  Process the argv
971ec4190ddSeric 	*/
972ec4190ddSeric 
973393e27f2Seric 	cmdname = *argv;
974393e27f2Seric 	for (ap = argv; *++ap != NULL; )
975ec4190ddSeric 	{
9761e0924d0Seric 		if (**ap == '-')
9771e0924d0Seric 		{
9781e0924d0Seric 			/* we have a flag */
9791e0924d0Seric 			switch ((*ap)[1])
9801e0924d0Seric 			{
9811e0924d0Seric 			  case 'b':
982ec4190ddSeric 				nobranch = TRUE;
9831e0924d0Seric 				break;
9841e0924d0Seric 
9851e0924d0Seric 			  case 'u':
9861e0924d0Seric 				if ((*ap)[2] != '\0')
9871e0924d0Seric 					usernm = &(*ap)[2];
9881e0924d0Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9891e0924d0Seric 					usernm = *++ap;
9901e0924d0Seric 				else
9911e0924d0Seric 					usernm = username();
9921e0924d0Seric 				break;
9931e0924d0Seric 			}
9941e0924d0Seric 		}
995393e27f2Seric 		else
996393e27f2Seric 		{
997393e27f2Seric 			if (subdir != NULL)
998393e27f2Seric 				usrerr("too many args");
999393e27f2Seric 			else
1000393e27f2Seric 				subdir = *ap;
1001393e27f2Seric 		}
1002ec4190ddSeric 	}
1003b2538d76Seric 
1004ff038098Seric 	/*
1005ff038098Seric 	**  Find and open the SCCS directory.
1006ff038098Seric 	*/
1007ff038098Seric 
1008f76dd34fSrrh 	gstrcpy(buf, SccsDir, sizeof(buf));
1009f77a08fbSeric 	if (buf[0] != '\0')
1010f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
1011393e27f2Seric 	if (subdir != NULL)
1012393e27f2Seric 	{
1013f76dd34fSrrh 		gstrcat(buf, subdir, sizeof(buf));
1014f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
1015393e27f2Seric 	}
1016f76dd34fSrrh 	gstrcat(buf, SccsPath, sizeof(buf));
1017393e27f2Seric 	bufend = &buf[strlen(buf)];
1018ff038098Seric 
1019bb638ad5Smckusick 	dirp = opendir(buf);
1020bb638ad5Smckusick 	if (dirp == NULL)
1021b2538d76Seric 	{
1022f77a08fbSeric 		usrerr("cannot open %s", buf);
102341290c52Seric 		return (EX_NOINPUT);
1024b2538d76Seric 	}
1025b2538d76Seric 
1026b2538d76Seric 	/*
1027b2538d76Seric 	**  Scan the SCCS directory looking for s. files.
1028ff038098Seric 	**	gotedit tells whether we have tried to clean any
1029ff038098Seric 	**		files that are being edited.
1030b2538d76Seric 	*/
1031b2538d76Seric 
10322caac8fdSeric 	gotedit = FALSE;
1033bb638ad5Smckusick 	while (dir = readdir(dirp)) {
1034683375a0Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
1035b2538d76Seric 			continue;
1036b2538d76Seric 
1037b2538d76Seric 		/* got an s. file -- see if the p. file exists */
1038f76dd34fSrrh 		gstrcpy(bufend, "/p.", sizeof(buf));
1039393e27f2Seric 		basefile = bufend + 3;
1040f76dd34fSrrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
1041ec4190ddSeric 
1042ec4190ddSeric 		/*
1043ec4190ddSeric 		**  open and scan the p-file.
1044ec4190ddSeric 		**	'gotpfent' tells if we have found a valid p-file
1045ec4190ddSeric 		**		entry.
1046ec4190ddSeric 		*/
1047ec4190ddSeric 
1048bf9c1e66Seric 		pfp = fopen(buf, "r");
1049ec4190ddSeric 		gotpfent = FALSE;
1050bf9c1e66Seric 		if (pfp != NULL)
10513a208c77Seric 		{
1052ff038098Seric 			/* the file exists -- report it's contents */
1053ec4190ddSeric 			while ((pf = getpfent(pfp)) != NULL)
10542734e237Seric 			{
1055ec4190ddSeric 				if (nobranch && isbranch(pf->p_nsid))
1056ec4190ddSeric 					continue;
10571e0924d0Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10581e0924d0Seric 					continue;
1059ec4190ddSeric 				gotedit = TRUE;
1060ec4190ddSeric 				gotpfent = TRUE;
1061ec4190ddSeric 				if (mode == TELLC)
1062ec4190ddSeric 				{
1063ec4190ddSeric 					printf("%s\n", basefile);
1064ec4190ddSeric 					break;
1065ec4190ddSeric 				}
106677a8a0dfSeric 				printf("%12s: being edited: ", basefile);
106777a8a0dfSeric 				putpfent(pf, stdout);
10682734e237Seric 			}
1069bf9c1e66Seric 			fclose(pfp);
10703a208c77Seric 		}
1071b2538d76Seric 
1072b2538d76Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10739d7ed7d6Seric 		if (mode == CLEANC && !gotpfent)
10743a208c77Seric 		{
1075f76dd34fSrrh 			char	unlinkbuf[FBUFSIZ];
1076f76dd34fSrrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
10770770051cSrrh 			unlink(unlinkbuf);
1078b2538d76Seric 		}
10793a208c77Seric 	}
1080b2538d76Seric 
1081ff038098Seric 	/* cleanup & report results */
1082bb638ad5Smckusick 	closedir(dirp);
108348ff0e7eSeric 	if (!gotedit && mode == INFOC)
10841e0924d0Seric 	{
10851e0924d0Seric 		printf("Nothing being edited");
10861e0924d0Seric 		if (nobranch)
10871e0924d0Seric 			printf(" (on trunk)");
10881e0924d0Seric 		if (usernm == NULL)
10891e0924d0Seric 			printf("\n");
10901e0924d0Seric 		else
10911e0924d0Seric 			printf(" by %s\n", usernm);
10921e0924d0Seric 	}
109348ff0e7eSeric 	if (mode == CHECKC)
109448ff0e7eSeric 		exit(gotedit);
109541290c52Seric 	return (EX_OK);
1096b2538d76Seric }
10970faf63f9Seric 
10980faf63f9Seric /*
1099ec4190ddSeric **  ISBRANCH -- is the SID a branch?
1100ec4190ddSeric **
1101ec4190ddSeric **	Parameters:
1102ec4190ddSeric **		sid -- the sid to check.
1103ec4190ddSeric **
1104ec4190ddSeric **	Returns:
1105ec4190ddSeric **		TRUE if the sid represents a branch.
1106ec4190ddSeric **		FALSE otherwise.
1107ec4190ddSeric **
1108ec4190ddSeric **	Side Effects:
1109ec4190ddSeric **		none.
1110ec4190ddSeric */
1111ec4190ddSeric 
isbranch(sid)1112ec4190ddSeric isbranch(sid)
1113ec4190ddSeric 	char *sid;
1114ec4190ddSeric {
1115ec4190ddSeric 	register char *p;
1116ec4190ddSeric 	int dots;
1117ec4190ddSeric 
1118ec4190ddSeric 	dots = 0;
1119ec4190ddSeric 	for (p = sid; *p != '\0'; p++)
1120ec4190ddSeric 	{
1121ec4190ddSeric 		if (*p == '.')
1122ec4190ddSeric 			dots++;
1123ec4190ddSeric 		if (dots > 1)
1124ec4190ddSeric 			return (TRUE);
1125ec4190ddSeric 	}
1126ec4190ddSeric 	return (FALSE);
1127ec4190ddSeric }
1128ec4190ddSeric 
1129ec4190ddSeric /*
1130e39a5722Seric **  UNEDIT -- unedit a file
1131e39a5722Seric **
1132e39a5722Seric **	Checks to see that the current user is actually editting
1133e39a5722Seric **	the file and arranges that s/he is not editting it.
1134e39a5722Seric **
1135e39a5722Seric **	Parameters:
11362444cd3eSeric **		fn -- the name of the file to be unedited.
1137e39a5722Seric **
1138e39a5722Seric **	Returns:
1139d51cd7e5Seric **		TRUE -- if the file was successfully unedited.
1140d51cd7e5Seric **		FALSE -- if the file was not unedited for some
1141d51cd7e5Seric **			reason.
1142e39a5722Seric **
1143e39a5722Seric **	Side Effects:
1144e39a5722Seric **		fn is removed
1145e39a5722Seric **		entries are removed from pfile.
1146e39a5722Seric */
1147e39a5722Seric 
1148d51cd7e5Seric bool
unedit(fn)1149e39a5722Seric unedit(fn)
1150e39a5722Seric 	char *fn;
1151e39a5722Seric {
1152e39a5722Seric 	register FILE *pfp;
11538e9f28e0Ssam 	char *cp, *pfn;
11547a8071bfSbostic 	static char tfn[] = _PATH_TMP;
1155e39a5722Seric 	FILE *tfp;
1156e39a5722Seric 	register char *q;
1157e39a5722Seric 	bool delete = FALSE;
1158e39a5722Seric 	bool others = FALSE;
1159e39a5722Seric 	char *myname;
11601e0924d0Seric 	extern char *username();
1161e39a5722Seric 	struct pfile *pent;
1162ec4190ddSeric 	extern struct pfile *getpfent();
1163f76dd34fSrrh 	char buf[PFILELG];
11640da7a6f3Sbostic 	extern char *makefile(), *rindex(), *tail();
1165e39a5722Seric 
1166e39a5722Seric 	/* make "s." filename & find the trailing component */
1167e39a5722Seric 	pfn = makefile(fn);
1168cdc1aa65Seric 	if (pfn == NULL)
1169cdc1aa65Seric 		return (FALSE);
1170cdc1aa65Seric 	q = rindex(pfn, '/');
1171cdc1aa65Seric 	if (q == NULL)
1172cdc1aa65Seric 		q = &pfn[-1];
1173cdc1aa65Seric 	if (q[1] != 's' || q[2] != '.')
1174e39a5722Seric 	{
1175095915d3Seric 		usrerr("bad file name \"%s\"", fn);
1176d51cd7e5Seric 		return (FALSE);
1177e39a5722Seric 	}
1178e39a5722Seric 
1179ff038098Seric 	/* turn "s." into "p." & try to open it */
1180e39a5722Seric 	*++q = 'p';
1181e39a5722Seric 
1182e39a5722Seric 	pfp = fopen(pfn, "r");
1183e39a5722Seric 	if (pfp == NULL)
1184e39a5722Seric 	{
11852444cd3eSeric 		printf("%12s: not being edited\n", fn);
1186d51cd7e5Seric 		return (FALSE);
1187e39a5722Seric 	}
1188e39a5722Seric 
1189ff038098Seric 	/* create temp file for editing p-file */
1190e39a5722Seric 	mktemp(tfn);
1191e39a5722Seric 	tfp = fopen(tfn, "w");
1192e39a5722Seric 	if (tfp == NULL)
1193e39a5722Seric 	{
1194095915d3Seric 		usrerr("cannot create \"%s\"", tfn);
1195e39a5722Seric 		exit(EX_OSERR);
1196e39a5722Seric 	}
1197e39a5722Seric 
1198ff038098Seric 	/* figure out who I am */
11991e0924d0Seric 	myname = username();
1200ff038098Seric 
1201ff038098Seric 	/*
1202ff038098Seric 	**  Copy p-file to temp file, doing deletions as needed.
1203ff038098Seric 	*/
1204ff038098Seric 
1205ec4190ddSeric 	while ((pent = getpfent(pfp)) != NULL)
1206e39a5722Seric 	{
1207e39a5722Seric 		if (strcmp(pent->p_user, myname) == 0)
1208e39a5722Seric 		{
1209e39a5722Seric 			/* a match */
1210e39a5722Seric 			delete++;
1211e39a5722Seric 		}
1212e39a5722Seric 		else
1213e39a5722Seric 		{
1214ff038098Seric 			/* output it again */
121577a8a0dfSeric 			putpfent(pent, tfp);
1216e39a5722Seric 			others++;
1217e39a5722Seric 		}
1218e39a5722Seric 	}
1219e39a5722Seric 
12208e9f28e0Ssam 	/*
12218e9f28e0Ssam 	 * Before changing anything, make sure we can remove
12228e9f28e0Ssam 	 * the file in question (assuming it exists).
12238e9f28e0Ssam 	 */
12248e9f28e0Ssam 	if (delete) {
12258e9f28e0Ssam 		extern int errno;
12268e9f28e0Ssam 
12278e9f28e0Ssam 		cp = tail(fn);
12288e9f28e0Ssam 		errno = 0;
12298e9f28e0Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
12308e9f28e0Ssam 			goto bad;
12318e9f28e0Ssam 		if (errno == 0)
12328e9f28e0Ssam 			/*
12338e9f28e0Ssam 			 * This is wrong, but the rest of the program
12348e9f28e0Ssam 			 * has built in assumptions about "." as well,
12358e9f28e0Ssam 			 * so why make unedit a special case?
12368e9f28e0Ssam 			 */
12378e9f28e0Ssam 			if (access(".", 2) < 0) {
12388e9f28e0Ssam 	bad:
12398e9f28e0Ssam 				printf("%12s: can't remove\n", cp);
12408e9f28e0Ssam 				fclose(tfp);
12418e9f28e0Ssam 				fclose(pfp);
12428e9f28e0Ssam 				unlink(tfn);
12438e9f28e0Ssam 				return (FALSE);
12448e9f28e0Ssam 			}
12458e9f28e0Ssam 	}
1246e39a5722Seric 	/* do final cleanup */
1247e39a5722Seric 	if (others)
1248e39a5722Seric 	{
1249ff038098Seric 		/* copy it back (perhaps it should be linked?) */
1250e39a5722Seric 		if (freopen(tfn, "r", tfp) == NULL)
1251e39a5722Seric 		{
1252095915d3Seric 			syserr("cannot reopen \"%s\"", tfn);
1253e39a5722Seric 			exit(EX_OSERR);
1254e39a5722Seric 		}
1255e39a5722Seric 		if (freopen(pfn, "w", pfp) == NULL)
1256e39a5722Seric 		{
1257095915d3Seric 			usrerr("cannot create \"%s\"", pfn);
1258d51cd7e5Seric 			return (FALSE);
1259e39a5722Seric 		}
1260e39a5722Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1261e39a5722Seric 			fputs(buf, pfp);
1262e39a5722Seric 	}
1263e39a5722Seric 	else
1264e39a5722Seric 	{
1265ff038098Seric 		/* it's empty -- remove it */
1266e39a5722Seric 		unlink(pfn);
1267e39a5722Seric 	}
1268e39a5722Seric 	fclose(tfp);
1269e39a5722Seric 	fclose(pfp);
1270e39a5722Seric 	unlink(tfn);
1271e39a5722Seric 
1272ff038098Seric 	/* actually remove the g-file */
1273e39a5722Seric 	if (delete)
1274e39a5722Seric 	{
12758e9f28e0Ssam 		/*
12768e9f28e0Ssam 		 * Since we've checked above, we can
12778e9f28e0Ssam 		 * use the return from unlink to
12788e9f28e0Ssam 		 * determine if the file existed or not.
12798e9f28e0Ssam 		 */
12808e9f28e0Ssam 		if (unlink(cp) >= 0)
12818e9f28e0Ssam 			printf("%12s: removed\n", cp);
1282d51cd7e5Seric 		return (TRUE);
1283e39a5722Seric 	}
1284e39a5722Seric 	else
1285e39a5722Seric 	{
12862444cd3eSeric 		printf("%12s: not being edited by you\n", fn);
1287d51cd7e5Seric 		return (FALSE);
1288e39a5722Seric 	}
1289e39a5722Seric }
12900faf63f9Seric 
12910faf63f9Seric /*
129278d41c6aSeric **  DODIFF -- diff an s-file against a g-file
129378d41c6aSeric **
129478d41c6aSeric **	Parameters:
129578d41c6aSeric **		getv -- argv for the 'get' command.
129678d41c6aSeric **		gfile -- name of the g-file to diff against.
129778d41c6aSeric **
129878d41c6aSeric **	Returns:
129978d41c6aSeric **		Result of get.
130078d41c6aSeric **
130178d41c6aSeric **	Side Effects:
130278d41c6aSeric **		none.
130378d41c6aSeric */
130478d41c6aSeric 
dodiff(getv,gfile)130578d41c6aSeric dodiff(getv, gfile)
130678d41c6aSeric 	char **getv;
130778d41c6aSeric 	char *gfile;
130878d41c6aSeric {
130978d41c6aSeric 	int pipev[2];
131078d41c6aSeric 	int rval;
131178d41c6aSeric 	register int i;
131278d41c6aSeric 	register int pid;
131378d41c6aSeric 	auto int st;
131478d41c6aSeric 	extern int errno;
13159d228ba4Sbostic 	sig_t osig;
131678d41c6aSeric 
1317cc4e4f0dSeric 	printf("\n------- %s -------\n", gfile);
13182569d37aSeric 	fflush(stdout);
1319cc4e4f0dSeric 
1320ff038098Seric 	/* create context for diff to run in */
132178d41c6aSeric 	if (pipe(pipev) < 0)
132278d41c6aSeric 	{
132378d41c6aSeric 		syserr("dodiff: pipe failed");
132478d41c6aSeric 		exit(EX_OSERR);
132578d41c6aSeric 	}
132678d41c6aSeric 	if ((pid = fork()) < 0)
132778d41c6aSeric 	{
132878d41c6aSeric 		syserr("dodiff: fork failed");
132978d41c6aSeric 		exit(EX_OSERR);
133078d41c6aSeric 	}
133178d41c6aSeric 	else if (pid > 0)
133278d41c6aSeric 	{
133378d41c6aSeric 		/* in parent; run get */
133478d41c6aSeric 		OutFile = pipev[1];
133578d41c6aSeric 		close(pipev[0]);
13362569d37aSeric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
133778d41c6aSeric 		osig = signal(SIGINT, SIG_IGN);
133878d41c6aSeric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
133978d41c6aSeric 			errno = 0;
134078d41c6aSeric 		signal(SIGINT, osig);
134178d41c6aSeric 		/* ignore result of diff */
134278d41c6aSeric 	}
134378d41c6aSeric 	else
134478d41c6aSeric 	{
134578d41c6aSeric 		/* in child, run diff */
134678d41c6aSeric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
134778d41c6aSeric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
134878d41c6aSeric 		{
134978d41c6aSeric 			syserr("dodiff: magic failed");
135078d41c6aSeric 			exit(EX_OSERR);
135178d41c6aSeric 		}
13522569d37aSeric 		command(&getv[1], FALSE, "-diff:elsfhbC");
135378d41c6aSeric 	}
135478d41c6aSeric 	return (rval);
135578d41c6aSeric }
135678d41c6aSeric 
135778d41c6aSeric /*
1358287bbaa2Seric **  TAIL -- return tail of filename.
1359287bbaa2Seric **
1360287bbaa2Seric **	Parameters:
1361287bbaa2Seric **		fn -- the filename.
1362287bbaa2Seric **
1363287bbaa2Seric **	Returns:
1364287bbaa2Seric **		a pointer to the tail of the filename; e.g., given
1365287bbaa2Seric **		"cmd/ls.c", "ls.c" is returned.
1366287bbaa2Seric **
1367287bbaa2Seric **	Side Effects:
1368287bbaa2Seric **		none.
1369287bbaa2Seric */
1370287bbaa2Seric 
1371287bbaa2Seric char *
tail(fn)1372287bbaa2Seric tail(fn)
1373287bbaa2Seric 	register char *fn;
1374287bbaa2Seric {
1375287bbaa2Seric 	register char *p;
1376287bbaa2Seric 
1377287bbaa2Seric 	for (p = fn; *p != 0; p++)
1378287bbaa2Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
1379287bbaa2Seric 			fn = &p[1];
1380287bbaa2Seric 	return (fn);
1381287bbaa2Seric }
1382287bbaa2Seric 
1383287bbaa2Seric /*
1384ec4190ddSeric **  GETPFENT -- get an entry from the p-file
1385e39a5722Seric **
1386e39a5722Seric **	Parameters:
1387e39a5722Seric **		pfp -- p-file file pointer
1388e39a5722Seric **
1389e39a5722Seric **	Returns:
1390e39a5722Seric **		pointer to p-file struct for next entry
1391e39a5722Seric **		NULL on EOF or error
1392e39a5722Seric **
1393e39a5722Seric **	Side Effects:
1394e39a5722Seric **		Each call wipes out results of previous call.
1395e39a5722Seric */
1396e39a5722Seric 
1397e39a5722Seric struct pfile *
getpfent(pfp)1398ec4190ddSeric getpfent(pfp)
1399e39a5722Seric 	FILE *pfp;
1400e39a5722Seric {
1401e39a5722Seric 	static struct pfile ent;
1402f76dd34fSrrh 	static char buf[PFILELG];
1403e39a5722Seric 	register char *p;
1404e39a5722Seric 	extern char *nextfield();
1405e39a5722Seric 
1406e39a5722Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1407e39a5722Seric 		return (NULL);
1408e39a5722Seric 
1409e39a5722Seric 	ent.p_osid = p = buf;
1410e39a5722Seric 	ent.p_nsid = p = nextfield(p);
1411e39a5722Seric 	ent.p_user = p = nextfield(p);
1412e39a5722Seric 	ent.p_date = p = nextfield(p);
1413e39a5722Seric 	ent.p_time = p = nextfield(p);
141477a8a0dfSeric 	ent.p_aux = p = nextfield(p);
1415e39a5722Seric 
1416e39a5722Seric 	return (&ent);
1417e39a5722Seric }
1418e39a5722Seric 
1419e39a5722Seric 
1420e39a5722Seric char *
nextfield(p)1421e39a5722Seric nextfield(p)
1422e39a5722Seric 	register char *p;
1423e39a5722Seric {
1424e39a5722Seric 	if (p == NULL || *p == '\0')
1425e39a5722Seric 		return (NULL);
1426e39a5722Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1427e39a5722Seric 		p++;
1428e39a5722Seric 	if (*p == '\n' || *p == '\0')
1429e39a5722Seric 	{
1430e39a5722Seric 		*p = '\0';
1431e39a5722Seric 		return (NULL);
1432e39a5722Seric 	}
1433e39a5722Seric 	*p++ = '\0';
1434e39a5722Seric 	return (p);
1435e39a5722Seric }
143677a8a0dfSeric /*
143777a8a0dfSeric **  PUTPFENT -- output a p-file entry to a file
143877a8a0dfSeric **
143977a8a0dfSeric **	Parameters:
144077a8a0dfSeric **		pf -- the p-file entry
144177a8a0dfSeric **		f -- the file to put it on.
144277a8a0dfSeric **
144377a8a0dfSeric **	Returns:
144477a8a0dfSeric **		none.
144577a8a0dfSeric **
144677a8a0dfSeric **	Side Effects:
144777a8a0dfSeric **		pf is written onto file f.
144877a8a0dfSeric */
144977a8a0dfSeric 
putpfent(pf,f)145077a8a0dfSeric putpfent(pf, f)
145177a8a0dfSeric 	register struct pfile *pf;
145277a8a0dfSeric 	register FILE *f;
145377a8a0dfSeric {
145477a8a0dfSeric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
145577a8a0dfSeric 		pf->p_user, pf->p_date, pf->p_time);
145677a8a0dfSeric 	if (pf->p_aux != NULL)
145777a8a0dfSeric 		fprintf(f, " %s", pf->p_aux);
145877a8a0dfSeric 	else
145977a8a0dfSeric 		fprintf(f, "\n");
146077a8a0dfSeric }
14610faf63f9Seric 
14620faf63f9Seric /*
1463095915d3Seric **  USRERR -- issue user-level error
1464095915d3Seric **
1465095915d3Seric **	Parameters:
1466095915d3Seric **		f -- format string.
1467095915d3Seric **		p1-p3 -- parameters to a printf.
1468095915d3Seric **
1469095915d3Seric **	Returns:
1470095915d3Seric **		-1
1471095915d3Seric **
1472095915d3Seric **	Side Effects:
1473095915d3Seric **		none.
1474095915d3Seric */
1475095915d3Seric 
1476fe7b004aSeric /*VARARGS1*/
usrerr(f,p1,p2,p3)1477095915d3Seric usrerr(f, p1, p2, p3)
1478095915d3Seric 	char *f;
1479095915d3Seric {
1480095915d3Seric 	fprintf(stderr, "\n%s: ", MyName);
1481095915d3Seric 	fprintf(stderr, f, p1, p2, p3);
1482095915d3Seric 	fprintf(stderr, "\n");
1483095915d3Seric 
1484095915d3Seric 	return (-1);
1485095915d3Seric }
14860faf63f9Seric 
14870faf63f9Seric /*
1488095915d3Seric **  SYSERR -- print system-generated error.
1489095915d3Seric **
1490095915d3Seric **	Parameters:
1491095915d3Seric **		f -- format string to a printf.
1492095915d3Seric **		p1, p2, p3 -- parameters to f.
1493095915d3Seric **
1494095915d3Seric **	Returns:
1495095915d3Seric **		never.
1496095915d3Seric **
1497095915d3Seric **	Side Effects:
1498095915d3Seric **		none.
1499095915d3Seric */
1500095915d3Seric 
1501fe7b004aSeric /*VARARGS1*/
syserr(f,p1,p2,p3)1502095915d3Seric syserr(f, p1, p2, p3)
1503095915d3Seric 	char *f;
1504095915d3Seric {
1505095915d3Seric 	extern int errno;
1506095915d3Seric 
1507095915d3Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
1508095915d3Seric 	fprintf(stderr, f, p1, p2, p3);
1509095915d3Seric 	fprintf(stderr, "\n");
1510095915d3Seric 	if (errno == 0)
1511095915d3Seric 		exit(EX_SOFTWARE);
1512095915d3Seric 	else
1513095915d3Seric 	{
1514fe7b004aSeric 		perror(NULL);
1515095915d3Seric 		exit(EX_OSERR);
1516095915d3Seric 	}
1517095915d3Seric }
15181e0924d0Seric /*
15191e0924d0Seric **  USERNAME -- return name of the current user
15201e0924d0Seric **
15211e0924d0Seric **	Parameters:
15221e0924d0Seric **		none
15231e0924d0Seric **
15241e0924d0Seric **	Returns:
15251e0924d0Seric **		name of current user
15261e0924d0Seric **
15271e0924d0Seric **	Side Effects:
15281e0924d0Seric **		none
15291e0924d0Seric */
15301e0924d0Seric 
15311e0924d0Seric char *
username()15321e0924d0Seric username()
15331e0924d0Seric {
15341e0924d0Seric # ifdef UIDUSER
15351e0924d0Seric 	extern struct passwd *getpwuid();
15361e0924d0Seric 	register struct passwd *pw;
15371e0924d0Seric 
15381e0924d0Seric 	pw = getpwuid(getuid());
15391e0924d0Seric 	if (pw == NULL)
15401e0924d0Seric 	{
15411e0924d0Seric 		syserr("who are you? (uid=%d)", getuid());
15421e0924d0Seric 		exit(EX_OSERR);
15431e0924d0Seric 	}
15441e0924d0Seric 	return (pw->pw_name);
15451e0924d0Seric # else
1546ed320446Seric 	extern char *getlogin();
1547bb5509cbSmckusick 	register char *p;
1548ed320446Seric 
1549bb5509cbSmckusick 	p = getenv("USER");
1550bb5509cbSmckusick 	if (p == NULL || p[0] == '\0')
1551bb5509cbSmckusick 		p = getlogin();
1552bb5509cbSmckusick 	return (p);
15531e0924d0Seric # endif UIDUSER
15541e0924d0Seric }
1555f76dd34fSrrh 
1556f76dd34fSrrh /*
1557f76dd34fSrrh **	Guarded string manipulation routines; the last argument
1558f76dd34fSrrh **	is the length of the buffer into which the strcpy or strcat
1559f76dd34fSrrh **	is to be done.
1560f76dd34fSrrh */
gstrcat(to,from,length)1561f76dd34fSrrh char *gstrcat(to, from, length)
1562f76dd34fSrrh 	char	*to, *from;
1563f76dd34fSrrh 	int	length;
1564f76dd34fSrrh {
1565f76dd34fSrrh 	if (strlen(from) + strlen(to) >= length) {
1566f76dd34fSrrh 		gstrbotch(to, from);
1567f76dd34fSrrh 	}
1568f76dd34fSrrh 	return(strcat(to, from));
1569f76dd34fSrrh }
1570f76dd34fSrrh 
gstrncat(to,from,n,length)1571f76dd34fSrrh char *gstrncat(to, from, n, length)
1572f76dd34fSrrh 	char	*to, *from;
1573f76dd34fSrrh 	int	n;
1574f76dd34fSrrh 	int	length;
1575f76dd34fSrrh {
1576f76dd34fSrrh 	if (n + strlen(to) >= length) {
1577f76dd34fSrrh 		gstrbotch(to, from);
1578f76dd34fSrrh 	}
1579f76dd34fSrrh 	return(strncat(to, from, n));
1580f76dd34fSrrh }
1581f76dd34fSrrh 
gstrcpy(to,from,length)1582f76dd34fSrrh char *gstrcpy(to, from, length)
1583f76dd34fSrrh 	char	*to, *from;
1584f76dd34fSrrh 	int	length;
1585f76dd34fSrrh {
1586f76dd34fSrrh 	if (strlen(from) >= length) {
1587f76dd34fSrrh 		gstrbotch(from, (char *)0);
1588f76dd34fSrrh 	}
1589f76dd34fSrrh 	return(strcpy(to, from));
1590f76dd34fSrrh }
gstrbotch(str1,str2)1591f76dd34fSrrh gstrbotch(str1, str2)
1592f76dd34fSrrh 	char	*str1, *str2;
1593f76dd34fSrrh {
1594f76dd34fSrrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
1595f76dd34fSrrh }
1596