xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 2348)
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*2348Seric static char SccsId[] = "@(#)sccs.c	1.61 02/05/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",
2012226Seric 	"branch",	CMACRO,	NO_SDOT,
2022226Seric 		"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;
305*2348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
306*2348Seric 					SccsPath = *++argv;
307262Seric 				break;
308148Seric 
309588Seric 			  case 'd':		/* directory to search from */
310588Seric 				SccsDir = ++p;
311*2348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
312*2348Seric 					SccsDir = *++argv;
313588Seric 				break;
3141205Seric # endif
315588Seric 
316393Seric # ifdef DEBUG
317393Seric 			  case 'T':		/* trace */
318393Seric 				Debug++;
319393Seric 				break;
320393Seric # endif
321393Seric 
322262Seric 			  default:
3231205Seric 				usrerr("unknown option -%s", p);
324262Seric 				break;
325262Seric 			}
326148Seric 		}
327262Seric 		if (SccsPath[0] == '\0')
328262Seric 			SccsPath = ".";
329148Seric 	}
330148Seric 
3311737Seric 	i = command(argv, FALSE, "");
3321282Seric 	exit(i);
333200Seric }
3341432Seric 
3351432Seric /*
3361282Seric **  COMMAND -- look up and perform a command
3371282Seric **
3381282Seric **	This routine is the guts of this program.  Given an
3391282Seric **	argument vector, it looks up the "command" (argv[0])
3401282Seric **	in the configuration table and does the necessary stuff.
3411282Seric **
3421282Seric **	Parameters:
3431282Seric **		argv -- an argument vector to process.
3441282Seric **		forkflag -- if set, fork before executing the command.
3451316Seric **		editflag -- if set, only include flags listed in the
3461316Seric **			sccsklets field of the command descriptor.
3471316Seric **		arg0 -- a space-seperated list of arguments to insert
3481316Seric **			before argv.
3491282Seric **
3501282Seric **	Returns:
3511282Seric **		zero -- command executed ok.
3521282Seric **		else -- error status.
3531282Seric **
3541282Seric **	Side Effects:
3551282Seric **		none.
3561282Seric */
357157Seric 
3581737Seric command(argv, forkflag, arg0)
359200Seric 	char **argv;
360201Seric 	bool forkflag;
3611316Seric 	char *arg0;
362200Seric {
363200Seric 	register struct sccsprog *cmd;
364200Seric 	register char *p;
365201Seric 	char buf[40];
366262Seric 	extern struct sccsprog *lookup();
3671316Seric 	char *nav[1000];
3681316Seric 	char **np;
3691431Seric 	register char **ap;
370585Seric 	register int i;
3711431Seric 	register char *q;
372585Seric 	extern bool unedit();
3731282Seric 	int rval = 0;
3741316Seric 	extern char *index();
3751316Seric 	extern char *makefile();
3761737Seric 	char *editchs;
3771435Seric 	extern char *tail();
378200Seric 
379393Seric # ifdef DEBUG
380393Seric 	if (Debug)
381393Seric 	{
3821316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3831316Seric 		for (np = argv; *np != NULL; np++)
3841316Seric 			printf("\t\"%s\"\n", *np);
385393Seric 	}
386393Seric # endif
387393Seric 
388157Seric 	/*
3891316Seric 	**  Copy arguments.
3901438Seric 	**	Copy from arg0 & if necessary at most one arg
3911438Seric 	**	from argv[0].
3921316Seric 	*/
3931316Seric 
3941431Seric 	np = ap = &nav[1];
3951737Seric 	editchs = NULL;
3961821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3971316Seric 	{
3981316Seric 		*np++ = q;
3991316Seric 		while (*p == ' ')
4001316Seric 			p++;
4011737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
4021316Seric 			*q++ = *p++;
4031316Seric 		*q++ = '\0';
4041737Seric 		if (*p == ':')
4051737Seric 		{
4061737Seric 			editchs = q;
4071821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4081737Seric 				*q++ = *p;
4091737Seric 			*q++ = '\0';
4101737Seric 		}
4111316Seric 	}
4121316Seric 	*np = NULL;
4131431Seric 	if (*ap == NULL)
4141316Seric 		*np++ = *argv++;
4151316Seric 
4161316Seric 	/*
417148Seric 	**  Look up command.
4181431Seric 	**	At this point, *ap is the command name.
419148Seric 	*/
420148Seric 
4211431Seric 	cmd = lookup(*ap);
422262Seric 	if (cmd == NULL)
423148Seric 	{
4241431Seric 		usrerr("Unknown command \"%s\"", *ap);
4251282Seric 		return (EX_USAGE);
426148Seric 	}
427148Seric 
428148Seric 	/*
4291316Seric 	**  Copy remaining arguments doing editing as appropriate.
4301316Seric 	*/
4311316Seric 
4321316Seric 	for (; *argv != NULL; argv++)
4331316Seric 	{
4341316Seric 		p = *argv;
4351316Seric 		if (*p == '-')
4361316Seric 		{
4371737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4381316Seric 				*np++ = p;
4391316Seric 		}
4401316Seric 		else
4411316Seric 		{
4421316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4431316Seric 				p = makefile(p);
4441316Seric 			if (p != NULL)
4451316Seric 				*np++ = p;
4461316Seric 		}
4471316Seric 	}
4481316Seric 	*np = NULL;
4491316Seric 
4501316Seric 	/*
451200Seric 	**  Interpret operation associated with this command.
452157Seric 	*/
453157Seric 
454200Seric 	switch (cmd->sccsoper)
455200Seric 	{
4561431Seric 	  case SHELL:		/* call a shell file */
4571431Seric 		*ap = cmd->sccspath;
4581431Seric 		*--ap = "sh";
4591431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4601431Seric 		break;
4611431Seric 
462200Seric 	  case PROG:		/* call an sccs prog */
4631431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
464201Seric 		break;
465201Seric 
466201Seric 	  case CMACRO:		/* command macro */
4671438Seric 		/* step through & execute each part of the macro */
468201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
469201Seric 		{
4701316Seric 			q = p;
4711316Seric 			while (*p != '\0' && *p != '/')
4721316Seric 				p++;
4731737Seric 			rval = command(&ap[1], *p != '\0', q);
4741282Seric 			if (rval != 0)
4751282Seric 				break;
476201Seric 		}
4771282Seric 		break;
478157Seric 
479226Seric 	  case FIX:		/* fix a delta */
4801431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
481226Seric 		{
4821205Seric 			usrerr("-r flag needed for fix command");
4831282Seric 			rval = EX_USAGE;
484226Seric 			break;
485226Seric 		}
4861438Seric 
4871438Seric 		/* get the version with all changes */
4881737Seric 		rval = command(&ap[1], TRUE, "get -k");
4891438Seric 
4901438Seric 		/* now remove that version from the s-file */
4911282Seric 		if (rval == 0)
4921737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4931438Seric 
4941438Seric 		/* and edit the old version (but don't clobber new vers) */
4951282Seric 		if (rval == 0)
4961737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4971282Seric 		break;
498226Seric 
499261Seric 	  case CLEAN:
5001822Seric 		rval = clean((int) cmd->sccspath, ap);
501261Seric 		break;
502261Seric 
503396Seric 	  case UNEDIT:
5041431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
505585Seric 		{
5061316Seric 			if (unedit(*argv))
5071316Seric 				*np++ = *argv;
508585Seric 		}
5091316Seric 		*np = NULL;
5101438Seric 
5111438Seric 		/* get all the files that we unedited successfully */
5121738Seric 		if (np > &ap[1])
5131737Seric 			rval = command(&ap[1], FALSE, "get");
514396Seric 		break;
515396Seric 
5161433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5171433Seric 		/* find the end of the flag arguments */
5181433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5191433Seric 			continue;
5201433Seric 		argv = np;
5211433Seric 
5221433Seric 		/* for each file, do the diff */
5231502Seric 		p = argv[1];
5241433Seric 		while (*np != NULL)
5251433Seric 		{
5261438Seric 			/* messy, but we need a null terminated argv */
5271433Seric 			*argv = *np++;
5281502Seric 			argv[1] = NULL;
5291435Seric 			i = dodiff(ap, tail(*argv));
5301433Seric 			if (rval == 0)
5311433Seric 				rval = i;
5321502Seric 			argv[1] = p;
5331433Seric 		}
5341433Seric 		break;
5351433Seric 
5361871Seric 	  case DODIFF:		/* internal diff call */
5371871Seric 		setuid(getuid());
5381871Seric 		for (np = ap; *np != NULL; np++)
5391871Seric 		{
5401871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5411871Seric 				(*np)[1] = 'c';
5421871Seric 		}
5431871Seric 
5441871Seric 		/* insert "-" argument */
5451871Seric 		np[1] = NULL;
5461871Seric 		np[0] = np[-1];
5471871Seric 		np[-1] = "-";
5481871Seric 
5491871Seric 		/* execute the diff program of choice */
5501871Seric # ifndef V6
5511871Seric 		execvp("diff", ap);
5521871Seric # endif
5531871Seric 		execv(cmd->sccspath, argv);
5541871Seric 		syserr("cannot exec %s", cmd->sccspath);
5551871Seric 		exit(EX_OSERR);
5561871Seric 
557200Seric 	  default:
5581205Seric 		syserr("oper %d", cmd->sccsoper);
559200Seric 		exit(EX_SOFTWARE);
560200Seric 	}
5611282Seric # ifdef DEBUG
5621282Seric 	if (Debug)
5631282Seric 		printf("command: rval=%d\n", rval);
5641282Seric # endif
5651282Seric 	return (rval);
566200Seric }
5671432Seric 
5681432Seric /*
569262Seric **  LOOKUP -- look up an SCCS command name.
570262Seric **
571262Seric **	Parameters:
572262Seric **		name -- the name of the command to look up.
573262Seric **
574262Seric **	Returns:
575262Seric **		ptr to command descriptor for this command.
576262Seric **		NULL if no such entry.
577262Seric **
578262Seric **	Side Effects:
579262Seric **		none.
580262Seric */
581200Seric 
582262Seric struct sccsprog *
583262Seric lookup(name)
584262Seric 	char *name;
585262Seric {
586262Seric 	register struct sccsprog *cmd;
587226Seric 
588262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
589262Seric 	{
590262Seric 		if (strcmp(cmd->sccsname, name) == 0)
591262Seric 			return (cmd);
592262Seric 	}
593262Seric 	return (NULL);
594262Seric }
5951432Seric 
5961432Seric /*
5971282Seric **  CALLPROG -- call a program
5981282Seric **
5991316Seric **	Used to call the SCCS programs.
6001282Seric **
6011282Seric **	Parameters:
6021282Seric **		progpath -- pathname of the program to call.
6031282Seric **		flags -- status flags from the command descriptors.
6041282Seric **		argv -- an argument vector to pass to the program.
6051282Seric **		forkflag -- if true, fork before calling, else just
6061282Seric **			exec.
6071282Seric **
6081282Seric **	Returns:
6091282Seric **		The exit status of the program.
6101282Seric **		Nothing if forkflag == FALSE.
6111282Seric **
6121282Seric **	Side Effects:
6131282Seric **		Can exit if forkflag == FALSE.
6141282Seric */
615226Seric 
616200Seric callprog(progpath, flags, argv, forkflag)
617200Seric 	char *progpath;
618200Seric 	short flags;
619200Seric 	char **argv;
620200Seric 	bool forkflag;
621200Seric {
622200Seric 	register int i;
623201Seric 	auto int st;
624200Seric 
6251316Seric # ifdef DEBUG
6261316Seric 	if (Debug)
6271316Seric 	{
6281316Seric 		printf("callprog:\n");
6291316Seric 		for (i = 0; argv[i] != NULL; i++)
6301316Seric 			printf("\t\"%s\"\n", argv[i]);
6311316Seric 	}
6321316Seric # endif
6331316Seric 
634200Seric 	if (*argv == NULL)
635200Seric 		return (-1);
636200Seric 
637157Seric 	/*
638226Seric 	**  Fork if appropriate.
639148Seric 	*/
640148Seric 
641200Seric 	if (forkflag)
642200Seric 	{
643393Seric # ifdef DEBUG
644393Seric 		if (Debug)
645393Seric 			printf("Forking\n");
646393Seric # endif
647200Seric 		i = fork();
648200Seric 		if (i < 0)
649200Seric 		{
6501205Seric 			syserr("cannot fork");
651200Seric 			exit(EX_OSERR);
652200Seric 		}
653200Seric 		else if (i > 0)
654201Seric 		{
655201Seric 			wait(&st);
6561282Seric 			if ((st & 0377) == 0)
6571282Seric 				st = (st >> 8) & 0377;
6581433Seric 			if (OutFile >= 0)
6591433Seric 			{
6601433Seric 				close(OutFile);
6611433Seric 				OutFile = -1;
6621433Seric 			}
663201Seric 			return (st);
664201Seric 		}
665200Seric 	}
6661433Seric 	else if (OutFile >= 0)
6671433Seric 	{
6681433Seric 		syserr("callprog: setting stdout w/o forking");
6691433Seric 		exit(EX_SOFTWARE);
6701433Seric 	}
671200Seric 
6721433Seric 	/* set protection as appropriate */
673200Seric 	if (bitset(REALUSER, flags))
674200Seric 		setuid(getuid());
6751433Seric 
6761433Seric 	/* change standard input & output if needed */
6771433Seric 	if (OutFile >= 0)
6781433Seric 	{
6791433Seric 		close(1);
6801433Seric 		dup(OutFile);
6811433Seric 		close(OutFile);
6821433Seric 	}
683226Seric 
6841433Seric 	/* call real SCCS program */
685226Seric 	execv(progpath, argv);
6861205Seric 	syserr("cannot execute %s", progpath);
687148Seric 	exit(EX_UNAVAILABLE);
6881738Seric 	/*NOTREACHED*/
689148Seric }
6901432Seric 
6911432Seric /*
692586Seric **  MAKEFILE -- make filename of SCCS file
693586Seric **
694586Seric **	If the name passed is already the name of an SCCS file,
695586Seric **	just return it.  Otherwise, munge the name into the name
696586Seric **	of the actual SCCS file.
697586Seric **
698586Seric **	There are cases when it is not clear what you want to
699586Seric **	do.  For example, if SccsPath is an absolute pathname
700586Seric **	and the name given is also an absolute pathname, we go
701586Seric **	for SccsPath (& only use the last component of the name
702586Seric **	passed) -- this is important for security reasons (if
703586Seric **	sccs is being used as a setuid front end), but not
704586Seric **	particularly intuitive.
705586Seric **
706586Seric **	Parameters:
707586Seric **		name -- the file name to be munged.
708586Seric **
709586Seric **	Returns:
710586Seric **		The pathname of the sccs file.
711586Seric **		NULL on error.
712586Seric **
713586Seric **	Side Effects:
714586Seric **		none.
715586Seric */
716148Seric 
717148Seric char *
718148Seric makefile(name)
719148Seric 	char *name;
720148Seric {
721148Seric 	register char *p;
722148Seric 	char buf[512];
723148Seric 	extern char *malloc();
724586Seric 	extern char *rindex();
725588Seric 	extern bool safepath();
726587Seric 	extern bool isdir();
727587Seric 	register char *q;
728148Seric 
729586Seric 	p = rindex(name, '/');
730586Seric 	if (p == NULL)
731586Seric 		p = name;
732586Seric 	else
733586Seric 		p++;
734586Seric 
735148Seric 	/*
736588Seric 	**  Check to see that the path is "safe", i.e., that we
737588Seric 	**  are not letting some nasty person use the setuid part
738588Seric 	**  of this program to look at or munge some presumably
739588Seric 	**  hidden files.
740148Seric 	*/
741148Seric 
742588Seric 	if (SccsDir[0] == '/' && !safepath(name))
743588Seric 		return (NULL);
744586Seric 
745586Seric 	/*
746588Seric 	**  Create the base pathname.
747586Seric 	*/
748586Seric 
7491438Seric 	/* first the directory part */
750588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
751148Seric 	{
752588Seric 		strcpy(buf, SccsDir);
753586Seric 		strcat(buf, "/");
754586Seric 	}
755586Seric 	else
756586Seric 		strcpy(buf, "");
7571438Seric 
7581438Seric 	/* then the head of the pathname */
759587Seric 	strncat(buf, name, p - name);
760587Seric 	q = &buf[strlen(buf)];
7611438Seric 
7621438Seric 	/* now copy the final part of the name, in case useful */
763587Seric 	strcpy(q, p);
7641438Seric 
7651438Seric 	/* so is it useful? */
766587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
767586Seric 	{
7681438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
769588Seric 		strcpy(q, SccsPath);
770588Seric 		strcat(buf, "/s.");
7711438Seric 
7721438Seric 		/* and now the end of the name */
773586Seric 		strcat(buf, p);
774586Seric 	}
775148Seric 
7761438Seric 	/* if i haven't changed it, why did I do all this? */
777588Seric 	if (strcmp(buf, name) == 0)
778588Seric 		p = name;
779588Seric 	else
780148Seric 	{
7811438Seric 		/* but if I have, squirrel it away */
782588Seric 		p = malloc(strlen(buf) + 1);
783588Seric 		if (p == NULL)
784588Seric 		{
785588Seric 			perror("Sccs: no mem");
786588Seric 			exit(EX_OSERR);
787588Seric 		}
788588Seric 		strcpy(p, buf);
789148Seric 	}
7901438Seric 
791148Seric 	return (p);
792148Seric }
7931432Seric 
7941432Seric /*
795587Seric **  ISDIR -- return true if the argument is a directory.
796587Seric **
797587Seric **	Parameters:
798587Seric **		name -- the pathname of the file to check.
799587Seric **
800587Seric **	Returns:
801587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
802587Seric **
803587Seric **	Side Effects:
804587Seric **		none.
805587Seric */
806587Seric 
807587Seric bool
808587Seric isdir(name)
809587Seric 	char *name;
810587Seric {
811587Seric 	struct stat stbuf;
812587Seric 
813587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
814587Seric }
8151432Seric 
8161432Seric /*
817586Seric **  SAFEPATH -- determine whether a pathname is "safe"
818586Seric **
819586Seric **	"Safe" pathnames only allow you to get deeper into the
820586Seric **	directory structure, i.e., full pathnames and ".." are
821586Seric **	not allowed.
822586Seric **
823586Seric **	Parameters:
824586Seric **		p -- the name to check.
825586Seric **
826586Seric **	Returns:
827586Seric **		TRUE -- if the path is safe.
828586Seric **		FALSE -- if the path is not safe.
829586Seric **
830586Seric **	Side Effects:
831586Seric **		Prints a message if the path is not safe.
832586Seric */
833586Seric 
834586Seric bool
835586Seric safepath(p)
836586Seric 	register char *p;
837586Seric {
838586Seric 	extern char *index();
839586Seric 
840586Seric 	if (*p != '/')
841586Seric 	{
842586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
843586Seric 		{
844586Seric 			p = index(p, '/');
845586Seric 			if (p == NULL)
846586Seric 				return (TRUE);
847586Seric 			p++;
848586Seric 		}
849586Seric 	}
850586Seric 
851586Seric 	printf("You may not use full pathnames or \"..\"\n");
852586Seric 	return (FALSE);
853586Seric }
8541432Seric 
8551432Seric /*
856261Seric **  CLEAN -- clean out recreatable files
857261Seric **
858261Seric **	Any file for which an "s." file exists but no "p." file
859261Seric **	exists in the current directory is purged.
860261Seric **
861261Seric **	Parameters:
8621822Seric **		mode -- tells whether this came from a "clean", "info", or
8631822Seric **			"check" command.
8641822Seric **		argv -- the rest of the argument vector.
865261Seric **
866261Seric **	Returns:
867261Seric **		none.
868261Seric **
869261Seric **	Side Effects:
870819Seric **		Removes files in the current directory.
871819Seric **		Prints information regarding files being edited.
872819Seric **		Exits if a "check" command.
873261Seric */
874261Seric 
8751822Seric clean(mode, argv)
876819Seric 	int mode;
8771822Seric 	char **argv;
878261Seric {
879261Seric 	struct direct dir;
880261Seric 	char buf[100];
8812140Seric 	char *bufend;
882346Seric 	register FILE *dirfd;
883346Seric 	register char *basefile;
884351Seric 	bool gotedit;
8851822Seric 	bool gotpfent;
886394Seric 	FILE *pfp;
8871822Seric 	bool nobranch = FALSE;
8881822Seric 	extern struct pfile *getpfent();
8891822Seric 	register struct pfile *pf;
8901822Seric 	register char **ap;
8911864Seric 	extern char *username();
8921864Seric 	char *usernm = NULL;
8932140Seric 	char *subdir = NULL;
8942140Seric 	char *cmdname;
895261Seric 
8961438Seric 	/*
8971822Seric 	**  Process the argv
8981822Seric 	*/
8991822Seric 
9002140Seric 	cmdname = *argv;
9012140Seric 	for (ap = argv; *++ap != NULL; )
9021822Seric 	{
9031864Seric 		if (**ap == '-')
9041864Seric 		{
9051864Seric 			/* we have a flag */
9061864Seric 			switch ((*ap)[1])
9071864Seric 			{
9081864Seric 			  case 'b':
9091864Seric 				nobranch = TRUE;
9101864Seric 				break;
9111864Seric 
9121864Seric 			  case 'u':
9131864Seric 				if ((*ap)[2] != '\0')
9141864Seric 					usernm = &(*ap)[2];
9151864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9161864Seric 					usernm = *++ap;
9171864Seric 				else
9181864Seric 					usernm = username();
9191864Seric 				break;
9201864Seric 			}
9211864Seric 		}
9222140Seric 		else
9232140Seric 		{
9242140Seric 			if (subdir != NULL)
9252140Seric 				usrerr("too many args");
9262140Seric 			else
9272140Seric 				subdir = *ap;
9282140Seric 		}
9291822Seric 	}
9301822Seric 
9311822Seric 	/*
9321438Seric 	**  Find and open the SCCS directory.
9331438Seric 	*/
9341438Seric 
9351207Seric 	strcpy(buf, SccsDir);
9361207Seric 	if (buf[0] != '\0')
9371207Seric 		strcat(buf, "/");
9382140Seric 	if (subdir != NULL)
9392140Seric 	{
9402140Seric 		strcat(buf, subdir);
9412140Seric 		strcat(buf, "/");
9422140Seric 	}
9431207Seric 	strcat(buf, SccsPath);
9442140Seric 	bufend = &buf[strlen(buf)];
9451438Seric 
9461207Seric 	dirfd = fopen(buf, "r");
947261Seric 	if (dirfd == NULL)
948261Seric 	{
9491207Seric 		usrerr("cannot open %s", buf);
9501282Seric 		return (EX_NOINPUT);
951261Seric 	}
952261Seric 
953261Seric 	/*
954261Seric 	**  Scan the SCCS directory looking for s. files.
9551438Seric 	**	gotedit tells whether we have tried to clean any
9561438Seric 	**		files that are being edited.
957261Seric 	*/
958261Seric 
959351Seric 	gotedit = FALSE;
9601738Seric 	while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL)
961261Seric 	{
962568Seric 		if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0)
963261Seric 			continue;
964261Seric 
965261Seric 		/* got an s. file -- see if the p. file exists */
9662140Seric 		strcpy(bufend, "/p.");
9672140Seric 		basefile = bufend + 3;
968568Seric 		strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
969346Seric 		basefile[sizeof dir.d_name - 2] = '\0';
9701822Seric 
9711822Seric 		/*
9721822Seric 		**  open and scan the p-file.
9731822Seric 		**	'gotpfent' tells if we have found a valid p-file
9741822Seric 		**		entry.
9751822Seric 		*/
9761822Seric 
977394Seric 		pfp = fopen(buf, "r");
9781822Seric 		gotpfent = FALSE;
979394Seric 		if (pfp != NULL)
980346Seric 		{
9811438Seric 			/* the file exists -- report it's contents */
9821822Seric 			while ((pf = getpfent(pfp)) != NULL)
9831730Seric 			{
9841822Seric 				if (nobranch && isbranch(pf->p_nsid))
9851822Seric 					continue;
9861864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
9871864Seric 					continue;
9881822Seric 				gotedit = TRUE;
9891822Seric 				gotpfent = TRUE;
9901822Seric 				if (mode == TELLC)
9911822Seric 				{
9921822Seric 					printf("%s\n", basefile);
9931822Seric 					break;
9941822Seric 				}
9952161Seric 				printf("%12s: being edited: ", basefile);
9962161Seric 				putpfent(pf, stdout);
9971730Seric 			}
998394Seric 			fclose(pfp);
9991822Seric 		}
1000261Seric 
1001261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10021870Seric 		if (mode == CLEANC && !gotpfent)
1003346Seric 		{
1004568Seric 			strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2);
1005346Seric 			buf[sizeof dir.d_name - 2] = '\0';
1006346Seric 			unlink(buf);
1007346Seric 		}
1008261Seric 	}
1009261Seric 
10101438Seric 	/* cleanup & report results */
1011261Seric 	fclose(dirfd);
1012819Seric 	if (!gotedit && mode == INFOC)
10131864Seric 	{
10141864Seric 		printf("Nothing being edited");
10151864Seric 		if (nobranch)
10161864Seric 			printf(" (on trunk)");
10171864Seric 		if (usernm == NULL)
10181864Seric 			printf("\n");
10191864Seric 		else
10201864Seric 			printf(" by %s\n", usernm);
10211864Seric 	}
1022819Seric 	if (mode == CHECKC)
1023819Seric 		exit(gotedit);
10241282Seric 	return (EX_OK);
1025261Seric }
10261432Seric 
10271432Seric /*
10281822Seric **  ISBRANCH -- is the SID a branch?
10291822Seric **
10301822Seric **	Parameters:
10311822Seric **		sid -- the sid to check.
10321822Seric **
10331822Seric **	Returns:
10341822Seric **		TRUE if the sid represents a branch.
10351822Seric **		FALSE otherwise.
10361822Seric **
10371822Seric **	Side Effects:
10381822Seric **		none.
10391822Seric */
10401822Seric 
10411822Seric isbranch(sid)
10421822Seric 	char *sid;
10431822Seric {
10441822Seric 	register char *p;
10451822Seric 	int dots;
10461822Seric 
10471822Seric 	dots = 0;
10481822Seric 	for (p = sid; *p != '\0'; p++)
10491822Seric 	{
10501822Seric 		if (*p == '.')
10511822Seric 			dots++;
10521822Seric 		if (dots > 1)
10531822Seric 			return (TRUE);
10541822Seric 	}
10551822Seric 	return (FALSE);
10561822Seric }
10571822Seric 
10581822Seric /*
1059396Seric **  UNEDIT -- unedit a file
1060396Seric **
1061396Seric **	Checks to see that the current user is actually editting
1062396Seric **	the file and arranges that s/he is not editting it.
1063396Seric **
1064396Seric **	Parameters:
1065416Seric **		fn -- the name of the file to be unedited.
1066396Seric **
1067396Seric **	Returns:
1068585Seric **		TRUE -- if the file was successfully unedited.
1069585Seric **		FALSE -- if the file was not unedited for some
1070585Seric **			reason.
1071396Seric **
1072396Seric **	Side Effects:
1073396Seric **		fn is removed
1074396Seric **		entries are removed from pfile.
1075396Seric */
1076396Seric 
1077585Seric bool
1078396Seric unedit(fn)
1079396Seric 	char *fn;
1080396Seric {
1081396Seric 	register FILE *pfp;
1082396Seric 	char *pfn;
1083396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1084396Seric 	FILE *tfp;
1085396Seric 	register char *q;
1086396Seric 	bool delete = FALSE;
1087396Seric 	bool others = FALSE;
1088396Seric 	char *myname;
10891864Seric 	extern char *username();
1090396Seric 	struct pfile *pent;
10911822Seric 	extern struct pfile *getpfent();
1092396Seric 	char buf[120];
10931316Seric 	extern char *makefile();
1094396Seric 
1095396Seric 	/* make "s." filename & find the trailing component */
1096396Seric 	pfn = makefile(fn);
1097586Seric 	if (pfn == NULL)
1098586Seric 		return (FALSE);
1099586Seric 	q = rindex(pfn, '/');
1100586Seric 	if (q == NULL)
1101586Seric 		q = &pfn[-1];
1102586Seric 	if (q[1] != 's' || q[2] != '.')
1103396Seric 	{
11041205Seric 		usrerr("bad file name \"%s\"", fn);
1105585Seric 		return (FALSE);
1106396Seric 	}
1107396Seric 
11081438Seric 	/* turn "s." into "p." & try to open it */
1109396Seric 	*++q = 'p';
1110396Seric 
1111396Seric 	pfp = fopen(pfn, "r");
1112396Seric 	if (pfp == NULL)
1113396Seric 	{
1114416Seric 		printf("%12s: not being edited\n", fn);
1115585Seric 		return (FALSE);
1116396Seric 	}
1117396Seric 
11181438Seric 	/* create temp file for editing p-file */
1119396Seric 	mktemp(tfn);
1120396Seric 	tfp = fopen(tfn, "w");
1121396Seric 	if (tfp == NULL)
1122396Seric 	{
11231205Seric 		usrerr("cannot create \"%s\"", tfn);
1124396Seric 		exit(EX_OSERR);
1125396Seric 	}
1126396Seric 
11271438Seric 	/* figure out who I am */
11281864Seric 	myname = username();
11291438Seric 
11301438Seric 	/*
11311438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11321438Seric 	*/
11331438Seric 
11341822Seric 	while ((pent = getpfent(pfp)) != NULL)
1135396Seric 	{
1136396Seric 		if (strcmp(pent->p_user, myname) == 0)
1137396Seric 		{
1138396Seric 			/* a match */
1139396Seric 			delete++;
1140396Seric 		}
1141396Seric 		else
1142396Seric 		{
11431438Seric 			/* output it again */
11442161Seric 			putpfent(pent, tfp);
1145396Seric 			others++;
1146396Seric 		}
1147396Seric 	}
1148396Seric 
1149396Seric 	/* do final cleanup */
1150396Seric 	if (others)
1151396Seric 	{
11521438Seric 		/* copy it back (perhaps it should be linked?) */
1153396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1154396Seric 		{
11551205Seric 			syserr("cannot reopen \"%s\"", tfn);
1156396Seric 			exit(EX_OSERR);
1157396Seric 		}
1158396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1159396Seric 		{
11601205Seric 			usrerr("cannot create \"%s\"", pfn);
1161585Seric 			return (FALSE);
1162396Seric 		}
1163396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1164396Seric 			fputs(buf, pfp);
1165396Seric 	}
1166396Seric 	else
1167396Seric 	{
11681438Seric 		/* it's empty -- remove it */
1169396Seric 		unlink(pfn);
1170396Seric 	}
1171396Seric 	fclose(tfp);
1172396Seric 	fclose(pfp);
1173396Seric 	unlink(tfn);
1174396Seric 
11751438Seric 	/* actually remove the g-file */
1176396Seric 	if (delete)
1177396Seric 	{
11781435Seric 		unlink(tail(fn));
11791435Seric 		printf("%12s: removed\n", tail(fn));
1180585Seric 		return (TRUE);
1181396Seric 	}
1182396Seric 	else
1183396Seric 	{
1184416Seric 		printf("%12s: not being edited by you\n", fn);
1185585Seric 		return (FALSE);
1186396Seric 	}
1187396Seric }
11881432Seric 
11891432Seric /*
11901433Seric **  DODIFF -- diff an s-file against a g-file
11911433Seric **
11921433Seric **	Parameters:
11931433Seric **		getv -- argv for the 'get' command.
11941433Seric **		gfile -- name of the g-file to diff against.
11951433Seric **
11961433Seric **	Returns:
11971433Seric **		Result of get.
11981433Seric **
11991433Seric **	Side Effects:
12001433Seric **		none.
12011433Seric */
12021433Seric 
12031433Seric dodiff(getv, gfile)
12041433Seric 	char **getv;
12051433Seric 	char *gfile;
12061433Seric {
12071433Seric 	int pipev[2];
12081433Seric 	int rval;
12091433Seric 	register int i;
12101433Seric 	register int pid;
12111433Seric 	auto int st;
12121433Seric 	extern int errno;
12131433Seric 	int (*osig)();
12141433Seric 
12151905Seric 	printf("\n------- %s -------\n", gfile);
12161871Seric 	fflush(stdout);
12171501Seric 
12181438Seric 	/* create context for diff to run in */
12191433Seric 	if (pipe(pipev) < 0)
12201433Seric 	{
12211433Seric 		syserr("dodiff: pipe failed");
12221433Seric 		exit(EX_OSERR);
12231433Seric 	}
12241433Seric 	if ((pid = fork()) < 0)
12251433Seric 	{
12261433Seric 		syserr("dodiff: fork failed");
12271433Seric 		exit(EX_OSERR);
12281433Seric 	}
12291433Seric 	else if (pid > 0)
12301433Seric 	{
12311433Seric 		/* in parent; run get */
12321433Seric 		OutFile = pipev[1];
12331433Seric 		close(pipev[0]);
12341871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12351433Seric 		osig = signal(SIGINT, SIG_IGN);
12361433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
12371433Seric 			errno = 0;
12381433Seric 		signal(SIGINT, osig);
12391433Seric 		/* ignore result of diff */
12401433Seric 	}
12411433Seric 	else
12421433Seric 	{
12431433Seric 		/* in child, run diff */
12441433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
12451433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
12461433Seric 		{
12471433Seric 			syserr("dodiff: magic failed");
12481433Seric 			exit(EX_OSERR);
12491433Seric 		}
12501871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
12511433Seric 	}
12521433Seric 	return (rval);
12531433Seric }
12541433Seric 
12551433Seric /*
12561435Seric **  TAIL -- return tail of filename.
12571435Seric **
12581435Seric **	Parameters:
12591435Seric **		fn -- the filename.
12601435Seric **
12611435Seric **	Returns:
12621435Seric **		a pointer to the tail of the filename; e.g., given
12631435Seric **		"cmd/ls.c", "ls.c" is returned.
12641435Seric **
12651435Seric **	Side Effects:
12661435Seric **		none.
12671435Seric */
12681435Seric 
12691435Seric char *
12701435Seric tail(fn)
12711435Seric 	register char *fn;
12721435Seric {
12731435Seric 	register char *p;
12741435Seric 
12751435Seric 	for (p = fn; *p != 0; p++)
12761435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
12771435Seric 			fn = &p[1];
12781435Seric 	return (fn);
12791435Seric }
12801435Seric 
12811435Seric /*
12821822Seric **  GETPFENT -- get an entry from the p-file
1283396Seric **
1284396Seric **	Parameters:
1285396Seric **		pfp -- p-file file pointer
1286396Seric **
1287396Seric **	Returns:
1288396Seric **		pointer to p-file struct for next entry
1289396Seric **		NULL on EOF or error
1290396Seric **
1291396Seric **	Side Effects:
1292396Seric **		Each call wipes out results of previous call.
1293396Seric */
1294396Seric 
1295396Seric struct pfile *
12961822Seric getpfent(pfp)
1297396Seric 	FILE *pfp;
1298396Seric {
1299396Seric 	static struct pfile ent;
1300396Seric 	static char buf[120];
1301396Seric 	register char *p;
1302396Seric 	extern char *nextfield();
1303396Seric 
1304396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1305396Seric 		return (NULL);
1306396Seric 
1307396Seric 	ent.p_osid = p = buf;
1308396Seric 	ent.p_nsid = p = nextfield(p);
1309396Seric 	ent.p_user = p = nextfield(p);
1310396Seric 	ent.p_date = p = nextfield(p);
1311396Seric 	ent.p_time = p = nextfield(p);
13122161Seric 	ent.p_aux = p = nextfield(p);
1313396Seric 
1314396Seric 	return (&ent);
1315396Seric }
1316396Seric 
1317396Seric 
1318396Seric char *
1319396Seric nextfield(p)
1320396Seric 	register char *p;
1321396Seric {
1322396Seric 	if (p == NULL || *p == '\0')
1323396Seric 		return (NULL);
1324396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1325396Seric 		p++;
1326396Seric 	if (*p == '\n' || *p == '\0')
1327396Seric 	{
1328396Seric 		*p = '\0';
1329396Seric 		return (NULL);
1330396Seric 	}
1331396Seric 	*p++ = '\0';
1332396Seric 	return (p);
1333396Seric }
13342161Seric /*
13352161Seric **  PUTPFENT -- output a p-file entry to a file
13362161Seric **
13372161Seric **	Parameters:
13382161Seric **		pf -- the p-file entry
13392161Seric **		f -- the file to put it on.
13402161Seric **
13412161Seric **	Returns:
13422161Seric **		none.
13432161Seric **
13442161Seric **	Side Effects:
13452161Seric **		pf is written onto file f.
13462161Seric */
13472161Seric 
13482161Seric putpfent(pf, f)
13492161Seric 	register struct pfile *pf;
13502161Seric 	register FILE *f;
13512161Seric {
13522161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
13532161Seric 		pf->p_user, pf->p_date, pf->p_time);
13542161Seric 	if (pf->p_aux != NULL)
13552161Seric 		fprintf(f, " %s", pf->p_aux);
13562161Seric 	else
13572161Seric 		fprintf(f, "\n");
13582161Seric }
13591432Seric 
13601432Seric /*
13611205Seric **  USRERR -- issue user-level error
13621205Seric **
13631205Seric **	Parameters:
13641205Seric **		f -- format string.
13651205Seric **		p1-p3 -- parameters to a printf.
13661205Seric **
13671205Seric **	Returns:
13681205Seric **		-1
13691205Seric **
13701205Seric **	Side Effects:
13711205Seric **		none.
13721205Seric */
13731205Seric 
13741738Seric /*VARARGS1*/
13751205Seric usrerr(f, p1, p2, p3)
13761205Seric 	char *f;
13771205Seric {
13781205Seric 	fprintf(stderr, "\n%s: ", MyName);
13791205Seric 	fprintf(stderr, f, p1, p2, p3);
13801205Seric 	fprintf(stderr, "\n");
13811205Seric 
13821205Seric 	return (-1);
13831205Seric }
13841432Seric 
13851432Seric /*
13861205Seric **  SYSERR -- print system-generated error.
13871205Seric **
13881205Seric **	Parameters:
13891205Seric **		f -- format string to a printf.
13901205Seric **		p1, p2, p3 -- parameters to f.
13911205Seric **
13921205Seric **	Returns:
13931205Seric **		never.
13941205Seric **
13951205Seric **	Side Effects:
13961205Seric **		none.
13971205Seric */
13981205Seric 
13991738Seric /*VARARGS1*/
14001205Seric syserr(f, p1, p2, p3)
14011205Seric 	char *f;
14021205Seric {
14031205Seric 	extern int errno;
14041205Seric 
14051205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
14061205Seric 	fprintf(stderr, f, p1, p2, p3);
14071205Seric 	fprintf(stderr, "\n");
14081205Seric 	if (errno == 0)
14091205Seric 		exit(EX_SOFTWARE);
14101205Seric 	else
14111205Seric 	{
14121738Seric 		perror(NULL);
14131205Seric 		exit(EX_OSERR);
14141205Seric 	}
14151205Seric }
14161864Seric /*
14171864Seric **  USERNAME -- return name of the current user
14181864Seric **
14191864Seric **	Parameters:
14201864Seric **		none
14211864Seric **
14221864Seric **	Returns:
14231864Seric **		name of current user
14241864Seric **
14251864Seric **	Side Effects:
14261864Seric **		none
14271864Seric */
14281864Seric 
14291864Seric char *
14301864Seric username()
14311864Seric {
14321864Seric # ifdef UIDUSER
14331864Seric 	extern struct passwd *getpwuid();
14341864Seric 	register struct passwd *pw;
14351864Seric 
14361864Seric 	pw = getpwuid(getuid());
14371864Seric 	if (pw == NULL)
14381864Seric 	{
14391864Seric 		syserr("who are you? (uid=%d)", getuid());
14401864Seric 		exit(EX_OSERR);
14411864Seric 	}
14421864Seric 	return (pw->pw_name);
14431864Seric # else
14441905Seric 	extern char *getlogin();
14451905Seric 
14461864Seric 	return (getlogin());
14471864Seric # endif UIDUSER
14481864Seric }
1449