xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 2226)
1148Seric # include <stdio.h>
2148Seric # include <sys/types.h>
3148Seric # include <sys/stat.h>
4261Seric # include <sys/dir.h>
51433Seric # include <errno.h>
61433Seric # include <signal.h>
7148Seric # include <sysexits.h>
8202Seric # include <whoami.h>
92140Seric # include <pwd.h>
10148Seric 
11828Seric /*
12828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
13828Seric **
14828Seric **	Without trying to add any functionality to speak of, this
15828Seric **	program tries to make SCCS a little more accessible to human
16828Seric **	types.  The main thing it does is automatically put the
17828Seric **	string "SCCS/s." on the front of names.  Also, it has a
18828Seric **	couple of things that are designed to shorten frequent
19828Seric **	combinations, e.g., "delget" which expands to a "delta"
20828Seric **	and a "get".
21828Seric **
22828Seric **	This program can also function as a setuid front end.
23828Seric **	To do this, you should copy the source, renaming it to
24828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
25828Seric **	in the program (e.g., syssccs might default -d to
26828Seric **	"/usr/src/sys").  Then recompile and put the result
27828Seric **	as setuid to whomever you want.  In this mode, sccs
28828Seric **	knows to not run setuid for certain programs in order
29828Seric **	to preserve security, and so forth.
30828Seric **
31828Seric **	Usage:
32828Seric **		sccs [flags] command [args]
33828Seric **
34828Seric **	Flags:
35828Seric **		-d<dir>		<dir> represents a directory to search
36828Seric **				out of.  It should be a full pathname
37828Seric **				for general usage.  E.g., if <dir> is
38828Seric **				"/usr/src/sys", then a reference to the
39828Seric **				file "dev/bio.c" becomes a reference to
40828Seric **				"/usr/src/sys/dev/bio.c".
41828Seric **		-p<path>	prepends <path> to the final component
42828Seric **				of the pathname.  By default, this is
43828Seric **				"SCCS".  For example, in the -d example
44828Seric **				above, the path then gets modified to
45828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
46828Seric **				more common usage (without the -d flag),
47828Seric **				"prog.c" would get modified to
48828Seric **				"SCCS/s.prog.c".  In both cases, the
49828Seric **				"s." gets automatically prepended.
50828Seric **		-r		run as the real user.
51828Seric **
52828Seric **	Commands:
53828Seric **		admin,
54828Seric **		get,
55828Seric **		delta,
56828Seric **		rmdel,
57828Seric **		chghist,
58828Seric **		etc.		Straight out of SCCS; only difference
59828Seric **				is that pathnames get modified as
60828Seric **				described above.
61828Seric **		edit		Macro for "get -e".
62828Seric **		unedit		Removes a file being edited, knowing
63828Seric **				about p-files, etc.
64828Seric **		delget		Macro for "delta" followed by "get".
65828Seric **		deledit		Macro for "delta" followed by "get -e".
66828Seric **		info		Tell what files being edited.
67828Seric **		clean		Remove all files that can be
68828Seric **				regenerated from SCCS files.
691205Seric **		check		Like info, but return exit status, for
70828Seric **				use in makefiles.
71828Seric **		fix		Remove a top delta & reedit, but save
72828Seric **				the previous changes in that delta.
73828Seric **
74828Seric **	Compilation Flags:
75828Seric **		UIDUSER -- determine who the user is by looking at the
76828Seric **			uid rather than the login name -- for machines
77828Seric **			where SCCS gets the user in this way.
781270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
791205Seric **			this value.  This is so that the setuid
801205Seric **			aspects of this program cannot be abused.
811270Seric **			This flag also disables the -p flag.
821270Seric **		SCCSPATH -- the default for the -p flag.
831437Seric **		MYNAME -- the title this program should print when it
841437Seric **			gives error messages.
85828Seric **
86828Seric **	Compilation Instructions:
87828Seric **		cc -O -n -s sccs.c
881437Seric **		The flags listed above can be -D defined to simplify
891437Seric **			recompilation for variant versions.
90828Seric **
91828Seric **	Author:
92828Seric **		Eric Allman, UCB/INGRES
931270Seric **		Copyright 1980 Regents of the University of California
94828Seric */
95155Seric 
96*2226Seric static char SccsId[] = "@(#)sccs.c	1.60 01/25/81";
971432Seric 
981270Seric /*******************  Configuration Information  ********************/
991270Seric 
1001437Seric /* special defines for local berkeley systems */
1011437Seric # include <whoami.h>
1021437Seric 
103828Seric # ifdef CSVAX
104828Seric # define UIDUSER
1051270Seric # define PROGPATH(name)	"/usr/local/name"
1061270Seric # endif CSVAX
107828Seric 
1081618Seric # ifdef INGVAX
1091618Seric # define PROGPATH(name)	"/usr/local/name"
1101618Seric # endif INGVAX
1111618Seric 
1121867Seric # ifdef CORY
1131867Seric # define PROGPATH(name)	"/usr/eecs/bin/name"
1141867Seric # endif CORY
1151867Seric 
1161437Seric /* end of berkeley systems defines */
1171437Seric 
1181437Seric # ifndef SCCSPATH
1191432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1201437Seric # endif NOT SCCSPATH
121828Seric 
1221437Seric # ifndef MYNAME
1231437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1241437Seric # endif NOT MYNAME
1251270Seric 
1261432Seric # ifndef PROGPATH
1271432Seric # define PROGPATH(name)	"/usr/sccs/name"	/* place to find binaries */
1281432Seric # endif PROGPATH
1291432Seric 
1301270Seric /****************  End of Configuration Information  ****************/
1311432Seric 
132157Seric typedef char	bool;
133200Seric # define TRUE	1
134200Seric # define FALSE	0
135157Seric 
1361438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1371438Seric 
138148Seric struct sccsprog
139148Seric {
140148Seric 	char	*sccsname;	/* name of SCCS routine */
141200Seric 	short	sccsoper;	/* opcode, see below */
142200Seric 	short	sccsflags;	/* flags, see below */
143148Seric 	char	*sccspath;	/* pathname of binary implementing */
144148Seric };
145148Seric 
146200Seric /* values for sccsoper */
147200Seric # define PROG		0	/* call a program */
148201Seric # define CMACRO		1	/* command substitution macro */
149226Seric # define FIX		2	/* fix a delta */
150261Seric # define CLEAN		3	/* clean out recreatable files */
151396Seric # define UNEDIT		4	/* unedit a file */
1521431Seric # define SHELL		5	/* call a shell file (like PROG) */
1531433Seric # define DIFFS		6	/* diff between sccs & file out */
1541871Seric # define DODIFF		7	/* internal call to diff program */
155200Seric 
156157Seric /* bits for sccsflags */
157200Seric # define NO_SDOT	0001	/* no s. on front of args */
158200Seric # define REALUSER	0002	/* protected (e.g., admin) */
159148Seric 
160819Seric /* modes for the "clean", "info", "check" ops */
161819Seric # define CLEANC		0	/* clean command */
162819Seric # define INFOC		1	/* info command */
163819Seric # define CHECKC		2	/* check command */
1641730Seric # define TELLC		3	/* give list of files being edited */
165819Seric 
1661432Seric /*
1671432Seric **  Description of commands known to this program.
1681432Seric **	First argument puts the command into a class.  Second arg is
1691432Seric **	info regarding treatment of this command.  Third arg is a
1701432Seric **	list of flags this command accepts from macros, etc.  Fourth
1711432Seric **	arg is the pathname of the implementing program, or the
1721432Seric **	macro definition, or the arg to a sub-algorithm.
1731432Seric */
174202Seric 
175148Seric struct sccsprog SccsProg[] =
176148Seric {
1771864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1781864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1791864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1801864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1811864Seric 	"get",		PROG,	0,			PROGPATH(get),
1821864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1832136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1841864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1851864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1862136Seric 	"val",		PROG,	0,			PROGPATH(val),
1871864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1881864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1891864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1901864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1912138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1921864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1931864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1941864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1951864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1961864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1971864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1981864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1991871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
2002137Seric 	"print",	CMACRO,	0,			"prt -e/get -p -m -s",
201*2226Seric 	"branch",	CMACRO,	NO_SDOT,
202*2226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
2031864Seric 	NULL,		-1,	0,			NULL
204148Seric };
205148Seric 
2061432Seric /* one line from a p-file */
207396Seric struct pfile
208396Seric {
209396Seric 	char	*p_osid;	/* old SID */
210396Seric 	char	*p_nsid;	/* new SID */
211396Seric 	char	*p_user;	/* user who did edit */
212396Seric 	char	*p_date;	/* date of get */
213396Seric 	char	*p_time;	/* time of get */
2142161Seric 	char	*p_aux;		/* extra info at end */
215396Seric };
216396Seric 
2171270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2181270Seric # ifdef SCCSDIR
2191270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2201205Seric # else
2211270Seric char	*SccsDir = "";
2221205Seric # endif
2231437Seric char	MyName[] = MYNAME;	/* name used in messages */
2241433Seric int	OutFile = -1;		/* override output file for commands */
225157Seric bool	RealUser;		/* if set, running as real user */
226393Seric # ifdef DEBUG
227393Seric bool	Debug;			/* turn on tracing */
228393Seric # endif
2292139Seric # ifndef V6
2302139Seric extern char	*getenv();
2312139Seric # endif V6
2321432Seric 
233148Seric main(argc, argv)
234148Seric 	int argc;
235148Seric 	char **argv;
236148Seric {
237148Seric 	register char *p;
238262Seric 	extern struct sccsprog *lookup();
2391282Seric 	register int i;
2402139Seric # ifndef V6
2412139Seric # ifndef SCCSDIR
2422140Seric 	register struct passwd *pw;
2432140Seric 	extern struct passwd *getpwnam();
2442140Seric 	char buf[100];
2452140Seric 
2462139Seric 	/* pull "SccsDir" out of the environment (possibly) */
2472139Seric 	p = getenv("PROJECT");
2482140Seric 	if (p != NULL && p[0] != '\0')
2492140Seric 	{
2502140Seric 		if (p[0] == '/')
2512140Seric 			SccsDir = p;
2522140Seric 		else
2532140Seric 		{
2542140Seric 			pw = getpwnam(p);
2552140Seric 			if (pw == NULL)
2562140Seric 			{
2572140Seric 				usrerr("user %s does not exist", p);
2582140Seric 				exit(EX_USAGE);
2592140Seric 			}
2602140Seric 			strcpy(buf, pw->pw_dir);
2612140Seric 			strcat(buf, "/src");
2622140Seric 			if (access(buf, 0) < 0)
2632140Seric 			{
2642140Seric 				strcpy(buf, pw->pw_dir);
2652140Seric 				strcat(buf, "/source");
2662140Seric 				if (access(buf, 0) < 0)
2672140Seric 				{
2682140Seric 					usrerr("project %s has no source!", p);
2692140Seric 					exit(EX_USAGE);
2702140Seric 				}
2712140Seric 			}
2722140Seric 			SccsDir = buf;
2732140Seric 		}
2742140Seric 	}
2752139Seric # endif SCCSDIR
2762139Seric # endif V6
2772139Seric 
278148Seric 	/*
279148Seric 	**  Detect and decode flags intended for this program.
280148Seric 	*/
281148Seric 
282200Seric 	if (argc < 2)
283148Seric 	{
2841205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
285200Seric 		exit(EX_USAGE);
286200Seric 	}
287200Seric 	argv[argc] = NULL;
288200Seric 
289262Seric 	if (lookup(argv[0]) == NULL)
290200Seric 	{
291262Seric 		while ((p = *++argv) != NULL)
292148Seric 		{
293262Seric 			if (*p != '-')
294262Seric 				break;
295262Seric 			switch (*++p)
296262Seric 			{
297262Seric 			  case 'r':		/* run as real user */
298262Seric 				setuid(getuid());
299262Seric 				RealUser++;
300262Seric 				break;
301148Seric 
3021270Seric # ifndef SCCSDIR
303262Seric 			  case 'p':		/* path of sccs files */
304262Seric 				SccsPath = ++p;
305262Seric 				break;
306148Seric 
307588Seric 			  case 'd':		/* directory to search from */
308588Seric 				SccsDir = ++p;
309588Seric 				break;
3101205Seric # endif
311588Seric 
312393Seric # ifdef DEBUG
313393Seric 			  case 'T':		/* trace */
314393Seric 				Debug++;
315393Seric 				break;
316393Seric # endif
317393Seric 
318262Seric 			  default:
3191205Seric 				usrerr("unknown option -%s", p);
320262Seric 				break;
321262Seric 			}
322148Seric 		}
323262Seric 		if (SccsPath[0] == '\0')
324262Seric 			SccsPath = ".";
325148Seric 	}
326148Seric 
3271737Seric 	i = command(argv, FALSE, "");
3281282Seric 	exit(i);
329200Seric }
3301432Seric 
3311432Seric /*
3321282Seric **  COMMAND -- look up and perform a command
3331282Seric **
3341282Seric **	This routine is the guts of this program.  Given an
3351282Seric **	argument vector, it looks up the "command" (argv[0])
3361282Seric **	in the configuration table and does the necessary stuff.
3371282Seric **
3381282Seric **	Parameters:
3391282Seric **		argv -- an argument vector to process.
3401282Seric **		forkflag -- if set, fork before executing the command.
3411316Seric **		editflag -- if set, only include flags listed in the
3421316Seric **			sccsklets field of the command descriptor.
3431316Seric **		arg0 -- a space-seperated list of arguments to insert
3441316Seric **			before argv.
3451282Seric **
3461282Seric **	Returns:
3471282Seric **		zero -- command executed ok.
3481282Seric **		else -- error status.
3491282Seric **
3501282Seric **	Side Effects:
3511282Seric **		none.
3521282Seric */
353157Seric 
3541737Seric command(argv, forkflag, arg0)
355200Seric 	char **argv;
356201Seric 	bool forkflag;
3571316Seric 	char *arg0;
358200Seric {
359200Seric 	register struct sccsprog *cmd;
360200Seric 	register char *p;
361201Seric 	char buf[40];
362262Seric 	extern struct sccsprog *lookup();
3631316Seric 	char *nav[1000];
3641316Seric 	char **np;
3651431Seric 	register char **ap;
366585Seric 	register int i;
3671431Seric 	register char *q;
368585Seric 	extern bool unedit();
3691282Seric 	int rval = 0;
3701316Seric 	extern char *index();
3711316Seric 	extern char *makefile();
3721737Seric 	char *editchs;
3731435Seric 	extern char *tail();
374200Seric 
375393Seric # ifdef DEBUG
376393Seric 	if (Debug)
377393Seric 	{
3781316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3791316Seric 		for (np = argv; *np != NULL; np++)
3801316Seric 			printf("\t\"%s\"\n", *np);
381393Seric 	}
382393Seric # endif
383393Seric 
384157Seric 	/*
3851316Seric 	**  Copy arguments.
3861438Seric 	**	Copy from arg0 & if necessary at most one arg
3871438Seric 	**	from argv[0].
3881316Seric 	*/
3891316Seric 
3901431Seric 	np = ap = &nav[1];
3911737Seric 	editchs = NULL;
3921821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3931316Seric 	{
3941316Seric 		*np++ = q;
3951316Seric 		while (*p == ' ')
3961316Seric 			p++;
3971737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3981316Seric 			*q++ = *p++;
3991316Seric 		*q++ = '\0';
4001737Seric 		if (*p == ':')
4011737Seric 		{
4021737Seric 			editchs = q;
4031821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4041737Seric 				*q++ = *p;
4051737Seric 			*q++ = '\0';
4061737Seric 		}
4071316Seric 	}
4081316Seric 	*np = NULL;
4091431Seric 	if (*ap == NULL)
4101316Seric 		*np++ = *argv++;
4111316Seric 
4121316Seric 	/*
413148Seric 	**  Look up command.
4141431Seric 	**	At this point, *ap is the command name.
415148Seric 	*/
416148Seric 
4171431Seric 	cmd = lookup(*ap);
418262Seric 	if (cmd == NULL)
419148Seric 	{
4201431Seric 		usrerr("Unknown command \"%s\"", *ap);
4211282Seric 		return (EX_USAGE);
422148Seric 	}
423148Seric 
424148Seric 	/*
4251316Seric 	**  Copy remaining arguments doing editing as appropriate.
4261316Seric 	*/
4271316Seric 
4281316Seric 	for (; *argv != NULL; argv++)
4291316Seric 	{
4301316Seric 		p = *argv;
4311316Seric 		if (*p == '-')
4321316Seric 		{
4331737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4341316Seric 				*np++ = p;
4351316Seric 		}
4361316Seric 		else
4371316Seric 		{
4381316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4391316Seric 				p = makefile(p);
4401316Seric 			if (p != NULL)
4411316Seric 				*np++ = p;
4421316Seric 		}
4431316Seric 	}
4441316Seric 	*np = NULL;
4451316Seric 
4461316Seric 	/*
447200Seric 	**  Interpret operation associated with this command.
448157Seric 	*/
449157Seric 
450200Seric 	switch (cmd->sccsoper)
451200Seric 	{
4521431Seric 	  case SHELL:		/* call a shell file */
4531431Seric 		*ap = cmd->sccspath;
4541431Seric 		*--ap = "sh";
4551431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4561431Seric 		break;
4571431Seric 
458200Seric 	  case PROG:		/* call an sccs prog */
4591431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
460201Seric 		break;
461201Seric 
462201Seric 	  case CMACRO:		/* command macro */
4631438Seric 		/* step through & execute each part of the macro */
464201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
465201Seric 		{
4661316Seric 			q = p;
4671316Seric 			while (*p != '\0' && *p != '/')
4681316Seric 				p++;
4691737Seric 			rval = command(&ap[1], *p != '\0', q);
4701282Seric 			if (rval != 0)
4711282Seric 				break;
472201Seric 		}
4731282Seric 		break;
474157Seric 
475226Seric 	  case FIX:		/* fix a delta */
4761431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
477226Seric 		{
4781205Seric 			usrerr("-r flag needed for fix command");
4791282Seric 			rval = EX_USAGE;
480226Seric 			break;
481226Seric 		}
4821438Seric 
4831438Seric 		/* get the version with all changes */
4841737Seric 		rval = command(&ap[1], TRUE, "get -k");
4851438Seric 
4861438Seric 		/* now remove that version from the s-file */
4871282Seric 		if (rval == 0)
4881737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4891438Seric 
4901438Seric 		/* and edit the old version (but don't clobber new vers) */
4911282Seric 		if (rval == 0)
4921737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4931282Seric 		break;
494226Seric 
495261Seric 	  case CLEAN:
4961822Seric 		rval = clean((int) cmd->sccspath, ap);
497261Seric 		break;
498261Seric 
499396Seric 	  case UNEDIT:
5001431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
501585Seric 		{
5021316Seric 			if (unedit(*argv))
5031316Seric 				*np++ = *argv;
504585Seric 		}
5051316Seric 		*np = NULL;
5061438Seric 
5071438Seric 		/* get all the files that we unedited successfully */
5081738Seric 		if (np > &ap[1])
5091737Seric 			rval = command(&ap[1], FALSE, "get");
510396Seric 		break;
511396Seric 
5121433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5131433Seric 		/* find the end of the flag arguments */
5141433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5151433Seric 			continue;
5161433Seric 		argv = np;
5171433Seric 
5181433Seric 		/* for each file, do the diff */
5191502Seric 		p = argv[1];
5201433Seric 		while (*np != NULL)
5211433Seric 		{
5221438Seric 			/* messy, but we need a null terminated argv */
5231433Seric 			*argv = *np++;
5241502Seric 			argv[1] = NULL;
5251435Seric 			i = dodiff(ap, tail(*argv));
5261433Seric 			if (rval == 0)
5271433Seric 				rval = i;
5281502Seric 			argv[1] = p;
5291433Seric 		}
5301433Seric 		break;
5311433Seric 
5321871Seric 	  case DODIFF:		/* internal diff call */
5331871Seric 		setuid(getuid());
5341871Seric 		for (np = ap; *np != NULL; np++)
5351871Seric 		{
5361871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5371871Seric 				(*np)[1] = 'c';
5381871Seric 		}
5391871Seric 
5401871Seric 		/* insert "-" argument */
5411871Seric 		np[1] = NULL;
5421871Seric 		np[0] = np[-1];
5431871Seric 		np[-1] = "-";
5441871Seric 
5451871Seric 		/* execute the diff program of choice */
5461871Seric # ifndef V6
5471871Seric 		execvp("diff", ap);
5481871Seric # endif
5491871Seric 		execv(cmd->sccspath, argv);
5501871Seric 		syserr("cannot exec %s", cmd->sccspath);
5511871Seric 		exit(EX_OSERR);
5521871Seric 
553200Seric 	  default:
5541205Seric 		syserr("oper %d", cmd->sccsoper);
555200Seric 		exit(EX_SOFTWARE);
556200Seric 	}
5571282Seric # ifdef DEBUG
5581282Seric 	if (Debug)
5591282Seric 		printf("command: rval=%d\n", rval);
5601282Seric # endif
5611282Seric 	return (rval);
562200Seric }
5631432Seric 
5641432Seric /*
565262Seric **  LOOKUP -- look up an SCCS command name.
566262Seric **
567262Seric **	Parameters:
568262Seric **		name -- the name of the command to look up.
569262Seric **
570262Seric **	Returns:
571262Seric **		ptr to command descriptor for this command.
572262Seric **		NULL if no such entry.
573262Seric **
574262Seric **	Side Effects:
575262Seric **		none.
576262Seric */
577200Seric 
578262Seric struct sccsprog *
579262Seric lookup(name)
580262Seric 	char *name;
581262Seric {
582262Seric 	register struct sccsprog *cmd;
583226Seric 
584262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
585262Seric 	{
586262Seric 		if (strcmp(cmd->sccsname, name) == 0)
587262Seric 			return (cmd);
588262Seric 	}
589262Seric 	return (NULL);
590262Seric }
5911432Seric 
5921432Seric /*
5931282Seric **  CALLPROG -- call a program
5941282Seric **
5951316Seric **	Used to call the SCCS programs.
5961282Seric **
5971282Seric **	Parameters:
5981282Seric **		progpath -- pathname of the program to call.
5991282Seric **		flags -- status flags from the command descriptors.
6001282Seric **		argv -- an argument vector to pass to the program.
6011282Seric **		forkflag -- if true, fork before calling, else just
6021282Seric **			exec.
6031282Seric **
6041282Seric **	Returns:
6051282Seric **		The exit status of the program.
6061282Seric **		Nothing if forkflag == FALSE.
6071282Seric **
6081282Seric **	Side Effects:
6091282Seric **		Can exit if forkflag == FALSE.
6101282Seric */
611226Seric 
612200Seric callprog(progpath, flags, argv, forkflag)
613200Seric 	char *progpath;
614200Seric 	short flags;
615200Seric 	char **argv;
616200Seric 	bool forkflag;
617200Seric {
618200Seric 	register int i;
619201Seric 	auto int st;
620200Seric 
6211316Seric # ifdef DEBUG
6221316Seric 	if (Debug)
6231316Seric 	{
6241316Seric 		printf("callprog:\n");
6251316Seric 		for (i = 0; argv[i] != NULL; i++)
6261316Seric 			printf("\t\"%s\"\n", argv[i]);
6271316Seric 	}
6281316Seric # endif
6291316Seric 
630200Seric 	if (*argv == NULL)
631200Seric 		return (-1);
632200Seric 
633157Seric 	/*
634226Seric 	**  Fork if appropriate.
635148Seric 	*/
636148Seric 
637200Seric 	if (forkflag)
638200Seric 	{
639393Seric # ifdef DEBUG
640393Seric 		if (Debug)
641393Seric 			printf("Forking\n");
642393Seric # endif
643200Seric 		i = fork();
644200Seric 		if (i < 0)
645200Seric 		{
6461205Seric 			syserr("cannot fork");
647200Seric 			exit(EX_OSERR);
648200Seric 		}
649200Seric 		else if (i > 0)
650201Seric 		{
651201Seric 			wait(&st);
6521282Seric 			if ((st & 0377) == 0)
6531282Seric 				st = (st >> 8) & 0377;
6541433Seric 			if (OutFile >= 0)
6551433Seric 			{
6561433Seric 				close(OutFile);
6571433Seric 				OutFile = -1;
6581433Seric 			}
659201Seric 			return (st);
660201Seric 		}
661200Seric 	}
6621433Seric 	else if (OutFile >= 0)
6631433Seric 	{
6641433Seric 		syserr("callprog: setting stdout w/o forking");
6651433Seric 		exit(EX_SOFTWARE);
6661433Seric 	}
667200Seric 
6681433Seric 	/* set protection as appropriate */
669200Seric 	if (bitset(REALUSER, flags))
670200Seric 		setuid(getuid());
6711433Seric 
6721433Seric 	/* change standard input & output if needed */
6731433Seric 	if (OutFile >= 0)
6741433Seric 	{
6751433Seric 		close(1);
6761433Seric 		dup(OutFile);
6771433Seric 		close(OutFile);
6781433Seric 	}
679226Seric 
6801433Seric 	/* call real SCCS program */
681226Seric 	execv(progpath, argv);
6821205Seric 	syserr("cannot execute %s", progpath);
683148Seric 	exit(EX_UNAVAILABLE);
6841738Seric 	/*NOTREACHED*/
685148Seric }
6861432Seric 
6871432Seric /*
688586Seric **  MAKEFILE -- make filename of SCCS file
689586Seric **
690586Seric **	If the name passed is already the name of an SCCS file,
691586Seric **	just return it.  Otherwise, munge the name into the name
692586Seric **	of the actual SCCS file.
693586Seric **
694586Seric **	There are cases when it is not clear what you want to
695586Seric **	do.  For example, if SccsPath is an absolute pathname
696586Seric **	and the name given is also an absolute pathname, we go
697586Seric **	for SccsPath (& only use the last component of the name
698586Seric **	passed) -- this is important for security reasons (if
699586Seric **	sccs is being used as a setuid front end), but not
700586Seric **	particularly intuitive.
701586Seric **
702586Seric **	Parameters:
703586Seric **		name -- the file name to be munged.
704586Seric **
705586Seric **	Returns:
706586Seric **		The pathname of the sccs file.
707586Seric **		NULL on error.
708586Seric **
709586Seric **	Side Effects:
710586Seric **		none.
711586Seric */
712148Seric 
713148Seric char *
714148Seric makefile(name)
715148Seric 	char *name;
716148Seric {
717148Seric 	register char *p;
718148Seric 	char buf[512];
719148Seric 	extern char *malloc();
720586Seric 	extern char *rindex();
721588Seric 	extern bool safepath();
722587Seric 	extern bool isdir();
723587Seric 	register char *q;
724148Seric 
725586Seric 	p = rindex(name, '/');
726586Seric 	if (p == NULL)
727586Seric 		p = name;
728586Seric 	else
729586Seric 		p++;
730586Seric 
731148Seric 	/*
732588Seric 	**  Check to see that the path is "safe", i.e., that we
733588Seric 	**  are not letting some nasty person use the setuid part
734588Seric 	**  of this program to look at or munge some presumably
735588Seric 	**  hidden files.
736148Seric 	*/
737148Seric 
738588Seric 	if (SccsDir[0] == '/' && !safepath(name))
739588Seric 		return (NULL);
740586Seric 
741586Seric 	/*
742588Seric 	**  Create the base pathname.
743586Seric 	*/
744586Seric 
7451438Seric 	/* first the directory part */
746588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
747148Seric 	{
748588Seric 		strcpy(buf, SccsDir);
749586Seric 		strcat(buf, "/");
750586Seric 	}
751586Seric 	else
752586Seric 		strcpy(buf, "");
7531438Seric 
7541438Seric 	/* then the head of the pathname */
755587Seric 	strncat(buf, name, p - name);
756587Seric 	q = &buf[strlen(buf)];
7571438Seric 
7581438Seric 	/* now copy the final part of the name, in case useful */
759587Seric 	strcpy(q, p);
7601438Seric 
7611438Seric 	/* so is it useful? */
762587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
763586Seric 	{
7641438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
765588Seric 		strcpy(q, SccsPath);
766588Seric 		strcat(buf, "/s.");
7671438Seric 
7681438Seric 		/* and now the end of the name */
769586Seric 		strcat(buf, p);
770586Seric 	}
771148Seric 
7721438Seric 	/* if i haven't changed it, why did I do all this? */
773588Seric 	if (strcmp(buf, name) == 0)
774588Seric 		p = name;
775588Seric 	else
776148Seric 	{
7771438Seric 		/* but if I have, squirrel it away */
778588Seric 		p = malloc(strlen(buf) + 1);
779588Seric 		if (p == NULL)
780588Seric 		{
781588Seric 			perror("Sccs: no mem");
782588Seric 			exit(EX_OSERR);
783588Seric 		}
784588Seric 		strcpy(p, buf);
785148Seric 	}
7861438Seric 
787148Seric 	return (p);
788148Seric }
7891432Seric 
7901432Seric /*
791587Seric **  ISDIR -- return true if the argument is a directory.
792587Seric **
793587Seric **	Parameters:
794587Seric **		name -- the pathname of the file to check.
795587Seric **
796587Seric **	Returns:
797587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
798587Seric **
799587Seric **	Side Effects:
800587Seric **		none.
801587Seric */
802587Seric 
803587Seric bool
804587Seric isdir(name)
805587Seric 	char *name;
806587Seric {
807587Seric 	struct stat stbuf;
808587Seric 
809587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
810587Seric }
8111432Seric 
8121432Seric /*
813586Seric **  SAFEPATH -- determine whether a pathname is "safe"
814586Seric **
815586Seric **	"Safe" pathnames only allow you to get deeper into the
816586Seric **	directory structure, i.e., full pathnames and ".." are
817586Seric **	not allowed.
818586Seric **
819586Seric **	Parameters:
820586Seric **		p -- the name to check.
821586Seric **
822586Seric **	Returns:
823586Seric **		TRUE -- if the path is safe.
824586Seric **		FALSE -- if the path is not safe.
825586Seric **
826586Seric **	Side Effects:
827586Seric **		Prints a message if the path is not safe.
828586Seric */
829586Seric 
830586Seric bool
831586Seric safepath(p)
832586Seric 	register char *p;
833586Seric {
834586Seric 	extern char *index();
835586Seric 
836586Seric 	if (*p != '/')
837586Seric 	{
838586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
839586Seric 		{
840586Seric 			p = index(p, '/');
841586Seric 			if (p == NULL)
842586Seric 				return (TRUE);
843586Seric 			p++;
844586Seric 		}
845586Seric 	}
846586Seric 
847586Seric 	printf("You may not use full pathnames or \"..\"\n");
848586Seric 	return (FALSE);
849586Seric }
8501432Seric 
8511432Seric /*
852261Seric **  CLEAN -- clean out recreatable files
853261Seric **
854261Seric **	Any file for which an "s." file exists but no "p." file
855261Seric **	exists in the current directory is purged.
856261Seric **
857261Seric **	Parameters:
8581822Seric **		mode -- tells whether this came from a "clean", "info", or
8591822Seric **			"check" command.
8601822Seric **		argv -- the rest of the argument vector.
861261Seric **
862261Seric **	Returns:
863261Seric **		none.
864261Seric **
865261Seric **	Side Effects:
866819Seric **		Removes files in the current directory.
867819Seric **		Prints information regarding files being edited.
868819Seric **		Exits if a "check" command.
869261Seric */
870261Seric 
8711822Seric clean(mode, argv)
872819Seric 	int mode;
8731822Seric 	char **argv;
874261Seric {
875261Seric 	struct direct dir;
876261Seric 	char buf[100];
8772140Seric 	char *bufend;
878346Seric 	register FILE *dirfd;
879346Seric 	register char *basefile;
880351Seric 	bool gotedit;
8811822Seric 	bool gotpfent;
882394Seric 	FILE *pfp;
8831822Seric 	bool nobranch = FALSE;
8841822Seric 	extern struct pfile *getpfent();
8851822Seric 	register struct pfile *pf;
8861822Seric 	register char **ap;
8871864Seric 	extern char *username();
8881864Seric 	char *usernm = NULL;
8892140Seric 	char *subdir = NULL;
8902140Seric 	char *cmdname;
891261Seric 
8921438Seric 	/*
8931822Seric 	**  Process the argv
8941822Seric 	*/
8951822Seric 
8962140Seric 	cmdname = *argv;
8972140Seric 	for (ap = argv; *++ap != NULL; )
8981822Seric 	{
8991864Seric 		if (**ap == '-')
9001864Seric 		{
9011864Seric 			/* we have a flag */
9021864Seric 			switch ((*ap)[1])
9031864Seric 			{
9041864Seric 			  case 'b':
9051864Seric 				nobranch = TRUE;
9061864Seric 				break;
9071864Seric 
9081864Seric 			  case 'u':
9091864Seric 				if ((*ap)[2] != '\0')
9101864Seric 					usernm = &(*ap)[2];
9111864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9121864Seric 					usernm = *++ap;
9131864Seric 				else
9141864Seric 					usernm = username();
9151864Seric 				break;
9161864Seric 			}
9171864Seric 		}
9182140Seric 		else
9192140Seric 		{
9202140Seric 			if (subdir != NULL)
9212140Seric 				usrerr("too many args");
9222140Seric 			else
9232140Seric 				subdir = *ap;
9242140Seric 		}
9251822Seric 	}
9261822Seric 
9271822Seric 	/*
9281438Seric 	**  Find and open the SCCS directory.
9291438Seric 	*/
9301438Seric 
9311207Seric 	strcpy(buf, SccsDir);
9321207Seric 	if (buf[0] != '\0')
9331207Seric 		strcat(buf, "/");
9342140Seric 	if (subdir != NULL)
9352140Seric 	{
9362140Seric 		strcat(buf, subdir);
9372140Seric 		strcat(buf, "/");
9382140Seric 	}
9391207Seric 	strcat(buf, SccsPath);
9402140Seric 	bufend = &buf[strlen(buf)];
9411438Seric 
9421207Seric 	dirfd = fopen(buf, "r");
943261Seric 	if (dirfd == NULL)
944261Seric 	{
9451207Seric 		usrerr("cannot open %s", buf);
9461282Seric 		return (EX_NOINPUT);
947261Seric 	}
948261Seric 
949261Seric 	/*
950261Seric 	**  Scan the SCCS directory looking for s. files.
9511438Seric 	**	gotedit tells whether we have tried to clean any
9521438Seric 	**		files that are being edited.
953261Seric 	*/
954261Seric 
955351Seric 	gotedit = FALSE;
9561738Seric 	while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL)
957261Seric 	{
958568Seric 		if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0)
959261Seric 			continue;
960261Seric 
961261Seric 		/* got an s. file -- see if the p. file exists */
9622140Seric 		strcpy(bufend, "/p.");
9632140Seric 		basefile = bufend + 3;
964568Seric 		strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
965346Seric 		basefile[sizeof dir.d_name - 2] = '\0';
9661822Seric 
9671822Seric 		/*
9681822Seric 		**  open and scan the p-file.
9691822Seric 		**	'gotpfent' tells if we have found a valid p-file
9701822Seric 		**		entry.
9711822Seric 		*/
9721822Seric 
973394Seric 		pfp = fopen(buf, "r");
9741822Seric 		gotpfent = FALSE;
975394Seric 		if (pfp != NULL)
976346Seric 		{
9771438Seric 			/* the file exists -- report it's contents */
9781822Seric 			while ((pf = getpfent(pfp)) != NULL)
9791730Seric 			{
9801822Seric 				if (nobranch && isbranch(pf->p_nsid))
9811822Seric 					continue;
9821864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
9831864Seric 					continue;
9841822Seric 				gotedit = TRUE;
9851822Seric 				gotpfent = TRUE;
9861822Seric 				if (mode == TELLC)
9871822Seric 				{
9881822Seric 					printf("%s\n", basefile);
9891822Seric 					break;
9901822Seric 				}
9912161Seric 				printf("%12s: being edited: ", basefile);
9922161Seric 				putpfent(pf, stdout);
9931730Seric 			}
994394Seric 			fclose(pfp);
9951822Seric 		}
996261Seric 
997261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
9981870Seric 		if (mode == CLEANC && !gotpfent)
999346Seric 		{
1000568Seric 			strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2);
1001346Seric 			buf[sizeof dir.d_name - 2] = '\0';
1002346Seric 			unlink(buf);
1003346Seric 		}
1004261Seric 	}
1005261Seric 
10061438Seric 	/* cleanup & report results */
1007261Seric 	fclose(dirfd);
1008819Seric 	if (!gotedit && mode == INFOC)
10091864Seric 	{
10101864Seric 		printf("Nothing being edited");
10111864Seric 		if (nobranch)
10121864Seric 			printf(" (on trunk)");
10131864Seric 		if (usernm == NULL)
10141864Seric 			printf("\n");
10151864Seric 		else
10161864Seric 			printf(" by %s\n", usernm);
10171864Seric 	}
1018819Seric 	if (mode == CHECKC)
1019819Seric 		exit(gotedit);
10201282Seric 	return (EX_OK);
1021261Seric }
10221432Seric 
10231432Seric /*
10241822Seric **  ISBRANCH -- is the SID a branch?
10251822Seric **
10261822Seric **	Parameters:
10271822Seric **		sid -- the sid to check.
10281822Seric **
10291822Seric **	Returns:
10301822Seric **		TRUE if the sid represents a branch.
10311822Seric **		FALSE otherwise.
10321822Seric **
10331822Seric **	Side Effects:
10341822Seric **		none.
10351822Seric */
10361822Seric 
10371822Seric isbranch(sid)
10381822Seric 	char *sid;
10391822Seric {
10401822Seric 	register char *p;
10411822Seric 	int dots;
10421822Seric 
10431822Seric 	dots = 0;
10441822Seric 	for (p = sid; *p != '\0'; p++)
10451822Seric 	{
10461822Seric 		if (*p == '.')
10471822Seric 			dots++;
10481822Seric 		if (dots > 1)
10491822Seric 			return (TRUE);
10501822Seric 	}
10511822Seric 	return (FALSE);
10521822Seric }
10531822Seric 
10541822Seric /*
1055396Seric **  UNEDIT -- unedit a file
1056396Seric **
1057396Seric **	Checks to see that the current user is actually editting
1058396Seric **	the file and arranges that s/he is not editting it.
1059396Seric **
1060396Seric **	Parameters:
1061416Seric **		fn -- the name of the file to be unedited.
1062396Seric **
1063396Seric **	Returns:
1064585Seric **		TRUE -- if the file was successfully unedited.
1065585Seric **		FALSE -- if the file was not unedited for some
1066585Seric **			reason.
1067396Seric **
1068396Seric **	Side Effects:
1069396Seric **		fn is removed
1070396Seric **		entries are removed from pfile.
1071396Seric */
1072396Seric 
1073585Seric bool
1074396Seric unedit(fn)
1075396Seric 	char *fn;
1076396Seric {
1077396Seric 	register FILE *pfp;
1078396Seric 	char *pfn;
1079396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1080396Seric 	FILE *tfp;
1081396Seric 	register char *q;
1082396Seric 	bool delete = FALSE;
1083396Seric 	bool others = FALSE;
1084396Seric 	char *myname;
10851864Seric 	extern char *username();
1086396Seric 	struct pfile *pent;
10871822Seric 	extern struct pfile *getpfent();
1088396Seric 	char buf[120];
10891316Seric 	extern char *makefile();
1090396Seric 
1091396Seric 	/* make "s." filename & find the trailing component */
1092396Seric 	pfn = makefile(fn);
1093586Seric 	if (pfn == NULL)
1094586Seric 		return (FALSE);
1095586Seric 	q = rindex(pfn, '/');
1096586Seric 	if (q == NULL)
1097586Seric 		q = &pfn[-1];
1098586Seric 	if (q[1] != 's' || q[2] != '.')
1099396Seric 	{
11001205Seric 		usrerr("bad file name \"%s\"", fn);
1101585Seric 		return (FALSE);
1102396Seric 	}
1103396Seric 
11041438Seric 	/* turn "s." into "p." & try to open it */
1105396Seric 	*++q = 'p';
1106396Seric 
1107396Seric 	pfp = fopen(pfn, "r");
1108396Seric 	if (pfp == NULL)
1109396Seric 	{
1110416Seric 		printf("%12s: not being edited\n", fn);
1111585Seric 		return (FALSE);
1112396Seric 	}
1113396Seric 
11141438Seric 	/* create temp file for editing p-file */
1115396Seric 	mktemp(tfn);
1116396Seric 	tfp = fopen(tfn, "w");
1117396Seric 	if (tfp == NULL)
1118396Seric 	{
11191205Seric 		usrerr("cannot create \"%s\"", tfn);
1120396Seric 		exit(EX_OSERR);
1121396Seric 	}
1122396Seric 
11231438Seric 	/* figure out who I am */
11241864Seric 	myname = username();
11251438Seric 
11261438Seric 	/*
11271438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11281438Seric 	*/
11291438Seric 
11301822Seric 	while ((pent = getpfent(pfp)) != NULL)
1131396Seric 	{
1132396Seric 		if (strcmp(pent->p_user, myname) == 0)
1133396Seric 		{
1134396Seric 			/* a match */
1135396Seric 			delete++;
1136396Seric 		}
1137396Seric 		else
1138396Seric 		{
11391438Seric 			/* output it again */
11402161Seric 			putpfent(pent, tfp);
1141396Seric 			others++;
1142396Seric 		}
1143396Seric 	}
1144396Seric 
1145396Seric 	/* do final cleanup */
1146396Seric 	if (others)
1147396Seric 	{
11481438Seric 		/* copy it back (perhaps it should be linked?) */
1149396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1150396Seric 		{
11511205Seric 			syserr("cannot reopen \"%s\"", tfn);
1152396Seric 			exit(EX_OSERR);
1153396Seric 		}
1154396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1155396Seric 		{
11561205Seric 			usrerr("cannot create \"%s\"", pfn);
1157585Seric 			return (FALSE);
1158396Seric 		}
1159396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1160396Seric 			fputs(buf, pfp);
1161396Seric 	}
1162396Seric 	else
1163396Seric 	{
11641438Seric 		/* it's empty -- remove it */
1165396Seric 		unlink(pfn);
1166396Seric 	}
1167396Seric 	fclose(tfp);
1168396Seric 	fclose(pfp);
1169396Seric 	unlink(tfn);
1170396Seric 
11711438Seric 	/* actually remove the g-file */
1172396Seric 	if (delete)
1173396Seric 	{
11741435Seric 		unlink(tail(fn));
11751435Seric 		printf("%12s: removed\n", tail(fn));
1176585Seric 		return (TRUE);
1177396Seric 	}
1178396Seric 	else
1179396Seric 	{
1180416Seric 		printf("%12s: not being edited by you\n", fn);
1181585Seric 		return (FALSE);
1182396Seric 	}
1183396Seric }
11841432Seric 
11851432Seric /*
11861433Seric **  DODIFF -- diff an s-file against a g-file
11871433Seric **
11881433Seric **	Parameters:
11891433Seric **		getv -- argv for the 'get' command.
11901433Seric **		gfile -- name of the g-file to diff against.
11911433Seric **
11921433Seric **	Returns:
11931433Seric **		Result of get.
11941433Seric **
11951433Seric **	Side Effects:
11961433Seric **		none.
11971433Seric */
11981433Seric 
11991433Seric dodiff(getv, gfile)
12001433Seric 	char **getv;
12011433Seric 	char *gfile;
12021433Seric {
12031433Seric 	int pipev[2];
12041433Seric 	int rval;
12051433Seric 	register int i;
12061433Seric 	register int pid;
12071433Seric 	auto int st;
12081433Seric 	extern int errno;
12091433Seric 	int (*osig)();
12101433Seric 
12111905Seric 	printf("\n------- %s -------\n", gfile);
12121871Seric 	fflush(stdout);
12131501Seric 
12141438Seric 	/* create context for diff to run in */
12151433Seric 	if (pipe(pipev) < 0)
12161433Seric 	{
12171433Seric 		syserr("dodiff: pipe failed");
12181433Seric 		exit(EX_OSERR);
12191433Seric 	}
12201433Seric 	if ((pid = fork()) < 0)
12211433Seric 	{
12221433Seric 		syserr("dodiff: fork failed");
12231433Seric 		exit(EX_OSERR);
12241433Seric 	}
12251433Seric 	else if (pid > 0)
12261433Seric 	{
12271433Seric 		/* in parent; run get */
12281433Seric 		OutFile = pipev[1];
12291433Seric 		close(pipev[0]);
12301871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12311433Seric 		osig = signal(SIGINT, SIG_IGN);
12321433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
12331433Seric 			errno = 0;
12341433Seric 		signal(SIGINT, osig);
12351433Seric 		/* ignore result of diff */
12361433Seric 	}
12371433Seric 	else
12381433Seric 	{
12391433Seric 		/* in child, run diff */
12401433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
12411433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
12421433Seric 		{
12431433Seric 			syserr("dodiff: magic failed");
12441433Seric 			exit(EX_OSERR);
12451433Seric 		}
12461871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
12471433Seric 	}
12481433Seric 	return (rval);
12491433Seric }
12501433Seric 
12511433Seric /*
12521435Seric **  TAIL -- return tail of filename.
12531435Seric **
12541435Seric **	Parameters:
12551435Seric **		fn -- the filename.
12561435Seric **
12571435Seric **	Returns:
12581435Seric **		a pointer to the tail of the filename; e.g., given
12591435Seric **		"cmd/ls.c", "ls.c" is returned.
12601435Seric **
12611435Seric **	Side Effects:
12621435Seric **		none.
12631435Seric */
12641435Seric 
12651435Seric char *
12661435Seric tail(fn)
12671435Seric 	register char *fn;
12681435Seric {
12691435Seric 	register char *p;
12701435Seric 
12711435Seric 	for (p = fn; *p != 0; p++)
12721435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
12731435Seric 			fn = &p[1];
12741435Seric 	return (fn);
12751435Seric }
12761435Seric 
12771435Seric /*
12781822Seric **  GETPFENT -- get an entry from the p-file
1279396Seric **
1280396Seric **	Parameters:
1281396Seric **		pfp -- p-file file pointer
1282396Seric **
1283396Seric **	Returns:
1284396Seric **		pointer to p-file struct for next entry
1285396Seric **		NULL on EOF or error
1286396Seric **
1287396Seric **	Side Effects:
1288396Seric **		Each call wipes out results of previous call.
1289396Seric */
1290396Seric 
1291396Seric struct pfile *
12921822Seric getpfent(pfp)
1293396Seric 	FILE *pfp;
1294396Seric {
1295396Seric 	static struct pfile ent;
1296396Seric 	static char buf[120];
1297396Seric 	register char *p;
1298396Seric 	extern char *nextfield();
1299396Seric 
1300396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1301396Seric 		return (NULL);
1302396Seric 
1303396Seric 	ent.p_osid = p = buf;
1304396Seric 	ent.p_nsid = p = nextfield(p);
1305396Seric 	ent.p_user = p = nextfield(p);
1306396Seric 	ent.p_date = p = nextfield(p);
1307396Seric 	ent.p_time = p = nextfield(p);
13082161Seric 	ent.p_aux = p = nextfield(p);
1309396Seric 
1310396Seric 	return (&ent);
1311396Seric }
1312396Seric 
1313396Seric 
1314396Seric char *
1315396Seric nextfield(p)
1316396Seric 	register char *p;
1317396Seric {
1318396Seric 	if (p == NULL || *p == '\0')
1319396Seric 		return (NULL);
1320396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1321396Seric 		p++;
1322396Seric 	if (*p == '\n' || *p == '\0')
1323396Seric 	{
1324396Seric 		*p = '\0';
1325396Seric 		return (NULL);
1326396Seric 	}
1327396Seric 	*p++ = '\0';
1328396Seric 	return (p);
1329396Seric }
13302161Seric /*
13312161Seric **  PUTPFENT -- output a p-file entry to a file
13322161Seric **
13332161Seric **	Parameters:
13342161Seric **		pf -- the p-file entry
13352161Seric **		f -- the file to put it on.
13362161Seric **
13372161Seric **	Returns:
13382161Seric **		none.
13392161Seric **
13402161Seric **	Side Effects:
13412161Seric **		pf is written onto file f.
13422161Seric */
13432161Seric 
13442161Seric putpfent(pf, f)
13452161Seric 	register struct pfile *pf;
13462161Seric 	register FILE *f;
13472161Seric {
13482161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
13492161Seric 		pf->p_user, pf->p_date, pf->p_time);
13502161Seric 	if (pf->p_aux != NULL)
13512161Seric 		fprintf(f, " %s", pf->p_aux);
13522161Seric 	else
13532161Seric 		fprintf(f, "\n");
13542161Seric }
13551432Seric 
13561432Seric /*
13571205Seric **  USRERR -- issue user-level error
13581205Seric **
13591205Seric **	Parameters:
13601205Seric **		f -- format string.
13611205Seric **		p1-p3 -- parameters to a printf.
13621205Seric **
13631205Seric **	Returns:
13641205Seric **		-1
13651205Seric **
13661205Seric **	Side Effects:
13671205Seric **		none.
13681205Seric */
13691205Seric 
13701738Seric /*VARARGS1*/
13711205Seric usrerr(f, p1, p2, p3)
13721205Seric 	char *f;
13731205Seric {
13741205Seric 	fprintf(stderr, "\n%s: ", MyName);
13751205Seric 	fprintf(stderr, f, p1, p2, p3);
13761205Seric 	fprintf(stderr, "\n");
13771205Seric 
13781205Seric 	return (-1);
13791205Seric }
13801432Seric 
13811432Seric /*
13821205Seric **  SYSERR -- print system-generated error.
13831205Seric **
13841205Seric **	Parameters:
13851205Seric **		f -- format string to a printf.
13861205Seric **		p1, p2, p3 -- parameters to f.
13871205Seric **
13881205Seric **	Returns:
13891205Seric **		never.
13901205Seric **
13911205Seric **	Side Effects:
13921205Seric **		none.
13931205Seric */
13941205Seric 
13951738Seric /*VARARGS1*/
13961205Seric syserr(f, p1, p2, p3)
13971205Seric 	char *f;
13981205Seric {
13991205Seric 	extern int errno;
14001205Seric 
14011205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
14021205Seric 	fprintf(stderr, f, p1, p2, p3);
14031205Seric 	fprintf(stderr, "\n");
14041205Seric 	if (errno == 0)
14051205Seric 		exit(EX_SOFTWARE);
14061205Seric 	else
14071205Seric 	{
14081738Seric 		perror(NULL);
14091205Seric 		exit(EX_OSERR);
14101205Seric 	}
14111205Seric }
14121864Seric /*
14131864Seric **  USERNAME -- return name of the current user
14141864Seric **
14151864Seric **	Parameters:
14161864Seric **		none
14171864Seric **
14181864Seric **	Returns:
14191864Seric **		name of current user
14201864Seric **
14211864Seric **	Side Effects:
14221864Seric **		none
14231864Seric */
14241864Seric 
14251864Seric char *
14261864Seric username()
14271864Seric {
14281864Seric # ifdef UIDUSER
14291864Seric 	extern struct passwd *getpwuid();
14301864Seric 	register struct passwd *pw;
14311864Seric 
14321864Seric 	pw = getpwuid(getuid());
14331864Seric 	if (pw == NULL)
14341864Seric 	{
14351864Seric 		syserr("who are you? (uid=%d)", getuid());
14361864Seric 		exit(EX_OSERR);
14371864Seric 	}
14381864Seric 	return (pw->pw_name);
14391864Seric # else
14401905Seric 	extern char *getlogin();
14411905Seric 
14421864Seric 	return (getlogin());
14431864Seric # endif UIDUSER
14441864Seric }
1445