xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 1738)
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>
9148Seric 
10828Seric /*
11828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
12828Seric **
13828Seric **	Without trying to add any functionality to speak of, this
14828Seric **	program tries to make SCCS a little more accessible to human
15828Seric **	types.  The main thing it does is automatically put the
16828Seric **	string "SCCS/s." on the front of names.  Also, it has a
17828Seric **	couple of things that are designed to shorten frequent
18828Seric **	combinations, e.g., "delget" which expands to a "delta"
19828Seric **	and a "get".
20828Seric **
21828Seric **	This program can also function as a setuid front end.
22828Seric **	To do this, you should copy the source, renaming it to
23828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
24828Seric **	in the program (e.g., syssccs might default -d to
25828Seric **	"/usr/src/sys").  Then recompile and put the result
26828Seric **	as setuid to whomever you want.  In this mode, sccs
27828Seric **	knows to not run setuid for certain programs in order
28828Seric **	to preserve security, and so forth.
29828Seric **
30828Seric **	Usage:
31828Seric **		sccs [flags] command [args]
32828Seric **
33828Seric **	Flags:
34828Seric **		-d<dir>		<dir> represents a directory to search
35828Seric **				out of.  It should be a full pathname
36828Seric **				for general usage.  E.g., if <dir> is
37828Seric **				"/usr/src/sys", then a reference to the
38828Seric **				file "dev/bio.c" becomes a reference to
39828Seric **				"/usr/src/sys/dev/bio.c".
40828Seric **		-p<path>	prepends <path> to the final component
41828Seric **				of the pathname.  By default, this is
42828Seric **				"SCCS".  For example, in the -d example
43828Seric **				above, the path then gets modified to
44828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
45828Seric **				more common usage (without the -d flag),
46828Seric **				"prog.c" would get modified to
47828Seric **				"SCCS/s.prog.c".  In both cases, the
48828Seric **				"s." gets automatically prepended.
49828Seric **		-r		run as the real user.
50828Seric **
51828Seric **	Commands:
52828Seric **		admin,
53828Seric **		get,
54828Seric **		delta,
55828Seric **		rmdel,
56828Seric **		chghist,
57828Seric **		etc.		Straight out of SCCS; only difference
58828Seric **				is that pathnames get modified as
59828Seric **				described above.
60828Seric **		edit		Macro for "get -e".
61828Seric **		unedit		Removes a file being edited, knowing
62828Seric **				about p-files, etc.
63828Seric **		delget		Macro for "delta" followed by "get".
64828Seric **		deledit		Macro for "delta" followed by "get -e".
65828Seric **		info		Tell what files being edited.
66828Seric **		clean		Remove all files that can be
67828Seric **				regenerated from SCCS files.
681205Seric **		check		Like info, but return exit status, for
69828Seric **				use in makefiles.
70828Seric **		fix		Remove a top delta & reedit, but save
71828Seric **				the previous changes in that delta.
72828Seric **
73828Seric **	Compilation Flags:
74828Seric **		UIDUSER -- determine who the user is by looking at the
75828Seric **			uid rather than the login name -- for machines
76828Seric **			where SCCS gets the user in this way.
771270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
781205Seric **			this value.  This is so that the setuid
791205Seric **			aspects of this program cannot be abused.
801270Seric **			This flag also disables the -p flag.
811270Seric **		SCCSPATH -- the default for the -p flag.
821437Seric **		MYNAME -- the title this program should print when it
831437Seric **			gives error messages.
84828Seric **
85828Seric **	Compilation Instructions:
86828Seric **		cc -O -n -s sccs.c
871437Seric **		The flags listed above can be -D defined to simplify
881437Seric **			recompilation for variant versions.
89828Seric **
90828Seric **	Author:
91828Seric **		Eric Allman, UCB/INGRES
921270Seric **		Copyright 1980 Regents of the University of California
93828Seric */
94155Seric 
95*1738Seric static char SccsId[] = "@(#)sccs.c	1.46 11/05/80";
961432Seric 
971270Seric /*******************  Configuration Information  ********************/
981270Seric 
991437Seric /* special defines for local berkeley systems */
1001437Seric # include <whoami.h>
1011437Seric 
102828Seric # ifdef CSVAX
103828Seric # define UIDUSER
1041270Seric # define PROGPATH(name)	"/usr/local/name"
1051270Seric # endif CSVAX
106828Seric 
1071618Seric # ifdef INGVAX
1081618Seric # define PROGPATH(name)	"/usr/local/name"
1091618Seric # endif INGVAX
1101618Seric 
1111437Seric /* end of berkeley systems defines */
1121437Seric 
1131437Seric # ifndef SCCSPATH
1141432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1151437Seric # endif NOT SCCSPATH
116828Seric 
1171437Seric # ifndef MYNAME
1181437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1191437Seric # endif NOT MYNAME
1201270Seric 
1211432Seric # ifndef PROGPATH
1221432Seric # define PROGPATH(name)	"/usr/sccs/name"	/* place to find binaries */
1231432Seric # endif PROGPATH
1241432Seric 
1251270Seric /****************  End of Configuration Information  ****************/
1261432Seric 
127157Seric typedef char	bool;
128200Seric # define TRUE	1
129200Seric # define FALSE	0
130157Seric 
1311438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1321438Seric 
133828Seric # ifdef UIDUSER
134828Seric # include <pwd.h>
135828Seric # endif UIDUSER
136828Seric 
137148Seric struct sccsprog
138148Seric {
139148Seric 	char	*sccsname;	/* name of SCCS routine */
140200Seric 	short	sccsoper;	/* opcode, see below */
141200Seric 	short	sccsflags;	/* flags, see below */
142148Seric 	char	*sccspath;	/* pathname of binary implementing */
143148Seric };
144148Seric 
145200Seric /* values for sccsoper */
146200Seric # define PROG		0	/* call a program */
147201Seric # define CMACRO		1	/* command substitution macro */
148226Seric # define FIX		2	/* fix a delta */
149261Seric # define CLEAN		3	/* clean out recreatable files */
150396Seric # define UNEDIT		4	/* unedit a file */
1511431Seric # define SHELL		5	/* call a shell file (like PROG) */
1521433Seric # define DIFFS		6	/* diff between sccs & file out */
153200Seric 
154157Seric /* bits for sccsflags */
155200Seric # define NO_SDOT	0001	/* no s. on front of args */
156200Seric # define REALUSER	0002	/* protected (e.g., admin) */
157148Seric 
158819Seric /* modes for the "clean", "info", "check" ops */
159819Seric # define CLEANC		0	/* clean command */
160819Seric # define INFOC		1	/* info command */
161819Seric # define CHECKC		2	/* check command */
1621730Seric # define TELLC		3	/* give list of files being edited */
163819Seric 
1641432Seric /*
1651432Seric **  Description of commands known to this program.
1661432Seric **	First argument puts the command into a class.  Second arg is
1671432Seric **	info regarding treatment of this command.  Third arg is a
1681432Seric **	list of flags this command accepts from macros, etc.  Fourth
1691432Seric **	arg is the pathname of the implementing program, or the
1701432Seric **	macro definition, or the arg to a sub-algorithm.
1711432Seric */
172202Seric 
173148Seric struct sccsprog SccsProg[] =
174148Seric {
1751737Seric 	"admin",	PROG,	REALUSER,	PROGPATH(admin),
1761737Seric 	"chghist",	PROG,	0,		PROGPATH(rmdel),
1771737Seric 	"comb",		PROG,	0,		PROGPATH(comb),
1781737Seric 	"delta",	PROG,	0,		PROGPATH(delta),
1791737Seric 	"get",		PROG,	0,		PROGPATH(get),
1801737Seric 	"help",		PROG,	NO_SDOT,	PROGPATH(help),
1811737Seric 	"prt",		PROG,	0,		PROGPATH(prt),
1821737Seric 	"rmdel",	PROG,	REALUSER,	PROGPATH(rmdel),
1831737Seric 	"what",		PROG,	NO_SDOT,	PROGPATH(what),
1841737Seric 	"sccsdiff",	SHELL,	REALUSER,	PROGPATH(sccsdiff),
1851737Seric 	"edit",		CMACRO,	NO_SDOT,	"get -e",
1861737Seric 	"delget",	CMACRO,	NO_SDOT,	"delta:mysrp/get:ixbeskecl -t",
1871737Seric 	"deledit",	CMACRO,	NO_SDOT,	"delta:mysrp/get:ixbeskecl -e -t",
1881737Seric 	"fix",		FIX,	NO_SDOT,	NULL,
1891737Seric 	"clean",	CLEAN,	REALUSER,	(char *) CLEANC,
1901737Seric 	"info",		CLEAN,	REALUSER,	(char *) INFOC,
1911737Seric 	"check",	CLEAN,	REALUSER,	(char *) CHECKC,
1921737Seric 	"tell",		CLEAN,	REALUSER,	(char *) TELLC,
1931737Seric 	"unedit",	UNEDIT,	NO_SDOT,	NULL,
1941737Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER, NULL,
1951737Seric 	NULL,		-1,	0,		NULL
196148Seric };
197148Seric 
1981432Seric /* one line from a p-file */
199396Seric struct pfile
200396Seric {
201396Seric 	char	*p_osid;	/* old SID */
202396Seric 	char	*p_nsid;	/* new SID */
203396Seric 	char	*p_user;	/* user who did edit */
204396Seric 	char	*p_date;	/* date of get */
205396Seric 	char	*p_time;	/* time of get */
206396Seric };
207396Seric 
2081270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2091270Seric # ifdef SCCSDIR
2101270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2111205Seric # else
2121270Seric char	*SccsDir = "";
2131205Seric # endif
2141437Seric char	MyName[] = MYNAME;	/* name used in messages */
2151433Seric int	OutFile = -1;		/* override output file for commands */
216157Seric bool	RealUser;		/* if set, running as real user */
217393Seric # ifdef DEBUG
218393Seric bool	Debug;			/* turn on tracing */
219393Seric # endif
2201432Seric 
221148Seric main(argc, argv)
222148Seric 	int argc;
223148Seric 	char **argv;
224148Seric {
225148Seric 	register char *p;
226262Seric 	extern struct sccsprog *lookup();
2271282Seric 	register int i;
228148Seric 
229148Seric 	/*
230148Seric 	**  Detect and decode flags intended for this program.
231148Seric 	*/
232148Seric 
233200Seric 	if (argc < 2)
234148Seric 	{
2351205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
236200Seric 		exit(EX_USAGE);
237200Seric 	}
238200Seric 	argv[argc] = NULL;
239200Seric 
240262Seric 	if (lookup(argv[0]) == NULL)
241200Seric 	{
242262Seric 		while ((p = *++argv) != NULL)
243148Seric 		{
244262Seric 			if (*p != '-')
245262Seric 				break;
246262Seric 			switch (*++p)
247262Seric 			{
248262Seric 			  case 'r':		/* run as real user */
249262Seric 				setuid(getuid());
250262Seric 				RealUser++;
251262Seric 				break;
252148Seric 
2531270Seric # ifndef SCCSDIR
254262Seric 			  case 'p':		/* path of sccs files */
255262Seric 				SccsPath = ++p;
256262Seric 				break;
257148Seric 
258588Seric 			  case 'd':		/* directory to search from */
259588Seric 				SccsDir = ++p;
260588Seric 				break;
2611205Seric # endif
262588Seric 
263393Seric # ifdef DEBUG
264393Seric 			  case 'T':		/* trace */
265393Seric 				Debug++;
266393Seric 				break;
267393Seric # endif
268393Seric 
269262Seric 			  default:
2701205Seric 				usrerr("unknown option -%s", p);
271262Seric 				break;
272262Seric 			}
273148Seric 		}
274262Seric 		if (SccsPath[0] == '\0')
275262Seric 			SccsPath = ".";
276148Seric 	}
277148Seric 
2781737Seric 	i = command(argv, FALSE, "");
2791282Seric 	exit(i);
280200Seric }
2811432Seric 
2821432Seric /*
2831282Seric **  COMMAND -- look up and perform a command
2841282Seric **
2851282Seric **	This routine is the guts of this program.  Given an
2861282Seric **	argument vector, it looks up the "command" (argv[0])
2871282Seric **	in the configuration table and does the necessary stuff.
2881282Seric **
2891282Seric **	Parameters:
2901282Seric **		argv -- an argument vector to process.
2911282Seric **		forkflag -- if set, fork before executing the command.
2921316Seric **		editflag -- if set, only include flags listed in the
2931316Seric **			sccsklets field of the command descriptor.
2941316Seric **		arg0 -- a space-seperated list of arguments to insert
2951316Seric **			before argv.
2961282Seric **
2971282Seric **	Returns:
2981282Seric **		zero -- command executed ok.
2991282Seric **		else -- error status.
3001282Seric **
3011282Seric **	Side Effects:
3021282Seric **		none.
3031282Seric */
304157Seric 
3051737Seric command(argv, forkflag, arg0)
306200Seric 	char **argv;
307201Seric 	bool forkflag;
3081316Seric 	char *arg0;
309200Seric {
310200Seric 	register struct sccsprog *cmd;
311200Seric 	register char *p;
312201Seric 	char buf[40];
313262Seric 	extern struct sccsprog *lookup();
3141316Seric 	char *nav[1000];
3151316Seric 	char **np;
3161431Seric 	register char **ap;
317585Seric 	register int i;
3181431Seric 	register char *q;
319585Seric 	extern bool unedit();
3201282Seric 	int rval = 0;
3211316Seric 	extern char *index();
3221316Seric 	extern char *makefile();
3231737Seric 	char *editchs;
3241435Seric 	extern char *tail();
325200Seric 
326393Seric # ifdef DEBUG
327393Seric 	if (Debug)
328393Seric 	{
3291316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3301316Seric 		for (np = argv; *np != NULL; np++)
3311316Seric 			printf("\t\"%s\"\n", *np);
332393Seric 	}
333393Seric # endif
334393Seric 
335157Seric 	/*
3361316Seric 	**  Copy arguments.
3371438Seric 	**	Copy from arg0 & if necessary at most one arg
3381438Seric 	**	from argv[0].
3391316Seric 	*/
3401316Seric 
3411431Seric 	np = ap = &nav[1];
3421737Seric 	editchs = NULL;
3431737Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/' && *p != ':'; )
3441316Seric 	{
3451316Seric 		*np++ = q;
3461316Seric 		while (*p == ' ')
3471316Seric 			p++;
3481737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3491316Seric 			*q++ = *p++;
3501316Seric 		*q++ = '\0';
3511737Seric 		if (*p == ':')
3521737Seric 		{
3531737Seric 			editchs = q;
3541737Seric 			while (*++p != '\0' && *p != '/')
3551737Seric 				*q++ = *p;
3561737Seric 			*q++ = '\0';
3571737Seric 		}
3581316Seric 	}
3591316Seric 	*np = NULL;
3601431Seric 	if (*ap == NULL)
3611316Seric 		*np++ = *argv++;
3621316Seric 
3631316Seric 	/*
364148Seric 	**  Look up command.
3651431Seric 	**	At this point, *ap is the command name.
366148Seric 	*/
367148Seric 
3681431Seric 	cmd = lookup(*ap);
369262Seric 	if (cmd == NULL)
370148Seric 	{
3711431Seric 		usrerr("Unknown command \"%s\"", *ap);
3721282Seric 		return (EX_USAGE);
373148Seric 	}
374148Seric 
375148Seric 	/*
3761316Seric 	**  Copy remaining arguments doing editing as appropriate.
3771316Seric 	*/
3781316Seric 
3791316Seric 	for (; *argv != NULL; argv++)
3801316Seric 	{
3811316Seric 		p = *argv;
3821316Seric 		if (*p == '-')
3831316Seric 		{
3841737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
3851316Seric 				*np++ = p;
3861316Seric 		}
3871316Seric 		else
3881316Seric 		{
3891316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
3901316Seric 				p = makefile(p);
3911316Seric 			if (p != NULL)
3921316Seric 				*np++ = p;
3931316Seric 		}
3941316Seric 	}
3951316Seric 	*np = NULL;
3961316Seric 
3971316Seric 	/*
398200Seric 	**  Interpret operation associated with this command.
399157Seric 	*/
400157Seric 
401200Seric 	switch (cmd->sccsoper)
402200Seric 	{
4031431Seric 	  case SHELL:		/* call a shell file */
4041431Seric 		*ap = cmd->sccspath;
4051431Seric 		*--ap = "sh";
4061431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4071431Seric 		break;
4081431Seric 
409200Seric 	  case PROG:		/* call an sccs prog */
4101431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
411201Seric 		break;
412201Seric 
413201Seric 	  case CMACRO:		/* command macro */
4141438Seric 		/* step through & execute each part of the macro */
415201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
416201Seric 		{
4171316Seric 			q = p;
4181316Seric 			while (*p != '\0' && *p != '/')
4191316Seric 				p++;
4201737Seric 			rval = command(&ap[1], *p != '\0', q);
4211282Seric 			if (rval != 0)
4221282Seric 				break;
423201Seric 		}
4241282Seric 		break;
425157Seric 
426226Seric 	  case FIX:		/* fix a delta */
4271431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
428226Seric 		{
4291205Seric 			usrerr("-r flag needed for fix command");
4301282Seric 			rval = EX_USAGE;
431226Seric 			break;
432226Seric 		}
4331438Seric 
4341438Seric 		/* get the version with all changes */
4351737Seric 		rval = command(&ap[1], TRUE, "get -k");
4361438Seric 
4371438Seric 		/* now remove that version from the s-file */
4381282Seric 		if (rval == 0)
4391737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4401438Seric 
4411438Seric 		/* and edit the old version (but don't clobber new vers) */
4421282Seric 		if (rval == 0)
4431737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4441282Seric 		break;
445226Seric 
446261Seric 	  case CLEAN:
4471282Seric 		rval = clean((int) cmd->sccspath);
448261Seric 		break;
449261Seric 
450396Seric 	  case UNEDIT:
4511431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
452585Seric 		{
4531316Seric 			if (unedit(*argv))
4541316Seric 				*np++ = *argv;
455585Seric 		}
4561316Seric 		*np = NULL;
4571438Seric 
4581438Seric 		/* get all the files that we unedited successfully */
459*1738Seric 		if (np > &ap[1])
4601737Seric 			rval = command(&ap[1], FALSE, "get");
461396Seric 		break;
462396Seric 
4631433Seric 	  case DIFFS:		/* diff between s-file & edit file */
4641433Seric 		/* find the end of the flag arguments */
4651433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
4661433Seric 			continue;
4671433Seric 		argv = np;
4681433Seric 
4691433Seric 		/* for each file, do the diff */
4701502Seric 		p = argv[1];
4711433Seric 		while (*np != NULL)
4721433Seric 		{
4731438Seric 			/* messy, but we need a null terminated argv */
4741433Seric 			*argv = *np++;
4751502Seric 			argv[1] = NULL;
4761435Seric 			i = dodiff(ap, tail(*argv));
4771433Seric 			if (rval == 0)
4781433Seric 				rval = i;
4791502Seric 			argv[1] = p;
4801433Seric 		}
4811433Seric 		break;
4821433Seric 
483200Seric 	  default:
4841205Seric 		syserr("oper %d", cmd->sccsoper);
485200Seric 		exit(EX_SOFTWARE);
486200Seric 	}
4871282Seric # ifdef DEBUG
4881282Seric 	if (Debug)
4891282Seric 		printf("command: rval=%d\n", rval);
4901282Seric # endif
4911282Seric 	return (rval);
492200Seric }
4931432Seric 
4941432Seric /*
495262Seric **  LOOKUP -- look up an SCCS command name.
496262Seric **
497262Seric **	Parameters:
498262Seric **		name -- the name of the command to look up.
499262Seric **
500262Seric **	Returns:
501262Seric **		ptr to command descriptor for this command.
502262Seric **		NULL if no such entry.
503262Seric **
504262Seric **	Side Effects:
505262Seric **		none.
506262Seric */
507200Seric 
508262Seric struct sccsprog *
509262Seric lookup(name)
510262Seric 	char *name;
511262Seric {
512262Seric 	register struct sccsprog *cmd;
513226Seric 
514262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
515262Seric 	{
516262Seric 		if (strcmp(cmd->sccsname, name) == 0)
517262Seric 			return (cmd);
518262Seric 	}
519262Seric 	return (NULL);
520262Seric }
5211432Seric 
5221432Seric /*
5231282Seric **  CALLPROG -- call a program
5241282Seric **
5251316Seric **	Used to call the SCCS programs.
5261282Seric **
5271282Seric **	Parameters:
5281282Seric **		progpath -- pathname of the program to call.
5291282Seric **		flags -- status flags from the command descriptors.
5301282Seric **		argv -- an argument vector to pass to the program.
5311282Seric **		forkflag -- if true, fork before calling, else just
5321282Seric **			exec.
5331282Seric **
5341282Seric **	Returns:
5351282Seric **		The exit status of the program.
5361282Seric **		Nothing if forkflag == FALSE.
5371282Seric **
5381282Seric **	Side Effects:
5391282Seric **		Can exit if forkflag == FALSE.
5401282Seric */
541226Seric 
542200Seric callprog(progpath, flags, argv, forkflag)
543200Seric 	char *progpath;
544200Seric 	short flags;
545200Seric 	char **argv;
546200Seric 	bool forkflag;
547200Seric {
548200Seric 	register int i;
549201Seric 	auto int st;
550200Seric 
5511316Seric # ifdef DEBUG
5521316Seric 	if (Debug)
5531316Seric 	{
5541316Seric 		printf("callprog:\n");
5551316Seric 		for (i = 0; argv[i] != NULL; i++)
5561316Seric 			printf("\t\"%s\"\n", argv[i]);
5571316Seric 	}
5581316Seric # endif
5591316Seric 
560200Seric 	if (*argv == NULL)
561200Seric 		return (-1);
562200Seric 
563157Seric 	/*
564226Seric 	**  Fork if appropriate.
565148Seric 	*/
566148Seric 
567200Seric 	if (forkflag)
568200Seric 	{
569393Seric # ifdef DEBUG
570393Seric 		if (Debug)
571393Seric 			printf("Forking\n");
572393Seric # endif
573200Seric 		i = fork();
574200Seric 		if (i < 0)
575200Seric 		{
5761205Seric 			syserr("cannot fork");
577200Seric 			exit(EX_OSERR);
578200Seric 		}
579200Seric 		else if (i > 0)
580201Seric 		{
581201Seric 			wait(&st);
5821282Seric 			if ((st & 0377) == 0)
5831282Seric 				st = (st >> 8) & 0377;
5841433Seric 			if (OutFile >= 0)
5851433Seric 			{
5861433Seric 				close(OutFile);
5871433Seric 				OutFile = -1;
5881433Seric 			}
589201Seric 			return (st);
590201Seric 		}
591200Seric 	}
5921433Seric 	else if (OutFile >= 0)
5931433Seric 	{
5941433Seric 		syserr("callprog: setting stdout w/o forking");
5951433Seric 		exit(EX_SOFTWARE);
5961433Seric 	}
597200Seric 
5981433Seric 	/* set protection as appropriate */
599200Seric 	if (bitset(REALUSER, flags))
600200Seric 		setuid(getuid());
6011433Seric 
6021433Seric 	/* change standard input & output if needed */
6031433Seric 	if (OutFile >= 0)
6041433Seric 	{
6051433Seric 		close(1);
6061433Seric 		dup(OutFile);
6071433Seric 		close(OutFile);
6081433Seric 	}
609226Seric 
6101433Seric 	/* call real SCCS program */
611226Seric 	execv(progpath, argv);
6121205Seric 	syserr("cannot execute %s", progpath);
613148Seric 	exit(EX_UNAVAILABLE);
614*1738Seric 	/*NOTREACHED*/
615148Seric }
6161432Seric 
6171432Seric /*
618586Seric **  MAKEFILE -- make filename of SCCS file
619586Seric **
620586Seric **	If the name passed is already the name of an SCCS file,
621586Seric **	just return it.  Otherwise, munge the name into the name
622586Seric **	of the actual SCCS file.
623586Seric **
624586Seric **	There are cases when it is not clear what you want to
625586Seric **	do.  For example, if SccsPath is an absolute pathname
626586Seric **	and the name given is also an absolute pathname, we go
627586Seric **	for SccsPath (& only use the last component of the name
628586Seric **	passed) -- this is important for security reasons (if
629586Seric **	sccs is being used as a setuid front end), but not
630586Seric **	particularly intuitive.
631586Seric **
632586Seric **	Parameters:
633586Seric **		name -- the file name to be munged.
634586Seric **
635586Seric **	Returns:
636586Seric **		The pathname of the sccs file.
637586Seric **		NULL on error.
638586Seric **
639586Seric **	Side Effects:
640586Seric **		none.
641586Seric */
642148Seric 
643148Seric char *
644148Seric makefile(name)
645148Seric 	char *name;
646148Seric {
647148Seric 	register char *p;
648148Seric 	char buf[512];
649148Seric 	extern char *malloc();
650586Seric 	extern char *rindex();
651588Seric 	extern bool safepath();
652587Seric 	extern bool isdir();
653587Seric 	register char *q;
654148Seric 
655586Seric 	p = rindex(name, '/');
656586Seric 	if (p == NULL)
657586Seric 		p = name;
658586Seric 	else
659586Seric 		p++;
660586Seric 
661148Seric 	/*
662588Seric 	**  Check to see that the path is "safe", i.e., that we
663588Seric 	**  are not letting some nasty person use the setuid part
664588Seric 	**  of this program to look at or munge some presumably
665588Seric 	**  hidden files.
666148Seric 	*/
667148Seric 
668588Seric 	if (SccsDir[0] == '/' && !safepath(name))
669588Seric 		return (NULL);
670586Seric 
671586Seric 	/*
672588Seric 	**  Create the base pathname.
673586Seric 	*/
674586Seric 
6751438Seric 	/* first the directory part */
676588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
677148Seric 	{
678588Seric 		strcpy(buf, SccsDir);
679586Seric 		strcat(buf, "/");
680586Seric 	}
681586Seric 	else
682586Seric 		strcpy(buf, "");
6831438Seric 
6841438Seric 	/* then the head of the pathname */
685587Seric 	strncat(buf, name, p - name);
686587Seric 	q = &buf[strlen(buf)];
6871438Seric 
6881438Seric 	/* now copy the final part of the name, in case useful */
689587Seric 	strcpy(q, p);
6901438Seric 
6911438Seric 	/* so is it useful? */
692587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
693586Seric 	{
6941438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
695588Seric 		strcpy(q, SccsPath);
696588Seric 		strcat(buf, "/s.");
6971438Seric 
6981438Seric 		/* and now the end of the name */
699586Seric 		strcat(buf, p);
700586Seric 	}
701148Seric 
7021438Seric 	/* if i haven't changed it, why did I do all this? */
703588Seric 	if (strcmp(buf, name) == 0)
704588Seric 		p = name;
705588Seric 	else
706148Seric 	{
7071438Seric 		/* but if I have, squirrel it away */
708588Seric 		p = malloc(strlen(buf) + 1);
709588Seric 		if (p == NULL)
710588Seric 		{
711588Seric 			perror("Sccs: no mem");
712588Seric 			exit(EX_OSERR);
713588Seric 		}
714588Seric 		strcpy(p, buf);
715148Seric 	}
7161438Seric 
717148Seric 	return (p);
718148Seric }
7191432Seric 
7201432Seric /*
721587Seric **  ISDIR -- return true if the argument is a directory.
722587Seric **
723587Seric **	Parameters:
724587Seric **		name -- the pathname of the file to check.
725587Seric **
726587Seric **	Returns:
727587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
728587Seric **
729587Seric **	Side Effects:
730587Seric **		none.
731587Seric */
732587Seric 
733587Seric bool
734587Seric isdir(name)
735587Seric 	char *name;
736587Seric {
737587Seric 	struct stat stbuf;
738587Seric 
739587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
740587Seric }
7411432Seric 
7421432Seric /*
743586Seric **  SAFEPATH -- determine whether a pathname is "safe"
744586Seric **
745586Seric **	"Safe" pathnames only allow you to get deeper into the
746586Seric **	directory structure, i.e., full pathnames and ".." are
747586Seric **	not allowed.
748586Seric **
749586Seric **	Parameters:
750586Seric **		p -- the name to check.
751586Seric **
752586Seric **	Returns:
753586Seric **		TRUE -- if the path is safe.
754586Seric **		FALSE -- if the path is not safe.
755586Seric **
756586Seric **	Side Effects:
757586Seric **		Prints a message if the path is not safe.
758586Seric */
759586Seric 
760586Seric bool
761586Seric safepath(p)
762586Seric 	register char *p;
763586Seric {
764586Seric 	extern char *index();
765586Seric 
766586Seric 	if (*p != '/')
767586Seric 	{
768586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
769586Seric 		{
770586Seric 			p = index(p, '/');
771586Seric 			if (p == NULL)
772586Seric 				return (TRUE);
773586Seric 			p++;
774586Seric 		}
775586Seric 	}
776586Seric 
777586Seric 	printf("You may not use full pathnames or \"..\"\n");
778586Seric 	return (FALSE);
779586Seric }
7801432Seric 
7811432Seric /*
782261Seric **  CLEAN -- clean out recreatable files
783261Seric **
784261Seric **	Any file for which an "s." file exists but no "p." file
785261Seric **	exists in the current directory is purged.
786261Seric **
787261Seric **	Parameters:
788819Seric **		tells whether this came from a "clean", "info", or
789819Seric **		"check" command.
790261Seric **
791261Seric **	Returns:
792261Seric **		none.
793261Seric **
794261Seric **	Side Effects:
795819Seric **		Removes files in the current directory.
796819Seric **		Prints information regarding files being edited.
797819Seric **		Exits if a "check" command.
798261Seric */
799261Seric 
800819Seric clean(mode)
801819Seric 	int mode;
802261Seric {
803261Seric 	struct direct dir;
804261Seric 	char buf[100];
805394Seric 	char pline[120];
806346Seric 	register FILE *dirfd;
807346Seric 	register char *basefile;
808351Seric 	bool gotedit;
809394Seric 	FILE *pfp;
810261Seric 
8111438Seric 	/*
8121438Seric 	**  Find and open the SCCS directory.
8131438Seric 	*/
8141438Seric 
8151207Seric 	strcpy(buf, SccsDir);
8161207Seric 	if (buf[0] != '\0')
8171207Seric 		strcat(buf, "/");
8181207Seric 	strcat(buf, SccsPath);
8191438Seric 
8201207Seric 	dirfd = fopen(buf, "r");
821261Seric 	if (dirfd == NULL)
822261Seric 	{
8231207Seric 		usrerr("cannot open %s", buf);
8241282Seric 		return (EX_NOINPUT);
825261Seric 	}
826261Seric 
827261Seric 	/*
828261Seric 	**  Scan the SCCS directory looking for s. files.
8291438Seric 	**	gotedit tells whether we have tried to clean any
8301438Seric 	**		files that are being edited.
831261Seric 	*/
832261Seric 
833351Seric 	gotedit = FALSE;
834*1738Seric 	while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL)
835261Seric 	{
836568Seric 		if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0)
837261Seric 			continue;
838261Seric 
839261Seric 		/* got an s. file -- see if the p. file exists */
8401207Seric 		strcpy(buf, SccsDir);
8411207Seric 		if (buf[0] != '\0')
8421207Seric 			strcat(buf, "/");
8431207Seric 		strcat(buf, SccsPath);
844261Seric 		strcat(buf, "/p.");
845346Seric 		basefile = &buf[strlen(buf)];
846568Seric 		strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
847346Seric 		basefile[sizeof dir.d_name - 2] = '\0';
848394Seric 		pfp = fopen(buf, "r");
849394Seric 		if (pfp != NULL)
850346Seric 		{
8511438Seric 			/* the file exists -- report it's contents */
8521730Seric 			if (mode == TELLC)
8531730Seric 				printf("%s\n", basefile);
8541730Seric 			else
8551730Seric 			{
8561730Seric 				while (fgets(pline, sizeof pline, pfp) != NULL)
8571730Seric 					printf("%12s: being edited: %s", basefile, pline);
8581730Seric 			}
859394Seric 			fclose(pfp);
860351Seric 			gotedit = TRUE;
861261Seric 			continue;
862346Seric 		}
863261Seric 
864261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
865819Seric 		if (mode == CLEANC)
866346Seric 		{
867568Seric 			strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2);
868346Seric 			buf[sizeof dir.d_name - 2] = '\0';
869346Seric 			unlink(buf);
870346Seric 		}
871261Seric 	}
872261Seric 
8731438Seric 	/* cleanup & report results */
874261Seric 	fclose(dirfd);
875819Seric 	if (!gotedit && mode == INFOC)
876416Seric 		printf("Nothing being edited\n");
877819Seric 	if (mode == CHECKC)
878819Seric 		exit(gotedit);
8791282Seric 	return (EX_OK);
880261Seric }
8811432Seric 
8821432Seric /*
883396Seric **  UNEDIT -- unedit a file
884396Seric **
885396Seric **	Checks to see that the current user is actually editting
886396Seric **	the file and arranges that s/he is not editting it.
887396Seric **
888396Seric **	Parameters:
889416Seric **		fn -- the name of the file to be unedited.
890396Seric **
891396Seric **	Returns:
892585Seric **		TRUE -- if the file was successfully unedited.
893585Seric **		FALSE -- if the file was not unedited for some
894585Seric **			reason.
895396Seric **
896396Seric **	Side Effects:
897396Seric **		fn is removed
898396Seric **		entries are removed from pfile.
899396Seric */
900396Seric 
901585Seric bool
902396Seric unedit(fn)
903396Seric 	char *fn;
904396Seric {
905396Seric 	register FILE *pfp;
906396Seric 	char *pfn;
907396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
908396Seric 	FILE *tfp;
909396Seric 	register char *q;
910396Seric 	bool delete = FALSE;
911396Seric 	bool others = FALSE;
912396Seric 	char *myname;
913396Seric 	extern char *getlogin();
914396Seric 	struct pfile *pent;
915396Seric 	extern struct pfile *getpfile();
916396Seric 	char buf[120];
9171316Seric 	extern char *makefile();
918828Seric # ifdef UIDUSER
919828Seric 	struct passwd *pw;
920828Seric 	extern struct passwd *getpwuid();
921828Seric # endif UIDUSER
922396Seric 
923396Seric 	/* make "s." filename & find the trailing component */
924396Seric 	pfn = makefile(fn);
925586Seric 	if (pfn == NULL)
926586Seric 		return (FALSE);
927586Seric 	q = rindex(pfn, '/');
928586Seric 	if (q == NULL)
929586Seric 		q = &pfn[-1];
930586Seric 	if (q[1] != 's' || q[2] != '.')
931396Seric 	{
9321205Seric 		usrerr("bad file name \"%s\"", fn);
933585Seric 		return (FALSE);
934396Seric 	}
935396Seric 
9361438Seric 	/* turn "s." into "p." & try to open it */
937396Seric 	*++q = 'p';
938396Seric 
939396Seric 	pfp = fopen(pfn, "r");
940396Seric 	if (pfp == NULL)
941396Seric 	{
942416Seric 		printf("%12s: not being edited\n", fn);
943585Seric 		return (FALSE);
944396Seric 	}
945396Seric 
9461438Seric 	/* create temp file for editing p-file */
947396Seric 	mktemp(tfn);
948396Seric 	tfp = fopen(tfn, "w");
949396Seric 	if (tfp == NULL)
950396Seric 	{
9511205Seric 		usrerr("cannot create \"%s\"", tfn);
952396Seric 		exit(EX_OSERR);
953396Seric 	}
954396Seric 
9551438Seric 	/* figure out who I am */
956828Seric # ifdef UIDUSER
957828Seric 	pw = getpwuid(getuid());
958828Seric 	if (pw == NULL)
959828Seric 	{
9601205Seric 		syserr("who are you? (uid=%d)", getuid());
961828Seric 		exit(EX_OSERR);
962828Seric 	}
963828Seric 	myname = pw->pw_name;
964828Seric # else
965396Seric 	myname = getlogin();
966828Seric # endif UIDUSER
9671438Seric 
9681438Seric 	/*
9691438Seric 	**  Copy p-file to temp file, doing deletions as needed.
9701438Seric 	*/
9711438Seric 
972396Seric 	while ((pent = getpfile(pfp)) != NULL)
973396Seric 	{
974396Seric 		if (strcmp(pent->p_user, myname) == 0)
975396Seric 		{
976396Seric 			/* a match */
977396Seric 			delete++;
978396Seric 		}
979396Seric 		else
980396Seric 		{
9811438Seric 			/* output it again */
982396Seric 			fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid,
983396Seric 			    pent->p_nsid, pent->p_user, pent->p_date,
984396Seric 			    pent->p_time);
985396Seric 			others++;
986396Seric 		}
987396Seric 	}
988396Seric 
989396Seric 	/* do final cleanup */
990396Seric 	if (others)
991396Seric 	{
9921438Seric 		/* copy it back (perhaps it should be linked?) */
993396Seric 		if (freopen(tfn, "r", tfp) == NULL)
994396Seric 		{
9951205Seric 			syserr("cannot reopen \"%s\"", tfn);
996396Seric 			exit(EX_OSERR);
997396Seric 		}
998396Seric 		if (freopen(pfn, "w", pfp) == NULL)
999396Seric 		{
10001205Seric 			usrerr("cannot create \"%s\"", pfn);
1001585Seric 			return (FALSE);
1002396Seric 		}
1003396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1004396Seric 			fputs(buf, pfp);
1005396Seric 	}
1006396Seric 	else
1007396Seric 	{
10081438Seric 		/* it's empty -- remove it */
1009396Seric 		unlink(pfn);
1010396Seric 	}
1011396Seric 	fclose(tfp);
1012396Seric 	fclose(pfp);
1013396Seric 	unlink(tfn);
1014396Seric 
10151438Seric 	/* actually remove the g-file */
1016396Seric 	if (delete)
1017396Seric 	{
10181435Seric 		unlink(tail(fn));
10191435Seric 		printf("%12s: removed\n", tail(fn));
1020585Seric 		return (TRUE);
1021396Seric 	}
1022396Seric 	else
1023396Seric 	{
1024416Seric 		printf("%12s: not being edited by you\n", fn);
1025585Seric 		return (FALSE);
1026396Seric 	}
1027396Seric }
10281432Seric 
10291432Seric /*
10301433Seric **  DODIFF -- diff an s-file against a g-file
10311433Seric **
10321433Seric **	Parameters:
10331433Seric **		getv -- argv for the 'get' command.
10341433Seric **		gfile -- name of the g-file to diff against.
10351433Seric **
10361433Seric **	Returns:
10371433Seric **		Result of get.
10381433Seric **
10391433Seric **	Side Effects:
10401433Seric **		none.
10411433Seric */
10421433Seric 
10431433Seric dodiff(getv, gfile)
10441433Seric 	char **getv;
10451433Seric 	char *gfile;
10461433Seric {
10471433Seric 	int pipev[2];
10481433Seric 	int rval;
10491433Seric 	register int i;
10501433Seric 	register int pid;
10511433Seric 	auto int st;
10521433Seric 	extern int errno;
10531433Seric 	int (*osig)();
10541433Seric 
10551501Seric 	printf("\n------- %s -------\n", gfile);
10561501Seric 
10571438Seric 	/* create context for diff to run in */
10581433Seric 	if (pipe(pipev) < 0)
10591433Seric 	{
10601433Seric 		syserr("dodiff: pipe failed");
10611433Seric 		exit(EX_OSERR);
10621433Seric 	}
10631433Seric 	if ((pid = fork()) < 0)
10641433Seric 	{
10651433Seric 		syserr("dodiff: fork failed");
10661433Seric 		exit(EX_OSERR);
10671433Seric 	}
10681433Seric 	else if (pid > 0)
10691433Seric 	{
10701433Seric 		/* in parent; run get */
10711433Seric 		OutFile = pipev[1];
10721433Seric 		close(pipev[0]);
10731737Seric 		rval = command(&getv[1], TRUE, "get -s -k -p");
10741433Seric 		osig = signal(SIGINT, SIG_IGN);
10751433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
10761433Seric 			errno = 0;
10771433Seric 		signal(SIGINT, osig);
10781433Seric 		/* ignore result of diff */
10791433Seric 	}
10801433Seric 	else
10811433Seric 	{
10821433Seric 		/* in child, run diff */
10831433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
10841433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
10851433Seric 		{
10861433Seric 			syserr("dodiff: magic failed");
10871433Seric 			exit(EX_OSERR);
10881433Seric 		}
10891433Seric 		execl(PROGPATH(bdiff), "bdiff", "-", gfile, NULL);
10901433Seric # ifndef V6
10911433Seric 		execlp("bdiff", "bdiff", "-", gfile, NULL);
10921433Seric 		execlp("diff", "diff", "-", gfile, NULL);
10931433Seric # endif NOT V6
10941433Seric 		syserr("bdiff: cannot execute");
10951433Seric 		exit(EX_OSERR);
10961433Seric 	}
10971433Seric 	return (rval);
10981433Seric }
10991433Seric 
11001433Seric /*
11011435Seric **  TAIL -- return tail of filename.
11021435Seric **
11031435Seric **	Parameters:
11041435Seric **		fn -- the filename.
11051435Seric **
11061435Seric **	Returns:
11071435Seric **		a pointer to the tail of the filename; e.g., given
11081435Seric **		"cmd/ls.c", "ls.c" is returned.
11091435Seric **
11101435Seric **	Side Effects:
11111435Seric **		none.
11121435Seric */
11131435Seric 
11141435Seric char *
11151435Seric tail(fn)
11161435Seric 	register char *fn;
11171435Seric {
11181435Seric 	register char *p;
11191435Seric 
11201435Seric 	for (p = fn; *p != 0; p++)
11211435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
11221435Seric 			fn = &p[1];
11231435Seric 	return (fn);
11241435Seric }
11251435Seric 
11261435Seric /*
1127396Seric **  GETPFILE -- get an entry from the p-file
1128396Seric **
1129396Seric **	Parameters:
1130396Seric **		pfp -- p-file file pointer
1131396Seric **
1132396Seric **	Returns:
1133396Seric **		pointer to p-file struct for next entry
1134396Seric **		NULL on EOF or error
1135396Seric **
1136396Seric **	Side Effects:
1137396Seric **		Each call wipes out results of previous call.
1138396Seric */
1139396Seric 
1140396Seric struct pfile *
1141396Seric getpfile(pfp)
1142396Seric 	FILE *pfp;
1143396Seric {
1144396Seric 	static struct pfile ent;
1145396Seric 	static char buf[120];
1146396Seric 	register char *p;
1147396Seric 	extern char *nextfield();
1148396Seric 
1149396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1150396Seric 		return (NULL);
1151396Seric 
1152396Seric 	ent.p_osid = p = buf;
1153396Seric 	ent.p_nsid = p = nextfield(p);
1154396Seric 	ent.p_user = p = nextfield(p);
1155396Seric 	ent.p_date = p = nextfield(p);
1156396Seric 	ent.p_time = p = nextfield(p);
1157396Seric 	if (p == NULL || nextfield(p) != NULL)
1158396Seric 		return (NULL);
1159396Seric 
1160396Seric 	return (&ent);
1161396Seric }
1162396Seric 
1163396Seric 
1164396Seric char *
1165396Seric nextfield(p)
1166396Seric 	register char *p;
1167396Seric {
1168396Seric 	if (p == NULL || *p == '\0')
1169396Seric 		return (NULL);
1170396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1171396Seric 		p++;
1172396Seric 	if (*p == '\n' || *p == '\0')
1173396Seric 	{
1174396Seric 		*p = '\0';
1175396Seric 		return (NULL);
1176396Seric 	}
1177396Seric 	*p++ = '\0';
1178396Seric 	return (p);
1179396Seric }
11801432Seric 
11811432Seric /*
11821205Seric **  USRERR -- issue user-level error
11831205Seric **
11841205Seric **	Parameters:
11851205Seric **		f -- format string.
11861205Seric **		p1-p3 -- parameters to a printf.
11871205Seric **
11881205Seric **	Returns:
11891205Seric **		-1
11901205Seric **
11911205Seric **	Side Effects:
11921205Seric **		none.
11931205Seric */
11941205Seric 
1195*1738Seric /*VARARGS1*/
11961205Seric usrerr(f, p1, p2, p3)
11971205Seric 	char *f;
11981205Seric {
11991205Seric 	fprintf(stderr, "\n%s: ", MyName);
12001205Seric 	fprintf(stderr, f, p1, p2, p3);
12011205Seric 	fprintf(stderr, "\n");
12021205Seric 
12031205Seric 	return (-1);
12041205Seric }
12051432Seric 
12061432Seric /*
12071205Seric **  SYSERR -- print system-generated error.
12081205Seric **
12091205Seric **	Parameters:
12101205Seric **		f -- format string to a printf.
12111205Seric **		p1, p2, p3 -- parameters to f.
12121205Seric **
12131205Seric **	Returns:
12141205Seric **		never.
12151205Seric **
12161205Seric **	Side Effects:
12171205Seric **		none.
12181205Seric */
12191205Seric 
1220*1738Seric /*VARARGS1*/
12211205Seric syserr(f, p1, p2, p3)
12221205Seric 	char *f;
12231205Seric {
12241205Seric 	extern int errno;
12251205Seric 
12261205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
12271205Seric 	fprintf(stderr, f, p1, p2, p3);
12281205Seric 	fprintf(stderr, "\n");
12291205Seric 	if (errno == 0)
12301205Seric 		exit(EX_SOFTWARE);
12311205Seric 	else
12321205Seric 	{
1233*1738Seric 		perror(NULL);
12341205Seric 		exit(EX_OSERR);
12351205Seric 	}
12361205Seric }
1237