xref: /original-bsd/usr.bin/sccs/sccs.c (revision 68dc9f68)
13bbbf6e6Sdist /*
23bbbf6e6Sdist  * Copyright (c) 1980 Regents of the University of California.
3*68dc9f68Sbostic  * All rights reserved.
4*68dc9f68Sbostic  *
5*68dc9f68Sbostic  * Redistribution and use in source and binary forms are permitted
6*68dc9f68Sbostic  * provided that this notice is preserved and that due credit is given
7*68dc9f68Sbostic  * to the University of California at Berkeley. The name of the University
8*68dc9f68Sbostic  * may not be used to endorse or promote products derived from this
9*68dc9f68Sbostic  * software without specific prior written permission. This software
10*68dc9f68Sbostic  * is provided ``as is'' without express or implied warranty.
113bbbf6e6Sdist  */
123bbbf6e6Sdist 
136d5ea7f2Ssam #ifndef lint
143bbbf6e6Sdist char copyright[] =
153bbbf6e6Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
163bbbf6e6Sdist  All rights reserved.\n";
17*68dc9f68Sbostic #endif /* not lint */
183bbbf6e6Sdist 
193bbbf6e6Sdist #ifndef lint
20*68dc9f68Sbostic static char sccsid[] = "@(#)sccs.c	5.5 (Berkeley) 05/05/88";
21*68dc9f68Sbostic #endif /* not lint */
226d5ea7f2Ssam 
23adf8f7d4Seric # include <stdio.h>
24683375a0Smckusick # include <sys/param.h>
25adf8f7d4Seric # include <sys/stat.h>
266d5ea7f2Ssam # include <sys/dir.h>
2778d41c6aSeric # include <errno.h>
2878d41c6aSeric # include <signal.h>
29adf8f7d4Seric # include <sysexits.h>
30393e27f2Seric # include <pwd.h>
31adf8f7d4Seric 
3210cd16efSeric /*
3310cd16efSeric **  SCCS.C -- human-oriented front end to the SCCS system.
3410cd16efSeric **
3510cd16efSeric **	Without trying to add any functionality to speak of, this
3610cd16efSeric **	program tries to make SCCS a little more accessible to human
3710cd16efSeric **	types.  The main thing it does is automatically put the
3810cd16efSeric **	string "SCCS/s." on the front of names.  Also, it has a
3910cd16efSeric **	couple of things that are designed to shorten frequent
4010cd16efSeric **	combinations, e.g., "delget" which expands to a "delta"
4110cd16efSeric **	and a "get".
4210cd16efSeric **
4310cd16efSeric **	This program can also function as a setuid front end.
4410cd16efSeric **	To do this, you should copy the source, renaming it to
4510cd16efSeric **	whatever you want, e.g., "syssccs".  Change any defaults
4610cd16efSeric **	in the program (e.g., syssccs might default -d to
4710cd16efSeric **	"/usr/src/sys").  Then recompile and put the result
4810cd16efSeric **	as setuid to whomever you want.  In this mode, sccs
4910cd16efSeric **	knows to not run setuid for certain programs in order
5010cd16efSeric **	to preserve security, and so forth.
5110cd16efSeric **
5210cd16efSeric **	Usage:
5310cd16efSeric **		sccs [flags] command [args]
5410cd16efSeric **
5510cd16efSeric **	Flags:
5610cd16efSeric **		-d<dir>		<dir> represents a directory to search
5710cd16efSeric **				out of.  It should be a full pathname
5810cd16efSeric **				for general usage.  E.g., if <dir> is
5910cd16efSeric **				"/usr/src/sys", then a reference to the
6010cd16efSeric **				file "dev/bio.c" becomes a reference to
6110cd16efSeric **				"/usr/src/sys/dev/bio.c".
6210cd16efSeric **		-p<path>	prepends <path> to the final component
6310cd16efSeric **				of the pathname.  By default, this is
6410cd16efSeric **				"SCCS".  For example, in the -d example
6510cd16efSeric **				above, the path then gets modified to
6610cd16efSeric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
6710cd16efSeric **				more common usage (without the -d flag),
6810cd16efSeric **				"prog.c" would get modified to
6910cd16efSeric **				"SCCS/s.prog.c".  In both cases, the
7010cd16efSeric **				"s." gets automatically prepended.
7110cd16efSeric **		-r		run as the real user.
7210cd16efSeric **
7310cd16efSeric **	Commands:
7410cd16efSeric **		admin,
7510cd16efSeric **		get,
7610cd16efSeric **		delta,
7710cd16efSeric **		rmdel,
786e36cbf6Sbostic **		cdc,
7910cd16efSeric **		etc.		Straight out of SCCS; only difference
8010cd16efSeric **				is that pathnames get modified as
8110cd16efSeric **				described above.
826e36cbf6Sbostic **		enter		Front end doing "sccs admin -i<name> <name>"
836e36cbf6Sbostic **		create		Macro for "enter" followed by "get".
8410cd16efSeric **		edit		Macro for "get -e".
8510cd16efSeric **		unedit		Removes a file being edited, knowing
8610cd16efSeric **				about p-files, etc.
8710cd16efSeric **		delget		Macro for "delta" followed by "get".
8810cd16efSeric **		deledit		Macro for "delta" followed by "get -e".
896e36cbf6Sbostic **		branch		Macro for "get -b -e", followed by "delta
906e36cbf6Sbostic **				-s -n", followd by "get -e -t -g".
916e36cbf6Sbostic **		diffs		"diff" the specified version of files
926e36cbf6Sbostic **				and the checked-out version.
936e36cbf6Sbostic **		print		Macro for "prs -e" followed by "get -p -m".
946e36cbf6Sbostic **		tell		List what files are being edited.
956e36cbf6Sbostic **		info		Print information about files being edited.
9610cd16efSeric **		clean		Remove all files that can be
9710cd16efSeric **				regenerated from SCCS files.
98095915d3Seric **		check		Like info, but return exit status, for
9910cd16efSeric **				use in makefiles.
10010cd16efSeric **		fix		Remove a top delta & reedit, but save
10110cd16efSeric **				the previous changes in that delta.
10210cd16efSeric **
10310cd16efSeric **	Compilation Flags:
10410cd16efSeric **		UIDUSER -- determine who the user is by looking at the
10510cd16efSeric **			uid rather than the login name -- for machines
10610cd16efSeric **			where SCCS gets the user in this way.
1077abec564Seric **		SCCSDIR -- if defined, forces the -d flag to take on
108095915d3Seric **			this value.  This is so that the setuid
109095915d3Seric **			aspects of this program cannot be abused.
1107abec564Seric **			This flag also disables the -p flag.
1117abec564Seric **		SCCSPATH -- the default for the -p flag.
112c4ad8825Seric **		MYNAME -- the title this program should print when it
113c4ad8825Seric **			gives error messages.
11410cd16efSeric **
11510cd16efSeric **	Compilation Instructions:
11610cd16efSeric **		cc -O -n -s sccs.c
117c4ad8825Seric **		The flags listed above can be -D defined to simplify
118c4ad8825Seric **			recompilation for variant versions.
11910cd16efSeric **
12010cd16efSeric **	Author:
12110cd16efSeric **		Eric Allman, UCB/INGRES
1227abec564Seric **		Copyright 1980 Regents of the University of California
12310cd16efSeric */
12410cd16efSeric 
1250faf63f9Seric 
1267abec564Seric /*******************  Configuration Information  ********************/
1277abec564Seric 
128c4ad8825Seric # ifndef SCCSPATH
129c4ad8825Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
130c4ad8825Seric # endif NOT SCCSPATH
131c4ad8825Seric 
132c4ad8825Seric # ifndef MYNAME
133c4ad8825Seric # define MYNAME		"sccs"	/* name used for printing errors */
134c4ad8825Seric # endif NOT MYNAME
1357abec564Seric 
1360faf63f9Seric # ifndef PROGPATH
137683375a0Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1380faf63f9Seric # endif PROGPATH
1390faf63f9Seric 
1407abec564Seric /****************  End of Configuration Information  ****************/
1410faf63f9Seric 
142c4432be4Seric typedef char	bool;
1437de81dc7Seric # define TRUE	1
1447de81dc7Seric # define FALSE	0
145d02a4f42Seric 
146ff038098Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
147ff038098Seric 
148adf8f7d4Seric struct sccsprog
149adf8f7d4Seric {
150adf8f7d4Seric 	char	*sccsname;	/* name of SCCS routine */
1517de81dc7Seric 	short	sccsoper;	/* opcode, see below */
1527de81dc7Seric 	short	sccsflags;	/* flags, see below */
153adf8f7d4Seric 	char	*sccspath;	/* pathname of binary implementing */
154adf8f7d4Seric };
155adf8f7d4Seric 
1567de81dc7Seric /* values for sccsoper */
1577de81dc7Seric # define PROG		0	/* call a program */
1581777fbcbSeric # define CMACRO		1	/* command substitution macro */
159172147faSeric # define FIX		2	/* fix a delta */
160b2538d76Seric # define CLEAN		3	/* clean out recreatable files */
161e39a5722Seric # define UNEDIT		4	/* unedit a file */
162419708b0Seric # define SHELL		5	/* call a shell file (like PROG) */
16378d41c6aSeric # define DIFFS		6	/* diff between sccs & file out */
1642569d37aSeric # define DODIFF		7	/* internal call to diff program */
16544c626a5Srrh # define ENTER		8	/* enter new files */
1667de81dc7Seric 
167c4432be4Seric /* bits for sccsflags */
1687de81dc7Seric # define NO_SDOT	0001	/* no s. on front of args */
1697de81dc7Seric # define REALUSER	0002	/* protected (e.g., admin) */
170adf8f7d4Seric 
17148ff0e7eSeric /* modes for the "clean", "info", "check" ops */
17248ff0e7eSeric # define CLEANC		0	/* clean command */
17348ff0e7eSeric # define INFOC		1	/* info command */
17448ff0e7eSeric # define CHECKC		2	/* check command */
1752734e237Seric # define TELLC		3	/* give list of files being edited */
17648ff0e7eSeric 
1770faf63f9Seric /*
1780faf63f9Seric **  Description of commands known to this program.
1790faf63f9Seric **	First argument puts the command into a class.  Second arg is
1800faf63f9Seric **	info regarding treatment of this command.  Third arg is a
1810faf63f9Seric **	list of flags this command accepts from macros, etc.  Fourth
1820faf63f9Seric **	arg is the pathname of the implementing program, or the
1830faf63f9Seric **	macro definition, or the arg to a sub-algorithm.
1840faf63f9Seric */
185b5d4f080Seric 
186adf8f7d4Seric struct sccsprog SccsProg[] =
187adf8f7d4Seric {
1883642a8d2Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1896e36cbf6Sbostic 	"cdc",		PROG,	0,			PROGPATH(rmdel),
1903642a8d2Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1913642a8d2Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1923642a8d2Seric 	"get",		PROG,	0,			PROGPATH(get),
1933642a8d2Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1943a3bf7bfSeric 	"prs",		PROG,	0,			PROGPATH(prs),
1953642a8d2Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1963642a8d2Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1973a3bf7bfSeric 	"val",		PROG,	0,			PROGPATH(val),
1983642a8d2Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1993642a8d2Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
2003642a8d2Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
20159e81a43Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
2021cf60de3Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
2033642a8d2Seric 	"fix",		FIX,	NO_SDOT,		NULL,
2041e0924d0Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
2051e0924d0Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
2061e0924d0Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
2071e0924d0Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
2083642a8d2Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
2093642a8d2Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
2102569d37aSeric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
2116e36cbf6Sbostic 	"print",	CMACRO,	0,			"prs -e/get -p -m -s",
212a4f81e73Seric 	"branch",	CMACRO,	NO_SDOT,
213a4f81e73Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
21444c626a5Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
21544c626a5Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
2163642a8d2Seric 	NULL,		-1,	0,			NULL
217adf8f7d4Seric };
218adf8f7d4Seric 
2190faf63f9Seric /* one line from a p-file */
220e39a5722Seric struct pfile
221e39a5722Seric {
222e39a5722Seric 	char	*p_osid;	/* old SID */
223e39a5722Seric 	char	*p_nsid;	/* new SID */
224e39a5722Seric 	char	*p_user;	/* user who did edit */
225e39a5722Seric 	char	*p_date;	/* date of get */
226e39a5722Seric 	char	*p_time;	/* time of get */
22777a8a0dfSeric 	char	*p_aux;		/* extra info at end */
228e39a5722Seric };
229e39a5722Seric 
2307abec564Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2317abec564Seric # ifdef SCCSDIR
2327abec564Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
233095915d3Seric # else
2347abec564Seric char	*SccsDir = "";
235095915d3Seric # endif
236c4ad8825Seric char	MyName[] = MYNAME;	/* name used in messages */
23778d41c6aSeric int	OutFile = -1;		/* override output file for commands */
238c4432be4Seric bool	RealUser;		/* if set, running as real user */
239f0cc3246Seric # ifdef DEBUG
240f0cc3246Seric bool	Debug;			/* turn on tracing */
241f0cc3246Seric # endif
24292fabd68Seric # ifndef V6
24392fabd68Seric extern char	*getenv();
24492fabd68Seric # endif V6
245f76dd34fSrrh 
2466e36cbf6Sbostic extern char	*sys_siglist[];
2476e36cbf6Sbostic 
248f76dd34fSrrh char *gstrcat(), *strcat();
249f76dd34fSrrh char *gstrncat(), *strncat();
250f76dd34fSrrh char *gstrcpy(), *strcpy();
251f76dd34fSrrh #define	FBUFSIZ	BUFSIZ
252f76dd34fSrrh #define	PFILELG	120
2530faf63f9Seric 
254adf8f7d4Seric main(argc, argv)
255adf8f7d4Seric 	int argc;
256adf8f7d4Seric 	char **argv;
257adf8f7d4Seric {
258adf8f7d4Seric 	register char *p;
259b1ed8a43Seric 	extern struct sccsprog *lookup();
26041290c52Seric 	register int i;
26192fabd68Seric # ifndef V6
26292fabd68Seric # ifndef SCCSDIR
263393e27f2Seric 	register struct passwd *pw;
264393e27f2Seric 	extern struct passwd *getpwnam();
265f76dd34fSrrh 	char buf[FBUFSIZ];
266393e27f2Seric 
26792fabd68Seric 	/* pull "SccsDir" out of the environment (possibly) */
268250566abSeric 	p = getenv("PROJECTDIR");
269393e27f2Seric 	if (p != NULL && p[0] != '\0')
270393e27f2Seric 	{
27192fabd68Seric 		if (p[0] == '/')
27292fabd68Seric 			SccsDir = p;
27392fabd68Seric 		else
274393e27f2Seric 		{
275393e27f2Seric 			pw = getpwnam(p);
276393e27f2Seric 			if (pw == NULL)
277393e27f2Seric 			{
278393e27f2Seric 				usrerr("user %s does not exist", p);
279393e27f2Seric 				exit(EX_USAGE);
280393e27f2Seric 			}
281f76dd34fSrrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
282f76dd34fSrrh 			gstrcat(buf, "/src", sizeof(buf));
283393e27f2Seric 			if (access(buf, 0) < 0)
284393e27f2Seric 			{
285f76dd34fSrrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
286f76dd34fSrrh 				gstrcat(buf, "/source", sizeof(buf));
287393e27f2Seric 				if (access(buf, 0) < 0)
288393e27f2Seric 				{
289393e27f2Seric 					usrerr("project %s has no source!", p);
290393e27f2Seric 					exit(EX_USAGE);
291393e27f2Seric 				}
292393e27f2Seric 			}
293393e27f2Seric 			SccsDir = buf;
294393e27f2Seric 		}
295393e27f2Seric 	}
29692fabd68Seric # endif SCCSDIR
29792fabd68Seric # endif V6
29892fabd68Seric 
299adf8f7d4Seric 	/*
300adf8f7d4Seric 	**  Detect and decode flags intended for this program.
301adf8f7d4Seric 	*/
302adf8f7d4Seric 
3037de81dc7Seric 	if (argc < 2)
304adf8f7d4Seric 	{
305095915d3Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
3067de81dc7Seric 		exit(EX_USAGE);
3077de81dc7Seric 	}
3087de81dc7Seric 	argv[argc] = NULL;
3097de81dc7Seric 
310b1ed8a43Seric 	if (lookup(argv[0]) == NULL)
311b1ed8a43Seric 	{
3127de81dc7Seric 		while ((p = *++argv) != NULL)
3137de81dc7Seric 		{
314adf8f7d4Seric 			if (*p != '-')
315adf8f7d4Seric 				break;
316adf8f7d4Seric 			switch (*++p)
317adf8f7d4Seric 			{
318adf8f7d4Seric 			  case 'r':		/* run as real user */
319adf8f7d4Seric 				setuid(getuid());
320c4432be4Seric 				RealUser++;
321adf8f7d4Seric 				break;
322adf8f7d4Seric 
3237abec564Seric # ifndef SCCSDIR
324adf8f7d4Seric 			  case 'p':		/* path of sccs files */
325adf8f7d4Seric 				SccsPath = ++p;
3266c7454d2Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3276c7454d2Seric 					SccsPath = *++argv;
328adf8f7d4Seric 				break;
329adf8f7d4Seric 
3300a8b9ab0Seric 			  case 'd':		/* directory to search from */
3310a8b9ab0Seric 				SccsDir = ++p;
3326c7454d2Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3336c7454d2Seric 					SccsDir = *++argv;
3340a8b9ab0Seric 				break;
335095915d3Seric # endif
3360a8b9ab0Seric 
337f0cc3246Seric # ifdef DEBUG
338f0cc3246Seric 			  case 'T':		/* trace */
339f0cc3246Seric 				Debug++;
340f0cc3246Seric 				break;
341f0cc3246Seric # endif
342f0cc3246Seric 
343adf8f7d4Seric 			  default:
344095915d3Seric 				usrerr("unknown option -%s", p);
345adf8f7d4Seric 				break;
346adf8f7d4Seric 			}
347adf8f7d4Seric 		}
3485cabffd9Seric 		if (SccsPath[0] == '\0')
3495cabffd9Seric 			SccsPath = ".";
350b1ed8a43Seric 	}
351adf8f7d4Seric 
3523642a8d2Seric 	i = command(argv, FALSE, "");
35341290c52Seric 	exit(i);
3547de81dc7Seric }
3550faf63f9Seric 
3560faf63f9Seric /*
35741290c52Seric **  COMMAND -- look up and perform a command
35841290c52Seric **
35941290c52Seric **	This routine is the guts of this program.  Given an
36041290c52Seric **	argument vector, it looks up the "command" (argv[0])
36141290c52Seric **	in the configuration table and does the necessary stuff.
36241290c52Seric **
36341290c52Seric **	Parameters:
36441290c52Seric **		argv -- an argument vector to process.
36541290c52Seric **		forkflag -- if set, fork before executing the command.
366108d6082Seric **		editflag -- if set, only include flags listed in the
367108d6082Seric **			sccsklets field of the command descriptor.
368108d6082Seric **		arg0 -- a space-seperated list of arguments to insert
369108d6082Seric **			before argv.
37041290c52Seric **
37141290c52Seric **	Returns:
37241290c52Seric **		zero -- command executed ok.
37341290c52Seric **		else -- error status.
37441290c52Seric **
37541290c52Seric **	Side Effects:
37641290c52Seric **		none.
37741290c52Seric */
3787de81dc7Seric 
3793642a8d2Seric command(argv, forkflag, arg0)
3807de81dc7Seric 	char **argv;
3811777fbcbSeric 	bool forkflag;
382108d6082Seric 	char *arg0;
3837de81dc7Seric {
3847de81dc7Seric 	register struct sccsprog *cmd;
3857de81dc7Seric 	register char *p;
386f76dd34fSrrh 	char buf[FBUFSIZ];
387b1ed8a43Seric 	extern struct sccsprog *lookup();
388108d6082Seric 	char *nav[1000];
389108d6082Seric 	char **np;
390419708b0Seric 	register char **ap;
391d51cd7e5Seric 	register int i;
392419708b0Seric 	register char *q;
393d51cd7e5Seric 	extern bool unedit();
39441290c52Seric 	int rval = 0;
395108d6082Seric 	extern char *index();
396108d6082Seric 	extern char *makefile();
3973642a8d2Seric 	char *editchs;
398287bbaa2Seric 	extern char *tail();
399f0cc3246Seric 
400f0cc3246Seric # ifdef DEBUG
401f0cc3246Seric 	if (Debug)
402f0cc3246Seric 	{
403108d6082Seric 		printf("command:\n\t\"%s\"\n", arg0);
404108d6082Seric 		for (np = argv; *np != NULL; np++)
405108d6082Seric 			printf("\t\"%s\"\n", *np);
406f0cc3246Seric 	}
407f0cc3246Seric # endif
408c4432be4Seric 
409c4432be4Seric 	/*
410108d6082Seric 	**  Copy arguments.
411ff038098Seric 	**	Copy from arg0 & if necessary at most one arg
412ff038098Seric 	**	from argv[0].
413adf8f7d4Seric 	*/
414adf8f7d4Seric 
415419708b0Seric 	np = ap = &nav[1];
4163642a8d2Seric 	editchs = NULL;
41759e81a43Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
418108d6082Seric 	{
419108d6082Seric 		*np++ = q;
420108d6082Seric 		while (*p == ' ')
421108d6082Seric 			p++;
4223642a8d2Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
423108d6082Seric 			*q++ = *p++;
424108d6082Seric 		*q++ = '\0';
4253642a8d2Seric 		if (*p == ':')
4263642a8d2Seric 		{
4273642a8d2Seric 			editchs = q;
42859e81a43Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4293642a8d2Seric 				*q++ = *p;
4303642a8d2Seric 			*q++ = '\0';
4313642a8d2Seric 		}
432108d6082Seric 	}
433108d6082Seric 	*np = NULL;
434419708b0Seric 	if (*ap == NULL)
435108d6082Seric 		*np++ = *argv++;
436108d6082Seric 
437108d6082Seric 	/*
438108d6082Seric 	**  Look up command.
439419708b0Seric 	**	At this point, *ap is the command name.
440108d6082Seric 	*/
441108d6082Seric 
442419708b0Seric 	cmd = lookup(*ap);
443b1ed8a43Seric 	if (cmd == NULL)
444adf8f7d4Seric 	{
445419708b0Seric 		usrerr("Unknown command \"%s\"", *ap);
44641290c52Seric 		return (EX_USAGE);
447adf8f7d4Seric 	}
448adf8f7d4Seric 
449adf8f7d4Seric 	/*
450108d6082Seric 	**  Copy remaining arguments doing editing as appropriate.
451108d6082Seric 	*/
452108d6082Seric 
453108d6082Seric 	for (; *argv != NULL; argv++)
454108d6082Seric 	{
455108d6082Seric 		p = *argv;
456108d6082Seric 		if (*p == '-')
457108d6082Seric 		{
4583642a8d2Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
459108d6082Seric 				*np++ = p;
460108d6082Seric 		}
461108d6082Seric 		else
462108d6082Seric 		{
463108d6082Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
464108d6082Seric 				p = makefile(p);
465108d6082Seric 			if (p != NULL)
466108d6082Seric 				*np++ = p;
467108d6082Seric 		}
468108d6082Seric 	}
469108d6082Seric 	*np = NULL;
470108d6082Seric 
471108d6082Seric 	/*
4727de81dc7Seric 	**  Interpret operation associated with this command.
473c4432be4Seric 	*/
474c4432be4Seric 
4757de81dc7Seric 	switch (cmd->sccsoper)
4767de81dc7Seric 	{
477419708b0Seric 	  case SHELL:		/* call a shell file */
478419708b0Seric 		*ap = cmd->sccspath;
479419708b0Seric 		*--ap = "sh";
480419708b0Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
481419708b0Seric 		break;
482419708b0Seric 
4837de81dc7Seric 	  case PROG:		/* call an sccs prog */
484419708b0Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
4851777fbcbSeric 		break;
4861777fbcbSeric 
4871777fbcbSeric 	  case CMACRO:		/* command macro */
488ff038098Seric 		/* step through & execute each part of the macro */
4891777fbcbSeric 		for (p = cmd->sccspath; *p != '\0'; p++)
4901777fbcbSeric 		{
491108d6082Seric 			q = p;
492108d6082Seric 			while (*p != '\0' && *p != '/')
493108d6082Seric 				p++;
4943642a8d2Seric 			rval = command(&ap[1], *p != '\0', q);
49541290c52Seric 			if (rval != 0)
49641290c52Seric 				break;
4971777fbcbSeric 		}
49841290c52Seric 		break;
4997de81dc7Seric 
500172147faSeric 	  case FIX:		/* fix a delta */
5016e36cbf6Sbostic 		if (ap[1]==0 || strncmp(ap[1], "-r", 2)!=0)
502172147faSeric 		{
503095915d3Seric 			usrerr("-r flag needed for fix command");
50441290c52Seric 			rval = EX_USAGE;
505172147faSeric 			break;
506172147faSeric 		}
507ff038098Seric 
508ff038098Seric 		/* get the version with all changes */
5093642a8d2Seric 		rval = command(&ap[1], TRUE, "get -k");
510ff038098Seric 
511ff038098Seric 		/* now remove that version from the s-file */
51241290c52Seric 		if (rval == 0)
5133642a8d2Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
514ff038098Seric 
515ff038098Seric 		/* and edit the old version (but don't clobber new vers) */
51641290c52Seric 		if (rval == 0)
5173642a8d2Seric 			rval = command(&ap[2], FALSE, "get -e -g");
51841290c52Seric 		break;
519172147faSeric 
520b2538d76Seric 	  case CLEAN:
521ec4190ddSeric 		rval = clean((int) cmd->sccspath, ap);
522b2538d76Seric 		break;
523b2538d76Seric 
524e39a5722Seric 	  case UNEDIT:
525419708b0Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
526d51cd7e5Seric 		{
527108d6082Seric 			if (unedit(*argv))
528108d6082Seric 				*np++ = *argv;
529d51cd7e5Seric 		}
530108d6082Seric 		*np = NULL;
531ff038098Seric 
532ff038098Seric 		/* get all the files that we unedited successfully */
533fe7b004aSeric 		if (np > &ap[1])
5343642a8d2Seric 			rval = command(&ap[1], FALSE, "get");
535e39a5722Seric 		break;
536e39a5722Seric 
53778d41c6aSeric 	  case DIFFS:		/* diff between s-file & edit file */
53878d41c6aSeric 		/* find the end of the flag arguments */
53978d41c6aSeric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
54078d41c6aSeric 			continue;
54178d41c6aSeric 		argv = np;
54278d41c6aSeric 
54378d41c6aSeric 		/* for each file, do the diff */
54496370756Seric 		p = argv[1];
54578d41c6aSeric 		while (*np != NULL)
54678d41c6aSeric 		{
547ff038098Seric 			/* messy, but we need a null terminated argv */
54878d41c6aSeric 			*argv = *np++;
54996370756Seric 			argv[1] = NULL;
550287bbaa2Seric 			i = dodiff(ap, tail(*argv));
55178d41c6aSeric 			if (rval == 0)
55278d41c6aSeric 				rval = i;
55396370756Seric 			argv[1] = p;
55478d41c6aSeric 		}
55578d41c6aSeric 		break;
55678d41c6aSeric 
5572569d37aSeric 	  case DODIFF:		/* internal diff call */
5582569d37aSeric 		setuid(getuid());
5592569d37aSeric 		for (np = ap; *np != NULL; np++)
5602569d37aSeric 		{
5612569d37aSeric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5622569d37aSeric 				(*np)[1] = 'c';
5632569d37aSeric 		}
5642569d37aSeric 
5652569d37aSeric 		/* insert "-" argument */
5662569d37aSeric 		np[1] = NULL;
5672569d37aSeric 		np[0] = np[-1];
5682569d37aSeric 		np[-1] = "-";
5692569d37aSeric 
5702569d37aSeric 		/* execute the diff program of choice */
5712569d37aSeric # ifndef V6
5722569d37aSeric 		execvp("diff", ap);
5732569d37aSeric # endif
5742569d37aSeric 		execv(cmd->sccspath, argv);
5752569d37aSeric 		syserr("cannot exec %s", cmd->sccspath);
5762569d37aSeric 		exit(EX_OSERR);
5772569d37aSeric 
57844c626a5Srrh 	  case ENTER:		/* enter new sccs files */
579bb5509cbSmckusick 		/* skip over flag arguments */
580bb5509cbSmckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
581bb5509cbSmckusick 			continue;
582bb5509cbSmckusick 		argv = np;
583bb5509cbSmckusick 
584bb5509cbSmckusick 		/* do an admin for each file */
585bb5509cbSmckusick 		p = argv[1];
586bb5509cbSmckusick 		while (*np != NULL)
587bb5509cbSmckusick 		{
588bb5509cbSmckusick 			printf("\n%s:\n", *np);
589f76dd34fSrrh 			strcpy(buf, "-i");
590f76dd34fSrrh 			gstrcat(buf, *np, sizeof(buf));
591bb5509cbSmckusick 			ap[0] = buf;
592bb5509cbSmckusick 			argv[0] = tail(*np);
593bb5509cbSmckusick 			argv[1] = NULL;
594bb5509cbSmckusick 			rval = command(ap, TRUE, "admin");
595bb5509cbSmckusick 			argv[1] = p;
596bb5509cbSmckusick 			if (rval == 0)
597bb5509cbSmckusick 			{
598f76dd34fSrrh 				strcpy(buf, ",");
599f76dd34fSrrh 				gstrcat(buf, tail(*np), sizeof(buf));
600bb5509cbSmckusick 				if (link(*np, buf) >= 0)
601bb5509cbSmckusick 					unlink(*np);
602bb5509cbSmckusick 			}
603bb5509cbSmckusick 			np++;
604bb5509cbSmckusick 		}
605bb5509cbSmckusick 		break;
606bb5509cbSmckusick 
6077de81dc7Seric 	  default:
608095915d3Seric 		syserr("oper %d", cmd->sccsoper);
6097de81dc7Seric 		exit(EX_SOFTWARE);
6107de81dc7Seric 	}
61141290c52Seric # ifdef DEBUG
61241290c52Seric 	if (Debug)
61341290c52Seric 		printf("command: rval=%d\n", rval);
61441290c52Seric # endif
61541290c52Seric 	return (rval);
6167de81dc7Seric }
6170faf63f9Seric 
6180faf63f9Seric /*
619b1ed8a43Seric **  LOOKUP -- look up an SCCS command name.
620b1ed8a43Seric **
621b1ed8a43Seric **	Parameters:
622b1ed8a43Seric **		name -- the name of the command to look up.
623b1ed8a43Seric **
624b1ed8a43Seric **	Returns:
625b1ed8a43Seric **		ptr to command descriptor for this command.
626b1ed8a43Seric **		NULL if no such entry.
627b1ed8a43Seric **
628b1ed8a43Seric **	Side Effects:
629b1ed8a43Seric **		none.
630b1ed8a43Seric */
631b1ed8a43Seric 
632b1ed8a43Seric struct sccsprog *
633b1ed8a43Seric lookup(name)
634b1ed8a43Seric 	char *name;
635b1ed8a43Seric {
636b1ed8a43Seric 	register struct sccsprog *cmd;
637b1ed8a43Seric 
638b1ed8a43Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
639b1ed8a43Seric 	{
640b1ed8a43Seric 		if (strcmp(cmd->sccsname, name) == 0)
641b1ed8a43Seric 			return (cmd);
642b1ed8a43Seric 	}
643b1ed8a43Seric 	return (NULL);
644b1ed8a43Seric }
6450faf63f9Seric 
6460faf63f9Seric /*
64741290c52Seric **  CALLPROG -- call a program
64841290c52Seric **
649108d6082Seric **	Used to call the SCCS programs.
65041290c52Seric **
65141290c52Seric **	Parameters:
65241290c52Seric **		progpath -- pathname of the program to call.
65341290c52Seric **		flags -- status flags from the command descriptors.
65441290c52Seric **		argv -- an argument vector to pass to the program.
65541290c52Seric **		forkflag -- if true, fork before calling, else just
65641290c52Seric **			exec.
65741290c52Seric **
65841290c52Seric **	Returns:
65941290c52Seric **		The exit status of the program.
66041290c52Seric **		Nothing if forkflag == FALSE.
66141290c52Seric **
66241290c52Seric **	Side Effects:
66341290c52Seric **		Can exit if forkflag == FALSE.
66441290c52Seric */
665172147faSeric 
6667de81dc7Seric callprog(progpath, flags, argv, forkflag)
6677de81dc7Seric 	char *progpath;
6687de81dc7Seric 	short flags;
6697de81dc7Seric 	char **argv;
6707de81dc7Seric 	bool forkflag;
6717de81dc7Seric {
6727de81dc7Seric 	register int i;
6736e36cbf6Sbostic 	register int wpid;
6741777fbcbSeric 	auto int st;
6756e36cbf6Sbostic 	register int sigcode;
6766e36cbf6Sbostic 	register int coredumped;
6776e36cbf6Sbostic 	register char *sigmsg;
6786e36cbf6Sbostic 	auto char sigmsgbuf[10+1];	/* "Signal 127" + terminating '\0' */
679108d6082Seric 
680108d6082Seric # ifdef DEBUG
681108d6082Seric 	if (Debug)
682108d6082Seric 	{
683108d6082Seric 		printf("callprog:\n");
684108d6082Seric 		for (i = 0; argv[i] != NULL; i++)
685108d6082Seric 			printf("\t\"%s\"\n", argv[i]);
686108d6082Seric 	}
687108d6082Seric # endif
6887de81dc7Seric 
6897de81dc7Seric 	if (*argv == NULL)
6907de81dc7Seric 		return (-1);
691c4432be4Seric 
692c4432be4Seric 	/*
693172147faSeric 	**  Fork if appropriate.
694adf8f7d4Seric 	*/
695adf8f7d4Seric 
6967de81dc7Seric 	if (forkflag)
6977de81dc7Seric 	{
698f0cc3246Seric # ifdef DEBUG
699f0cc3246Seric 		if (Debug)
700f0cc3246Seric 			printf("Forking\n");
701f0cc3246Seric # endif
7027de81dc7Seric 		i = fork();
7037de81dc7Seric 		if (i < 0)
7047de81dc7Seric 		{
705095915d3Seric 			syserr("cannot fork");
7067de81dc7Seric 			exit(EX_OSERR);
7077de81dc7Seric 		}
7087de81dc7Seric 		else if (i > 0)
7091777fbcbSeric 		{
7106e36cbf6Sbostic 			while ((wpid = wait(&st)) != -1 && wpid != i)
7116e36cbf6Sbostic 				;
7126e36cbf6Sbostic 			if ((sigcode = st & 0377) == 0)
71341290c52Seric 				st = (st >> 8) & 0377;
7146e36cbf6Sbostic 			else
7156e36cbf6Sbostic 			{
7166e36cbf6Sbostic 				coredumped = sigcode & 0200;
7176e36cbf6Sbostic 				sigcode &= 0177;
7186e36cbf6Sbostic 				if (sigcode != SIGINT && sigcode != SIGPIPE)
7196e36cbf6Sbostic 				{
7206e36cbf6Sbostic 					if (sigcode < NSIG)
7216e36cbf6Sbostic 						sigmsg = sys_siglist[sigcode];
7226e36cbf6Sbostic 					else
7236e36cbf6Sbostic 					{
7246e36cbf6Sbostic 						sprintf(sigmsgbuf, "Signal %d",
7256e36cbf6Sbostic 						    sigcode);
7266e36cbf6Sbostic 						sigmsg = sigmsgbuf;
7276e36cbf6Sbostic 					}
7286e36cbf6Sbostic 					fprintf(stderr, "sccs: %s: %s%s", argv[0],
7296e36cbf6Sbostic 					    sigmsg,
7306e36cbf6Sbostic 					    coredumped ? " - core dumped": "");
7316e36cbf6Sbostic 				}
7326e36cbf6Sbostic 				st = EX_SOFTWARE;
7336e36cbf6Sbostic 			}
73478d41c6aSeric 			if (OutFile >= 0)
73578d41c6aSeric 			{
73678d41c6aSeric 				close(OutFile);
73778d41c6aSeric 				OutFile = -1;
73878d41c6aSeric 			}
7391777fbcbSeric 			return (st);
7401777fbcbSeric 		}
7417de81dc7Seric 	}
74278d41c6aSeric 	else if (OutFile >= 0)
74378d41c6aSeric 	{
74478d41c6aSeric 		syserr("callprog: setting stdout w/o forking");
74578d41c6aSeric 		exit(EX_SOFTWARE);
74678d41c6aSeric 	}
7477de81dc7Seric 
74878d41c6aSeric 	/* set protection as appropriate */
7497de81dc7Seric 	if (bitset(REALUSER, flags))
7507de81dc7Seric 		setuid(getuid());
7517de81dc7Seric 
75278d41c6aSeric 	/* change standard input & output if needed */
75378d41c6aSeric 	if (OutFile >= 0)
75478d41c6aSeric 	{
75578d41c6aSeric 		close(1);
75678d41c6aSeric 		dup(OutFile);
75778d41c6aSeric 		close(OutFile);
75878d41c6aSeric 	}
7597de81dc7Seric 
76078d41c6aSeric 	/* call real SCCS program */
761172147faSeric 	execv(progpath, argv);
762095915d3Seric 	syserr("cannot execute %s", progpath);
763adf8f7d4Seric 	exit(EX_UNAVAILABLE);
764fe7b004aSeric 	/*NOTREACHED*/
765adf8f7d4Seric }
7660faf63f9Seric 
7670faf63f9Seric /*
768cdc1aa65Seric **  MAKEFILE -- make filename of SCCS file
769cdc1aa65Seric **
770cdc1aa65Seric **	If the name passed is already the name of an SCCS file,
771cdc1aa65Seric **	just return it.  Otherwise, munge the name into the name
772cdc1aa65Seric **	of the actual SCCS file.
773cdc1aa65Seric **
774cdc1aa65Seric **	There are cases when it is not clear what you want to
775cdc1aa65Seric **	do.  For example, if SccsPath is an absolute pathname
776cdc1aa65Seric **	and the name given is also an absolute pathname, we go
777cdc1aa65Seric **	for SccsPath (& only use the last component of the name
778cdc1aa65Seric **	passed) -- this is important for security reasons (if
779cdc1aa65Seric **	sccs is being used as a setuid front end), but not
780cdc1aa65Seric **	particularly intuitive.
781cdc1aa65Seric **
782cdc1aa65Seric **	Parameters:
783cdc1aa65Seric **		name -- the file name to be munged.
784cdc1aa65Seric **
785cdc1aa65Seric **	Returns:
786cdc1aa65Seric **		The pathname of the sccs file.
787cdc1aa65Seric **		NULL on error.
788cdc1aa65Seric **
789cdc1aa65Seric **	Side Effects:
790cdc1aa65Seric **		none.
791cdc1aa65Seric */
792adf8f7d4Seric 
793adf8f7d4Seric char *
794adf8f7d4Seric makefile(name)
795adf8f7d4Seric 	char *name;
796adf8f7d4Seric {
797adf8f7d4Seric 	register char *p;
798f76dd34fSrrh 	char buf[3*FBUFSIZ];
799adf8f7d4Seric 	extern char *malloc();
800cdc1aa65Seric 	extern char *rindex();
8010a8b9ab0Seric 	extern bool safepath();
8027d70f5d8Seric 	extern bool isdir();
8037d70f5d8Seric 	register char *q;
804cdc1aa65Seric 
805cdc1aa65Seric 	p = rindex(name, '/');
806cdc1aa65Seric 	if (p == NULL)
807cdc1aa65Seric 		p = name;
808cdc1aa65Seric 	else
809cdc1aa65Seric 		p++;
810adf8f7d4Seric 
811adf8f7d4Seric 	/*
8120a8b9ab0Seric 	**  Check to see that the path is "safe", i.e., that we
8130a8b9ab0Seric 	**  are not letting some nasty person use the setuid part
8140a8b9ab0Seric 	**  of this program to look at or munge some presumably
8150a8b9ab0Seric 	**  hidden files.
816adf8f7d4Seric 	*/
817adf8f7d4Seric 
8180a8b9ab0Seric 	if (SccsDir[0] == '/' && !safepath(name))
8190a8b9ab0Seric 		return (NULL);
820cdc1aa65Seric 
821cdc1aa65Seric 	/*
8220a8b9ab0Seric 	**  Create the base pathname.
823cdc1aa65Seric 	*/
824cdc1aa65Seric 
825ff038098Seric 	/* first the directory part */
8260a8b9ab0Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
827adf8f7d4Seric 	{
828f76dd34fSrrh 		gstrcpy(buf, SccsDir, sizeof(buf));
829f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
830cdc1aa65Seric 	}
831cdc1aa65Seric 	else
832f76dd34fSrrh 		gstrcpy(buf, "", sizeof(buf));
833ff038098Seric 
834ff038098Seric 	/* then the head of the pathname */
835f76dd34fSrrh 	gstrncat(buf, name, p - name, sizeof(buf));
8367d70f5d8Seric 	q = &buf[strlen(buf)];
837ff038098Seric 
838ff038098Seric 	/* now copy the final part of the name, in case useful */
839f76dd34fSrrh 	gstrcpy(q, p, sizeof(buf));
840ff038098Seric 
841ff038098Seric 	/* so is it useful? */
8427d70f5d8Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
8437d70f5d8Seric 	{
844ff038098Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
845f76dd34fSrrh 		gstrcpy(q, SccsPath, sizeof(buf));
846f76dd34fSrrh 		gstrcat(buf, "/s.", sizeof(buf));
847ff038098Seric 
848ff038098Seric 		/* and now the end of the name */
849f76dd34fSrrh 		gstrcat(buf, p, sizeof(buf));
850cdc1aa65Seric 	}
851adf8f7d4Seric 
852ff038098Seric 	/* if i haven't changed it, why did I do all this? */
8530a8b9ab0Seric 	if (strcmp(buf, name) == 0)
8540a8b9ab0Seric 		p = name;
8550a8b9ab0Seric 	else
8560a8b9ab0Seric 	{
857ff038098Seric 		/* but if I have, squirrel it away */
858adf8f7d4Seric 		p = malloc(strlen(buf) + 1);
859adf8f7d4Seric 		if (p == NULL)
860adf8f7d4Seric 		{
861adf8f7d4Seric 			perror("Sccs: no mem");
862adf8f7d4Seric 			exit(EX_OSERR);
863adf8f7d4Seric 		}
864adf8f7d4Seric 		strcpy(p, buf);
8650a8b9ab0Seric 	}
866ff038098Seric 
867adf8f7d4Seric 	return (p);
868adf8f7d4Seric }
8690faf63f9Seric 
8700faf63f9Seric /*
8717d70f5d8Seric **  ISDIR -- return true if the argument is a directory.
8727d70f5d8Seric **
8737d70f5d8Seric **	Parameters:
8747d70f5d8Seric **		name -- the pathname of the file to check.
8757d70f5d8Seric **
8767d70f5d8Seric **	Returns:
8777d70f5d8Seric **		TRUE if 'name' is a directory, FALSE otherwise.
8787d70f5d8Seric **
8797d70f5d8Seric **	Side Effects:
8807d70f5d8Seric **		none.
8817d70f5d8Seric */
8827d70f5d8Seric 
8837d70f5d8Seric bool
8847d70f5d8Seric isdir(name)
8857d70f5d8Seric 	char *name;
8867d70f5d8Seric {
8877d70f5d8Seric 	struct stat stbuf;
8887d70f5d8Seric 
8897d70f5d8Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
8907d70f5d8Seric }
8910faf63f9Seric 
8920faf63f9Seric /*
893cdc1aa65Seric **  SAFEPATH -- determine whether a pathname is "safe"
894cdc1aa65Seric **
895cdc1aa65Seric **	"Safe" pathnames only allow you to get deeper into the
896cdc1aa65Seric **	directory structure, i.e., full pathnames and ".." are
897cdc1aa65Seric **	not allowed.
898cdc1aa65Seric **
899cdc1aa65Seric **	Parameters:
900cdc1aa65Seric **		p -- the name to check.
901cdc1aa65Seric **
902cdc1aa65Seric **	Returns:
903cdc1aa65Seric **		TRUE -- if the path is safe.
904cdc1aa65Seric **		FALSE -- if the path is not safe.
905cdc1aa65Seric **
906cdc1aa65Seric **	Side Effects:
907cdc1aa65Seric **		Prints a message if the path is not safe.
908cdc1aa65Seric */
909cdc1aa65Seric 
910cdc1aa65Seric bool
911cdc1aa65Seric safepath(p)
912cdc1aa65Seric 	register char *p;
913cdc1aa65Seric {
914cdc1aa65Seric 	extern char *index();
915cdc1aa65Seric 
916cdc1aa65Seric 	if (*p != '/')
917cdc1aa65Seric 	{
918cdc1aa65Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
919cdc1aa65Seric 		{
920cdc1aa65Seric 			p = index(p, '/');
921cdc1aa65Seric 			if (p == NULL)
922cdc1aa65Seric 				return (TRUE);
923cdc1aa65Seric 			p++;
924cdc1aa65Seric 		}
925cdc1aa65Seric 	}
926cdc1aa65Seric 
927cdc1aa65Seric 	printf("You may not use full pathnames or \"..\"\n");
928cdc1aa65Seric 	return (FALSE);
929cdc1aa65Seric }
9300faf63f9Seric 
9310faf63f9Seric /*
932b2538d76Seric **  CLEAN -- clean out recreatable files
933b2538d76Seric **
934b2538d76Seric **	Any file for which an "s." file exists but no "p." file
935b2538d76Seric **	exists in the current directory is purged.
936b2538d76Seric **
937b2538d76Seric **	Parameters:
938ec4190ddSeric **		mode -- tells whether this came from a "clean", "info", or
93948ff0e7eSeric **			"check" command.
940ec4190ddSeric **		argv -- the rest of the argument vector.
941b2538d76Seric **
942b2538d76Seric **	Returns:
943b2538d76Seric **		none.
944b2538d76Seric **
945b2538d76Seric **	Side Effects:
94648ff0e7eSeric **		Removes files in the current directory.
94748ff0e7eSeric **		Prints information regarding files being edited.
94848ff0e7eSeric **		Exits if a "check" command.
949b2538d76Seric */
950b2538d76Seric 
951ec4190ddSeric clean(mode, argv)
95248ff0e7eSeric 	int mode;
953ec4190ddSeric 	char **argv;
954b2538d76Seric {
955683375a0Smckusick 	struct direct *dir;
956f76dd34fSrrh 	char buf[FBUFSIZ];
957393e27f2Seric 	char *bufend;
958bb638ad5Smckusick 	register DIR *dirp;
9593a208c77Seric 	register char *basefile;
9602caac8fdSeric 	bool gotedit;
961ec4190ddSeric 	bool gotpfent;
962bf9c1e66Seric 	FILE *pfp;
963ec4190ddSeric 	bool nobranch = FALSE;
964ec4190ddSeric 	extern struct pfile *getpfent();
965ec4190ddSeric 	register struct pfile *pf;
966ec4190ddSeric 	register char **ap;
9671e0924d0Seric 	extern char *username();
9681e0924d0Seric 	char *usernm = NULL;
969393e27f2Seric 	char *subdir = NULL;
970393e27f2Seric 	char *cmdname;
971ec4190ddSeric 
972ec4190ddSeric 	/*
973ec4190ddSeric 	**  Process the argv
974ec4190ddSeric 	*/
975ec4190ddSeric 
976393e27f2Seric 	cmdname = *argv;
977393e27f2Seric 	for (ap = argv; *++ap != NULL; )
978ec4190ddSeric 	{
9791e0924d0Seric 		if (**ap == '-')
9801e0924d0Seric 		{
9811e0924d0Seric 			/* we have a flag */
9821e0924d0Seric 			switch ((*ap)[1])
9831e0924d0Seric 			{
9841e0924d0Seric 			  case 'b':
985ec4190ddSeric 				nobranch = TRUE;
9861e0924d0Seric 				break;
9871e0924d0Seric 
9881e0924d0Seric 			  case 'u':
9891e0924d0Seric 				if ((*ap)[2] != '\0')
9901e0924d0Seric 					usernm = &(*ap)[2];
9911e0924d0Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9921e0924d0Seric 					usernm = *++ap;
9931e0924d0Seric 				else
9941e0924d0Seric 					usernm = username();
9951e0924d0Seric 				break;
9961e0924d0Seric 			}
9971e0924d0Seric 		}
998393e27f2Seric 		else
999393e27f2Seric 		{
1000393e27f2Seric 			if (subdir != NULL)
1001393e27f2Seric 				usrerr("too many args");
1002393e27f2Seric 			else
1003393e27f2Seric 				subdir = *ap;
1004393e27f2Seric 		}
1005ec4190ddSeric 	}
1006b2538d76Seric 
1007ff038098Seric 	/*
1008ff038098Seric 	**  Find and open the SCCS directory.
1009ff038098Seric 	*/
1010ff038098Seric 
1011f76dd34fSrrh 	gstrcpy(buf, SccsDir, sizeof(buf));
1012f77a08fbSeric 	if (buf[0] != '\0')
1013f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
1014393e27f2Seric 	if (subdir != NULL)
1015393e27f2Seric 	{
1016f76dd34fSrrh 		gstrcat(buf, subdir, sizeof(buf));
1017f76dd34fSrrh 		gstrcat(buf, "/", sizeof(buf));
1018393e27f2Seric 	}
1019f76dd34fSrrh 	gstrcat(buf, SccsPath, sizeof(buf));
1020393e27f2Seric 	bufend = &buf[strlen(buf)];
1021ff038098Seric 
1022bb638ad5Smckusick 	dirp = opendir(buf);
1023bb638ad5Smckusick 	if (dirp == NULL)
1024b2538d76Seric 	{
1025f77a08fbSeric 		usrerr("cannot open %s", buf);
102641290c52Seric 		return (EX_NOINPUT);
1027b2538d76Seric 	}
1028b2538d76Seric 
1029b2538d76Seric 	/*
1030b2538d76Seric 	**  Scan the SCCS directory looking for s. files.
1031ff038098Seric 	**	gotedit tells whether we have tried to clean any
1032ff038098Seric 	**		files that are being edited.
1033b2538d76Seric 	*/
1034b2538d76Seric 
10352caac8fdSeric 	gotedit = FALSE;
1036bb638ad5Smckusick 	while (dir = readdir(dirp)) {
1037683375a0Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
1038b2538d76Seric 			continue;
1039b2538d76Seric 
1040b2538d76Seric 		/* got an s. file -- see if the p. file exists */
1041f76dd34fSrrh 		gstrcpy(bufend, "/p.", sizeof(buf));
1042393e27f2Seric 		basefile = bufend + 3;
1043f76dd34fSrrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
1044ec4190ddSeric 
1045ec4190ddSeric 		/*
1046ec4190ddSeric 		**  open and scan the p-file.
1047ec4190ddSeric 		**	'gotpfent' tells if we have found a valid p-file
1048ec4190ddSeric 		**		entry.
1049ec4190ddSeric 		*/
1050ec4190ddSeric 
1051bf9c1e66Seric 		pfp = fopen(buf, "r");
1052ec4190ddSeric 		gotpfent = FALSE;
1053bf9c1e66Seric 		if (pfp != NULL)
10543a208c77Seric 		{
1055ff038098Seric 			/* the file exists -- report it's contents */
1056ec4190ddSeric 			while ((pf = getpfent(pfp)) != NULL)
10572734e237Seric 			{
1058ec4190ddSeric 				if (nobranch && isbranch(pf->p_nsid))
1059ec4190ddSeric 					continue;
10601e0924d0Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10611e0924d0Seric 					continue;
1062ec4190ddSeric 				gotedit = TRUE;
1063ec4190ddSeric 				gotpfent = TRUE;
1064ec4190ddSeric 				if (mode == TELLC)
1065ec4190ddSeric 				{
1066ec4190ddSeric 					printf("%s\n", basefile);
1067ec4190ddSeric 					break;
1068ec4190ddSeric 				}
106977a8a0dfSeric 				printf("%12s: being edited: ", basefile);
107077a8a0dfSeric 				putpfent(pf, stdout);
10712734e237Seric 			}
1072bf9c1e66Seric 			fclose(pfp);
10733a208c77Seric 		}
1074b2538d76Seric 
1075b2538d76Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10769d7ed7d6Seric 		if (mode == CLEANC && !gotpfent)
10773a208c77Seric 		{
1078f76dd34fSrrh 			char	unlinkbuf[FBUFSIZ];
1079f76dd34fSrrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
10800770051cSrrh 			unlink(unlinkbuf);
1081b2538d76Seric 		}
10823a208c77Seric 	}
1083b2538d76Seric 
1084ff038098Seric 	/* cleanup & report results */
1085bb638ad5Smckusick 	closedir(dirp);
108648ff0e7eSeric 	if (!gotedit && mode == INFOC)
10871e0924d0Seric 	{
10881e0924d0Seric 		printf("Nothing being edited");
10891e0924d0Seric 		if (nobranch)
10901e0924d0Seric 			printf(" (on trunk)");
10911e0924d0Seric 		if (usernm == NULL)
10921e0924d0Seric 			printf("\n");
10931e0924d0Seric 		else
10941e0924d0Seric 			printf(" by %s\n", usernm);
10951e0924d0Seric 	}
109648ff0e7eSeric 	if (mode == CHECKC)
109748ff0e7eSeric 		exit(gotedit);
109841290c52Seric 	return (EX_OK);
1099b2538d76Seric }
11000faf63f9Seric 
11010faf63f9Seric /*
1102ec4190ddSeric **  ISBRANCH -- is the SID a branch?
1103ec4190ddSeric **
1104ec4190ddSeric **	Parameters:
1105ec4190ddSeric **		sid -- the sid to check.
1106ec4190ddSeric **
1107ec4190ddSeric **	Returns:
1108ec4190ddSeric **		TRUE if the sid represents a branch.
1109ec4190ddSeric **		FALSE otherwise.
1110ec4190ddSeric **
1111ec4190ddSeric **	Side Effects:
1112ec4190ddSeric **		none.
1113ec4190ddSeric */
1114ec4190ddSeric 
1115ec4190ddSeric isbranch(sid)
1116ec4190ddSeric 	char *sid;
1117ec4190ddSeric {
1118ec4190ddSeric 	register char *p;
1119ec4190ddSeric 	int dots;
1120ec4190ddSeric 
1121ec4190ddSeric 	dots = 0;
1122ec4190ddSeric 	for (p = sid; *p != '\0'; p++)
1123ec4190ddSeric 	{
1124ec4190ddSeric 		if (*p == '.')
1125ec4190ddSeric 			dots++;
1126ec4190ddSeric 		if (dots > 1)
1127ec4190ddSeric 			return (TRUE);
1128ec4190ddSeric 	}
1129ec4190ddSeric 	return (FALSE);
1130ec4190ddSeric }
1131ec4190ddSeric 
1132ec4190ddSeric /*
1133e39a5722Seric **  UNEDIT -- unedit a file
1134e39a5722Seric **
1135e39a5722Seric **	Checks to see that the current user is actually editting
1136e39a5722Seric **	the file and arranges that s/he is not editting it.
1137e39a5722Seric **
1138e39a5722Seric **	Parameters:
11392444cd3eSeric **		fn -- the name of the file to be unedited.
1140e39a5722Seric **
1141e39a5722Seric **	Returns:
1142d51cd7e5Seric **		TRUE -- if the file was successfully unedited.
1143d51cd7e5Seric **		FALSE -- if the file was not unedited for some
1144d51cd7e5Seric **			reason.
1145e39a5722Seric **
1146e39a5722Seric **	Side Effects:
1147e39a5722Seric **		fn is removed
1148e39a5722Seric **		entries are removed from pfile.
1149e39a5722Seric */
1150e39a5722Seric 
1151d51cd7e5Seric bool
1152e39a5722Seric unedit(fn)
1153e39a5722Seric 	char *fn;
1154e39a5722Seric {
1155e39a5722Seric 	register FILE *pfp;
11568e9f28e0Ssam 	char *cp, *pfn;
1157e39a5722Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1158e39a5722Seric 	FILE *tfp;
1159e39a5722Seric 	register char *q;
1160e39a5722Seric 	bool delete = FALSE;
1161e39a5722Seric 	bool others = FALSE;
1162e39a5722Seric 	char *myname;
11631e0924d0Seric 	extern char *username();
1164e39a5722Seric 	struct pfile *pent;
1165ec4190ddSeric 	extern struct pfile *getpfent();
1166f76dd34fSrrh 	char buf[PFILELG];
11670da7a6f3Sbostic 	extern char *makefile(), *rindex(), *tail();
1168e39a5722Seric 
1169e39a5722Seric 	/* make "s." filename & find the trailing component */
1170e39a5722Seric 	pfn = makefile(fn);
1171cdc1aa65Seric 	if (pfn == NULL)
1172cdc1aa65Seric 		return (FALSE);
1173cdc1aa65Seric 	q = rindex(pfn, '/');
1174cdc1aa65Seric 	if (q == NULL)
1175cdc1aa65Seric 		q = &pfn[-1];
1176cdc1aa65Seric 	if (q[1] != 's' || q[2] != '.')
1177e39a5722Seric 	{
1178095915d3Seric 		usrerr("bad file name \"%s\"", fn);
1179d51cd7e5Seric 		return (FALSE);
1180e39a5722Seric 	}
1181e39a5722Seric 
1182ff038098Seric 	/* turn "s." into "p." & try to open it */
1183e39a5722Seric 	*++q = 'p';
1184e39a5722Seric 
1185e39a5722Seric 	pfp = fopen(pfn, "r");
1186e39a5722Seric 	if (pfp == NULL)
1187e39a5722Seric 	{
11882444cd3eSeric 		printf("%12s: not being edited\n", fn);
1189d51cd7e5Seric 		return (FALSE);
1190e39a5722Seric 	}
1191e39a5722Seric 
1192ff038098Seric 	/* create temp file for editing p-file */
1193e39a5722Seric 	mktemp(tfn);
1194e39a5722Seric 	tfp = fopen(tfn, "w");
1195e39a5722Seric 	if (tfp == NULL)
1196e39a5722Seric 	{
1197095915d3Seric 		usrerr("cannot create \"%s\"", tfn);
1198e39a5722Seric 		exit(EX_OSERR);
1199e39a5722Seric 	}
1200e39a5722Seric 
1201ff038098Seric 	/* figure out who I am */
12021e0924d0Seric 	myname = username();
1203ff038098Seric 
1204ff038098Seric 	/*
1205ff038098Seric 	**  Copy p-file to temp file, doing deletions as needed.
1206ff038098Seric 	*/
1207ff038098Seric 
1208ec4190ddSeric 	while ((pent = getpfent(pfp)) != NULL)
1209e39a5722Seric 	{
1210e39a5722Seric 		if (strcmp(pent->p_user, myname) == 0)
1211e39a5722Seric 		{
1212e39a5722Seric 			/* a match */
1213e39a5722Seric 			delete++;
1214e39a5722Seric 		}
1215e39a5722Seric 		else
1216e39a5722Seric 		{
1217ff038098Seric 			/* output it again */
121877a8a0dfSeric 			putpfent(pent, tfp);
1219e39a5722Seric 			others++;
1220e39a5722Seric 		}
1221e39a5722Seric 	}
1222e39a5722Seric 
12238e9f28e0Ssam 	/*
12248e9f28e0Ssam 	 * Before changing anything, make sure we can remove
12258e9f28e0Ssam 	 * the file in question (assuming it exists).
12268e9f28e0Ssam 	 */
12278e9f28e0Ssam 	if (delete) {
12288e9f28e0Ssam 		extern int errno;
12298e9f28e0Ssam 
12308e9f28e0Ssam 		cp = tail(fn);
12318e9f28e0Ssam 		errno = 0;
12328e9f28e0Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
12338e9f28e0Ssam 			goto bad;
12348e9f28e0Ssam 		if (errno == 0)
12358e9f28e0Ssam 			/*
12368e9f28e0Ssam 			 * This is wrong, but the rest of the program
12378e9f28e0Ssam 			 * has built in assumptions about "." as well,
12388e9f28e0Ssam 			 * so why make unedit a special case?
12398e9f28e0Ssam 			 */
12408e9f28e0Ssam 			if (access(".", 2) < 0) {
12418e9f28e0Ssam 	bad:
12428e9f28e0Ssam 				printf("%12s: can't remove\n", cp);
12438e9f28e0Ssam 				fclose(tfp);
12448e9f28e0Ssam 				fclose(pfp);
12458e9f28e0Ssam 				unlink(tfn);
12468e9f28e0Ssam 				return (FALSE);
12478e9f28e0Ssam 			}
12488e9f28e0Ssam 	}
1249e39a5722Seric 	/* do final cleanup */
1250e39a5722Seric 	if (others)
1251e39a5722Seric 	{
1252ff038098Seric 		/* copy it back (perhaps it should be linked?) */
1253e39a5722Seric 		if (freopen(tfn, "r", tfp) == NULL)
1254e39a5722Seric 		{
1255095915d3Seric 			syserr("cannot reopen \"%s\"", tfn);
1256e39a5722Seric 			exit(EX_OSERR);
1257e39a5722Seric 		}
1258e39a5722Seric 		if (freopen(pfn, "w", pfp) == NULL)
1259e39a5722Seric 		{
1260095915d3Seric 			usrerr("cannot create \"%s\"", pfn);
1261d51cd7e5Seric 			return (FALSE);
1262e39a5722Seric 		}
1263e39a5722Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1264e39a5722Seric 			fputs(buf, pfp);
1265e39a5722Seric 	}
1266e39a5722Seric 	else
1267e39a5722Seric 	{
1268ff038098Seric 		/* it's empty -- remove it */
1269e39a5722Seric 		unlink(pfn);
1270e39a5722Seric 	}
1271e39a5722Seric 	fclose(tfp);
1272e39a5722Seric 	fclose(pfp);
1273e39a5722Seric 	unlink(tfn);
1274e39a5722Seric 
1275ff038098Seric 	/* actually remove the g-file */
1276e39a5722Seric 	if (delete)
1277e39a5722Seric 	{
12788e9f28e0Ssam 		/*
12798e9f28e0Ssam 		 * Since we've checked above, we can
12808e9f28e0Ssam 		 * use the return from unlink to
12818e9f28e0Ssam 		 * determine if the file existed or not.
12828e9f28e0Ssam 		 */
12838e9f28e0Ssam 		if (unlink(cp) >= 0)
12848e9f28e0Ssam 			printf("%12s: removed\n", cp);
1285d51cd7e5Seric 		return (TRUE);
1286e39a5722Seric 	}
1287e39a5722Seric 	else
1288e39a5722Seric 	{
12892444cd3eSeric 		printf("%12s: not being edited by you\n", fn);
1290d51cd7e5Seric 		return (FALSE);
1291e39a5722Seric 	}
1292e39a5722Seric }
12930faf63f9Seric 
12940faf63f9Seric /*
129578d41c6aSeric **  DODIFF -- diff an s-file against a g-file
129678d41c6aSeric **
129778d41c6aSeric **	Parameters:
129878d41c6aSeric **		getv -- argv for the 'get' command.
129978d41c6aSeric **		gfile -- name of the g-file to diff against.
130078d41c6aSeric **
130178d41c6aSeric **	Returns:
130278d41c6aSeric **		Result of get.
130378d41c6aSeric **
130478d41c6aSeric **	Side Effects:
130578d41c6aSeric **		none.
130678d41c6aSeric */
130778d41c6aSeric 
130878d41c6aSeric dodiff(getv, gfile)
130978d41c6aSeric 	char **getv;
131078d41c6aSeric 	char *gfile;
131178d41c6aSeric {
131278d41c6aSeric 	int pipev[2];
131378d41c6aSeric 	int rval;
131478d41c6aSeric 	register int i;
131578d41c6aSeric 	register int pid;
131678d41c6aSeric 	auto int st;
131778d41c6aSeric 	extern int errno;
131878d41c6aSeric 	int (*osig)();
131978d41c6aSeric 
1320cc4e4f0dSeric 	printf("\n------- %s -------\n", gfile);
13212569d37aSeric 	fflush(stdout);
1322cc4e4f0dSeric 
1323ff038098Seric 	/* create context for diff to run in */
132478d41c6aSeric 	if (pipe(pipev) < 0)
132578d41c6aSeric 	{
132678d41c6aSeric 		syserr("dodiff: pipe failed");
132778d41c6aSeric 		exit(EX_OSERR);
132878d41c6aSeric 	}
132978d41c6aSeric 	if ((pid = fork()) < 0)
133078d41c6aSeric 	{
133178d41c6aSeric 		syserr("dodiff: fork failed");
133278d41c6aSeric 		exit(EX_OSERR);
133378d41c6aSeric 	}
133478d41c6aSeric 	else if (pid > 0)
133578d41c6aSeric 	{
133678d41c6aSeric 		/* in parent; run get */
133778d41c6aSeric 		OutFile = pipev[1];
133878d41c6aSeric 		close(pipev[0]);
13392569d37aSeric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
134078d41c6aSeric 		osig = signal(SIGINT, SIG_IGN);
134178d41c6aSeric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
134278d41c6aSeric 			errno = 0;
134378d41c6aSeric 		signal(SIGINT, osig);
134478d41c6aSeric 		/* ignore result of diff */
134578d41c6aSeric 	}
134678d41c6aSeric 	else
134778d41c6aSeric 	{
134878d41c6aSeric 		/* in child, run diff */
134978d41c6aSeric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
135078d41c6aSeric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
135178d41c6aSeric 		{
135278d41c6aSeric 			syserr("dodiff: magic failed");
135378d41c6aSeric 			exit(EX_OSERR);
135478d41c6aSeric 		}
13552569d37aSeric 		command(&getv[1], FALSE, "-diff:elsfhbC");
135678d41c6aSeric 	}
135778d41c6aSeric 	return (rval);
135878d41c6aSeric }
135978d41c6aSeric 
136078d41c6aSeric /*
1361287bbaa2Seric **  TAIL -- return tail of filename.
1362287bbaa2Seric **
1363287bbaa2Seric **	Parameters:
1364287bbaa2Seric **		fn -- the filename.
1365287bbaa2Seric **
1366287bbaa2Seric **	Returns:
1367287bbaa2Seric **		a pointer to the tail of the filename; e.g., given
1368287bbaa2Seric **		"cmd/ls.c", "ls.c" is returned.
1369287bbaa2Seric **
1370287bbaa2Seric **	Side Effects:
1371287bbaa2Seric **		none.
1372287bbaa2Seric */
1373287bbaa2Seric 
1374287bbaa2Seric char *
1375287bbaa2Seric tail(fn)
1376287bbaa2Seric 	register char *fn;
1377287bbaa2Seric {
1378287bbaa2Seric 	register char *p;
1379287bbaa2Seric 
1380287bbaa2Seric 	for (p = fn; *p != 0; p++)
1381287bbaa2Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
1382287bbaa2Seric 			fn = &p[1];
1383287bbaa2Seric 	return (fn);
1384287bbaa2Seric }
1385287bbaa2Seric 
1386287bbaa2Seric /*
1387ec4190ddSeric **  GETPFENT -- get an entry from the p-file
1388e39a5722Seric **
1389e39a5722Seric **	Parameters:
1390e39a5722Seric **		pfp -- p-file file pointer
1391e39a5722Seric **
1392e39a5722Seric **	Returns:
1393e39a5722Seric **		pointer to p-file struct for next entry
1394e39a5722Seric **		NULL on EOF or error
1395e39a5722Seric **
1396e39a5722Seric **	Side Effects:
1397e39a5722Seric **		Each call wipes out results of previous call.
1398e39a5722Seric */
1399e39a5722Seric 
1400e39a5722Seric struct pfile *
1401ec4190ddSeric getpfent(pfp)
1402e39a5722Seric 	FILE *pfp;
1403e39a5722Seric {
1404e39a5722Seric 	static struct pfile ent;
1405f76dd34fSrrh 	static char buf[PFILELG];
1406e39a5722Seric 	register char *p;
1407e39a5722Seric 	extern char *nextfield();
1408e39a5722Seric 
1409e39a5722Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1410e39a5722Seric 		return (NULL);
1411e39a5722Seric 
1412e39a5722Seric 	ent.p_osid = p = buf;
1413e39a5722Seric 	ent.p_nsid = p = nextfield(p);
1414e39a5722Seric 	ent.p_user = p = nextfield(p);
1415e39a5722Seric 	ent.p_date = p = nextfield(p);
1416e39a5722Seric 	ent.p_time = p = nextfield(p);
141777a8a0dfSeric 	ent.p_aux = p = nextfield(p);
1418e39a5722Seric 
1419e39a5722Seric 	return (&ent);
1420e39a5722Seric }
1421e39a5722Seric 
1422e39a5722Seric 
1423e39a5722Seric char *
1424e39a5722Seric nextfield(p)
1425e39a5722Seric 	register char *p;
1426e39a5722Seric {
1427e39a5722Seric 	if (p == NULL || *p == '\0')
1428e39a5722Seric 		return (NULL);
1429e39a5722Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1430e39a5722Seric 		p++;
1431e39a5722Seric 	if (*p == '\n' || *p == '\0')
1432e39a5722Seric 	{
1433e39a5722Seric 		*p = '\0';
1434e39a5722Seric 		return (NULL);
1435e39a5722Seric 	}
1436e39a5722Seric 	*p++ = '\0';
1437e39a5722Seric 	return (p);
1438e39a5722Seric }
143977a8a0dfSeric /*
144077a8a0dfSeric **  PUTPFENT -- output a p-file entry to a file
144177a8a0dfSeric **
144277a8a0dfSeric **	Parameters:
144377a8a0dfSeric **		pf -- the p-file entry
144477a8a0dfSeric **		f -- the file to put it on.
144577a8a0dfSeric **
144677a8a0dfSeric **	Returns:
144777a8a0dfSeric **		none.
144877a8a0dfSeric **
144977a8a0dfSeric **	Side Effects:
145077a8a0dfSeric **		pf is written onto file f.
145177a8a0dfSeric */
145277a8a0dfSeric 
145377a8a0dfSeric putpfent(pf, f)
145477a8a0dfSeric 	register struct pfile *pf;
145577a8a0dfSeric 	register FILE *f;
145677a8a0dfSeric {
145777a8a0dfSeric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
145877a8a0dfSeric 		pf->p_user, pf->p_date, pf->p_time);
145977a8a0dfSeric 	if (pf->p_aux != NULL)
146077a8a0dfSeric 		fprintf(f, " %s", pf->p_aux);
146177a8a0dfSeric 	else
146277a8a0dfSeric 		fprintf(f, "\n");
146377a8a0dfSeric }
14640faf63f9Seric 
14650faf63f9Seric /*
1466095915d3Seric **  USRERR -- issue user-level error
1467095915d3Seric **
1468095915d3Seric **	Parameters:
1469095915d3Seric **		f -- format string.
1470095915d3Seric **		p1-p3 -- parameters to a printf.
1471095915d3Seric **
1472095915d3Seric **	Returns:
1473095915d3Seric **		-1
1474095915d3Seric **
1475095915d3Seric **	Side Effects:
1476095915d3Seric **		none.
1477095915d3Seric */
1478095915d3Seric 
1479fe7b004aSeric /*VARARGS1*/
1480095915d3Seric usrerr(f, p1, p2, p3)
1481095915d3Seric 	char *f;
1482095915d3Seric {
1483095915d3Seric 	fprintf(stderr, "\n%s: ", MyName);
1484095915d3Seric 	fprintf(stderr, f, p1, p2, p3);
1485095915d3Seric 	fprintf(stderr, "\n");
1486095915d3Seric 
1487095915d3Seric 	return (-1);
1488095915d3Seric }
14890faf63f9Seric 
14900faf63f9Seric /*
1491095915d3Seric **  SYSERR -- print system-generated error.
1492095915d3Seric **
1493095915d3Seric **	Parameters:
1494095915d3Seric **		f -- format string to a printf.
1495095915d3Seric **		p1, p2, p3 -- parameters to f.
1496095915d3Seric **
1497095915d3Seric **	Returns:
1498095915d3Seric **		never.
1499095915d3Seric **
1500095915d3Seric **	Side Effects:
1501095915d3Seric **		none.
1502095915d3Seric */
1503095915d3Seric 
1504fe7b004aSeric /*VARARGS1*/
1505095915d3Seric syserr(f, p1, p2, p3)
1506095915d3Seric 	char *f;
1507095915d3Seric {
1508095915d3Seric 	extern int errno;
1509095915d3Seric 
1510095915d3Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
1511095915d3Seric 	fprintf(stderr, f, p1, p2, p3);
1512095915d3Seric 	fprintf(stderr, "\n");
1513095915d3Seric 	if (errno == 0)
1514095915d3Seric 		exit(EX_SOFTWARE);
1515095915d3Seric 	else
1516095915d3Seric 	{
1517fe7b004aSeric 		perror(NULL);
1518095915d3Seric 		exit(EX_OSERR);
1519095915d3Seric 	}
1520095915d3Seric }
15211e0924d0Seric /*
15221e0924d0Seric **  USERNAME -- return name of the current user
15231e0924d0Seric **
15241e0924d0Seric **	Parameters:
15251e0924d0Seric **		none
15261e0924d0Seric **
15271e0924d0Seric **	Returns:
15281e0924d0Seric **		name of current user
15291e0924d0Seric **
15301e0924d0Seric **	Side Effects:
15311e0924d0Seric **		none
15321e0924d0Seric */
15331e0924d0Seric 
15341e0924d0Seric char *
15351e0924d0Seric username()
15361e0924d0Seric {
15371e0924d0Seric # ifdef UIDUSER
15381e0924d0Seric 	extern struct passwd *getpwuid();
15391e0924d0Seric 	register struct passwd *pw;
15401e0924d0Seric 
15411e0924d0Seric 	pw = getpwuid(getuid());
15421e0924d0Seric 	if (pw == NULL)
15431e0924d0Seric 	{
15441e0924d0Seric 		syserr("who are you? (uid=%d)", getuid());
15451e0924d0Seric 		exit(EX_OSERR);
15461e0924d0Seric 	}
15471e0924d0Seric 	return (pw->pw_name);
15481e0924d0Seric # else
1549ed320446Seric 	extern char *getlogin();
1550bb5509cbSmckusick 	register char *p;
1551ed320446Seric 
1552bb5509cbSmckusick 	p = getenv("USER");
1553bb5509cbSmckusick 	if (p == NULL || p[0] == '\0')
1554bb5509cbSmckusick 		p = getlogin();
1555bb5509cbSmckusick 	return (p);
15561e0924d0Seric # endif UIDUSER
15571e0924d0Seric }
1558f76dd34fSrrh 
1559f76dd34fSrrh /*
1560f76dd34fSrrh **	Guarded string manipulation routines; the last argument
1561f76dd34fSrrh **	is the length of the buffer into which the strcpy or strcat
1562f76dd34fSrrh **	is to be done.
1563f76dd34fSrrh */
1564f76dd34fSrrh char *gstrcat(to, from, length)
1565f76dd34fSrrh 	char	*to, *from;
1566f76dd34fSrrh 	int	length;
1567f76dd34fSrrh {
1568f76dd34fSrrh 	if (strlen(from) + strlen(to) >= length) {
1569f76dd34fSrrh 		gstrbotch(to, from);
1570f76dd34fSrrh 	}
1571f76dd34fSrrh 	return(strcat(to, from));
1572f76dd34fSrrh }
1573f76dd34fSrrh 
1574f76dd34fSrrh char *gstrncat(to, from, n, length)
1575f76dd34fSrrh 	char	*to, *from;
1576f76dd34fSrrh 	int	n;
1577f76dd34fSrrh 	int	length;
1578f76dd34fSrrh {
1579f76dd34fSrrh 	if (n + strlen(to) >= length) {
1580f76dd34fSrrh 		gstrbotch(to, from);
1581f76dd34fSrrh 	}
1582f76dd34fSrrh 	return(strncat(to, from, n));
1583f76dd34fSrrh }
1584f76dd34fSrrh 
1585f76dd34fSrrh char *gstrcpy(to, from, length)
1586f76dd34fSrrh 	char	*to, *from;
1587f76dd34fSrrh 	int	length;
1588f76dd34fSrrh {
1589f76dd34fSrrh 	if (strlen(from) >= length) {
1590f76dd34fSrrh 		gstrbotch(from, (char *)0);
1591f76dd34fSrrh 	}
1592f76dd34fSrrh 	return(strcpy(to, from));
1593f76dd34fSrrh }
1594f76dd34fSrrh gstrbotch(str1, str2)
1595f76dd34fSrrh 	char	*str1, *str2;
1596f76dd34fSrrh {
1597f76dd34fSrrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
1598f76dd34fSrrh }
1599