xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 10260)
1148Seric # include <stdio.h>
26784Smckusick # include <sys/param.h>
3148Seric # include <sys/stat.h>
46784Smckusick # include <dir.h>
51433Seric # include <errno.h>
61433Seric # include <signal.h>
7148Seric # include <sysexits.h>
82140Seric # include <pwd.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*10260Seric static char SccsId[] = "@(#)sccs.c	1.69 01/13/83";
961432Seric 
971270Seric /*******************  Configuration Information  ********************/
981270Seric 
991437Seric # ifndef SCCSPATH
1001432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1011437Seric # endif NOT SCCSPATH
102828Seric 
1031437Seric # ifndef MYNAME
1041437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1051437Seric # endif NOT MYNAME
1061270Seric 
1071432Seric # ifndef PROGPATH
1086784Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1091432Seric # endif PROGPATH
1101432Seric 
1111270Seric /****************  End of Configuration Information  ****************/
1121432Seric 
113157Seric typedef char	bool;
114200Seric # define TRUE	1
115200Seric # define FALSE	0
116157Seric 
1171438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1181438Seric 
119148Seric struct sccsprog
120148Seric {
121148Seric 	char	*sccsname;	/* name of SCCS routine */
122200Seric 	short	sccsoper;	/* opcode, see below */
123200Seric 	short	sccsflags;	/* flags, see below */
124148Seric 	char	*sccspath;	/* pathname of binary implementing */
125148Seric };
126148Seric 
127200Seric /* values for sccsoper */
128200Seric # define PROG		0	/* call a program */
129201Seric # define CMACRO		1	/* command substitution macro */
130226Seric # define FIX		2	/* fix a delta */
131261Seric # define CLEAN		3	/* clean out recreatable files */
132396Seric # define UNEDIT		4	/* unedit a file */
1331431Seric # define SHELL		5	/* call a shell file (like PROG) */
1341433Seric # define DIFFS		6	/* diff between sccs & file out */
1351871Seric # define DODIFF		7	/* internal call to diff program */
13610104Srrh # define ENTER		8	/* enter new files */
137200Seric 
138157Seric /* bits for sccsflags */
139200Seric # define NO_SDOT	0001	/* no s. on front of args */
140200Seric # define REALUSER	0002	/* protected (e.g., admin) */
141148Seric 
142819Seric /* modes for the "clean", "info", "check" ops */
143819Seric # define CLEANC		0	/* clean command */
144819Seric # define INFOC		1	/* info command */
145819Seric # define CHECKC		2	/* check command */
1461730Seric # define TELLC		3	/* give list of files being edited */
147819Seric 
1481432Seric /*
1491432Seric **  Description of commands known to this program.
1501432Seric **	First argument puts the command into a class.  Second arg is
1511432Seric **	info regarding treatment of this command.  Third arg is a
1521432Seric **	list of flags this command accepts from macros, etc.  Fourth
1531432Seric **	arg is the pathname of the implementing program, or the
1541432Seric **	macro definition, or the arg to a sub-algorithm.
1551432Seric */
156202Seric 
157148Seric struct sccsprog SccsProg[] =
158148Seric {
1591864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1601864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1611864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1621864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1631864Seric 	"get",		PROG,	0,			PROGPATH(get),
1641864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1652136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1661864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1671864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1682136Seric 	"val",		PROG,	0,			PROGPATH(val),
1691864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1701864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1711864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1721864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1732138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1741864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1751864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1761864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1771864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1781864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1791864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1801864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1811871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
1822137Seric 	"print",	CMACRO,	0,			"prt -e/get -p -m -s",
1832226Seric 	"branch",	CMACRO,	NO_SDOT,
1842226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
18510104Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
18610104Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
1871864Seric 	NULL,		-1,	0,			NULL
188148Seric };
189148Seric 
1901432Seric /* one line from a p-file */
191396Seric struct pfile
192396Seric {
193396Seric 	char	*p_osid;	/* old SID */
194396Seric 	char	*p_nsid;	/* new SID */
195396Seric 	char	*p_user;	/* user who did edit */
196396Seric 	char	*p_date;	/* date of get */
197396Seric 	char	*p_time;	/* time of get */
1982161Seric 	char	*p_aux;		/* extra info at end */
199396Seric };
200396Seric 
2011270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2021270Seric # ifdef SCCSDIR
2031270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2041205Seric # else
2051270Seric char	*SccsDir = "";
2061205Seric # endif
2071437Seric char	MyName[] = MYNAME;	/* name used in messages */
2081433Seric int	OutFile = -1;		/* override output file for commands */
209157Seric bool	RealUser;		/* if set, running as real user */
210393Seric # ifdef DEBUG
211393Seric bool	Debug;			/* turn on tracing */
212393Seric # endif
2132139Seric # ifndef V6
2142139Seric extern char	*getenv();
2152139Seric # endif V6
21610110Srrh 
21710110Srrh char *gstrcat(), *strcat();
21810110Srrh char *gstrncat(), *strncat();
21910110Srrh char *gstrcpy(), *strcpy();
22010110Srrh #define	FBUFSIZ	BUFSIZ
22110110Srrh #define	PFILELG	120
2221432Seric 
223148Seric main(argc, argv)
224148Seric 	int argc;
225148Seric 	char **argv;
226148Seric {
227148Seric 	register char *p;
228262Seric 	extern struct sccsprog *lookup();
2291282Seric 	register int i;
2302139Seric # ifndef V6
2312139Seric # ifndef SCCSDIR
2322140Seric 	register struct passwd *pw;
2332140Seric 	extern struct passwd *getpwnam();
23410110Srrh 	char buf[FBUFSIZ];
2352140Seric 
2362139Seric 	/* pull "SccsDir" out of the environment (possibly) */
237*10260Seric 	p = getenv("PROJECTDIR");
2382140Seric 	if (p != NULL && p[0] != '\0')
2392140Seric 	{
2402140Seric 		if (p[0] == '/')
2412140Seric 			SccsDir = p;
2422140Seric 		else
2432140Seric 		{
2442140Seric 			pw = getpwnam(p);
2452140Seric 			if (pw == NULL)
2462140Seric 			{
2472140Seric 				usrerr("user %s does not exist", p);
2482140Seric 				exit(EX_USAGE);
2492140Seric 			}
25010110Srrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
25110110Srrh 			gstrcat(buf, "/src", sizeof(buf));
2522140Seric 			if (access(buf, 0) < 0)
2532140Seric 			{
25410110Srrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
25510110Srrh 				gstrcat(buf, "/source", sizeof(buf));
2562140Seric 				if (access(buf, 0) < 0)
2572140Seric 				{
2582140Seric 					usrerr("project %s has no source!", p);
2592140Seric 					exit(EX_USAGE);
2602140Seric 				}
2612140Seric 			}
2622140Seric 			SccsDir = buf;
2632140Seric 		}
2642140Seric 	}
2652139Seric # endif SCCSDIR
2662139Seric # endif V6
2672139Seric 
268148Seric 	/*
269148Seric 	**  Detect and decode flags intended for this program.
270148Seric 	*/
271148Seric 
272200Seric 	if (argc < 2)
273148Seric 	{
2741205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
275200Seric 		exit(EX_USAGE);
276200Seric 	}
277200Seric 	argv[argc] = NULL;
278200Seric 
279262Seric 	if (lookup(argv[0]) == NULL)
280200Seric 	{
281262Seric 		while ((p = *++argv) != NULL)
282148Seric 		{
283262Seric 			if (*p != '-')
284262Seric 				break;
285262Seric 			switch (*++p)
286262Seric 			{
287262Seric 			  case 'r':		/* run as real user */
288262Seric 				setuid(getuid());
289262Seric 				RealUser++;
290262Seric 				break;
291148Seric 
2921270Seric # ifndef SCCSDIR
293262Seric 			  case 'p':		/* path of sccs files */
294262Seric 				SccsPath = ++p;
2952348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
2962348Seric 					SccsPath = *++argv;
297262Seric 				break;
298148Seric 
299588Seric 			  case 'd':		/* directory to search from */
300588Seric 				SccsDir = ++p;
3012348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3022348Seric 					SccsDir = *++argv;
303588Seric 				break;
3041205Seric # endif
305588Seric 
306393Seric # ifdef DEBUG
307393Seric 			  case 'T':		/* trace */
308393Seric 				Debug++;
309393Seric 				break;
310393Seric # endif
311393Seric 
312262Seric 			  default:
3131205Seric 				usrerr("unknown option -%s", p);
314262Seric 				break;
315262Seric 			}
316148Seric 		}
317262Seric 		if (SccsPath[0] == '\0')
318262Seric 			SccsPath = ".";
319148Seric 	}
320148Seric 
3211737Seric 	i = command(argv, FALSE, "");
3221282Seric 	exit(i);
323200Seric }
3241432Seric 
3251432Seric /*
3261282Seric **  COMMAND -- look up and perform a command
3271282Seric **
3281282Seric **	This routine is the guts of this program.  Given an
3291282Seric **	argument vector, it looks up the "command" (argv[0])
3301282Seric **	in the configuration table and does the necessary stuff.
3311282Seric **
3321282Seric **	Parameters:
3331282Seric **		argv -- an argument vector to process.
3341282Seric **		forkflag -- if set, fork before executing the command.
3351316Seric **		editflag -- if set, only include flags listed in the
3361316Seric **			sccsklets field of the command descriptor.
3371316Seric **		arg0 -- a space-seperated list of arguments to insert
3381316Seric **			before argv.
3391282Seric **
3401282Seric **	Returns:
3411282Seric **		zero -- command executed ok.
3421282Seric **		else -- error status.
3431282Seric **
3441282Seric **	Side Effects:
3451282Seric **		none.
3461282Seric */
347157Seric 
3481737Seric command(argv, forkflag, arg0)
349200Seric 	char **argv;
350201Seric 	bool forkflag;
3511316Seric 	char *arg0;
352200Seric {
353200Seric 	register struct sccsprog *cmd;
354200Seric 	register char *p;
35510110Srrh 	char buf[FBUFSIZ];
356262Seric 	extern struct sccsprog *lookup();
3571316Seric 	char *nav[1000];
3581316Seric 	char **np;
3591431Seric 	register char **ap;
360585Seric 	register int i;
3611431Seric 	register char *q;
362585Seric 	extern bool unedit();
3631282Seric 	int rval = 0;
3641316Seric 	extern char *index();
3651316Seric 	extern char *makefile();
3661737Seric 	char *editchs;
3671435Seric 	extern char *tail();
368200Seric 
369393Seric # ifdef DEBUG
370393Seric 	if (Debug)
371393Seric 	{
3721316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3731316Seric 		for (np = argv; *np != NULL; np++)
3741316Seric 			printf("\t\"%s\"\n", *np);
375393Seric 	}
376393Seric # endif
377393Seric 
378157Seric 	/*
3791316Seric 	**  Copy arguments.
3801438Seric 	**	Copy from arg0 & if necessary at most one arg
3811438Seric 	**	from argv[0].
3821316Seric 	*/
3831316Seric 
3841431Seric 	np = ap = &nav[1];
3851737Seric 	editchs = NULL;
3861821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3871316Seric 	{
3881316Seric 		*np++ = q;
3891316Seric 		while (*p == ' ')
3901316Seric 			p++;
3911737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3921316Seric 			*q++ = *p++;
3931316Seric 		*q++ = '\0';
3941737Seric 		if (*p == ':')
3951737Seric 		{
3961737Seric 			editchs = q;
3971821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
3981737Seric 				*q++ = *p;
3991737Seric 			*q++ = '\0';
4001737Seric 		}
4011316Seric 	}
4021316Seric 	*np = NULL;
4031431Seric 	if (*ap == NULL)
4041316Seric 		*np++ = *argv++;
4051316Seric 
4061316Seric 	/*
407148Seric 	**  Look up command.
4081431Seric 	**	At this point, *ap is the command name.
409148Seric 	*/
410148Seric 
4111431Seric 	cmd = lookup(*ap);
412262Seric 	if (cmd == NULL)
413148Seric 	{
4141431Seric 		usrerr("Unknown command \"%s\"", *ap);
4151282Seric 		return (EX_USAGE);
416148Seric 	}
417148Seric 
418148Seric 	/*
4191316Seric 	**  Copy remaining arguments doing editing as appropriate.
4201316Seric 	*/
4211316Seric 
4221316Seric 	for (; *argv != NULL; argv++)
4231316Seric 	{
4241316Seric 		p = *argv;
4251316Seric 		if (*p == '-')
4261316Seric 		{
4271737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4281316Seric 				*np++ = p;
4291316Seric 		}
4301316Seric 		else
4311316Seric 		{
4321316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4331316Seric 				p = makefile(p);
4341316Seric 			if (p != NULL)
4351316Seric 				*np++ = p;
4361316Seric 		}
4371316Seric 	}
4381316Seric 	*np = NULL;
4391316Seric 
4401316Seric 	/*
441200Seric 	**  Interpret operation associated with this command.
442157Seric 	*/
443157Seric 
444200Seric 	switch (cmd->sccsoper)
445200Seric 	{
4461431Seric 	  case SHELL:		/* call a shell file */
4471431Seric 		*ap = cmd->sccspath;
4481431Seric 		*--ap = "sh";
4491431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4501431Seric 		break;
4511431Seric 
452200Seric 	  case PROG:		/* call an sccs prog */
4531431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
454201Seric 		break;
455201Seric 
456201Seric 	  case CMACRO:		/* command macro */
4571438Seric 		/* step through & execute each part of the macro */
458201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
459201Seric 		{
4601316Seric 			q = p;
4611316Seric 			while (*p != '\0' && *p != '/')
4621316Seric 				p++;
4631737Seric 			rval = command(&ap[1], *p != '\0', q);
4641282Seric 			if (rval != 0)
4651282Seric 				break;
466201Seric 		}
4671282Seric 		break;
468157Seric 
469226Seric 	  case FIX:		/* fix a delta */
4701431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
471226Seric 		{
4721205Seric 			usrerr("-r flag needed for fix command");
4731282Seric 			rval = EX_USAGE;
474226Seric 			break;
475226Seric 		}
4761438Seric 
4771438Seric 		/* get the version with all changes */
4781737Seric 		rval = command(&ap[1], TRUE, "get -k");
4791438Seric 
4801438Seric 		/* now remove that version from the s-file */
4811282Seric 		if (rval == 0)
4821737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4831438Seric 
4841438Seric 		/* and edit the old version (but don't clobber new vers) */
4851282Seric 		if (rval == 0)
4861737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4871282Seric 		break;
488226Seric 
489261Seric 	  case CLEAN:
4901822Seric 		rval = clean((int) cmd->sccspath, ap);
491261Seric 		break;
492261Seric 
493396Seric 	  case UNEDIT:
4941431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
495585Seric 		{
4961316Seric 			if (unedit(*argv))
4971316Seric 				*np++ = *argv;
498585Seric 		}
4991316Seric 		*np = NULL;
5001438Seric 
5011438Seric 		/* get all the files that we unedited successfully */
5021738Seric 		if (np > &ap[1])
5031737Seric 			rval = command(&ap[1], FALSE, "get");
504396Seric 		break;
505396Seric 
5061433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5071433Seric 		/* find the end of the flag arguments */
5081433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5091433Seric 			continue;
5101433Seric 		argv = np;
5111433Seric 
5121433Seric 		/* for each file, do the diff */
5131502Seric 		p = argv[1];
5141433Seric 		while (*np != NULL)
5151433Seric 		{
5161438Seric 			/* messy, but we need a null terminated argv */
5171433Seric 			*argv = *np++;
5181502Seric 			argv[1] = NULL;
5191435Seric 			i = dodiff(ap, tail(*argv));
5201433Seric 			if (rval == 0)
5211433Seric 				rval = i;
5221502Seric 			argv[1] = p;
5231433Seric 		}
5241433Seric 		break;
5251433Seric 
5261871Seric 	  case DODIFF:		/* internal diff call */
5271871Seric 		setuid(getuid());
5281871Seric 		for (np = ap; *np != NULL; np++)
5291871Seric 		{
5301871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5311871Seric 				(*np)[1] = 'c';
5321871Seric 		}
5331871Seric 
5341871Seric 		/* insert "-" argument */
5351871Seric 		np[1] = NULL;
5361871Seric 		np[0] = np[-1];
5371871Seric 		np[-1] = "-";
5381871Seric 
5391871Seric 		/* execute the diff program of choice */
5401871Seric # ifndef V6
5411871Seric 		execvp("diff", ap);
5421871Seric # endif
5431871Seric 		execv(cmd->sccspath, argv);
5441871Seric 		syserr("cannot exec %s", cmd->sccspath);
5451871Seric 		exit(EX_OSERR);
5461871Seric 
54710104Srrh 	  case ENTER:		/* enter new sccs files */
5486785Smckusick 		/* skip over flag arguments */
5496785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5506785Smckusick 			continue;
5516785Smckusick 		argv = np;
5526785Smckusick 
5536785Smckusick 		/* do an admin for each file */
5546785Smckusick 		p = argv[1];
5556785Smckusick 		while (*np != NULL)
5566785Smckusick 		{
5576785Smckusick 			printf("\n%s:\n", *np);
55810110Srrh 			strcpy(buf, "-i");
55910110Srrh 			gstrcat(buf, *np, sizeof(buf));
5606785Smckusick 			ap[0] = buf;
5616785Smckusick 			argv[0] = tail(*np);
5626785Smckusick 			argv[1] = NULL;
5636785Smckusick 			rval = command(ap, TRUE, "admin");
5646785Smckusick 			argv[1] = p;
5656785Smckusick 			if (rval == 0)
5666785Smckusick 			{
56710110Srrh 				strcpy(buf, ",");
56810110Srrh 				gstrcat(buf, tail(*np), sizeof(buf));
5696785Smckusick 				if (link(*np, buf) >= 0)
5706785Smckusick 					unlink(*np);
5716785Smckusick 			}
5726785Smckusick 			np++;
5736785Smckusick 		}
5746785Smckusick 		break;
5756785Smckusick 
576200Seric 	  default:
5771205Seric 		syserr("oper %d", cmd->sccsoper);
578200Seric 		exit(EX_SOFTWARE);
579200Seric 	}
5801282Seric # ifdef DEBUG
5811282Seric 	if (Debug)
5821282Seric 		printf("command: rval=%d\n", rval);
5831282Seric # endif
5841282Seric 	return (rval);
585200Seric }
5861432Seric 
5871432Seric /*
588262Seric **  LOOKUP -- look up an SCCS command name.
589262Seric **
590262Seric **	Parameters:
591262Seric **		name -- the name of the command to look up.
592262Seric **
593262Seric **	Returns:
594262Seric **		ptr to command descriptor for this command.
595262Seric **		NULL if no such entry.
596262Seric **
597262Seric **	Side Effects:
598262Seric **		none.
599262Seric */
600200Seric 
601262Seric struct sccsprog *
602262Seric lookup(name)
603262Seric 	char *name;
604262Seric {
605262Seric 	register struct sccsprog *cmd;
606226Seric 
607262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
608262Seric 	{
609262Seric 		if (strcmp(cmd->sccsname, name) == 0)
610262Seric 			return (cmd);
611262Seric 	}
612262Seric 	return (NULL);
613262Seric }
6141432Seric 
6151432Seric /*
6161282Seric **  CALLPROG -- call a program
6171282Seric **
6181316Seric **	Used to call the SCCS programs.
6191282Seric **
6201282Seric **	Parameters:
6211282Seric **		progpath -- pathname of the program to call.
6221282Seric **		flags -- status flags from the command descriptors.
6231282Seric **		argv -- an argument vector to pass to the program.
6241282Seric **		forkflag -- if true, fork before calling, else just
6251282Seric **			exec.
6261282Seric **
6271282Seric **	Returns:
6281282Seric **		The exit status of the program.
6291282Seric **		Nothing if forkflag == FALSE.
6301282Seric **
6311282Seric **	Side Effects:
6321282Seric **		Can exit if forkflag == FALSE.
6331282Seric */
634226Seric 
635200Seric callprog(progpath, flags, argv, forkflag)
636200Seric 	char *progpath;
637200Seric 	short flags;
638200Seric 	char **argv;
639200Seric 	bool forkflag;
640200Seric {
641200Seric 	register int i;
642201Seric 	auto int st;
643200Seric 
6441316Seric # ifdef DEBUG
6451316Seric 	if (Debug)
6461316Seric 	{
6471316Seric 		printf("callprog:\n");
6481316Seric 		for (i = 0; argv[i] != NULL; i++)
6491316Seric 			printf("\t\"%s\"\n", argv[i]);
6501316Seric 	}
6511316Seric # endif
6521316Seric 
653200Seric 	if (*argv == NULL)
654200Seric 		return (-1);
655200Seric 
656157Seric 	/*
657226Seric 	**  Fork if appropriate.
658148Seric 	*/
659148Seric 
660200Seric 	if (forkflag)
661200Seric 	{
662393Seric # ifdef DEBUG
663393Seric 		if (Debug)
664393Seric 			printf("Forking\n");
665393Seric # endif
666200Seric 		i = fork();
667200Seric 		if (i < 0)
668200Seric 		{
6691205Seric 			syserr("cannot fork");
670200Seric 			exit(EX_OSERR);
671200Seric 		}
672200Seric 		else if (i > 0)
673201Seric 		{
674201Seric 			wait(&st);
6751282Seric 			if ((st & 0377) == 0)
6761282Seric 				st = (st >> 8) & 0377;
6771433Seric 			if (OutFile >= 0)
6781433Seric 			{
6791433Seric 				close(OutFile);
6801433Seric 				OutFile = -1;
6811433Seric 			}
682201Seric 			return (st);
683201Seric 		}
684200Seric 	}
6851433Seric 	else if (OutFile >= 0)
6861433Seric 	{
6871433Seric 		syserr("callprog: setting stdout w/o forking");
6881433Seric 		exit(EX_SOFTWARE);
6891433Seric 	}
690200Seric 
6911433Seric 	/* set protection as appropriate */
692200Seric 	if (bitset(REALUSER, flags))
693200Seric 		setuid(getuid());
6941433Seric 
6951433Seric 	/* change standard input & output if needed */
6961433Seric 	if (OutFile >= 0)
6971433Seric 	{
6981433Seric 		close(1);
6991433Seric 		dup(OutFile);
7001433Seric 		close(OutFile);
7011433Seric 	}
702226Seric 
7031433Seric 	/* call real SCCS program */
704226Seric 	execv(progpath, argv);
7051205Seric 	syserr("cannot execute %s", progpath);
706148Seric 	exit(EX_UNAVAILABLE);
7071738Seric 	/*NOTREACHED*/
708148Seric }
7091432Seric 
7101432Seric /*
711586Seric **  MAKEFILE -- make filename of SCCS file
712586Seric **
713586Seric **	If the name passed is already the name of an SCCS file,
714586Seric **	just return it.  Otherwise, munge the name into the name
715586Seric **	of the actual SCCS file.
716586Seric **
717586Seric **	There are cases when it is not clear what you want to
718586Seric **	do.  For example, if SccsPath is an absolute pathname
719586Seric **	and the name given is also an absolute pathname, we go
720586Seric **	for SccsPath (& only use the last component of the name
721586Seric **	passed) -- this is important for security reasons (if
722586Seric **	sccs is being used as a setuid front end), but not
723586Seric **	particularly intuitive.
724586Seric **
725586Seric **	Parameters:
726586Seric **		name -- the file name to be munged.
727586Seric **
728586Seric **	Returns:
729586Seric **		The pathname of the sccs file.
730586Seric **		NULL on error.
731586Seric **
732586Seric **	Side Effects:
733586Seric **		none.
734586Seric */
735148Seric 
736148Seric char *
737148Seric makefile(name)
738148Seric 	char *name;
739148Seric {
740148Seric 	register char *p;
74110110Srrh 	char buf[3*FBUFSIZ];
742148Seric 	extern char *malloc();
743586Seric 	extern char *rindex();
744588Seric 	extern bool safepath();
745587Seric 	extern bool isdir();
746587Seric 	register char *q;
747148Seric 
748586Seric 	p = rindex(name, '/');
749586Seric 	if (p == NULL)
750586Seric 		p = name;
751586Seric 	else
752586Seric 		p++;
753586Seric 
754148Seric 	/*
755588Seric 	**  Check to see that the path is "safe", i.e., that we
756588Seric 	**  are not letting some nasty person use the setuid part
757588Seric 	**  of this program to look at or munge some presumably
758588Seric 	**  hidden files.
759148Seric 	*/
760148Seric 
761588Seric 	if (SccsDir[0] == '/' && !safepath(name))
762588Seric 		return (NULL);
763586Seric 
764586Seric 	/*
765588Seric 	**  Create the base pathname.
766586Seric 	*/
767586Seric 
7681438Seric 	/* first the directory part */
769588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
770148Seric 	{
77110110Srrh 		gstrcpy(buf, SccsDir, sizeof(buf));
77210110Srrh 		gstrcat(buf, "/", sizeof(buf));
773586Seric 	}
774586Seric 	else
77510110Srrh 		gstrcpy(buf, "", sizeof(buf));
7761438Seric 
7771438Seric 	/* then the head of the pathname */
77810110Srrh 	gstrncat(buf, name, p - name, sizeof(buf));
779587Seric 	q = &buf[strlen(buf)];
7801438Seric 
7811438Seric 	/* now copy the final part of the name, in case useful */
78210110Srrh 	gstrcpy(q, p, sizeof(buf));
7831438Seric 
7841438Seric 	/* so is it useful? */
785587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
786586Seric 	{
7871438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
78810110Srrh 		gstrcpy(q, SccsPath, sizeof(buf));
78910110Srrh 		gstrcat(buf, "/s.", sizeof(buf));
7901438Seric 
7911438Seric 		/* and now the end of the name */
79210110Srrh 		gstrcat(buf, p, sizeof(buf));
793586Seric 	}
794148Seric 
7951438Seric 	/* if i haven't changed it, why did I do all this? */
796588Seric 	if (strcmp(buf, name) == 0)
797588Seric 		p = name;
798588Seric 	else
799148Seric 	{
8001438Seric 		/* but if I have, squirrel it away */
801588Seric 		p = malloc(strlen(buf) + 1);
802588Seric 		if (p == NULL)
803588Seric 		{
804588Seric 			perror("Sccs: no mem");
805588Seric 			exit(EX_OSERR);
806588Seric 		}
807588Seric 		strcpy(p, buf);
808148Seric 	}
8091438Seric 
810148Seric 	return (p);
811148Seric }
8121432Seric 
8131432Seric /*
814587Seric **  ISDIR -- return true if the argument is a directory.
815587Seric **
816587Seric **	Parameters:
817587Seric **		name -- the pathname of the file to check.
818587Seric **
819587Seric **	Returns:
820587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
821587Seric **
822587Seric **	Side Effects:
823587Seric **		none.
824587Seric */
825587Seric 
826587Seric bool
827587Seric isdir(name)
828587Seric 	char *name;
829587Seric {
830587Seric 	struct stat stbuf;
831587Seric 
832587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
833587Seric }
8341432Seric 
8351432Seric /*
836586Seric **  SAFEPATH -- determine whether a pathname is "safe"
837586Seric **
838586Seric **	"Safe" pathnames only allow you to get deeper into the
839586Seric **	directory structure, i.e., full pathnames and ".." are
840586Seric **	not allowed.
841586Seric **
842586Seric **	Parameters:
843586Seric **		p -- the name to check.
844586Seric **
845586Seric **	Returns:
846586Seric **		TRUE -- if the path is safe.
847586Seric **		FALSE -- if the path is not safe.
848586Seric **
849586Seric **	Side Effects:
850586Seric **		Prints a message if the path is not safe.
851586Seric */
852586Seric 
853586Seric bool
854586Seric safepath(p)
855586Seric 	register char *p;
856586Seric {
857586Seric 	extern char *index();
858586Seric 
859586Seric 	if (*p != '/')
860586Seric 	{
861586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
862586Seric 		{
863586Seric 			p = index(p, '/');
864586Seric 			if (p == NULL)
865586Seric 				return (TRUE);
866586Seric 			p++;
867586Seric 		}
868586Seric 	}
869586Seric 
870586Seric 	printf("You may not use full pathnames or \"..\"\n");
871586Seric 	return (FALSE);
872586Seric }
8731432Seric 
8741432Seric /*
875261Seric **  CLEAN -- clean out recreatable files
876261Seric **
877261Seric **	Any file for which an "s." file exists but no "p." file
878261Seric **	exists in the current directory is purged.
879261Seric **
880261Seric **	Parameters:
8811822Seric **		mode -- tells whether this came from a "clean", "info", or
8821822Seric **			"check" command.
8831822Seric **		argv -- the rest of the argument vector.
884261Seric **
885261Seric **	Returns:
886261Seric **		none.
887261Seric **
888261Seric **	Side Effects:
889819Seric **		Removes files in the current directory.
890819Seric **		Prints information regarding files being edited.
891819Seric **		Exits if a "check" command.
892261Seric */
893261Seric 
8941822Seric clean(mode, argv)
895819Seric 	int mode;
8961822Seric 	char **argv;
897261Seric {
8986784Smckusick 	struct direct *dir;
89910110Srrh 	char buf[FBUFSIZ];
9002140Seric 	char *bufend;
9016784Smckusick 	register DIR *dirfd;
902346Seric 	register char *basefile;
903351Seric 	bool gotedit;
9041822Seric 	bool gotpfent;
905394Seric 	FILE *pfp;
9061822Seric 	bool nobranch = FALSE;
9071822Seric 	extern struct pfile *getpfent();
9081822Seric 	register struct pfile *pf;
9091822Seric 	register char **ap;
9101864Seric 	extern char *username();
9111864Seric 	char *usernm = NULL;
9122140Seric 	char *subdir = NULL;
9132140Seric 	char *cmdname;
914261Seric 
9151438Seric 	/*
9161822Seric 	**  Process the argv
9171822Seric 	*/
9181822Seric 
9192140Seric 	cmdname = *argv;
9202140Seric 	for (ap = argv; *++ap != NULL; )
9211822Seric 	{
9221864Seric 		if (**ap == '-')
9231864Seric 		{
9241864Seric 			/* we have a flag */
9251864Seric 			switch ((*ap)[1])
9261864Seric 			{
9271864Seric 			  case 'b':
9281864Seric 				nobranch = TRUE;
9291864Seric 				break;
9301864Seric 
9311864Seric 			  case 'u':
9321864Seric 				if ((*ap)[2] != '\0')
9331864Seric 					usernm = &(*ap)[2];
9341864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9351864Seric 					usernm = *++ap;
9361864Seric 				else
9371864Seric 					usernm = username();
9381864Seric 				break;
9391864Seric 			}
9401864Seric 		}
9412140Seric 		else
9422140Seric 		{
9432140Seric 			if (subdir != NULL)
9442140Seric 				usrerr("too many args");
9452140Seric 			else
9462140Seric 				subdir = *ap;
9472140Seric 		}
9481822Seric 	}
9491822Seric 
9501822Seric 	/*
9511438Seric 	**  Find and open the SCCS directory.
9521438Seric 	*/
9531438Seric 
95410110Srrh 	gstrcpy(buf, SccsDir, sizeof(buf));
9551207Seric 	if (buf[0] != '\0')
95610110Srrh 		gstrcat(buf, "/", sizeof(buf));
9572140Seric 	if (subdir != NULL)
9582140Seric 	{
95910110Srrh 		gstrcat(buf, subdir, sizeof(buf));
96010110Srrh 		gstrcat(buf, "/", sizeof(buf));
9612140Seric 	}
96210110Srrh 	gstrcat(buf, SccsPath, sizeof(buf));
9632140Seric 	bufend = &buf[strlen(buf)];
9641438Seric 
9656784Smckusick 	dirfd = opendir(buf);
966261Seric 	if (dirfd == NULL)
967261Seric 	{
9681207Seric 		usrerr("cannot open %s", buf);
9691282Seric 		return (EX_NOINPUT);
970261Seric 	}
971261Seric 
972261Seric 	/*
973261Seric 	**  Scan the SCCS directory looking for s. files.
9741438Seric 	**	gotedit tells whether we have tried to clean any
9751438Seric 	**		files that are being edited.
976261Seric 	*/
977261Seric 
978351Seric 	gotedit = FALSE;
9796784Smckusick 	while (dir = readdir(dirfd)) {
9806784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
981261Seric 			continue;
982261Seric 
983261Seric 		/* got an s. file -- see if the p. file exists */
98410110Srrh 		gstrcpy(bufend, "/p.", sizeof(buf));
9852140Seric 		basefile = bufend + 3;
98610110Srrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
9871822Seric 
9881822Seric 		/*
9891822Seric 		**  open and scan the p-file.
9901822Seric 		**	'gotpfent' tells if we have found a valid p-file
9911822Seric 		**		entry.
9921822Seric 		*/
9931822Seric 
994394Seric 		pfp = fopen(buf, "r");
9951822Seric 		gotpfent = FALSE;
996394Seric 		if (pfp != NULL)
997346Seric 		{
9981438Seric 			/* the file exists -- report it's contents */
9991822Seric 			while ((pf = getpfent(pfp)) != NULL)
10001730Seric 			{
10011822Seric 				if (nobranch && isbranch(pf->p_nsid))
10021822Seric 					continue;
10031864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10041864Seric 					continue;
10051822Seric 				gotedit = TRUE;
10061822Seric 				gotpfent = TRUE;
10071822Seric 				if (mode == TELLC)
10081822Seric 				{
10091822Seric 					printf("%s\n", basefile);
10101822Seric 					break;
10111822Seric 				}
10122161Seric 				printf("%12s: being edited: ", basefile);
10132161Seric 				putpfent(pf, stdout);
10141730Seric 			}
1015394Seric 			fclose(pfp);
10161822Seric 		}
1017261Seric 
1018261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10191870Seric 		if (mode == CLEANC && !gotpfent)
1020346Seric 		{
102110110Srrh 			char	unlinkbuf[FBUFSIZ];
102210110Srrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
102310103Srrh 			unlink(unlinkbuf);
1024346Seric 		}
1025261Seric 	}
1026261Seric 
10271438Seric 	/* cleanup & report results */
10286784Smckusick 	closedir(dirfd);
1029819Seric 	if (!gotedit && mode == INFOC)
10301864Seric 	{
10311864Seric 		printf("Nothing being edited");
10321864Seric 		if (nobranch)
10331864Seric 			printf(" (on trunk)");
10341864Seric 		if (usernm == NULL)
10351864Seric 			printf("\n");
10361864Seric 		else
10371864Seric 			printf(" by %s\n", usernm);
10381864Seric 	}
1039819Seric 	if (mode == CHECKC)
1040819Seric 		exit(gotedit);
10411282Seric 	return (EX_OK);
1042261Seric }
10431432Seric 
10441432Seric /*
10451822Seric **  ISBRANCH -- is the SID a branch?
10461822Seric **
10471822Seric **	Parameters:
10481822Seric **		sid -- the sid to check.
10491822Seric **
10501822Seric **	Returns:
10511822Seric **		TRUE if the sid represents a branch.
10521822Seric **		FALSE otherwise.
10531822Seric **
10541822Seric **	Side Effects:
10551822Seric **		none.
10561822Seric */
10571822Seric 
10581822Seric isbranch(sid)
10591822Seric 	char *sid;
10601822Seric {
10611822Seric 	register char *p;
10621822Seric 	int dots;
10631822Seric 
10641822Seric 	dots = 0;
10651822Seric 	for (p = sid; *p != '\0'; p++)
10661822Seric 	{
10671822Seric 		if (*p == '.')
10681822Seric 			dots++;
10691822Seric 		if (dots > 1)
10701822Seric 			return (TRUE);
10711822Seric 	}
10721822Seric 	return (FALSE);
10731822Seric }
10741822Seric 
10751822Seric /*
1076396Seric **  UNEDIT -- unedit a file
1077396Seric **
1078396Seric **	Checks to see that the current user is actually editting
1079396Seric **	the file and arranges that s/he is not editting it.
1080396Seric **
1081396Seric **	Parameters:
1082416Seric **		fn -- the name of the file to be unedited.
1083396Seric **
1084396Seric **	Returns:
1085585Seric **		TRUE -- if the file was successfully unedited.
1086585Seric **		FALSE -- if the file was not unedited for some
1087585Seric **			reason.
1088396Seric **
1089396Seric **	Side Effects:
1090396Seric **		fn is removed
1091396Seric **		entries are removed from pfile.
1092396Seric */
1093396Seric 
1094585Seric bool
1095396Seric unedit(fn)
1096396Seric 	char *fn;
1097396Seric {
1098396Seric 	register FILE *pfp;
1099396Seric 	char *pfn;
1100396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1101396Seric 	FILE *tfp;
1102396Seric 	register char *q;
1103396Seric 	bool delete = FALSE;
1104396Seric 	bool others = FALSE;
1105396Seric 	char *myname;
11061864Seric 	extern char *username();
1107396Seric 	struct pfile *pent;
11081822Seric 	extern struct pfile *getpfent();
110910110Srrh 	char buf[PFILELG];
11101316Seric 	extern char *makefile();
1111396Seric 
1112396Seric 	/* make "s." filename & find the trailing component */
1113396Seric 	pfn = makefile(fn);
1114586Seric 	if (pfn == NULL)
1115586Seric 		return (FALSE);
1116586Seric 	q = rindex(pfn, '/');
1117586Seric 	if (q == NULL)
1118586Seric 		q = &pfn[-1];
1119586Seric 	if (q[1] != 's' || q[2] != '.')
1120396Seric 	{
11211205Seric 		usrerr("bad file name \"%s\"", fn);
1122585Seric 		return (FALSE);
1123396Seric 	}
1124396Seric 
11251438Seric 	/* turn "s." into "p." & try to open it */
1126396Seric 	*++q = 'p';
1127396Seric 
1128396Seric 	pfp = fopen(pfn, "r");
1129396Seric 	if (pfp == NULL)
1130396Seric 	{
1131416Seric 		printf("%12s: not being edited\n", fn);
1132585Seric 		return (FALSE);
1133396Seric 	}
1134396Seric 
11351438Seric 	/* create temp file for editing p-file */
1136396Seric 	mktemp(tfn);
1137396Seric 	tfp = fopen(tfn, "w");
1138396Seric 	if (tfp == NULL)
1139396Seric 	{
11401205Seric 		usrerr("cannot create \"%s\"", tfn);
1141396Seric 		exit(EX_OSERR);
1142396Seric 	}
1143396Seric 
11441438Seric 	/* figure out who I am */
11451864Seric 	myname = username();
11461438Seric 
11471438Seric 	/*
11481438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11491438Seric 	*/
11501438Seric 
11511822Seric 	while ((pent = getpfent(pfp)) != NULL)
1152396Seric 	{
1153396Seric 		if (strcmp(pent->p_user, myname) == 0)
1154396Seric 		{
1155396Seric 			/* a match */
1156396Seric 			delete++;
1157396Seric 		}
1158396Seric 		else
1159396Seric 		{
11601438Seric 			/* output it again */
11612161Seric 			putpfent(pent, tfp);
1162396Seric 			others++;
1163396Seric 		}
1164396Seric 	}
1165396Seric 
1166396Seric 	/* do final cleanup */
1167396Seric 	if (others)
1168396Seric 	{
11691438Seric 		/* copy it back (perhaps it should be linked?) */
1170396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1171396Seric 		{
11721205Seric 			syserr("cannot reopen \"%s\"", tfn);
1173396Seric 			exit(EX_OSERR);
1174396Seric 		}
1175396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1176396Seric 		{
11771205Seric 			usrerr("cannot create \"%s\"", pfn);
1178585Seric 			return (FALSE);
1179396Seric 		}
1180396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1181396Seric 			fputs(buf, pfp);
1182396Seric 	}
1183396Seric 	else
1184396Seric 	{
11851438Seric 		/* it's empty -- remove it */
1186396Seric 		unlink(pfn);
1187396Seric 	}
1188396Seric 	fclose(tfp);
1189396Seric 	fclose(pfp);
1190396Seric 	unlink(tfn);
1191396Seric 
11921438Seric 	/* actually remove the g-file */
1193396Seric 	if (delete)
1194396Seric 	{
11951435Seric 		unlink(tail(fn));
11961435Seric 		printf("%12s: removed\n", tail(fn));
1197585Seric 		return (TRUE);
1198396Seric 	}
1199396Seric 	else
1200396Seric 	{
1201416Seric 		printf("%12s: not being edited by you\n", fn);
1202585Seric 		return (FALSE);
1203396Seric 	}
1204396Seric }
12051432Seric 
12061432Seric /*
12071433Seric **  DODIFF -- diff an s-file against a g-file
12081433Seric **
12091433Seric **	Parameters:
12101433Seric **		getv -- argv for the 'get' command.
12111433Seric **		gfile -- name of the g-file to diff against.
12121433Seric **
12131433Seric **	Returns:
12141433Seric **		Result of get.
12151433Seric **
12161433Seric **	Side Effects:
12171433Seric **		none.
12181433Seric */
12191433Seric 
12201433Seric dodiff(getv, gfile)
12211433Seric 	char **getv;
12221433Seric 	char *gfile;
12231433Seric {
12241433Seric 	int pipev[2];
12251433Seric 	int rval;
12261433Seric 	register int i;
12271433Seric 	register int pid;
12281433Seric 	auto int st;
12291433Seric 	extern int errno;
12301433Seric 	int (*osig)();
12311433Seric 
12321905Seric 	printf("\n------- %s -------\n", gfile);
12331871Seric 	fflush(stdout);
12341501Seric 
12351438Seric 	/* create context for diff to run in */
12361433Seric 	if (pipe(pipev) < 0)
12371433Seric 	{
12381433Seric 		syserr("dodiff: pipe failed");
12391433Seric 		exit(EX_OSERR);
12401433Seric 	}
12411433Seric 	if ((pid = fork()) < 0)
12421433Seric 	{
12431433Seric 		syserr("dodiff: fork failed");
12441433Seric 		exit(EX_OSERR);
12451433Seric 	}
12461433Seric 	else if (pid > 0)
12471433Seric 	{
12481433Seric 		/* in parent; run get */
12491433Seric 		OutFile = pipev[1];
12501433Seric 		close(pipev[0]);
12511871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12521433Seric 		osig = signal(SIGINT, SIG_IGN);
12531433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
12541433Seric 			errno = 0;
12551433Seric 		signal(SIGINT, osig);
12561433Seric 		/* ignore result of diff */
12571433Seric 	}
12581433Seric 	else
12591433Seric 	{
12601433Seric 		/* in child, run diff */
12611433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
12621433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
12631433Seric 		{
12641433Seric 			syserr("dodiff: magic failed");
12651433Seric 			exit(EX_OSERR);
12661433Seric 		}
12671871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
12681433Seric 	}
12691433Seric 	return (rval);
12701433Seric }
12711433Seric 
12721433Seric /*
12731435Seric **  TAIL -- return tail of filename.
12741435Seric **
12751435Seric **	Parameters:
12761435Seric **		fn -- the filename.
12771435Seric **
12781435Seric **	Returns:
12791435Seric **		a pointer to the tail of the filename; e.g., given
12801435Seric **		"cmd/ls.c", "ls.c" is returned.
12811435Seric **
12821435Seric **	Side Effects:
12831435Seric **		none.
12841435Seric */
12851435Seric 
12861435Seric char *
12871435Seric tail(fn)
12881435Seric 	register char *fn;
12891435Seric {
12901435Seric 	register char *p;
12911435Seric 
12921435Seric 	for (p = fn; *p != 0; p++)
12931435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
12941435Seric 			fn = &p[1];
12951435Seric 	return (fn);
12961435Seric }
12971435Seric 
12981435Seric /*
12991822Seric **  GETPFENT -- get an entry from the p-file
1300396Seric **
1301396Seric **	Parameters:
1302396Seric **		pfp -- p-file file pointer
1303396Seric **
1304396Seric **	Returns:
1305396Seric **		pointer to p-file struct for next entry
1306396Seric **		NULL on EOF or error
1307396Seric **
1308396Seric **	Side Effects:
1309396Seric **		Each call wipes out results of previous call.
1310396Seric */
1311396Seric 
1312396Seric struct pfile *
13131822Seric getpfent(pfp)
1314396Seric 	FILE *pfp;
1315396Seric {
1316396Seric 	static struct pfile ent;
131710110Srrh 	static char buf[PFILELG];
1318396Seric 	register char *p;
1319396Seric 	extern char *nextfield();
1320396Seric 
1321396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1322396Seric 		return (NULL);
1323396Seric 
1324396Seric 	ent.p_osid = p = buf;
1325396Seric 	ent.p_nsid = p = nextfield(p);
1326396Seric 	ent.p_user = p = nextfield(p);
1327396Seric 	ent.p_date = p = nextfield(p);
1328396Seric 	ent.p_time = p = nextfield(p);
13292161Seric 	ent.p_aux = p = nextfield(p);
1330396Seric 
1331396Seric 	return (&ent);
1332396Seric }
1333396Seric 
1334396Seric 
1335396Seric char *
1336396Seric nextfield(p)
1337396Seric 	register char *p;
1338396Seric {
1339396Seric 	if (p == NULL || *p == '\0')
1340396Seric 		return (NULL);
1341396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1342396Seric 		p++;
1343396Seric 	if (*p == '\n' || *p == '\0')
1344396Seric 	{
1345396Seric 		*p = '\0';
1346396Seric 		return (NULL);
1347396Seric 	}
1348396Seric 	*p++ = '\0';
1349396Seric 	return (p);
1350396Seric }
13512161Seric /*
13522161Seric **  PUTPFENT -- output a p-file entry to a file
13532161Seric **
13542161Seric **	Parameters:
13552161Seric **		pf -- the p-file entry
13562161Seric **		f -- the file to put it on.
13572161Seric **
13582161Seric **	Returns:
13592161Seric **		none.
13602161Seric **
13612161Seric **	Side Effects:
13622161Seric **		pf is written onto file f.
13632161Seric */
13642161Seric 
13652161Seric putpfent(pf, f)
13662161Seric 	register struct pfile *pf;
13672161Seric 	register FILE *f;
13682161Seric {
13692161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
13702161Seric 		pf->p_user, pf->p_date, pf->p_time);
13712161Seric 	if (pf->p_aux != NULL)
13722161Seric 		fprintf(f, " %s", pf->p_aux);
13732161Seric 	else
13742161Seric 		fprintf(f, "\n");
13752161Seric }
13761432Seric 
13771432Seric /*
13781205Seric **  USRERR -- issue user-level error
13791205Seric **
13801205Seric **	Parameters:
13811205Seric **		f -- format string.
13821205Seric **		p1-p3 -- parameters to a printf.
13831205Seric **
13841205Seric **	Returns:
13851205Seric **		-1
13861205Seric **
13871205Seric **	Side Effects:
13881205Seric **		none.
13891205Seric */
13901205Seric 
13911738Seric /*VARARGS1*/
13921205Seric usrerr(f, p1, p2, p3)
13931205Seric 	char *f;
13941205Seric {
13951205Seric 	fprintf(stderr, "\n%s: ", MyName);
13961205Seric 	fprintf(stderr, f, p1, p2, p3);
13971205Seric 	fprintf(stderr, "\n");
13981205Seric 
13991205Seric 	return (-1);
14001205Seric }
14011432Seric 
14021432Seric /*
14031205Seric **  SYSERR -- print system-generated error.
14041205Seric **
14051205Seric **	Parameters:
14061205Seric **		f -- format string to a printf.
14071205Seric **		p1, p2, p3 -- parameters to f.
14081205Seric **
14091205Seric **	Returns:
14101205Seric **		never.
14111205Seric **
14121205Seric **	Side Effects:
14131205Seric **		none.
14141205Seric */
14151205Seric 
14161738Seric /*VARARGS1*/
14171205Seric syserr(f, p1, p2, p3)
14181205Seric 	char *f;
14191205Seric {
14201205Seric 	extern int errno;
14211205Seric 
14221205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
14231205Seric 	fprintf(stderr, f, p1, p2, p3);
14241205Seric 	fprintf(stderr, "\n");
14251205Seric 	if (errno == 0)
14261205Seric 		exit(EX_SOFTWARE);
14271205Seric 	else
14281205Seric 	{
14291738Seric 		perror(NULL);
14301205Seric 		exit(EX_OSERR);
14311205Seric 	}
14321205Seric }
14331864Seric /*
14341864Seric **  USERNAME -- return name of the current user
14351864Seric **
14361864Seric **	Parameters:
14371864Seric **		none
14381864Seric **
14391864Seric **	Returns:
14401864Seric **		name of current user
14411864Seric **
14421864Seric **	Side Effects:
14431864Seric **		none
14441864Seric */
14451864Seric 
14461864Seric char *
14471864Seric username()
14481864Seric {
14491864Seric # ifdef UIDUSER
14501864Seric 	extern struct passwd *getpwuid();
14511864Seric 	register struct passwd *pw;
14521864Seric 
14531864Seric 	pw = getpwuid(getuid());
14541864Seric 	if (pw == NULL)
14551864Seric 	{
14561864Seric 		syserr("who are you? (uid=%d)", getuid());
14571864Seric 		exit(EX_OSERR);
14581864Seric 	}
14591864Seric 	return (pw->pw_name);
14601864Seric # else
14611905Seric 	extern char *getlogin();
14626785Smckusick 	extern char *getenv();
14636785Smckusick 	register char *p;
14641905Seric 
14656785Smckusick 	p = getenv("USER");
14666785Smckusick 	if (p == NULL || p[0] == '\0')
14676785Smckusick 		p = getlogin();
14686785Smckusick 	return (p);
14691864Seric # endif UIDUSER
14701864Seric }
147110110Srrh 
147210110Srrh /*
147310110Srrh **	Guarded string manipulation routines; the last argument
147410110Srrh **	is the length of the buffer into which the strcpy or strcat
147510110Srrh **	is to be done.
147610110Srrh */
147710110Srrh char *gstrcat(to, from, length)
147810110Srrh 	char	*to, *from;
147910110Srrh 	int	length;
148010110Srrh {
148110110Srrh 	if (strlen(from) + strlen(to) >= length) {
148210110Srrh 		gstrbotch(to, from);
148310110Srrh 	}
148410110Srrh 	return(strcat(to, from));
148510110Srrh }
148610110Srrh 
148710110Srrh char *gstrncat(to, from, n, length)
148810110Srrh 	char	*to, *from;
148910110Srrh 	int	n;
149010110Srrh 	int	length;
149110110Srrh {
149210110Srrh 	if (n + strlen(to) >= length) {
149310110Srrh 		gstrbotch(to, from);
149410110Srrh 	}
149510110Srrh 	return(strncat(to, from, n));
149610110Srrh }
149710110Srrh 
149810110Srrh char *gstrcpy(to, from, length)
149910110Srrh 	char	*to, *from;
150010110Srrh 	int	length;
150110110Srrh {
150210110Srrh 	if (strlen(from) >= length) {
150310110Srrh 		gstrbotch(from, (char *)0);
150410110Srrh 	}
150510110Srrh 	return(strcpy(to, from));
150610110Srrh }
150710110Srrh gstrbotch(str1, str2)
150810110Srrh 	char	*str1, *str2;
150910110Srrh {
151010110Srrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
151110110Srrh }
1512