xref: /original-bsd/usr.bin/sccs/sccs.c (revision aa05f8e7)
13bbbf6e6Sdist /*
23bbbf6e6Sdist  * Copyright (c) 1980 Regents of the University of California.
368dc9f68Sbostic  * All rights reserved.
468dc9f68Sbostic  *
5*aa05f8e7Sbostic  * %sccs.include.redist.c%
63bbbf6e6Sdist  */
73bbbf6e6Sdist 
86d5ea7f2Ssam #ifndef lint
93bbbf6e6Sdist char copyright[] =
103bbbf6e6Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
113bbbf6e6Sdist  All rights reserved.\n";
1268dc9f68Sbostic #endif /* not lint */
133bbbf6e6Sdist 
143bbbf6e6Sdist #ifndef lint
15*aa05f8e7Sbostic static char sccsid[] = "@(#)sccs.c	5.9 (Berkeley) 06/01/90";
1668dc9f68Sbostic #endif /* not lint */
176d5ea7f2Ssam 
18adf8f7d4Seric # include <stdio.h>
19683375a0Smckusick # include <sys/param.h>
20adf8f7d4Seric # include <sys/stat.h>
216d5ea7f2Ssam # include <sys/dir.h>
2278d41c6aSeric # include <errno.h>
2378d41c6aSeric # include <signal.h>
24adf8f7d4Seric # include <sysexits.h>
25393e27f2Seric # include <pwd.h>
267a8071bfSbostic # include "pathnames.h"
27adf8f7d4Seric 
2810cd16efSeric /*
2910cd16efSeric **  SCCS.C -- human-oriented front end to the SCCS system.
3010cd16efSeric **
3110cd16efSeric **	Without trying to add any functionality to speak of, this
3210cd16efSeric **	program tries to make SCCS a little more accessible to human
3310cd16efSeric **	types.  The main thing it does is automatically put the
3410cd16efSeric **	string "SCCS/s." on the front of names.  Also, it has a
3510cd16efSeric **	couple of things that are designed to shorten frequent
3610cd16efSeric **	combinations, e.g., "delget" which expands to a "delta"
3710cd16efSeric **	and a "get".
3810cd16efSeric **
3910cd16efSeric **	This program can also function as a setuid front end.
4010cd16efSeric **	To do this, you should copy the source, renaming it to
4110cd16efSeric **	whatever you want, e.g., "syssccs".  Change any defaults
4210cd16efSeric **	in the program (e.g., syssccs might default -d to
4310cd16efSeric **	"/usr/src/sys").  Then recompile and put the result
4410cd16efSeric **	as setuid to whomever you want.  In this mode, sccs
4510cd16efSeric **	knows to not run setuid for certain programs in order
4610cd16efSeric **	to preserve security, and so forth.
4710cd16efSeric **
4810cd16efSeric **	Usage:
4910cd16efSeric **		sccs [flags] command [args]
5010cd16efSeric **
5110cd16efSeric **	Flags:
5210cd16efSeric **		-d<dir>		<dir> represents a directory to search
5310cd16efSeric **				out of.  It should be a full pathname
5410cd16efSeric **				for general usage.  E.g., if <dir> is
5510cd16efSeric **				"/usr/src/sys", then a reference to the
5610cd16efSeric **				file "dev/bio.c" becomes a reference to
5710cd16efSeric **				"/usr/src/sys/dev/bio.c".
5810cd16efSeric **		-p<path>	prepends <path> to the final component
5910cd16efSeric **				of the pathname.  By default, this is
6010cd16efSeric **				"SCCS".  For example, in the -d example
6110cd16efSeric **				above, the path then gets modified to
6210cd16efSeric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
6310cd16efSeric **				more common usage (without the -d flag),
6410cd16efSeric **				"prog.c" would get modified to
6510cd16efSeric **				"SCCS/s.prog.c".  In both cases, the
6610cd16efSeric **				"s." gets automatically prepended.
6710cd16efSeric **		-r		run as the real user.
6810cd16efSeric **
6910cd16efSeric **	Commands:
7010cd16efSeric **		admin,
7110cd16efSeric **		get,
7210cd16efSeric **		delta,
7310cd16efSeric **		rmdel,
746e36cbf6Sbostic **		cdc,
7510cd16efSeric **		etc.		Straight out of SCCS; only difference
7610cd16efSeric **				is that pathnames get modified as
7710cd16efSeric **				described above.
786e36cbf6Sbostic **		enter		Front end doing "sccs admin -i<name> <name>"
796e36cbf6Sbostic **		create		Macro for "enter" followed by "get".
8010cd16efSeric **		edit		Macro for "get -e".
8110cd16efSeric **		unedit		Removes a file being edited, knowing
8210cd16efSeric **				about p-files, etc.
8310cd16efSeric **		delget		Macro for "delta" followed by "get".
8410cd16efSeric **		deledit		Macro for "delta" followed by "get -e".
856e36cbf6Sbostic **		branch		Macro for "get -b -e", followed by "delta
866e36cbf6Sbostic **				-s -n", followd by "get -e -t -g".
876e36cbf6Sbostic **		diffs		"diff" the specified version of files
886e36cbf6Sbostic **				and the checked-out version.
896e36cbf6Sbostic **		print		Macro for "prs -e" followed by "get -p -m".
906e36cbf6Sbostic **		tell		List what files are being edited.
916e36cbf6Sbostic **		info		Print information about files being edited.
9210cd16efSeric **		clean		Remove all files that can be
9310cd16efSeric **				regenerated from SCCS files.
94095915d3Seric **		check		Like info, but return exit status, for
9510cd16efSeric **				use in makefiles.
9610cd16efSeric **		fix		Remove a top delta & reedit, but save
9710cd16efSeric **				the previous changes in that delta.
9810cd16efSeric **
9910cd16efSeric **	Compilation Flags:
10010cd16efSeric **		UIDUSER -- determine who the user is by looking at the
10110cd16efSeric **			uid rather than the login name -- for machines
10210cd16efSeric **			where SCCS gets the user in this way.
1037abec564Seric **		SCCSDIR -- if defined, forces the -d flag to take on
104095915d3Seric **			this value.  This is so that the setuid
105095915d3Seric **			aspects of this program cannot be abused.
1067abec564Seric **			This flag also disables the -p flag.
1077abec564Seric **		SCCSPATH -- the default for the -p flag.
108c4ad8825Seric **		MYNAME -- the title this program should print when it
109c4ad8825Seric **			gives error messages.
11010cd16efSeric **
11110cd16efSeric **	Compilation Instructions:
11210cd16efSeric **		cc -O -n -s sccs.c
113c4ad8825Seric **		The flags listed above can be -D defined to simplify
114c4ad8825Seric **			recompilation for variant versions.
11510cd16efSeric **
11610cd16efSeric **	Author:
11710cd16efSeric **		Eric Allman, UCB/INGRES
1187abec564Seric **		Copyright 1980 Regents of the University of California
11910cd16efSeric */
12010cd16efSeric 
1210faf63f9Seric 
1227abec564Seric /*******************  Configuration Information  ********************/
1237abec564Seric 
124c4ad8825Seric # ifndef SCCSPATH
125c4ad8825Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
126c4ad8825Seric # endif NOT SCCSPATH
127c4ad8825Seric 
128c4ad8825Seric # ifndef MYNAME
129c4ad8825Seric # define MYNAME		"sccs"	/* name used for printing errors */
130c4ad8825Seric # endif NOT MYNAME
1317abec564Seric 
1327abec564Seric /****************  End of Configuration Information  ****************/
1330faf63f9Seric 
134c4432be4Seric typedef char	bool;
1357de81dc7Seric # define TRUE	1
1367de81dc7Seric # define FALSE	0
137d02a4f42Seric 
138ff038098Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
139ff038098Seric 
140adf8f7d4Seric struct sccsprog
141adf8f7d4Seric {
142adf8f7d4Seric 	char	*sccsname;	/* name of SCCS routine */
1437de81dc7Seric 	short	sccsoper;	/* opcode, see below */
1447de81dc7Seric 	short	sccsflags;	/* flags, see below */
145adf8f7d4Seric 	char	*sccspath;	/* pathname of binary implementing */
146adf8f7d4Seric };
147adf8f7d4Seric 
1487de81dc7Seric /* values for sccsoper */
1497de81dc7Seric # define PROG		0	/* call a program */
1501777fbcbSeric # define CMACRO		1	/* command substitution macro */
151172147faSeric # define FIX		2	/* fix a delta */
152b2538d76Seric # define CLEAN		3	/* clean out recreatable files */
153e39a5722Seric # define UNEDIT		4	/* unedit a file */
154419708b0Seric # define SHELL		5	/* call a shell file (like PROG) */
15578d41c6aSeric # define DIFFS		6	/* diff between sccs & file out */
1562569d37aSeric # define DODIFF		7	/* internal call to diff program */
15744c626a5Srrh # define ENTER		8	/* enter new files */
1587de81dc7Seric 
159c4432be4Seric /* bits for sccsflags */
1607de81dc7Seric # define NO_SDOT	0001	/* no s. on front of args */
1617de81dc7Seric # define REALUSER	0002	/* protected (e.g., admin) */
162adf8f7d4Seric 
16348ff0e7eSeric /* modes for the "clean", "info", "check" ops */
16448ff0e7eSeric # define CLEANC		0	/* clean command */
16548ff0e7eSeric # define INFOC		1	/* info command */
16648ff0e7eSeric # define CHECKC		2	/* check command */
1672734e237Seric # define TELLC		3	/* give list of files being edited */
16848ff0e7eSeric 
1690faf63f9Seric /*
1700faf63f9Seric **  Description of commands known to this program.
1710faf63f9Seric **	First argument puts the command into a class.  Second arg is
1720faf63f9Seric **	info regarding treatment of this command.  Third arg is a
1730faf63f9Seric **	list of flags this command accepts from macros, etc.  Fourth
1740faf63f9Seric **	arg is the pathname of the implementing program, or the
1750faf63f9Seric **	macro definition, or the arg to a sub-algorithm.
1760faf63f9Seric */
177b5d4f080Seric 
178adf8f7d4Seric struct sccsprog SccsProg[] =
179adf8f7d4Seric {
1803642a8d2Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1816e36cbf6Sbostic 	"cdc",		PROG,	0,			PROGPATH(rmdel),
1823642a8d2Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1833642a8d2Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1843642a8d2Seric 	"get",		PROG,	0,			PROGPATH(get),
1853642a8d2Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1863a3bf7bfSeric 	"prs",		PROG,	0,			PROGPATH(prs),
1873642a8d2Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1883642a8d2Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1893a3bf7bfSeric 	"val",		PROG,	0,			PROGPATH(val),
1903642a8d2Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1913642a8d2Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1923642a8d2Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
19359e81a43Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1941cf60de3Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1953642a8d2Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1961e0924d0Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1971e0924d0Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1981e0924d0Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1991e0924d0Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
2003642a8d2Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
2013642a8d2Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
2022569d37aSeric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
2036e36cbf6Sbostic 	"print",	CMACRO,	0,			"prs -e/get -p -m -s",
204a4f81e73Seric 	"branch",	CMACRO,	NO_SDOT,
205a4f81e73Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
20644c626a5Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
20744c626a5Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
2083642a8d2Seric 	NULL,		-1,	0,			NULL
209adf8f7d4Seric };
210adf8f7d4Seric 
2110faf63f9Seric /* one line from a p-file */
212e39a5722Seric struct pfile
213e39a5722Seric {
214e39a5722Seric 	char	*p_osid;	/* old SID */
215e39a5722Seric 	char	*p_nsid;	/* new SID */
216e39a5722Seric 	char	*p_user;	/* user who did edit */
217e39a5722Seric 	char	*p_date;	/* date of get */
218e39a5722Seric 	char	*p_time;	/* time of get */
21977a8a0dfSeric 	char	*p_aux;		/* extra info at end */
220e39a5722Seric };
221e39a5722Seric 
2227abec564Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2237abec564Seric # ifdef SCCSDIR
2247abec564Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
225095915d3Seric # else
2267abec564Seric char	*SccsDir = "";
227095915d3Seric # endif
228c4ad8825Seric char	MyName[] = MYNAME;	/* name used in messages */
22978d41c6aSeric int	OutFile = -1;		/* override output file for commands */
230c4432be4Seric bool	RealUser;		/* if set, running as real user */
231f0cc3246Seric # ifdef DEBUG
232f0cc3246Seric bool	Debug;			/* turn on tracing */
233f0cc3246Seric # endif
23492fabd68Seric # ifndef V6
23592fabd68Seric extern char	*getenv();
23692fabd68Seric # endif V6
237f76dd34fSrrh 
2386e36cbf6Sbostic extern char	*sys_siglist[];
2396e36cbf6Sbostic 
240f76dd34fSrrh char *gstrcat(), *strcat();
241f76dd34fSrrh char *gstrncat(), *strncat();
242f76dd34fSrrh char *gstrcpy(), *strcpy();
243f76dd34fSrrh #define	FBUFSIZ	BUFSIZ
244f76dd34fSrrh #define	PFILELG	120
2450faf63f9Seric 
246adf8f7d4Seric main(argc, argv)
247adf8f7d4Seric 	int argc;
248adf8f7d4Seric 	char **argv;
249adf8f7d4Seric {
250adf8f7d4Seric 	register char *p;
251b1ed8a43Seric 	extern struct sccsprog *lookup();
25241290c52Seric 	register int i;
25392fabd68Seric # ifndef V6
25492fabd68Seric # ifndef SCCSDIR
255393e27f2Seric 	register struct passwd *pw;
256393e27f2Seric 	extern struct passwd *getpwnam();
257f76dd34fSrrh 	char buf[FBUFSIZ];
258393e27f2Seric 
25992fabd68Seric 	/* pull "SccsDir" out of the environment (possibly) */
260250566abSeric 	p = getenv("PROJECTDIR");
261393e27f2Seric 	if (p != NULL && p[0] != '\0')
262393e27f2Seric 	{
26392fabd68Seric 		if (p[0] == '/')
26492fabd68Seric 			SccsDir = p;
26592fabd68Seric 		else
266393e27f2Seric 		{
267393e27f2Seric 			pw = getpwnam(p);
268393e27f2Seric 			if (pw == NULL)
269393e27f2Seric 			{
270393e27f2Seric 				usrerr("user %s does not exist", p);
271393e27f2Seric 				exit(EX_USAGE);
272393e27f2Seric 			}
273f76dd34fSrrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
274f76dd34fSrrh 			gstrcat(buf, "/src", sizeof(buf));
275393e27f2Seric 			if (access(buf, 0) < 0)
276393e27f2Seric 			{
277f76dd34fSrrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
278f76dd34fSrrh 				gstrcat(buf, "/source", sizeof(buf));
279393e27f2Seric 				if (access(buf, 0) < 0)
280393e27f2Seric 				{
281393e27f2Seric 					usrerr("project %s has no source!", p);
282393e27f2Seric 					exit(EX_USAGE);
283393e27f2Seric 				}
284393e27f2Seric 			}
285393e27f2Seric 			SccsDir = buf;
286393e27f2Seric 		}
287393e27f2Seric 	}
28892fabd68Seric # endif SCCSDIR
28992fabd68Seric # endif V6
29092fabd68Seric 
291adf8f7d4Seric 	/*
292adf8f7d4Seric 	**  Detect and decode flags intended for this program.
293adf8f7d4Seric 	*/
294adf8f7d4Seric 
2957de81dc7Seric 	if (argc < 2)
296adf8f7d4Seric 	{
297095915d3Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
2987de81dc7Seric 		exit(EX_USAGE);
2997de81dc7Seric 	}
3007de81dc7Seric 	argv[argc] = NULL;
3017de81dc7Seric 
302b1ed8a43Seric 	if (lookup(argv[0]) == NULL)
303b1ed8a43Seric 	{
3047de81dc7Seric 		while ((p = *++argv) != NULL)
3057de81dc7Seric 		{
306adf8f7d4Seric 			if (*p != '-')
307adf8f7d4Seric 				break;
308adf8f7d4Seric 			switch (*++p)
309adf8f7d4Seric 			{
310adf8f7d4Seric 			  case 'r':		/* run as real user */
311adf8f7d4Seric 				setuid(getuid());
312c4432be4Seric 				RealUser++;
313adf8f7d4Seric 				break;
314adf8f7d4Seric 
3157abec564Seric # ifndef SCCSDIR
316adf8f7d4Seric 			  case 'p':		/* path of sccs files */
317adf8f7d4Seric 				SccsPath = ++p;
3186c7454d2Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3196c7454d2Seric 					SccsPath = *++argv;
320adf8f7d4Seric 				break;
321adf8f7d4Seric 
3220a8b9ab0Seric 			  case 'd':		/* directory to search from */
3230a8b9ab0Seric 				SccsDir = ++p;
3246c7454d2Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3256c7454d2Seric 					SccsDir = *++argv;
3260a8b9ab0Seric 				break;
327095915d3Seric # endif
3280a8b9ab0Seric 
329f0cc3246Seric # ifdef DEBUG
330f0cc3246Seric 			  case 'T':		/* trace */
331f0cc3246Seric 				Debug++;
332f0cc3246Seric 				break;
333f0cc3246Seric # endif
334f0cc3246Seric 
335adf8f7d4Seric 			  default:
336095915d3Seric 				usrerr("unknown option -%s", p);
337adf8f7d4Seric 				break;
338adf8f7d4Seric 			}
339adf8f7d4Seric 		}
3405cabffd9Seric 		if (SccsPath[0] == '\0')
3415cabffd9Seric 			SccsPath = ".";
342b1ed8a43Seric 	}
343adf8f7d4Seric 
3443642a8d2Seric 	i = command(argv, FALSE, "");
34541290c52Seric 	exit(i);
3467de81dc7Seric }
3470faf63f9Seric 
3480faf63f9Seric /*
34941290c52Seric **  COMMAND -- look up and perform a command
35041290c52Seric **
35141290c52Seric **	This routine is the guts of this program.  Given an
35241290c52Seric **	argument vector, it looks up the "command" (argv[0])
35341290c52Seric **	in the configuration table and does the necessary stuff.
35441290c52Seric **
35541290c52Seric **	Parameters:
35641290c52Seric **		argv -- an argument vector to process.
35741290c52Seric **		forkflag -- if set, fork before executing the command.
358108d6082Seric **		editflag -- if set, only include flags listed in the
359108d6082Seric **			sccsklets field of the command descriptor.
360108d6082Seric **		arg0 -- a space-seperated list of arguments to insert
361108d6082Seric **			before argv.
36241290c52Seric **
36341290c52Seric **	Returns:
36441290c52Seric **		zero -- command executed ok.
36541290c52Seric **		else -- error status.
36641290c52Seric **
36741290c52Seric **	Side Effects:
36841290c52Seric **		none.
36941290c52Seric */
3707de81dc7Seric 
3713642a8d2Seric command(argv, forkflag, arg0)
3727de81dc7Seric 	char **argv;
3731777fbcbSeric 	bool forkflag;
374108d6082Seric 	char *arg0;
3757de81dc7Seric {
3767de81dc7Seric 	register struct sccsprog *cmd;
3777de81dc7Seric 	register char *p;
378f76dd34fSrrh 	char buf[FBUFSIZ];
379b1ed8a43Seric 	extern struct sccsprog *lookup();
380108d6082Seric 	char *nav[1000];
381108d6082Seric 	char **np;
382419708b0Seric 	register char **ap;
383d51cd7e5Seric 	register int i;
384419708b0Seric 	register char *q;
385d51cd7e5Seric 	extern bool unedit();
38641290c52Seric 	int rval = 0;
387108d6082Seric 	extern char *index();
388108d6082Seric 	extern char *makefile();
3893642a8d2Seric 	char *editchs;
390287bbaa2Seric 	extern char *tail();
391f0cc3246Seric 
392f0cc3246Seric # ifdef DEBUG
393f0cc3246Seric 	if (Debug)
394f0cc3246Seric 	{
395108d6082Seric 		printf("command:\n\t\"%s\"\n", arg0);
396108d6082Seric 		for (np = argv; *np != NULL; np++)
397108d6082Seric 			printf("\t\"%s\"\n", *np);
398f0cc3246Seric 	}
399f0cc3246Seric # endif
400c4432be4Seric 
401c4432be4Seric 	/*
402108d6082Seric 	**  Copy arguments.
403ff038098Seric 	**	Copy from arg0 & if necessary at most one arg
404ff038098Seric 	**	from argv[0].
405adf8f7d4Seric 	*/
406adf8f7d4Seric 
407419708b0Seric 	np = ap = &nav[1];
4083642a8d2Seric 	editchs = NULL;
40959e81a43Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
410108d6082Seric 	{
411108d6082Seric 		*np++ = q;
412108d6082Seric 		while (*p == ' ')
413108d6082Seric 			p++;
4143642a8d2Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
415108d6082Seric 			*q++ = *p++;
416108d6082Seric 		*q++ = '\0';
4173642a8d2Seric 		if (*p == ':')
4183642a8d2Seric 		{
4193642a8d2Seric 			editchs = q;
42059e81a43Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4213642a8d2Seric 				*q++ = *p;
4223642a8d2Seric 			*q++ = '\0';
4233642a8d2Seric 		}
424108d6082Seric 	}
425108d6082Seric 	*np = NULL;
426419708b0Seric 	if (*ap == NULL)
427108d6082Seric 		*np++ = *argv++;
428108d6082Seric 
429108d6082Seric 	/*
430108d6082Seric 	**  Look up command.
431419708b0Seric 	**	At this point, *ap is the command name.
432108d6082Seric 	*/
433108d6082Seric 
434419708b0Seric 	cmd = lookup(*ap);
435b1ed8a43Seric 	if (cmd == NULL)
436adf8f7d4Seric 	{
437419708b0Seric 		usrerr("Unknown command \"%s\"", *ap);
43841290c52Seric 		return (EX_USAGE);
439adf8f7d4Seric 	}
440adf8f7d4Seric 
441adf8f7d4Seric 	/*
442108d6082Seric 	**  Copy remaining arguments doing editing as appropriate.
443108d6082Seric 	*/
444108d6082Seric 
445108d6082Seric 	for (; *argv != NULL; argv++)
446108d6082Seric 	{
447108d6082Seric 		p = *argv;
448108d6082Seric 		if (*p == '-')
449108d6082Seric 		{
4503642a8d2Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
451108d6082Seric 				*np++ = p;
452108d6082Seric 		}
453108d6082Seric 		else
454108d6082Seric 		{
455108d6082Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
456108d6082Seric 				p = makefile(p);
457108d6082Seric 			if (p != NULL)
458108d6082Seric 				*np++ = p;
459108d6082Seric 		}
460108d6082Seric 	}
461108d6082Seric 	*np = NULL;
462108d6082Seric 
463108d6082Seric 	/*
4647de81dc7Seric 	**  Interpret operation associated with this command.
465c4432be4Seric 	*/
466c4432be4Seric 
4677de81dc7Seric 	switch (cmd->sccsoper)
4687de81dc7Seric 	{
469419708b0Seric 	  case SHELL:		/* call a shell file */
470419708b0Seric 		*ap = cmd->sccspath;
471419708b0Seric 		*--ap = "sh";
4727a8071bfSbostic 		rval = callprog(_PATH_BSHELL, cmd->sccsflags, ap, forkflag);
473419708b0Seric 		break;
474419708b0Seric 
4757de81dc7Seric 	  case PROG:		/* call an sccs prog */
476419708b0Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
4771777fbcbSeric 		break;
4781777fbcbSeric 
4791777fbcbSeric 	  case CMACRO:		/* command macro */
480ff038098Seric 		/* step through & execute each part of the macro */
4811777fbcbSeric 		for (p = cmd->sccspath; *p != '\0'; p++)
4821777fbcbSeric 		{
483108d6082Seric 			q = p;
484108d6082Seric 			while (*p != '\0' && *p != '/')
485108d6082Seric 				p++;
4863642a8d2Seric 			rval = command(&ap[1], *p != '\0', q);
48741290c52Seric 			if (rval != 0)
48841290c52Seric 				break;
4891777fbcbSeric 		}
49041290c52Seric 		break;
4917de81dc7Seric 
492172147faSeric 	  case FIX:		/* fix a delta */
4936e36cbf6Sbostic 		if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
494172147faSeric 		{
495095915d3Seric 			usrerr("-r flag needed for fix command");
49641290c52Seric 			rval = EX_USAGE;
497172147faSeric 			break;
498172147faSeric 		}
499ff038098Seric 
500ff038098Seric 		/* get the version with all changes */
5013642a8d2Seric 		rval = command(&ap[1], TRUE, "get -k");
502ff038098Seric 
503ff038098Seric 		/* now remove that version from the s-file */
50441290c52Seric 		if (rval == 0)
5053642a8d2Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
506ff038098Seric 
507ff038098Seric 		/* and edit the old version (but don't clobber new vers) */
50841290c52Seric 		if (rval == 0)
5093642a8d2Seric 			rval = command(&ap[2], FALSE, "get -e -g");
51041290c52Seric 		break;
511172147faSeric 
512b2538d76Seric 	  case CLEAN:
513ec4190ddSeric 		rval = clean((int) cmd->sccspath, ap);
514b2538d76Seric 		break;
515b2538d76Seric 
516e39a5722Seric 	  case UNEDIT:
517419708b0Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
518d51cd7e5Seric 		{
519108d6082Seric 			if (unedit(*argv))
520108d6082Seric 				*np++ = *argv;
521d51cd7e5Seric 		}
522108d6082Seric 		*np = NULL;
523ff038098Seric 
524ff038098Seric 		/* get all the files that we unedited successfully */
525fe7b004aSeric 		if (np > &ap[1])
5263642a8d2Seric 			rval = command(&ap[1], FALSE, "get");
527e39a5722Seric 		break;
528e39a5722Seric 
52978d41c6aSeric 	  case DIFFS:		/* diff between s-file & edit file */
53078d41c6aSeric 		/* find the end of the flag arguments */
53178d41c6aSeric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
53278d41c6aSeric 			continue;
53378d41c6aSeric 		argv = np;
53478d41c6aSeric 
53578d41c6aSeric 		/* for each file, do the diff */
53696370756Seric 		p = argv[1];
53778d41c6aSeric 		while (*np != NULL)
53878d41c6aSeric 		{
539ff038098Seric 			/* messy, but we need a null terminated argv */
54078d41c6aSeric 			*argv = *np++;
54196370756Seric 			argv[1] = NULL;
542287bbaa2Seric 			i = dodiff(ap, tail(*argv));
54378d41c6aSeric 			if (rval == 0)
54478d41c6aSeric 				rval = i;
54596370756Seric 			argv[1] = p;
54678d41c6aSeric 		}
54778d41c6aSeric 		break;
54878d41c6aSeric 
5492569d37aSeric 	  case DODIFF:		/* internal diff call */
5502569d37aSeric 		setuid(getuid());
5512569d37aSeric 		for (np = ap; *np != NULL; np++)
5522569d37aSeric 		{
5532569d37aSeric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5542569d37aSeric 				(*np)[1] = 'c';
5552569d37aSeric 		}
5562569d37aSeric 
5572569d37aSeric 		/* insert "-" argument */
5582569d37aSeric 		np[1] = NULL;
5592569d37aSeric 		np[0] = np[-1];
5602569d37aSeric 		np[-1] = "-";
5612569d37aSeric 
5622569d37aSeric 		/* execute the diff program of choice */
5632569d37aSeric # ifndef V6
5642569d37aSeric 		execvp("diff", ap);
5652569d37aSeric # endif
5662569d37aSeric 		execv(cmd->sccspath, argv);
5672569d37aSeric 		syserr("cannot exec %s", cmd->sccspath);
5682569d37aSeric 		exit(EX_OSERR);
5692569d37aSeric 
57044c626a5Srrh 	  case ENTER:		/* enter new sccs files */
571bb5509cbSmckusick 		/* skip over flag arguments */
572bb5509cbSmckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
573bb5509cbSmckusick 			continue;
574bb5509cbSmckusick 		argv = np;
575bb5509cbSmckusick 
576bb5509cbSmckusick 		/* do an admin for each file */
577bb5509cbSmckusick 		p = argv[1];
578bb5509cbSmckusick 		while (*np != NULL)
579bb5509cbSmckusick 		{
580bb5509cbSmckusick 			printf("\n%s:\n", *np);
581f76dd34fSrrh 			strcpy(buf, "-i");
582f76dd34fSrrh 			gstrcat(buf, *np, sizeof(buf));
583bb5509cbSmckusick 			ap[0] = buf;
584bb5509cbSmckusick 			argv[0] = tail(*np);
585bb5509cbSmckusick 			argv[1] = NULL;
586bb5509cbSmckusick 			rval = command(ap, TRUE, "admin");
587bb5509cbSmckusick 			argv[1] = p;
588bb5509cbSmckusick 			if (rval == 0)
589bb5509cbSmckusick 			{
590f76dd34fSrrh 				strcpy(buf, ",");
591f76dd34fSrrh 				gstrcat(buf, tail(*np), sizeof(buf));
592bb5509cbSmckusick 				if (link(*np, buf) >= 0)
593bb5509cbSmckusick 					unlink(*np);
594bb5509cbSmckusick 			}
595bb5509cbSmckusick 			np++;
596bb5509cbSmckusick 		}
597bb5509cbSmckusick 		break;
598bb5509cbSmckusick 
5997de81dc7Seric 	  default:
600095915d3Seric 		syserr("oper %d", cmd->sccsoper);
6017de81dc7Seric 		exit(EX_SOFTWARE);
6027de81dc7Seric 	}
60341290c52Seric # ifdef DEBUG
60441290c52Seric 	if (Debug)
60541290c52Seric 		printf("command: rval=%d\n", rval);
60641290c52Seric # endif
60741290c52Seric 	return (rval);
6087de81dc7Seric }
6090faf63f9Seric 
6100faf63f9Seric /*
611b1ed8a43Seric **  LOOKUP -- look up an SCCS command name.
612b1ed8a43Seric **
613b1ed8a43Seric **	Parameters:
614b1ed8a43Seric **		name -- the name of the command to look up.
615b1ed8a43Seric **
616b1ed8a43Seric **	Returns:
617b1ed8a43Seric **		ptr to command descriptor for this command.
618b1ed8a43Seric **		NULL if no such entry.
619b1ed8a43Seric **
620b1ed8a43Seric **	Side Effects:
621b1ed8a43Seric **		none.
622b1ed8a43Seric */
623b1ed8a43Seric 
624b1ed8a43Seric struct sccsprog *
625b1ed8a43Seric lookup(name)
626b1ed8a43Seric 	char *name;
627b1ed8a43Seric {
628b1ed8a43Seric 	register struct sccsprog *cmd;
629b1ed8a43Seric 
630b1ed8a43Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
631b1ed8a43Seric 	{
632b1ed8a43Seric 		if (strcmp(cmd->sccsname, name) == 0)
633b1ed8a43Seric 			return (cmd);
634b1ed8a43Seric 	}
635b1ed8a43Seric 	return (NULL);
636b1ed8a43Seric }
6370faf63f9Seric 
6380faf63f9Seric /*
63941290c52Seric **  CALLPROG -- call a program
64041290c52Seric **
641108d6082Seric **	Used to call the SCCS programs.
64241290c52Seric **
64341290c52Seric **	Parameters:
64441290c52Seric **		progpath -- pathname of the program to call.
64541290c52Seric **		flags -- status flags from the command descriptors.
64641290c52Seric **		argv -- an argument vector to pass to the program.
64741290c52Seric **		forkflag -- if true, fork before calling, else just
64841290c52Seric **			exec.
64941290c52Seric **
65041290c52Seric **	Returns:
65141290c52Seric **		The exit status of the program.
65241290c52Seric **		Nothing if forkflag == FALSE.
65341290c52Seric **
65441290c52Seric **	Side Effects:
65541290c52Seric **		Can exit if forkflag == FALSE.
65641290c52Seric */
657172147faSeric 
6587de81dc7Seric callprog(progpath, flags, argv, forkflag)
6597de81dc7Seric 	char *progpath;
6607de81dc7Seric 	short flags;
6617de81dc7Seric 	char **argv;
6627de81dc7Seric 	bool forkflag;
6637de81dc7Seric {
6647de81dc7Seric 	register int i;
6656e36cbf6Sbostic 	register int wpid;
6661777fbcbSeric 	auto int st;
6676e36cbf6Sbostic 	register int sigcode;
6686e36cbf6Sbostic 	register int coredumped;
6696e36cbf6Sbostic 	register char *sigmsg;
6706e36cbf6Sbostic 	auto char sigmsgbuf[10+1];	/* "Signal 127" + terminating '\0' */
671108d6082Seric 
672108d6082Seric # ifdef DEBUG
673108d6082Seric 	if (Debug)
674108d6082Seric 	{
675108d6082Seric 		printf("callprog:\n");
676108d6082Seric 		for (i = 0; argv[i] != NULL; i++)
677108d6082Seric 			printf("\t\"%s\"\n", argv[i]);
678108d6082Seric 	}
679108d6082Seric # endif
6807de81dc7Seric 
6817de81dc7Seric 	if (*argv == NULL)
6827de81dc7Seric 		return (-1);
683c4432be4Seric 
684c4432be4Seric 	/*
685172147faSeric 	**  Fork if appropriate.
686adf8f7d4Seric 	*/
687adf8f7d4Seric 
6887de81dc7Seric 	if (forkflag)
6897de81dc7Seric 	{
690f0cc3246Seric # ifdef DEBUG
691f0cc3246Seric 		if (Debug)
692f0cc3246Seric 			printf("Forking\n");
693f0cc3246Seric # endif
6947de81dc7Seric 		i = fork();
6957de81dc7Seric 		if (i < 0)
6967de81dc7Seric 		{
697095915d3Seric 			syserr("cannot fork");
6987de81dc7Seric 			exit(EX_OSERR);
6997de81dc7Seric 		}
7007de81dc7Seric 		else if (i > 0)
7011777fbcbSeric 		{
7026e36cbf6Sbostic 			while ((wpid = wait(&st)) != -1 && wpid != i)
7036e36cbf6Sbostic 				;
7046e36cbf6Sbostic 			if ((sigcode = st & 0377) == 0)
70541290c52Seric 				st = (st >> 8) & 0377;
7066e36cbf6Sbostic 			else
7076e36cbf6Sbostic 			{
7086e36cbf6Sbostic 				coredumped = sigcode & 0200;
7096e36cbf6Sbostic 				sigcode &= 0177;
7106e36cbf6Sbostic 				if (sigcode != SIGINT && sigcode != SIGPIPE)
7116e36cbf6Sbostic 				{
7126e36cbf6Sbostic 					if (sigcode < NSIG)
7136e36cbf6Sbostic 						sigmsg = sys_siglist[sigcode];
7146e36cbf6Sbostic 					else
7156e36cbf6Sbostic 					{
7166e36cbf6Sbostic 						sprintf(sigmsgbuf, "Signal %d",
7176e36cbf6Sbostic 						    sigcode);
7186e36cbf6Sbostic 						sigmsg = sigmsgbuf;
7196e36cbf6Sbostic 					}
7206e36cbf6Sbostic 					fprintf(stderr, "sccs: %s: %s%s", argv[0],
7216e36cbf6Sbostic 					    sigmsg,
7226e36cbf6Sbostic 					    coredumped ? " - core dumped": "");
7236e36cbf6Sbostic 				}
7246e36cbf6Sbostic 				st = EX_SOFTWARE;
7256e36cbf6Sbostic 			}
72678d41c6aSeric 			if (OutFile >= 0)
72778d41c6aSeric 			{
72878d41c6aSeric 				close(OutFile);
72978d41c6aSeric 				OutFile = -1;
73078d41c6aSeric 			}
7311777fbcbSeric 			return (st);
7321777fbcbSeric 		}
7337de81dc7Seric 	}
73478d41c6aSeric 	else if (OutFile >= 0)
73578d41c6aSeric 	{
73678d41c6aSeric 		syserr("callprog: setting stdout w/o forking");
73778d41c6aSeric 		exit(EX_SOFTWARE);
73878d41c6aSeric 	}
7397de81dc7Seric 
74078d41c6aSeric 	/* set protection as appropriate */
7417de81dc7Seric 	if (bitset(REALUSER, flags))
7427de81dc7Seric 		setuid(getuid());
7437de81dc7Seric 
74478d41c6aSeric 	/* change standard input & output if needed */
74578d41c6aSeric 	if (OutFile >= 0)
74678d41c6aSeric 	{
74778d41c6aSeric 		close(1);
74878d41c6aSeric 		dup(OutFile);
74978d41c6aSeric 		close(OutFile);
75078d41c6aSeric 	}
7517de81dc7Seric 
75278d41c6aSeric 	/* call real SCCS program */
753172147faSeric 	execv(progpath, argv);
754095915d3Seric 	syserr("cannot execute %s", progpath);
755adf8f7d4Seric 	exit(EX_UNAVAILABLE);
756fe7b004aSeric 	/*NOTREACHED*/
757adf8f7d4Seric }
7580faf63f9Seric 
7590faf63f9Seric /*
760cdc1aa65Seric **  MAKEFILE -- make filename of SCCS file
761cdc1aa65Seric **
762cdc1aa65Seric **	If the name passed is already the name of an SCCS file,
763cdc1aa65Seric **	just return it.  Otherwise, munge the name into the name
764cdc1aa65Seric **	of the actual SCCS file.
765cdc1aa65Seric **
766cdc1aa65Seric **	There are cases when it is not clear what you want to
767cdc1aa65Seric **	do.  For example, if SccsPath is an absolute pathname
768cdc1aa65Seric **	and the name given is also an absolute pathname, we go
769cdc1aa65Seric **	for SccsPath (& only use the last component of the name
770cdc1aa65Seric **	passed) -- this is important for security reasons (if
771cdc1aa65Seric **	sccs is being used as a setuid front end), but not
772cdc1aa65Seric **	particularly intuitive.
773cdc1aa65Seric **
774cdc1aa65Seric **	Parameters:
775cdc1aa65Seric **		name -- the file name to be munged.
776cdc1aa65Seric **
777cdc1aa65Seric **	Returns:
778cdc1aa65Seric **		The pathname of the sccs file.
779cdc1aa65Seric **		NULL on error.
780cdc1aa65Seric **
781cdc1aa65Seric **	Side Effects:
782cdc1aa65Seric **		none.
783cdc1aa65Seric */
784adf8f7d4Seric 
785adf8f7d4Seric char *
786adf8f7d4Seric makefile(name)
787adf8f7d4Seric 	char *name;
788adf8f7d4Seric {
789adf8f7d4Seric 	register char *p;
790f76dd34fSrrh 	char buf[3*FBUFSIZ];
791adf8f7d4Seric 	extern char *malloc();
792cdc1aa65Seric 	extern char *rindex();
7930a8b9ab0Seric 	extern bool safepath();
7947d70f5d8Seric 	extern bool isdir();
7957d70f5d8Seric 	register char *q;
796cdc1aa65Seric 
797cdc1aa65Seric 	p = rindex(name, '/');
798cdc1aa65Seric 	if (p == NULL)
799cdc1aa65Seric 		p = name;
800cdc1aa65Seric 	else
801cdc1aa65Seric 		p++;
802adf8f7d4Seric 
803adf8f7d4Seric 	/*
8040a8b9ab0Seric 	**  Check to see that the path is "safe", i.e., that we
8050a8b9ab0Seric 	**  are not letting some nasty person use the setuid part
8060a8b9ab0Seric 	**  of this program to look at or munge some presumably
8070a8b9ab0Seric 	**  hidden files.
808adf8f7d4Seric 	*/
809adf8f7d4Seric 
8100a8b9ab0Seric 	if (SccsDir[0] == '/' && !safepath(name))
8110a8b9ab0Seric 		return (NULL);
812cdc1aa65Seric 
813cdc1aa65Seric 	/*
8140a8b9ab0Seric 	**  Create the base pathname.
815cdc1aa65Seric 	*/
816cdc1aa65Seric 
817ff038098Seric 	/* first the directory part */
8180a8b9ab0Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
819adf8f7d4Seric 	{
820f76dd34fSrrh 		gstrcpy(buf, SccsDir, sizeof(buf));
821f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
822cdc1aa65Seric 	}
823cdc1aa65Seric 	else
824f76dd34fSrrh 		gstrcpy(buf, "", sizeof(buf));
825ff038098Seric 
826ff038098Seric 	/* then the head of the pathname */
827f76dd34fSrrh 	gstrncat(buf, name, p - name, sizeof(buf));
8287d70f5d8Seric 	q = &buf[strlen(buf)];
829ff038098Seric 
830ff038098Seric 	/* now copy the final part of the name, in case useful */
831f76dd34fSrrh 	gstrcpy(q, p, sizeof(buf));
832ff038098Seric 
833ff038098Seric 	/* so is it useful? */
8347d70f5d8Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
8357d70f5d8Seric 	{
836ff038098Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
837f76dd34fSrrh 		gstrcpy(q, SccsPath, sizeof(buf));
838f76dd34fSrrh 		gstrcat(buf, "/s.", sizeof(buf));
839ff038098Seric 
840ff038098Seric 		/* and now the end of the name */
841f76dd34fSrrh 		gstrcat(buf, p, sizeof(buf));
842cdc1aa65Seric 	}
843adf8f7d4Seric 
844ff038098Seric 	/* if i haven't changed it, why did I do all this? */
8450a8b9ab0Seric 	if (strcmp(buf, name) == 0)
8460a8b9ab0Seric 		p = name;
8470a8b9ab0Seric 	else
8480a8b9ab0Seric 	{
849ff038098Seric 		/* but if I have, squirrel it away */
850adf8f7d4Seric 		p = malloc(strlen(buf) + 1);
851adf8f7d4Seric 		if (p == NULL)
852adf8f7d4Seric 		{
853adf8f7d4Seric 			perror("Sccs: no mem");
854adf8f7d4Seric 			exit(EX_OSERR);
855adf8f7d4Seric 		}
856adf8f7d4Seric 		strcpy(p, buf);
8570a8b9ab0Seric 	}
858ff038098Seric 
859adf8f7d4Seric 	return (p);
860adf8f7d4Seric }
8610faf63f9Seric 
8620faf63f9Seric /*
8637d70f5d8Seric **  ISDIR -- return true if the argument is a directory.
8647d70f5d8Seric **
8657d70f5d8Seric **	Parameters:
8667d70f5d8Seric **		name -- the pathname of the file to check.
8677d70f5d8Seric **
8687d70f5d8Seric **	Returns:
8697d70f5d8Seric **		TRUE if 'name' is a directory, FALSE otherwise.
8707d70f5d8Seric **
8717d70f5d8Seric **	Side Effects:
8727d70f5d8Seric **		none.
8737d70f5d8Seric */
8747d70f5d8Seric 
8757d70f5d8Seric bool
8767d70f5d8Seric isdir(name)
8777d70f5d8Seric 	char *name;
8787d70f5d8Seric {
8797d70f5d8Seric 	struct stat stbuf;
8807d70f5d8Seric 
8817d70f5d8Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
8827d70f5d8Seric }
8830faf63f9Seric 
8840faf63f9Seric /*
885cdc1aa65Seric **  SAFEPATH -- determine whether a pathname is "safe"
886cdc1aa65Seric **
887cdc1aa65Seric **	"Safe" pathnames only allow you to get deeper into the
888cdc1aa65Seric **	directory structure, i.e., full pathnames and ".." are
889cdc1aa65Seric **	not allowed.
890cdc1aa65Seric **
891cdc1aa65Seric **	Parameters:
892cdc1aa65Seric **		p -- the name to check.
893cdc1aa65Seric **
894cdc1aa65Seric **	Returns:
895cdc1aa65Seric **		TRUE -- if the path is safe.
896cdc1aa65Seric **		FALSE -- if the path is not safe.
897cdc1aa65Seric **
898cdc1aa65Seric **	Side Effects:
899cdc1aa65Seric **		Prints a message if the path is not safe.
900cdc1aa65Seric */
901cdc1aa65Seric 
902cdc1aa65Seric bool
903cdc1aa65Seric safepath(p)
904cdc1aa65Seric 	register char *p;
905cdc1aa65Seric {
906cdc1aa65Seric 	extern char *index();
907cdc1aa65Seric 
908cdc1aa65Seric 	if (*p != '/')
909cdc1aa65Seric 	{
910cdc1aa65Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
911cdc1aa65Seric 		{
912cdc1aa65Seric 			p = index(p, '/');
913cdc1aa65Seric 			if (p == NULL)
914cdc1aa65Seric 				return (TRUE);
915cdc1aa65Seric 			p++;
916cdc1aa65Seric 		}
917cdc1aa65Seric 	}
918cdc1aa65Seric 
919cdc1aa65Seric 	printf("You may not use full pathnames or \"..\"\n");
920cdc1aa65Seric 	return (FALSE);
921cdc1aa65Seric }
9220faf63f9Seric 
9230faf63f9Seric /*
924b2538d76Seric **  CLEAN -- clean out recreatable files
925b2538d76Seric **
926b2538d76Seric **	Any file for which an "s." file exists but no "p." file
927b2538d76Seric **	exists in the current directory is purged.
928b2538d76Seric **
929b2538d76Seric **	Parameters:
930ec4190ddSeric **		mode -- tells whether this came from a "clean", "info", or
93148ff0e7eSeric **			"check" command.
932ec4190ddSeric **		argv -- the rest of the argument vector.
933b2538d76Seric **
934b2538d76Seric **	Returns:
935b2538d76Seric **		none.
936b2538d76Seric **
937b2538d76Seric **	Side Effects:
93848ff0e7eSeric **		Removes files in the current directory.
93948ff0e7eSeric **		Prints information regarding files being edited.
94048ff0e7eSeric **		Exits if a "check" command.
941b2538d76Seric */
942b2538d76Seric 
943ec4190ddSeric clean(mode, argv)
94448ff0e7eSeric 	int mode;
945ec4190ddSeric 	char **argv;
946b2538d76Seric {
947683375a0Smckusick 	struct direct *dir;
948f76dd34fSrrh 	char buf[FBUFSIZ];
949393e27f2Seric 	char *bufend;
950bb638ad5Smckusick 	register DIR *dirp;
9513a208c77Seric 	register char *basefile;
9522caac8fdSeric 	bool gotedit;
953ec4190ddSeric 	bool gotpfent;
954bf9c1e66Seric 	FILE *pfp;
955ec4190ddSeric 	bool nobranch = FALSE;
956ec4190ddSeric 	extern struct pfile *getpfent();
957ec4190ddSeric 	register struct pfile *pf;
958ec4190ddSeric 	register char **ap;
9591e0924d0Seric 	extern char *username();
9601e0924d0Seric 	char *usernm = NULL;
961393e27f2Seric 	char *subdir = NULL;
962393e27f2Seric 	char *cmdname;
963ec4190ddSeric 
964ec4190ddSeric 	/*
965ec4190ddSeric 	**  Process the argv
966ec4190ddSeric 	*/
967ec4190ddSeric 
968393e27f2Seric 	cmdname = *argv;
969393e27f2Seric 	for (ap = argv; *++ap != NULL; )
970ec4190ddSeric 	{
9711e0924d0Seric 		if (**ap == '-')
9721e0924d0Seric 		{
9731e0924d0Seric 			/* we have a flag */
9741e0924d0Seric 			switch ((*ap)[1])
9751e0924d0Seric 			{
9761e0924d0Seric 			  case 'b':
977ec4190ddSeric 				nobranch = TRUE;
9781e0924d0Seric 				break;
9791e0924d0Seric 
9801e0924d0Seric 			  case 'u':
9811e0924d0Seric 				if ((*ap)[2] != '\0')
9821e0924d0Seric 					usernm = &(*ap)[2];
9831e0924d0Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9841e0924d0Seric 					usernm = *++ap;
9851e0924d0Seric 				else
9861e0924d0Seric 					usernm = username();
9871e0924d0Seric 				break;
9881e0924d0Seric 			}
9891e0924d0Seric 		}
990393e27f2Seric 		else
991393e27f2Seric 		{
992393e27f2Seric 			if (subdir != NULL)
993393e27f2Seric 				usrerr("too many args");
994393e27f2Seric 			else
995393e27f2Seric 				subdir = *ap;
996393e27f2Seric 		}
997ec4190ddSeric 	}
998b2538d76Seric 
999ff038098Seric 	/*
1000ff038098Seric 	**  Find and open the SCCS directory.
1001ff038098Seric 	*/
1002ff038098Seric 
1003f76dd34fSrrh 	gstrcpy(buf, SccsDir, sizeof(buf));
1004f77a08fbSeric 	if (buf[0] != '\0')
1005f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
1006393e27f2Seric 	if (subdir != NULL)
1007393e27f2Seric 	{
1008f76dd34fSrrh 		gstrcat(buf, subdir, sizeof(buf));
1009f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
1010393e27f2Seric 	}
1011f76dd34fSrrh 	gstrcat(buf, SccsPath, sizeof(buf));
1012393e27f2Seric 	bufend = &buf[strlen(buf)];
1013ff038098Seric 
1014bb638ad5Smckusick 	dirp = opendir(buf);
1015bb638ad5Smckusick 	if (dirp == NULL)
1016b2538d76Seric 	{
1017f77a08fbSeric 		usrerr("cannot open %s", buf);
101841290c52Seric 		return (EX_NOINPUT);
1019b2538d76Seric 	}
1020b2538d76Seric 
1021b2538d76Seric 	/*
1022b2538d76Seric 	**  Scan the SCCS directory looking for s. files.
1023ff038098Seric 	**	gotedit tells whether we have tried to clean any
1024ff038098Seric 	**		files that are being edited.
1025b2538d76Seric 	*/
1026b2538d76Seric 
10272caac8fdSeric 	gotedit = FALSE;
1028bb638ad5Smckusick 	while (dir = readdir(dirp)) {
1029683375a0Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
1030b2538d76Seric 			continue;
1031b2538d76Seric 
1032b2538d76Seric 		/* got an s. file -- see if the p. file exists */
1033f76dd34fSrrh 		gstrcpy(bufend, "/p.", sizeof(buf));
1034393e27f2Seric 		basefile = bufend + 3;
1035f76dd34fSrrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
1036ec4190ddSeric 
1037ec4190ddSeric 		/*
1038ec4190ddSeric 		**  open and scan the p-file.
1039ec4190ddSeric 		**	'gotpfent' tells if we have found a valid p-file
1040ec4190ddSeric 		**		entry.
1041ec4190ddSeric 		*/
1042ec4190ddSeric 
1043bf9c1e66Seric 		pfp = fopen(buf, "r");
1044ec4190ddSeric 		gotpfent = FALSE;
1045bf9c1e66Seric 		if (pfp != NULL)
10463a208c77Seric 		{
1047ff038098Seric 			/* the file exists -- report it's contents */
1048ec4190ddSeric 			while ((pf = getpfent(pfp)) != NULL)
10492734e237Seric 			{
1050ec4190ddSeric 				if (nobranch && isbranch(pf->p_nsid))
1051ec4190ddSeric 					continue;
10521e0924d0Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10531e0924d0Seric 					continue;
1054ec4190ddSeric 				gotedit = TRUE;
1055ec4190ddSeric 				gotpfent = TRUE;
1056ec4190ddSeric 				if (mode == TELLC)
1057ec4190ddSeric 				{
1058ec4190ddSeric 					printf("%s\n", basefile);
1059ec4190ddSeric 					break;
1060ec4190ddSeric 				}
106177a8a0dfSeric 				printf("%12s: being edited: ", basefile);
106277a8a0dfSeric 				putpfent(pf, stdout);
10632734e237Seric 			}
1064bf9c1e66Seric 			fclose(pfp);
10653a208c77Seric 		}
1066b2538d76Seric 
1067b2538d76Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10689d7ed7d6Seric 		if (mode == CLEANC && !gotpfent)
10693a208c77Seric 		{
1070f76dd34fSrrh 			char	unlinkbuf[FBUFSIZ];
1071f76dd34fSrrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
10720770051cSrrh 			unlink(unlinkbuf);
1073b2538d76Seric 		}
10743a208c77Seric 	}
1075b2538d76Seric 
1076ff038098Seric 	/* cleanup & report results */
1077bb638ad5Smckusick 	closedir(dirp);
107848ff0e7eSeric 	if (!gotedit && mode == INFOC)
10791e0924d0Seric 	{
10801e0924d0Seric 		printf("Nothing being edited");
10811e0924d0Seric 		if (nobranch)
10821e0924d0Seric 			printf(" (on trunk)");
10831e0924d0Seric 		if (usernm == NULL)
10841e0924d0Seric 			printf("\n");
10851e0924d0Seric 		else
10861e0924d0Seric 			printf(" by %s\n", usernm);
10871e0924d0Seric 	}
108848ff0e7eSeric 	if (mode == CHECKC)
108948ff0e7eSeric 		exit(gotedit);
109041290c52Seric 	return (EX_OK);
1091b2538d76Seric }
10920faf63f9Seric 
10930faf63f9Seric /*
1094ec4190ddSeric **  ISBRANCH -- is the SID a branch?
1095ec4190ddSeric **
1096ec4190ddSeric **	Parameters:
1097ec4190ddSeric **		sid -- the sid to check.
1098ec4190ddSeric **
1099ec4190ddSeric **	Returns:
1100ec4190ddSeric **		TRUE if the sid represents a branch.
1101ec4190ddSeric **		FALSE otherwise.
1102ec4190ddSeric **
1103ec4190ddSeric **	Side Effects:
1104ec4190ddSeric **		none.
1105ec4190ddSeric */
1106ec4190ddSeric 
1107ec4190ddSeric isbranch(sid)
1108ec4190ddSeric 	char *sid;
1109ec4190ddSeric {
1110ec4190ddSeric 	register char *p;
1111ec4190ddSeric 	int dots;
1112ec4190ddSeric 
1113ec4190ddSeric 	dots = 0;
1114ec4190ddSeric 	for (p = sid; *p != '\0'; p++)
1115ec4190ddSeric 	{
1116ec4190ddSeric 		if (*p == '.')
1117ec4190ddSeric 			dots++;
1118ec4190ddSeric 		if (dots > 1)
1119ec4190ddSeric 			return (TRUE);
1120ec4190ddSeric 	}
1121ec4190ddSeric 	return (FALSE);
1122ec4190ddSeric }
1123ec4190ddSeric 
1124ec4190ddSeric /*
1125e39a5722Seric **  UNEDIT -- unedit a file
1126e39a5722Seric **
1127e39a5722Seric **	Checks to see that the current user is actually editting
1128e39a5722Seric **	the file and arranges that s/he is not editting it.
1129e39a5722Seric **
1130e39a5722Seric **	Parameters:
11312444cd3eSeric **		fn -- the name of the file to be unedited.
1132e39a5722Seric **
1133e39a5722Seric **	Returns:
1134d51cd7e5Seric **		TRUE -- if the file was successfully unedited.
1135d51cd7e5Seric **		FALSE -- if the file was not unedited for some
1136d51cd7e5Seric **			reason.
1137e39a5722Seric **
1138e39a5722Seric **	Side Effects:
1139e39a5722Seric **		fn is removed
1140e39a5722Seric **		entries are removed from pfile.
1141e39a5722Seric */
1142e39a5722Seric 
1143d51cd7e5Seric bool
1144e39a5722Seric unedit(fn)
1145e39a5722Seric 	char *fn;
1146e39a5722Seric {
1147e39a5722Seric 	register FILE *pfp;
11488e9f28e0Ssam 	char *cp, *pfn;
11497a8071bfSbostic 	static char tfn[] = _PATH_TMP;
1150e39a5722Seric 	FILE *tfp;
1151e39a5722Seric 	register char *q;
1152e39a5722Seric 	bool delete = FALSE;
1153e39a5722Seric 	bool others = FALSE;
1154e39a5722Seric 	char *myname;
11551e0924d0Seric 	extern char *username();
1156e39a5722Seric 	struct pfile *pent;
1157ec4190ddSeric 	extern struct pfile *getpfent();
1158f76dd34fSrrh 	char buf[PFILELG];
11590da7a6f3Sbostic 	extern char *makefile(), *rindex(), *tail();
1160e39a5722Seric 
1161e39a5722Seric 	/* make "s." filename & find the trailing component */
1162e39a5722Seric 	pfn = makefile(fn);
1163cdc1aa65Seric 	if (pfn == NULL)
1164cdc1aa65Seric 		return (FALSE);
1165cdc1aa65Seric 	q = rindex(pfn, '/');
1166cdc1aa65Seric 	if (q == NULL)
1167cdc1aa65Seric 		q = &pfn[-1];
1168cdc1aa65Seric 	if (q[1] != 's' || q[2] != '.')
1169e39a5722Seric 	{
1170095915d3Seric 		usrerr("bad file name \"%s\"", fn);
1171d51cd7e5Seric 		return (FALSE);
1172e39a5722Seric 	}
1173e39a5722Seric 
1174ff038098Seric 	/* turn "s." into "p." & try to open it */
1175e39a5722Seric 	*++q = 'p';
1176e39a5722Seric 
1177e39a5722Seric 	pfp = fopen(pfn, "r");
1178e39a5722Seric 	if (pfp == NULL)
1179e39a5722Seric 	{
11802444cd3eSeric 		printf("%12s: not being edited\n", fn);
1181d51cd7e5Seric 		return (FALSE);
1182e39a5722Seric 	}
1183e39a5722Seric 
1184ff038098Seric 	/* create temp file for editing p-file */
1185e39a5722Seric 	mktemp(tfn);
1186e39a5722Seric 	tfp = fopen(tfn, "w");
1187e39a5722Seric 	if (tfp == NULL)
1188e39a5722Seric 	{
1189095915d3Seric 		usrerr("cannot create \"%s\"", tfn);
1190e39a5722Seric 		exit(EX_OSERR);
1191e39a5722Seric 	}
1192e39a5722Seric 
1193ff038098Seric 	/* figure out who I am */
11941e0924d0Seric 	myname = username();
1195ff038098Seric 
1196ff038098Seric 	/*
1197ff038098Seric 	**  Copy p-file to temp file, doing deletions as needed.
1198ff038098Seric 	*/
1199ff038098Seric 
1200ec4190ddSeric 	while ((pent = getpfent(pfp)) != NULL)
1201e39a5722Seric 	{
1202e39a5722Seric 		if (strcmp(pent->p_user, myname) == 0)
1203e39a5722Seric 		{
1204e39a5722Seric 			/* a match */
1205e39a5722Seric 			delete++;
1206e39a5722Seric 		}
1207e39a5722Seric 		else
1208e39a5722Seric 		{
1209ff038098Seric 			/* output it again */
121077a8a0dfSeric 			putpfent(pent, tfp);
1211e39a5722Seric 			others++;
1212e39a5722Seric 		}
1213e39a5722Seric 	}
1214e39a5722Seric 
12158e9f28e0Ssam 	/*
12168e9f28e0Ssam 	 * Before changing anything, make sure we can remove
12178e9f28e0Ssam 	 * the file in question (assuming it exists).
12188e9f28e0Ssam 	 */
12198e9f28e0Ssam 	if (delete) {
12208e9f28e0Ssam 		extern int errno;
12218e9f28e0Ssam 
12228e9f28e0Ssam 		cp = tail(fn);
12238e9f28e0Ssam 		errno = 0;
12248e9f28e0Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
12258e9f28e0Ssam 			goto bad;
12268e9f28e0Ssam 		if (errno == 0)
12278e9f28e0Ssam 			/*
12288e9f28e0Ssam 			 * This is wrong, but the rest of the program
12298e9f28e0Ssam 			 * has built in assumptions about "." as well,
12308e9f28e0Ssam 			 * so why make unedit a special case?
12318e9f28e0Ssam 			 */
12328e9f28e0Ssam 			if (access(".", 2) < 0) {
12338e9f28e0Ssam 	bad:
12348e9f28e0Ssam 				printf("%12s: can't remove\n", cp);
12358e9f28e0Ssam 				fclose(tfp);
12368e9f28e0Ssam 				fclose(pfp);
12378e9f28e0Ssam 				unlink(tfn);
12388e9f28e0Ssam 				return (FALSE);
12398e9f28e0Ssam 			}
12408e9f28e0Ssam 	}
1241e39a5722Seric 	/* do final cleanup */
1242e39a5722Seric 	if (others)
1243e39a5722Seric 	{
1244ff038098Seric 		/* copy it back (perhaps it should be linked?) */
1245e39a5722Seric 		if (freopen(tfn, "r", tfp) == NULL)
1246e39a5722Seric 		{
1247095915d3Seric 			syserr("cannot reopen \"%s\"", tfn);
1248e39a5722Seric 			exit(EX_OSERR);
1249e39a5722Seric 		}
1250e39a5722Seric 		if (freopen(pfn, "w", pfp) == NULL)
1251e39a5722Seric 		{
1252095915d3Seric 			usrerr("cannot create \"%s\"", pfn);
1253d51cd7e5Seric 			return (FALSE);
1254e39a5722Seric 		}
1255e39a5722Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1256e39a5722Seric 			fputs(buf, pfp);
1257e39a5722Seric 	}
1258e39a5722Seric 	else
1259e39a5722Seric 	{
1260ff038098Seric 		/* it's empty -- remove it */
1261e39a5722Seric 		unlink(pfn);
1262e39a5722Seric 	}
1263e39a5722Seric 	fclose(tfp);
1264e39a5722Seric 	fclose(pfp);
1265e39a5722Seric 	unlink(tfn);
1266e39a5722Seric 
1267ff038098Seric 	/* actually remove the g-file */
1268e39a5722Seric 	if (delete)
1269e39a5722Seric 	{
12708e9f28e0Ssam 		/*
12718e9f28e0Ssam 		 * Since we've checked above, we can
12728e9f28e0Ssam 		 * use the return from unlink to
12738e9f28e0Ssam 		 * determine if the file existed or not.
12748e9f28e0Ssam 		 */
12758e9f28e0Ssam 		if (unlink(cp) >= 0)
12768e9f28e0Ssam 			printf("%12s: removed\n", cp);
1277d51cd7e5Seric 		return (TRUE);
1278e39a5722Seric 	}
1279e39a5722Seric 	else
1280e39a5722Seric 	{
12812444cd3eSeric 		printf("%12s: not being edited by you\n", fn);
1282d51cd7e5Seric 		return (FALSE);
1283e39a5722Seric 	}
1284e39a5722Seric }
12850faf63f9Seric 
12860faf63f9Seric /*
128778d41c6aSeric **  DODIFF -- diff an s-file against a g-file
128878d41c6aSeric **
128978d41c6aSeric **	Parameters:
129078d41c6aSeric **		getv -- argv for the 'get' command.
129178d41c6aSeric **		gfile -- name of the g-file to diff against.
129278d41c6aSeric **
129378d41c6aSeric **	Returns:
129478d41c6aSeric **		Result of get.
129578d41c6aSeric **
129678d41c6aSeric **	Side Effects:
129778d41c6aSeric **		none.
129878d41c6aSeric */
129978d41c6aSeric 
130078d41c6aSeric dodiff(getv, gfile)
130178d41c6aSeric 	char **getv;
130278d41c6aSeric 	char *gfile;
130378d41c6aSeric {
130478d41c6aSeric 	int pipev[2];
130578d41c6aSeric 	int rval;
130678d41c6aSeric 	register int i;
130778d41c6aSeric 	register int pid;
130878d41c6aSeric 	auto int st;
130978d41c6aSeric 	extern int errno;
13109d228ba4Sbostic 	sig_t osig;
131178d41c6aSeric 
1312cc4e4f0dSeric 	printf("\n------- %s -------\n", gfile);
13132569d37aSeric 	fflush(stdout);
1314cc4e4f0dSeric 
1315ff038098Seric 	/* create context for diff to run in */
131678d41c6aSeric 	if (pipe(pipev) < 0)
131778d41c6aSeric 	{
131878d41c6aSeric 		syserr("dodiff: pipe failed");
131978d41c6aSeric 		exit(EX_OSERR);
132078d41c6aSeric 	}
132178d41c6aSeric 	if ((pid = fork()) < 0)
132278d41c6aSeric 	{
132378d41c6aSeric 		syserr("dodiff: fork failed");
132478d41c6aSeric 		exit(EX_OSERR);
132578d41c6aSeric 	}
132678d41c6aSeric 	else if (pid > 0)
132778d41c6aSeric 	{
132878d41c6aSeric 		/* in parent; run get */
132978d41c6aSeric 		OutFile = pipev[1];
133078d41c6aSeric 		close(pipev[0]);
13312569d37aSeric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
133278d41c6aSeric 		osig = signal(SIGINT, SIG_IGN);
133378d41c6aSeric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
133478d41c6aSeric 			errno = 0;
133578d41c6aSeric 		signal(SIGINT, osig);
133678d41c6aSeric 		/* ignore result of diff */
133778d41c6aSeric 	}
133878d41c6aSeric 	else
133978d41c6aSeric 	{
134078d41c6aSeric 		/* in child, run diff */
134178d41c6aSeric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
134278d41c6aSeric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
134378d41c6aSeric 		{
134478d41c6aSeric 			syserr("dodiff: magic failed");
134578d41c6aSeric 			exit(EX_OSERR);
134678d41c6aSeric 		}
13472569d37aSeric 		command(&getv[1], FALSE, "-diff:elsfhbC");
134878d41c6aSeric 	}
134978d41c6aSeric 	return (rval);
135078d41c6aSeric }
135178d41c6aSeric 
135278d41c6aSeric /*
1353287bbaa2Seric **  TAIL -- return tail of filename.
1354287bbaa2Seric **
1355287bbaa2Seric **	Parameters:
1356287bbaa2Seric **		fn -- the filename.
1357287bbaa2Seric **
1358287bbaa2Seric **	Returns:
1359287bbaa2Seric **		a pointer to the tail of the filename; e.g., given
1360287bbaa2Seric **		"cmd/ls.c", "ls.c" is returned.
1361287bbaa2Seric **
1362287bbaa2Seric **	Side Effects:
1363287bbaa2Seric **		none.
1364287bbaa2Seric */
1365287bbaa2Seric 
1366287bbaa2Seric char *
1367287bbaa2Seric tail(fn)
1368287bbaa2Seric 	register char *fn;
1369287bbaa2Seric {
1370287bbaa2Seric 	register char *p;
1371287bbaa2Seric 
1372287bbaa2Seric 	for (p = fn; *p != 0; p++)
1373287bbaa2Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
1374287bbaa2Seric 			fn = &p[1];
1375287bbaa2Seric 	return (fn);
1376287bbaa2Seric }
1377287bbaa2Seric 
1378287bbaa2Seric /*
1379ec4190ddSeric **  GETPFENT -- get an entry from the p-file
1380e39a5722Seric **
1381e39a5722Seric **	Parameters:
1382e39a5722Seric **		pfp -- p-file file pointer
1383e39a5722Seric **
1384e39a5722Seric **	Returns:
1385e39a5722Seric **		pointer to p-file struct for next entry
1386e39a5722Seric **		NULL on EOF or error
1387e39a5722Seric **
1388e39a5722Seric **	Side Effects:
1389e39a5722Seric **		Each call wipes out results of previous call.
1390e39a5722Seric */
1391e39a5722Seric 
1392e39a5722Seric struct pfile *
1393ec4190ddSeric getpfent(pfp)
1394e39a5722Seric 	FILE *pfp;
1395e39a5722Seric {
1396e39a5722Seric 	static struct pfile ent;
1397f76dd34fSrrh 	static char buf[PFILELG];
1398e39a5722Seric 	register char *p;
1399e39a5722Seric 	extern char *nextfield();
1400e39a5722Seric 
1401e39a5722Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1402e39a5722Seric 		return (NULL);
1403e39a5722Seric 
1404e39a5722Seric 	ent.p_osid = p = buf;
1405e39a5722Seric 	ent.p_nsid = p = nextfield(p);
1406e39a5722Seric 	ent.p_user = p = nextfield(p);
1407e39a5722Seric 	ent.p_date = p = nextfield(p);
1408e39a5722Seric 	ent.p_time = p = nextfield(p);
140977a8a0dfSeric 	ent.p_aux = p = nextfield(p);
1410e39a5722Seric 
1411e39a5722Seric 	return (&ent);
1412e39a5722Seric }
1413e39a5722Seric 
1414e39a5722Seric 
1415e39a5722Seric char *
1416e39a5722Seric nextfield(p)
1417e39a5722Seric 	register char *p;
1418e39a5722Seric {
1419e39a5722Seric 	if (p == NULL || *p == '\0')
1420e39a5722Seric 		return (NULL);
1421e39a5722Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1422e39a5722Seric 		p++;
1423e39a5722Seric 	if (*p == '\n' || *p == '\0')
1424e39a5722Seric 	{
1425e39a5722Seric 		*p = '\0';
1426e39a5722Seric 		return (NULL);
1427e39a5722Seric 	}
1428e39a5722Seric 	*p++ = '\0';
1429e39a5722Seric 	return (p);
1430e39a5722Seric }
143177a8a0dfSeric /*
143277a8a0dfSeric **  PUTPFENT -- output a p-file entry to a file
143377a8a0dfSeric **
143477a8a0dfSeric **	Parameters:
143577a8a0dfSeric **		pf -- the p-file entry
143677a8a0dfSeric **		f -- the file to put it on.
143777a8a0dfSeric **
143877a8a0dfSeric **	Returns:
143977a8a0dfSeric **		none.
144077a8a0dfSeric **
144177a8a0dfSeric **	Side Effects:
144277a8a0dfSeric **		pf is written onto file f.
144377a8a0dfSeric */
144477a8a0dfSeric 
144577a8a0dfSeric putpfent(pf, f)
144677a8a0dfSeric 	register struct pfile *pf;
144777a8a0dfSeric 	register FILE *f;
144877a8a0dfSeric {
144977a8a0dfSeric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
145077a8a0dfSeric 		pf->p_user, pf->p_date, pf->p_time);
145177a8a0dfSeric 	if (pf->p_aux != NULL)
145277a8a0dfSeric 		fprintf(f, " %s", pf->p_aux);
145377a8a0dfSeric 	else
145477a8a0dfSeric 		fprintf(f, "\n");
145577a8a0dfSeric }
14560faf63f9Seric 
14570faf63f9Seric /*
1458095915d3Seric **  USRERR -- issue user-level error
1459095915d3Seric **
1460095915d3Seric **	Parameters:
1461095915d3Seric **		f -- format string.
1462095915d3Seric **		p1-p3 -- parameters to a printf.
1463095915d3Seric **
1464095915d3Seric **	Returns:
1465095915d3Seric **		-1
1466095915d3Seric **
1467095915d3Seric **	Side Effects:
1468095915d3Seric **		none.
1469095915d3Seric */
1470095915d3Seric 
1471fe7b004aSeric /*VARARGS1*/
1472095915d3Seric usrerr(f, p1, p2, p3)
1473095915d3Seric 	char *f;
1474095915d3Seric {
1475095915d3Seric 	fprintf(stderr, "\n%s: ", MyName);
1476095915d3Seric 	fprintf(stderr, f, p1, p2, p3);
1477095915d3Seric 	fprintf(stderr, "\n");
1478095915d3Seric 
1479095915d3Seric 	return (-1);
1480095915d3Seric }
14810faf63f9Seric 
14820faf63f9Seric /*
1483095915d3Seric **  SYSERR -- print system-generated error.
1484095915d3Seric **
1485095915d3Seric **	Parameters:
1486095915d3Seric **		f -- format string to a printf.
1487095915d3Seric **		p1, p2, p3 -- parameters to f.
1488095915d3Seric **
1489095915d3Seric **	Returns:
1490095915d3Seric **		never.
1491095915d3Seric **
1492095915d3Seric **	Side Effects:
1493095915d3Seric **		none.
1494095915d3Seric */
1495095915d3Seric 
1496fe7b004aSeric /*VARARGS1*/
1497095915d3Seric syserr(f, p1, p2, p3)
1498095915d3Seric 	char *f;
1499095915d3Seric {
1500095915d3Seric 	extern int errno;
1501095915d3Seric 
1502095915d3Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
1503095915d3Seric 	fprintf(stderr, f, p1, p2, p3);
1504095915d3Seric 	fprintf(stderr, "\n");
1505095915d3Seric 	if (errno == 0)
1506095915d3Seric 		exit(EX_SOFTWARE);
1507095915d3Seric 	else
1508095915d3Seric 	{
1509fe7b004aSeric 		perror(NULL);
1510095915d3Seric 		exit(EX_OSERR);
1511095915d3Seric 	}
1512095915d3Seric }
15131e0924d0Seric /*
15141e0924d0Seric **  USERNAME -- return name of the current user
15151e0924d0Seric **
15161e0924d0Seric **	Parameters:
15171e0924d0Seric **		none
15181e0924d0Seric **
15191e0924d0Seric **	Returns:
15201e0924d0Seric **		name of current user
15211e0924d0Seric **
15221e0924d0Seric **	Side Effects:
15231e0924d0Seric **		none
15241e0924d0Seric */
15251e0924d0Seric 
15261e0924d0Seric char *
15271e0924d0Seric username()
15281e0924d0Seric {
15291e0924d0Seric # ifdef UIDUSER
15301e0924d0Seric 	extern struct passwd *getpwuid();
15311e0924d0Seric 	register struct passwd *pw;
15321e0924d0Seric 
15331e0924d0Seric 	pw = getpwuid(getuid());
15341e0924d0Seric 	if (pw == NULL)
15351e0924d0Seric 	{
15361e0924d0Seric 		syserr("who are you? (uid=%d)", getuid());
15371e0924d0Seric 		exit(EX_OSERR);
15381e0924d0Seric 	}
15391e0924d0Seric 	return (pw->pw_name);
15401e0924d0Seric # else
1541ed320446Seric 	extern char *getlogin();
1542bb5509cbSmckusick 	register char *p;
1543ed320446Seric 
1544bb5509cbSmckusick 	p = getenv("USER");
1545bb5509cbSmckusick 	if (p == NULL || p[0] == '\0')
1546bb5509cbSmckusick 		p = getlogin();
1547bb5509cbSmckusick 	return (p);
15481e0924d0Seric # endif UIDUSER
15491e0924d0Seric }
1550f76dd34fSrrh 
1551f76dd34fSrrh /*
1552f76dd34fSrrh **	Guarded string manipulation routines; the last argument
1553f76dd34fSrrh **	is the length of the buffer into which the strcpy or strcat
1554f76dd34fSrrh **	is to be done.
1555f76dd34fSrrh */
1556f76dd34fSrrh char *gstrcat(to, from, length)
1557f76dd34fSrrh 	char	*to, *from;
1558f76dd34fSrrh 	int	length;
1559f76dd34fSrrh {
1560f76dd34fSrrh 	if (strlen(from) + strlen(to) >= length) {
1561f76dd34fSrrh 		gstrbotch(to, from);
1562f76dd34fSrrh 	}
1563f76dd34fSrrh 	return(strcat(to, from));
1564f76dd34fSrrh }
1565f76dd34fSrrh 
1566f76dd34fSrrh char *gstrncat(to, from, n, length)
1567f76dd34fSrrh 	char	*to, *from;
1568f76dd34fSrrh 	int	n;
1569f76dd34fSrrh 	int	length;
1570f76dd34fSrrh {
1571f76dd34fSrrh 	if (n + strlen(to) >= length) {
1572f76dd34fSrrh 		gstrbotch(to, from);
1573f76dd34fSrrh 	}
1574f76dd34fSrrh 	return(strncat(to, from, n));
1575f76dd34fSrrh }
1576f76dd34fSrrh 
1577f76dd34fSrrh char *gstrcpy(to, from, length)
1578f76dd34fSrrh 	char	*to, *from;
1579f76dd34fSrrh 	int	length;
1580f76dd34fSrrh {
1581f76dd34fSrrh 	if (strlen(from) >= length) {
1582f76dd34fSrrh 		gstrbotch(from, (char *)0);
1583f76dd34fSrrh 	}
1584f76dd34fSrrh 	return(strcpy(to, from));
1585f76dd34fSrrh }
1586f76dd34fSrrh gstrbotch(str1, str2)
1587f76dd34fSrrh 	char	*str1, *str2;
1588f76dd34fSrrh {
1589f76dd34fSrrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
1590f76dd34fSrrh }
1591