xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 13622)
1*13622Ssam #ifndef lint
2*13622Ssam static char sccsid[] = "@(#)sccs.c	1.71 (Berkeley) 07/02/83";
3*13622Ssam #endif
4*13622Ssam 
5148Seric # include <stdio.h>
66784Smckusick # include <sys/param.h>
7148Seric # include <sys/stat.h>
8*13622Ssam # include <sys/dir.h>
91433Seric # include <errno.h>
101433Seric # include <signal.h>
11148Seric # include <sysexits.h>
122140Seric # include <pwd.h>
13148Seric 
14828Seric /*
15828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
16828Seric **
17828Seric **	Without trying to add any functionality to speak of, this
18828Seric **	program tries to make SCCS a little more accessible to human
19828Seric **	types.  The main thing it does is automatically put the
20828Seric **	string "SCCS/s." on the front of names.  Also, it has a
21828Seric **	couple of things that are designed to shorten frequent
22828Seric **	combinations, e.g., "delget" which expands to a "delta"
23828Seric **	and a "get".
24828Seric **
25828Seric **	This program can also function as a setuid front end.
26828Seric **	To do this, you should copy the source, renaming it to
27828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
28828Seric **	in the program (e.g., syssccs might default -d to
29828Seric **	"/usr/src/sys").  Then recompile and put the result
30828Seric **	as setuid to whomever you want.  In this mode, sccs
31828Seric **	knows to not run setuid for certain programs in order
32828Seric **	to preserve security, and so forth.
33828Seric **
34828Seric **	Usage:
35828Seric **		sccs [flags] command [args]
36828Seric **
37828Seric **	Flags:
38828Seric **		-d<dir>		<dir> represents a directory to search
39828Seric **				out of.  It should be a full pathname
40828Seric **				for general usage.  E.g., if <dir> is
41828Seric **				"/usr/src/sys", then a reference to the
42828Seric **				file "dev/bio.c" becomes a reference to
43828Seric **				"/usr/src/sys/dev/bio.c".
44828Seric **		-p<path>	prepends <path> to the final component
45828Seric **				of the pathname.  By default, this is
46828Seric **				"SCCS".  For example, in the -d example
47828Seric **				above, the path then gets modified to
48828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
49828Seric **				more common usage (without the -d flag),
50828Seric **				"prog.c" would get modified to
51828Seric **				"SCCS/s.prog.c".  In both cases, the
52828Seric **				"s." gets automatically prepended.
53828Seric **		-r		run as the real user.
54828Seric **
55828Seric **	Commands:
56828Seric **		admin,
57828Seric **		get,
58828Seric **		delta,
59828Seric **		rmdel,
60828Seric **		chghist,
61828Seric **		etc.		Straight out of SCCS; only difference
62828Seric **				is that pathnames get modified as
63828Seric **				described above.
64828Seric **		edit		Macro for "get -e".
65828Seric **		unedit		Removes a file being edited, knowing
66828Seric **				about p-files, etc.
67828Seric **		delget		Macro for "delta" followed by "get".
68828Seric **		deledit		Macro for "delta" followed by "get -e".
69828Seric **		info		Tell what files being edited.
70828Seric **		clean		Remove all files that can be
71828Seric **				regenerated from SCCS files.
721205Seric **		check		Like info, but return exit status, for
73828Seric **				use in makefiles.
74828Seric **		fix		Remove a top delta & reedit, but save
75828Seric **				the previous changes in that delta.
76828Seric **
77828Seric **	Compilation Flags:
78828Seric **		UIDUSER -- determine who the user is by looking at the
79828Seric **			uid rather than the login name -- for machines
80828Seric **			where SCCS gets the user in this way.
811270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
821205Seric **			this value.  This is so that the setuid
831205Seric **			aspects of this program cannot be abused.
841270Seric **			This flag also disables the -p flag.
851270Seric **		SCCSPATH -- the default for the -p flag.
861437Seric **		MYNAME -- the title this program should print when it
871437Seric **			gives error messages.
88828Seric **
89828Seric **	Compilation Instructions:
90828Seric **		cc -O -n -s sccs.c
911437Seric **		The flags listed above can be -D defined to simplify
921437Seric **			recompilation for variant versions.
93828Seric **
94828Seric **	Author:
95828Seric **		Eric Allman, UCB/INGRES
961270Seric **		Copyright 1980 Regents of the University of California
97828Seric */
98155Seric 
991432Seric 
1001270Seric /*******************  Configuration Information  ********************/
1011270Seric 
1021437Seric # ifndef SCCSPATH
1031432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1041437Seric # endif NOT SCCSPATH
105828Seric 
1061437Seric # ifndef MYNAME
1071437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1081437Seric # endif NOT MYNAME
1091270Seric 
1101432Seric # ifndef PROGPATH
1116784Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1121432Seric # endif PROGPATH
1131432Seric 
1141270Seric /****************  End of Configuration Information  ****************/
1151432Seric 
116157Seric typedef char	bool;
117200Seric # define TRUE	1
118200Seric # define FALSE	0
119157Seric 
1201438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1211438Seric 
122148Seric struct sccsprog
123148Seric {
124148Seric 	char	*sccsname;	/* name of SCCS routine */
125200Seric 	short	sccsoper;	/* opcode, see below */
126200Seric 	short	sccsflags;	/* flags, see below */
127148Seric 	char	*sccspath;	/* pathname of binary implementing */
128148Seric };
129148Seric 
130200Seric /* values for sccsoper */
131200Seric # define PROG		0	/* call a program */
132201Seric # define CMACRO		1	/* command substitution macro */
133226Seric # define FIX		2	/* fix a delta */
134261Seric # define CLEAN		3	/* clean out recreatable files */
135396Seric # define UNEDIT		4	/* unedit a file */
1361431Seric # define SHELL		5	/* call a shell file (like PROG) */
1371433Seric # define DIFFS		6	/* diff between sccs & file out */
1381871Seric # define DODIFF		7	/* internal call to diff program */
13910104Srrh # define ENTER		8	/* enter new files */
140200Seric 
141157Seric /* bits for sccsflags */
142200Seric # define NO_SDOT	0001	/* no s. on front of args */
143200Seric # define REALUSER	0002	/* protected (e.g., admin) */
144148Seric 
145819Seric /* modes for the "clean", "info", "check" ops */
146819Seric # define CLEANC		0	/* clean command */
147819Seric # define INFOC		1	/* info command */
148819Seric # define CHECKC		2	/* check command */
1491730Seric # define TELLC		3	/* give list of files being edited */
150819Seric 
1511432Seric /*
1521432Seric **  Description of commands known to this program.
1531432Seric **	First argument puts the command into a class.  Second arg is
1541432Seric **	info regarding treatment of this command.  Third arg is a
1551432Seric **	list of flags this command accepts from macros, etc.  Fourth
1561432Seric **	arg is the pathname of the implementing program, or the
1571432Seric **	macro definition, or the arg to a sub-algorithm.
1581432Seric */
159202Seric 
160148Seric struct sccsprog SccsProg[] =
161148Seric {
1621864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1631864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1641864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1651864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1661864Seric 	"get",		PROG,	0,			PROGPATH(get),
1671864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1682136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1691864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1701864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1712136Seric 	"val",		PROG,	0,			PROGPATH(val),
1721864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1731864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1741864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1751864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1762138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1771864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1781864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1791864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1801864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1811864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1821864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1831864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1841871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
1852137Seric 	"print",	CMACRO,	0,			"prt -e/get -p -m -s",
1862226Seric 	"branch",	CMACRO,	NO_SDOT,
1872226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
18810104Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
18910104Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
1901864Seric 	NULL,		-1,	0,			NULL
191148Seric };
192148Seric 
1931432Seric /* one line from a p-file */
194396Seric struct pfile
195396Seric {
196396Seric 	char	*p_osid;	/* old SID */
197396Seric 	char	*p_nsid;	/* new SID */
198396Seric 	char	*p_user;	/* user who did edit */
199396Seric 	char	*p_date;	/* date of get */
200396Seric 	char	*p_time;	/* time of get */
2012161Seric 	char	*p_aux;		/* extra info at end */
202396Seric };
203396Seric 
2041270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2051270Seric # ifdef SCCSDIR
2061270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2071205Seric # else
2081270Seric char	*SccsDir = "";
2091205Seric # endif
2101437Seric char	MyName[] = MYNAME;	/* name used in messages */
2111433Seric int	OutFile = -1;		/* override output file for commands */
212157Seric bool	RealUser;		/* if set, running as real user */
213393Seric # ifdef DEBUG
214393Seric bool	Debug;			/* turn on tracing */
215393Seric # endif
2162139Seric # ifndef V6
2172139Seric extern char	*getenv();
2182139Seric # endif V6
21910110Srrh 
22010110Srrh char *gstrcat(), *strcat();
22110110Srrh char *gstrncat(), *strncat();
22210110Srrh char *gstrcpy(), *strcpy();
22310110Srrh #define	FBUFSIZ	BUFSIZ
22410110Srrh #define	PFILELG	120
2251432Seric 
226148Seric main(argc, argv)
227148Seric 	int argc;
228148Seric 	char **argv;
229148Seric {
230148Seric 	register char *p;
231262Seric 	extern struct sccsprog *lookup();
2321282Seric 	register int i;
2332139Seric # ifndef V6
2342139Seric # ifndef SCCSDIR
2352140Seric 	register struct passwd *pw;
2362140Seric 	extern struct passwd *getpwnam();
23710110Srrh 	char buf[FBUFSIZ];
2382140Seric 
2392139Seric 	/* pull "SccsDir" out of the environment (possibly) */
24010260Seric 	p = getenv("PROJECTDIR");
2412140Seric 	if (p != NULL && p[0] != '\0')
2422140Seric 	{
2432140Seric 		if (p[0] == '/')
2442140Seric 			SccsDir = p;
2452140Seric 		else
2462140Seric 		{
2472140Seric 			pw = getpwnam(p);
2482140Seric 			if (pw == NULL)
2492140Seric 			{
2502140Seric 				usrerr("user %s does not exist", p);
2512140Seric 				exit(EX_USAGE);
2522140Seric 			}
25310110Srrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
25410110Srrh 			gstrcat(buf, "/src", sizeof(buf));
2552140Seric 			if (access(buf, 0) < 0)
2562140Seric 			{
25710110Srrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
25810110Srrh 				gstrcat(buf, "/source", sizeof(buf));
2592140Seric 				if (access(buf, 0) < 0)
2602140Seric 				{
2612140Seric 					usrerr("project %s has no source!", p);
2622140Seric 					exit(EX_USAGE);
2632140Seric 				}
2642140Seric 			}
2652140Seric 			SccsDir = buf;
2662140Seric 		}
2672140Seric 	}
2682139Seric # endif SCCSDIR
2692139Seric # endif V6
2702139Seric 
271148Seric 	/*
272148Seric 	**  Detect and decode flags intended for this program.
273148Seric 	*/
274148Seric 
275200Seric 	if (argc < 2)
276148Seric 	{
2771205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
278200Seric 		exit(EX_USAGE);
279200Seric 	}
280200Seric 	argv[argc] = NULL;
281200Seric 
282262Seric 	if (lookup(argv[0]) == NULL)
283200Seric 	{
284262Seric 		while ((p = *++argv) != NULL)
285148Seric 		{
286262Seric 			if (*p != '-')
287262Seric 				break;
288262Seric 			switch (*++p)
289262Seric 			{
290262Seric 			  case 'r':		/* run as real user */
291262Seric 				setuid(getuid());
292262Seric 				RealUser++;
293262Seric 				break;
294148Seric 
2951270Seric # ifndef SCCSDIR
296262Seric 			  case 'p':		/* path of sccs files */
297262Seric 				SccsPath = ++p;
2982348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
2992348Seric 					SccsPath = *++argv;
300262Seric 				break;
301148Seric 
302588Seric 			  case 'd':		/* directory to search from */
303588Seric 				SccsDir = ++p;
3042348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3052348Seric 					SccsDir = *++argv;
306588Seric 				break;
3071205Seric # endif
308588Seric 
309393Seric # ifdef DEBUG
310393Seric 			  case 'T':		/* trace */
311393Seric 				Debug++;
312393Seric 				break;
313393Seric # endif
314393Seric 
315262Seric 			  default:
3161205Seric 				usrerr("unknown option -%s", p);
317262Seric 				break;
318262Seric 			}
319148Seric 		}
320262Seric 		if (SccsPath[0] == '\0')
321262Seric 			SccsPath = ".";
322148Seric 	}
323148Seric 
3241737Seric 	i = command(argv, FALSE, "");
3251282Seric 	exit(i);
326200Seric }
3271432Seric 
3281432Seric /*
3291282Seric **  COMMAND -- look up and perform a command
3301282Seric **
3311282Seric **	This routine is the guts of this program.  Given an
3321282Seric **	argument vector, it looks up the "command" (argv[0])
3331282Seric **	in the configuration table and does the necessary stuff.
3341282Seric **
3351282Seric **	Parameters:
3361282Seric **		argv -- an argument vector to process.
3371282Seric **		forkflag -- if set, fork before executing the command.
3381316Seric **		editflag -- if set, only include flags listed in the
3391316Seric **			sccsklets field of the command descriptor.
3401316Seric **		arg0 -- a space-seperated list of arguments to insert
3411316Seric **			before argv.
3421282Seric **
3431282Seric **	Returns:
3441282Seric **		zero -- command executed ok.
3451282Seric **		else -- error status.
3461282Seric **
3471282Seric **	Side Effects:
3481282Seric **		none.
3491282Seric */
350157Seric 
3511737Seric command(argv, forkflag, arg0)
352200Seric 	char **argv;
353201Seric 	bool forkflag;
3541316Seric 	char *arg0;
355200Seric {
356200Seric 	register struct sccsprog *cmd;
357200Seric 	register char *p;
35810110Srrh 	char buf[FBUFSIZ];
359262Seric 	extern struct sccsprog *lookup();
3601316Seric 	char *nav[1000];
3611316Seric 	char **np;
3621431Seric 	register char **ap;
363585Seric 	register int i;
3641431Seric 	register char *q;
365585Seric 	extern bool unedit();
3661282Seric 	int rval = 0;
3671316Seric 	extern char *index();
3681316Seric 	extern char *makefile();
3691737Seric 	char *editchs;
3701435Seric 	extern char *tail();
371200Seric 
372393Seric # ifdef DEBUG
373393Seric 	if (Debug)
374393Seric 	{
3751316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3761316Seric 		for (np = argv; *np != NULL; np++)
3771316Seric 			printf("\t\"%s\"\n", *np);
378393Seric 	}
379393Seric # endif
380393Seric 
381157Seric 	/*
3821316Seric 	**  Copy arguments.
3831438Seric 	**	Copy from arg0 & if necessary at most one arg
3841438Seric 	**	from argv[0].
3851316Seric 	*/
3861316Seric 
3871431Seric 	np = ap = &nav[1];
3881737Seric 	editchs = NULL;
3891821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3901316Seric 	{
3911316Seric 		*np++ = q;
3921316Seric 		while (*p == ' ')
3931316Seric 			p++;
3941737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3951316Seric 			*q++ = *p++;
3961316Seric 		*q++ = '\0';
3971737Seric 		if (*p == ':')
3981737Seric 		{
3991737Seric 			editchs = q;
4001821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4011737Seric 				*q++ = *p;
4021737Seric 			*q++ = '\0';
4031737Seric 		}
4041316Seric 	}
4051316Seric 	*np = NULL;
4061431Seric 	if (*ap == NULL)
4071316Seric 		*np++ = *argv++;
4081316Seric 
4091316Seric 	/*
410148Seric 	**  Look up command.
4111431Seric 	**	At this point, *ap is the command name.
412148Seric 	*/
413148Seric 
4141431Seric 	cmd = lookup(*ap);
415262Seric 	if (cmd == NULL)
416148Seric 	{
4171431Seric 		usrerr("Unknown command \"%s\"", *ap);
4181282Seric 		return (EX_USAGE);
419148Seric 	}
420148Seric 
421148Seric 	/*
4221316Seric 	**  Copy remaining arguments doing editing as appropriate.
4231316Seric 	*/
4241316Seric 
4251316Seric 	for (; *argv != NULL; argv++)
4261316Seric 	{
4271316Seric 		p = *argv;
4281316Seric 		if (*p == '-')
4291316Seric 		{
4301737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4311316Seric 				*np++ = p;
4321316Seric 		}
4331316Seric 		else
4341316Seric 		{
4351316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4361316Seric 				p = makefile(p);
4371316Seric 			if (p != NULL)
4381316Seric 				*np++ = p;
4391316Seric 		}
4401316Seric 	}
4411316Seric 	*np = NULL;
4421316Seric 
4431316Seric 	/*
444200Seric 	**  Interpret operation associated with this command.
445157Seric 	*/
446157Seric 
447200Seric 	switch (cmd->sccsoper)
448200Seric 	{
4491431Seric 	  case SHELL:		/* call a shell file */
4501431Seric 		*ap = cmd->sccspath;
4511431Seric 		*--ap = "sh";
4521431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4531431Seric 		break;
4541431Seric 
455200Seric 	  case PROG:		/* call an sccs prog */
4561431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
457201Seric 		break;
458201Seric 
459201Seric 	  case CMACRO:		/* command macro */
4601438Seric 		/* step through & execute each part of the macro */
461201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
462201Seric 		{
4631316Seric 			q = p;
4641316Seric 			while (*p != '\0' && *p != '/')
4651316Seric 				p++;
4661737Seric 			rval = command(&ap[1], *p != '\0', q);
4671282Seric 			if (rval != 0)
4681282Seric 				break;
469201Seric 		}
4701282Seric 		break;
471157Seric 
472226Seric 	  case FIX:		/* fix a delta */
4731431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
474226Seric 		{
4751205Seric 			usrerr("-r flag needed for fix command");
4761282Seric 			rval = EX_USAGE;
477226Seric 			break;
478226Seric 		}
4791438Seric 
4801438Seric 		/* get the version with all changes */
4811737Seric 		rval = command(&ap[1], TRUE, "get -k");
4821438Seric 
4831438Seric 		/* now remove that version from the s-file */
4841282Seric 		if (rval == 0)
4851737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4861438Seric 
4871438Seric 		/* and edit the old version (but don't clobber new vers) */
4881282Seric 		if (rval == 0)
4891737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4901282Seric 		break;
491226Seric 
492261Seric 	  case CLEAN:
4931822Seric 		rval = clean((int) cmd->sccspath, ap);
494261Seric 		break;
495261Seric 
496396Seric 	  case UNEDIT:
4971431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
498585Seric 		{
4991316Seric 			if (unedit(*argv))
5001316Seric 				*np++ = *argv;
501585Seric 		}
5021316Seric 		*np = NULL;
5031438Seric 
5041438Seric 		/* get all the files that we unedited successfully */
5051738Seric 		if (np > &ap[1])
5061737Seric 			rval = command(&ap[1], FALSE, "get");
507396Seric 		break;
508396Seric 
5091433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5101433Seric 		/* find the end of the flag arguments */
5111433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5121433Seric 			continue;
5131433Seric 		argv = np;
5141433Seric 
5151433Seric 		/* for each file, do the diff */
5161502Seric 		p = argv[1];
5171433Seric 		while (*np != NULL)
5181433Seric 		{
5191438Seric 			/* messy, but we need a null terminated argv */
5201433Seric 			*argv = *np++;
5211502Seric 			argv[1] = NULL;
5221435Seric 			i = dodiff(ap, tail(*argv));
5231433Seric 			if (rval == 0)
5241433Seric 				rval = i;
5251502Seric 			argv[1] = p;
5261433Seric 		}
5271433Seric 		break;
5281433Seric 
5291871Seric 	  case DODIFF:		/* internal diff call */
5301871Seric 		setuid(getuid());
5311871Seric 		for (np = ap; *np != NULL; np++)
5321871Seric 		{
5331871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5341871Seric 				(*np)[1] = 'c';
5351871Seric 		}
5361871Seric 
5371871Seric 		/* insert "-" argument */
5381871Seric 		np[1] = NULL;
5391871Seric 		np[0] = np[-1];
5401871Seric 		np[-1] = "-";
5411871Seric 
5421871Seric 		/* execute the diff program of choice */
5431871Seric # ifndef V6
5441871Seric 		execvp("diff", ap);
5451871Seric # endif
5461871Seric 		execv(cmd->sccspath, argv);
5471871Seric 		syserr("cannot exec %s", cmd->sccspath);
5481871Seric 		exit(EX_OSERR);
5491871Seric 
55010104Srrh 	  case ENTER:		/* enter new sccs files */
5516785Smckusick 		/* skip over flag arguments */
5526785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5536785Smckusick 			continue;
5546785Smckusick 		argv = np;
5556785Smckusick 
5566785Smckusick 		/* do an admin for each file */
5576785Smckusick 		p = argv[1];
5586785Smckusick 		while (*np != NULL)
5596785Smckusick 		{
5606785Smckusick 			printf("\n%s:\n", *np);
56110110Srrh 			strcpy(buf, "-i");
56210110Srrh 			gstrcat(buf, *np, sizeof(buf));
5636785Smckusick 			ap[0] = buf;
5646785Smckusick 			argv[0] = tail(*np);
5656785Smckusick 			argv[1] = NULL;
5666785Smckusick 			rval = command(ap, TRUE, "admin");
5676785Smckusick 			argv[1] = p;
5686785Smckusick 			if (rval == 0)
5696785Smckusick 			{
57010110Srrh 				strcpy(buf, ",");
57110110Srrh 				gstrcat(buf, tail(*np), sizeof(buf));
5726785Smckusick 				if (link(*np, buf) >= 0)
5736785Smckusick 					unlink(*np);
5746785Smckusick 			}
5756785Smckusick 			np++;
5766785Smckusick 		}
5776785Smckusick 		break;
5786785Smckusick 
579200Seric 	  default:
5801205Seric 		syserr("oper %d", cmd->sccsoper);
581200Seric 		exit(EX_SOFTWARE);
582200Seric 	}
5831282Seric # ifdef DEBUG
5841282Seric 	if (Debug)
5851282Seric 		printf("command: rval=%d\n", rval);
5861282Seric # endif
5871282Seric 	return (rval);
588200Seric }
5891432Seric 
5901432Seric /*
591262Seric **  LOOKUP -- look up an SCCS command name.
592262Seric **
593262Seric **	Parameters:
594262Seric **		name -- the name of the command to look up.
595262Seric **
596262Seric **	Returns:
597262Seric **		ptr to command descriptor for this command.
598262Seric **		NULL if no such entry.
599262Seric **
600262Seric **	Side Effects:
601262Seric **		none.
602262Seric */
603200Seric 
604262Seric struct sccsprog *
605262Seric lookup(name)
606262Seric 	char *name;
607262Seric {
608262Seric 	register struct sccsprog *cmd;
609226Seric 
610262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
611262Seric 	{
612262Seric 		if (strcmp(cmd->sccsname, name) == 0)
613262Seric 			return (cmd);
614262Seric 	}
615262Seric 	return (NULL);
616262Seric }
6171432Seric 
6181432Seric /*
6191282Seric **  CALLPROG -- call a program
6201282Seric **
6211316Seric **	Used to call the SCCS programs.
6221282Seric **
6231282Seric **	Parameters:
6241282Seric **		progpath -- pathname of the program to call.
6251282Seric **		flags -- status flags from the command descriptors.
6261282Seric **		argv -- an argument vector to pass to the program.
6271282Seric **		forkflag -- if true, fork before calling, else just
6281282Seric **			exec.
6291282Seric **
6301282Seric **	Returns:
6311282Seric **		The exit status of the program.
6321282Seric **		Nothing if forkflag == FALSE.
6331282Seric **
6341282Seric **	Side Effects:
6351282Seric **		Can exit if forkflag == FALSE.
6361282Seric */
637226Seric 
638200Seric callprog(progpath, flags, argv, forkflag)
639200Seric 	char *progpath;
640200Seric 	short flags;
641200Seric 	char **argv;
642200Seric 	bool forkflag;
643200Seric {
644200Seric 	register int i;
645201Seric 	auto int st;
646200Seric 
6471316Seric # ifdef DEBUG
6481316Seric 	if (Debug)
6491316Seric 	{
6501316Seric 		printf("callprog:\n");
6511316Seric 		for (i = 0; argv[i] != NULL; i++)
6521316Seric 			printf("\t\"%s\"\n", argv[i]);
6531316Seric 	}
6541316Seric # endif
6551316Seric 
656200Seric 	if (*argv == NULL)
657200Seric 		return (-1);
658200Seric 
659157Seric 	/*
660226Seric 	**  Fork if appropriate.
661148Seric 	*/
662148Seric 
663200Seric 	if (forkflag)
664200Seric 	{
665393Seric # ifdef DEBUG
666393Seric 		if (Debug)
667393Seric 			printf("Forking\n");
668393Seric # endif
669200Seric 		i = fork();
670200Seric 		if (i < 0)
671200Seric 		{
6721205Seric 			syserr("cannot fork");
673200Seric 			exit(EX_OSERR);
674200Seric 		}
675200Seric 		else if (i > 0)
676201Seric 		{
677201Seric 			wait(&st);
6781282Seric 			if ((st & 0377) == 0)
6791282Seric 				st = (st >> 8) & 0377;
6801433Seric 			if (OutFile >= 0)
6811433Seric 			{
6821433Seric 				close(OutFile);
6831433Seric 				OutFile = -1;
6841433Seric 			}
685201Seric 			return (st);
686201Seric 		}
687200Seric 	}
6881433Seric 	else if (OutFile >= 0)
6891433Seric 	{
6901433Seric 		syserr("callprog: setting stdout w/o forking");
6911433Seric 		exit(EX_SOFTWARE);
6921433Seric 	}
693200Seric 
6941433Seric 	/* set protection as appropriate */
695200Seric 	if (bitset(REALUSER, flags))
696200Seric 		setuid(getuid());
6971433Seric 
6981433Seric 	/* change standard input & output if needed */
6991433Seric 	if (OutFile >= 0)
7001433Seric 	{
7011433Seric 		close(1);
7021433Seric 		dup(OutFile);
7031433Seric 		close(OutFile);
7041433Seric 	}
705226Seric 
7061433Seric 	/* call real SCCS program */
707226Seric 	execv(progpath, argv);
7081205Seric 	syserr("cannot execute %s", progpath);
709148Seric 	exit(EX_UNAVAILABLE);
7101738Seric 	/*NOTREACHED*/
711148Seric }
7121432Seric 
7131432Seric /*
714586Seric **  MAKEFILE -- make filename of SCCS file
715586Seric **
716586Seric **	If the name passed is already the name of an SCCS file,
717586Seric **	just return it.  Otherwise, munge the name into the name
718586Seric **	of the actual SCCS file.
719586Seric **
720586Seric **	There are cases when it is not clear what you want to
721586Seric **	do.  For example, if SccsPath is an absolute pathname
722586Seric **	and the name given is also an absolute pathname, we go
723586Seric **	for SccsPath (& only use the last component of the name
724586Seric **	passed) -- this is important for security reasons (if
725586Seric **	sccs is being used as a setuid front end), but not
726586Seric **	particularly intuitive.
727586Seric **
728586Seric **	Parameters:
729586Seric **		name -- the file name to be munged.
730586Seric **
731586Seric **	Returns:
732586Seric **		The pathname of the sccs file.
733586Seric **		NULL on error.
734586Seric **
735586Seric **	Side Effects:
736586Seric **		none.
737586Seric */
738148Seric 
739148Seric char *
740148Seric makefile(name)
741148Seric 	char *name;
742148Seric {
743148Seric 	register char *p;
74410110Srrh 	char buf[3*FBUFSIZ];
745148Seric 	extern char *malloc();
746586Seric 	extern char *rindex();
747588Seric 	extern bool safepath();
748587Seric 	extern bool isdir();
749587Seric 	register char *q;
750148Seric 
751586Seric 	p = rindex(name, '/');
752586Seric 	if (p == NULL)
753586Seric 		p = name;
754586Seric 	else
755586Seric 		p++;
756586Seric 
757148Seric 	/*
758588Seric 	**  Check to see that the path is "safe", i.e., that we
759588Seric 	**  are not letting some nasty person use the setuid part
760588Seric 	**  of this program to look at or munge some presumably
761588Seric 	**  hidden files.
762148Seric 	*/
763148Seric 
764588Seric 	if (SccsDir[0] == '/' && !safepath(name))
765588Seric 		return (NULL);
766586Seric 
767586Seric 	/*
768588Seric 	**  Create the base pathname.
769586Seric 	*/
770586Seric 
7711438Seric 	/* first the directory part */
772588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
773148Seric 	{
77410110Srrh 		gstrcpy(buf, SccsDir, sizeof(buf));
77510110Srrh 		gstrcat(buf, "/", sizeof(buf));
776586Seric 	}
777586Seric 	else
77810110Srrh 		gstrcpy(buf, "", sizeof(buf));
7791438Seric 
7801438Seric 	/* then the head of the pathname */
78110110Srrh 	gstrncat(buf, name, p - name, sizeof(buf));
782587Seric 	q = &buf[strlen(buf)];
7831438Seric 
7841438Seric 	/* now copy the final part of the name, in case useful */
78510110Srrh 	gstrcpy(q, p, sizeof(buf));
7861438Seric 
7871438Seric 	/* so is it useful? */
788587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
789586Seric 	{
7901438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
79110110Srrh 		gstrcpy(q, SccsPath, sizeof(buf));
79210110Srrh 		gstrcat(buf, "/s.", sizeof(buf));
7931438Seric 
7941438Seric 		/* and now the end of the name */
79510110Srrh 		gstrcat(buf, p, sizeof(buf));
796586Seric 	}
797148Seric 
7981438Seric 	/* if i haven't changed it, why did I do all this? */
799588Seric 	if (strcmp(buf, name) == 0)
800588Seric 		p = name;
801588Seric 	else
802148Seric 	{
8031438Seric 		/* but if I have, squirrel it away */
804588Seric 		p = malloc(strlen(buf) + 1);
805588Seric 		if (p == NULL)
806588Seric 		{
807588Seric 			perror("Sccs: no mem");
808588Seric 			exit(EX_OSERR);
809588Seric 		}
810588Seric 		strcpy(p, buf);
811148Seric 	}
8121438Seric 
813148Seric 	return (p);
814148Seric }
8151432Seric 
8161432Seric /*
817587Seric **  ISDIR -- return true if the argument is a directory.
818587Seric **
819587Seric **	Parameters:
820587Seric **		name -- the pathname of the file to check.
821587Seric **
822587Seric **	Returns:
823587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
824587Seric **
825587Seric **	Side Effects:
826587Seric **		none.
827587Seric */
828587Seric 
829587Seric bool
830587Seric isdir(name)
831587Seric 	char *name;
832587Seric {
833587Seric 	struct stat stbuf;
834587Seric 
835587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
836587Seric }
8371432Seric 
8381432Seric /*
839586Seric **  SAFEPATH -- determine whether a pathname is "safe"
840586Seric **
841586Seric **	"Safe" pathnames only allow you to get deeper into the
842586Seric **	directory structure, i.e., full pathnames and ".." are
843586Seric **	not allowed.
844586Seric **
845586Seric **	Parameters:
846586Seric **		p -- the name to check.
847586Seric **
848586Seric **	Returns:
849586Seric **		TRUE -- if the path is safe.
850586Seric **		FALSE -- if the path is not safe.
851586Seric **
852586Seric **	Side Effects:
853586Seric **		Prints a message if the path is not safe.
854586Seric */
855586Seric 
856586Seric bool
857586Seric safepath(p)
858586Seric 	register char *p;
859586Seric {
860586Seric 	extern char *index();
861586Seric 
862586Seric 	if (*p != '/')
863586Seric 	{
864586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
865586Seric 		{
866586Seric 			p = index(p, '/');
867586Seric 			if (p == NULL)
868586Seric 				return (TRUE);
869586Seric 			p++;
870586Seric 		}
871586Seric 	}
872586Seric 
873586Seric 	printf("You may not use full pathnames or \"..\"\n");
874586Seric 	return (FALSE);
875586Seric }
8761432Seric 
8771432Seric /*
878261Seric **  CLEAN -- clean out recreatable files
879261Seric **
880261Seric **	Any file for which an "s." file exists but no "p." file
881261Seric **	exists in the current directory is purged.
882261Seric **
883261Seric **	Parameters:
8841822Seric **		mode -- tells whether this came from a "clean", "info", or
8851822Seric **			"check" command.
8861822Seric **		argv -- the rest of the argument vector.
887261Seric **
888261Seric **	Returns:
889261Seric **		none.
890261Seric **
891261Seric **	Side Effects:
892819Seric **		Removes files in the current directory.
893819Seric **		Prints information regarding files being edited.
894819Seric **		Exits if a "check" command.
895261Seric */
896261Seric 
8971822Seric clean(mode, argv)
898819Seric 	int mode;
8991822Seric 	char **argv;
900261Seric {
9016784Smckusick 	struct direct *dir;
90210110Srrh 	char buf[FBUFSIZ];
9032140Seric 	char *bufend;
9046784Smckusick 	register DIR *dirfd;
905346Seric 	register char *basefile;
906351Seric 	bool gotedit;
9071822Seric 	bool gotpfent;
908394Seric 	FILE *pfp;
9091822Seric 	bool nobranch = FALSE;
9101822Seric 	extern struct pfile *getpfent();
9111822Seric 	register struct pfile *pf;
9121822Seric 	register char **ap;
9131864Seric 	extern char *username();
9141864Seric 	char *usernm = NULL;
9152140Seric 	char *subdir = NULL;
9162140Seric 	char *cmdname;
917261Seric 
9181438Seric 	/*
9191822Seric 	**  Process the argv
9201822Seric 	*/
9211822Seric 
9222140Seric 	cmdname = *argv;
9232140Seric 	for (ap = argv; *++ap != NULL; )
9241822Seric 	{
9251864Seric 		if (**ap == '-')
9261864Seric 		{
9271864Seric 			/* we have a flag */
9281864Seric 			switch ((*ap)[1])
9291864Seric 			{
9301864Seric 			  case 'b':
9311864Seric 				nobranch = TRUE;
9321864Seric 				break;
9331864Seric 
9341864Seric 			  case 'u':
9351864Seric 				if ((*ap)[2] != '\0')
9361864Seric 					usernm = &(*ap)[2];
9371864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9381864Seric 					usernm = *++ap;
9391864Seric 				else
9401864Seric 					usernm = username();
9411864Seric 				break;
9421864Seric 			}
9431864Seric 		}
9442140Seric 		else
9452140Seric 		{
9462140Seric 			if (subdir != NULL)
9472140Seric 				usrerr("too many args");
9482140Seric 			else
9492140Seric 				subdir = *ap;
9502140Seric 		}
9511822Seric 	}
9521822Seric 
9531822Seric 	/*
9541438Seric 	**  Find and open the SCCS directory.
9551438Seric 	*/
9561438Seric 
95710110Srrh 	gstrcpy(buf, SccsDir, sizeof(buf));
9581207Seric 	if (buf[0] != '\0')
95910110Srrh 		gstrcat(buf, "/", sizeof(buf));
9602140Seric 	if (subdir != NULL)
9612140Seric 	{
96210110Srrh 		gstrcat(buf, subdir, sizeof(buf));
96310110Srrh 		gstrcat(buf, "/", sizeof(buf));
9642140Seric 	}
96510110Srrh 	gstrcat(buf, SccsPath, sizeof(buf));
9662140Seric 	bufend = &buf[strlen(buf)];
9671438Seric 
9686784Smckusick 	dirfd = opendir(buf);
969261Seric 	if (dirfd == NULL)
970261Seric 	{
9711207Seric 		usrerr("cannot open %s", buf);
9721282Seric 		return (EX_NOINPUT);
973261Seric 	}
974261Seric 
975261Seric 	/*
976261Seric 	**  Scan the SCCS directory looking for s. files.
9771438Seric 	**	gotedit tells whether we have tried to clean any
9781438Seric 	**		files that are being edited.
979261Seric 	*/
980261Seric 
981351Seric 	gotedit = FALSE;
9826784Smckusick 	while (dir = readdir(dirfd)) {
9836784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
984261Seric 			continue;
985261Seric 
986261Seric 		/* got an s. file -- see if the p. file exists */
98710110Srrh 		gstrcpy(bufend, "/p.", sizeof(buf));
9882140Seric 		basefile = bufend + 3;
98910110Srrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
9901822Seric 
9911822Seric 		/*
9921822Seric 		**  open and scan the p-file.
9931822Seric 		**	'gotpfent' tells if we have found a valid p-file
9941822Seric 		**		entry.
9951822Seric 		*/
9961822Seric 
997394Seric 		pfp = fopen(buf, "r");
9981822Seric 		gotpfent = FALSE;
999394Seric 		if (pfp != NULL)
1000346Seric 		{
10011438Seric 			/* the file exists -- report it's contents */
10021822Seric 			while ((pf = getpfent(pfp)) != NULL)
10031730Seric 			{
10041822Seric 				if (nobranch && isbranch(pf->p_nsid))
10051822Seric 					continue;
10061864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10071864Seric 					continue;
10081822Seric 				gotedit = TRUE;
10091822Seric 				gotpfent = TRUE;
10101822Seric 				if (mode == TELLC)
10111822Seric 				{
10121822Seric 					printf("%s\n", basefile);
10131822Seric 					break;
10141822Seric 				}
10152161Seric 				printf("%12s: being edited: ", basefile);
10162161Seric 				putpfent(pf, stdout);
10171730Seric 			}
1018394Seric 			fclose(pfp);
10191822Seric 		}
1020261Seric 
1021261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10221870Seric 		if (mode == CLEANC && !gotpfent)
1023346Seric 		{
102410110Srrh 			char	unlinkbuf[FBUFSIZ];
102510110Srrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
102610103Srrh 			unlink(unlinkbuf);
1027346Seric 		}
1028261Seric 	}
1029261Seric 
10301438Seric 	/* cleanup & report results */
10316784Smckusick 	closedir(dirfd);
1032819Seric 	if (!gotedit && mode == INFOC)
10331864Seric 	{
10341864Seric 		printf("Nothing being edited");
10351864Seric 		if (nobranch)
10361864Seric 			printf(" (on trunk)");
10371864Seric 		if (usernm == NULL)
10381864Seric 			printf("\n");
10391864Seric 		else
10401864Seric 			printf(" by %s\n", usernm);
10411864Seric 	}
1042819Seric 	if (mode == CHECKC)
1043819Seric 		exit(gotedit);
10441282Seric 	return (EX_OK);
1045261Seric }
10461432Seric 
10471432Seric /*
10481822Seric **  ISBRANCH -- is the SID a branch?
10491822Seric **
10501822Seric **	Parameters:
10511822Seric **		sid -- the sid to check.
10521822Seric **
10531822Seric **	Returns:
10541822Seric **		TRUE if the sid represents a branch.
10551822Seric **		FALSE otherwise.
10561822Seric **
10571822Seric **	Side Effects:
10581822Seric **		none.
10591822Seric */
10601822Seric 
10611822Seric isbranch(sid)
10621822Seric 	char *sid;
10631822Seric {
10641822Seric 	register char *p;
10651822Seric 	int dots;
10661822Seric 
10671822Seric 	dots = 0;
10681822Seric 	for (p = sid; *p != '\0'; p++)
10691822Seric 	{
10701822Seric 		if (*p == '.')
10711822Seric 			dots++;
10721822Seric 		if (dots > 1)
10731822Seric 			return (TRUE);
10741822Seric 	}
10751822Seric 	return (FALSE);
10761822Seric }
10771822Seric 
10781822Seric /*
1079396Seric **  UNEDIT -- unedit a file
1080396Seric **
1081396Seric **	Checks to see that the current user is actually editting
1082396Seric **	the file and arranges that s/he is not editting it.
1083396Seric **
1084396Seric **	Parameters:
1085416Seric **		fn -- the name of the file to be unedited.
1086396Seric **
1087396Seric **	Returns:
1088585Seric **		TRUE -- if the file was successfully unedited.
1089585Seric **		FALSE -- if the file was not unedited for some
1090585Seric **			reason.
1091396Seric **
1092396Seric **	Side Effects:
1093396Seric **		fn is removed
1094396Seric **		entries are removed from pfile.
1095396Seric */
1096396Seric 
1097585Seric bool
1098396Seric unedit(fn)
1099396Seric 	char *fn;
1100396Seric {
1101396Seric 	register FILE *pfp;
110212153Ssam 	char *cp, *pfn;
1103396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1104396Seric 	FILE *tfp;
1105396Seric 	register char *q;
1106396Seric 	bool delete = FALSE;
1107396Seric 	bool others = FALSE;
1108396Seric 	char *myname;
11091864Seric 	extern char *username();
1110396Seric 	struct pfile *pent;
11111822Seric 	extern struct pfile *getpfent();
111210110Srrh 	char buf[PFILELG];
11131316Seric 	extern char *makefile();
1114396Seric 
1115396Seric 	/* make "s." filename & find the trailing component */
1116396Seric 	pfn = makefile(fn);
1117586Seric 	if (pfn == NULL)
1118586Seric 		return (FALSE);
1119586Seric 	q = rindex(pfn, '/');
1120586Seric 	if (q == NULL)
1121586Seric 		q = &pfn[-1];
1122586Seric 	if (q[1] != 's' || q[2] != '.')
1123396Seric 	{
11241205Seric 		usrerr("bad file name \"%s\"", fn);
1125585Seric 		return (FALSE);
1126396Seric 	}
1127396Seric 
11281438Seric 	/* turn "s." into "p." & try to open it */
1129396Seric 	*++q = 'p';
1130396Seric 
1131396Seric 	pfp = fopen(pfn, "r");
1132396Seric 	if (pfp == NULL)
1133396Seric 	{
1134416Seric 		printf("%12s: not being edited\n", fn);
1135585Seric 		return (FALSE);
1136396Seric 	}
1137396Seric 
11381438Seric 	/* create temp file for editing p-file */
1139396Seric 	mktemp(tfn);
1140396Seric 	tfp = fopen(tfn, "w");
1141396Seric 	if (tfp == NULL)
1142396Seric 	{
11431205Seric 		usrerr("cannot create \"%s\"", tfn);
1144396Seric 		exit(EX_OSERR);
1145396Seric 	}
1146396Seric 
11471438Seric 	/* figure out who I am */
11481864Seric 	myname = username();
11491438Seric 
11501438Seric 	/*
11511438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11521438Seric 	*/
11531438Seric 
11541822Seric 	while ((pent = getpfent(pfp)) != NULL)
1155396Seric 	{
1156396Seric 		if (strcmp(pent->p_user, myname) == 0)
1157396Seric 		{
1158396Seric 			/* a match */
1159396Seric 			delete++;
1160396Seric 		}
1161396Seric 		else
1162396Seric 		{
11631438Seric 			/* output it again */
11642161Seric 			putpfent(pent, tfp);
1165396Seric 			others++;
1166396Seric 		}
1167396Seric 	}
1168396Seric 
116912153Ssam 	/*
117012153Ssam 	 * Before changing anything, make sure we can remove
117112153Ssam 	 * the file in question (assuming it exists).
117212153Ssam 	 */
117312153Ssam 	if (delete) {
117412153Ssam 		extern int errno;
117512153Ssam 
117612153Ssam 		cp = tail(fn);
117712153Ssam 		errno = 0;
117812153Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
117912153Ssam 			goto bad;
118012153Ssam 		if (errno == 0)
118112153Ssam 			/*
118212153Ssam 			 * This is wrong, but the rest of the program
118312153Ssam 			 * has built in assumptions about "." as well,
118412153Ssam 			 * so why make unedit a special case?
118512153Ssam 			 */
118612153Ssam 			if (access(".", 2) < 0) {
118712153Ssam 	bad:
118812153Ssam 				printf("%12s: can't remove\n", cp);
118912153Ssam 				fclose(tfp);
119012153Ssam 				fclose(pfp);
119112153Ssam 				unlink(tfn);
119212153Ssam 				return (FALSE);
119312153Ssam 			}
119412153Ssam 	}
1195396Seric 	/* do final cleanup */
1196396Seric 	if (others)
1197396Seric 	{
11981438Seric 		/* copy it back (perhaps it should be linked?) */
1199396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1200396Seric 		{
12011205Seric 			syserr("cannot reopen \"%s\"", tfn);
1202396Seric 			exit(EX_OSERR);
1203396Seric 		}
1204396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1205396Seric 		{
12061205Seric 			usrerr("cannot create \"%s\"", pfn);
1207585Seric 			return (FALSE);
1208396Seric 		}
1209396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1210396Seric 			fputs(buf, pfp);
1211396Seric 	}
1212396Seric 	else
1213396Seric 	{
12141438Seric 		/* it's empty -- remove it */
1215396Seric 		unlink(pfn);
1216396Seric 	}
1217396Seric 	fclose(tfp);
1218396Seric 	fclose(pfp);
1219396Seric 	unlink(tfn);
1220396Seric 
12211438Seric 	/* actually remove the g-file */
1222396Seric 	if (delete)
1223396Seric 	{
122412153Ssam 		/*
122512153Ssam 		 * Since we've checked above, we can
122612153Ssam 		 * use the return from unlink to
122712153Ssam 		 * determine if the file existed or not.
122812153Ssam 		 */
122912153Ssam 		if (unlink(cp) >= 0)
123012153Ssam 			printf("%12s: removed\n", cp);
1231585Seric 		return (TRUE);
1232396Seric 	}
1233396Seric 	else
1234396Seric 	{
1235416Seric 		printf("%12s: not being edited by you\n", fn);
1236585Seric 		return (FALSE);
1237396Seric 	}
1238396Seric }
12391432Seric 
12401432Seric /*
12411433Seric **  DODIFF -- diff an s-file against a g-file
12421433Seric **
12431433Seric **	Parameters:
12441433Seric **		getv -- argv for the 'get' command.
12451433Seric **		gfile -- name of the g-file to diff against.
12461433Seric **
12471433Seric **	Returns:
12481433Seric **		Result of get.
12491433Seric **
12501433Seric **	Side Effects:
12511433Seric **		none.
12521433Seric */
12531433Seric 
12541433Seric dodiff(getv, gfile)
12551433Seric 	char **getv;
12561433Seric 	char *gfile;
12571433Seric {
12581433Seric 	int pipev[2];
12591433Seric 	int rval;
12601433Seric 	register int i;
12611433Seric 	register int pid;
12621433Seric 	auto int st;
12631433Seric 	extern int errno;
12641433Seric 	int (*osig)();
12651433Seric 
12661905Seric 	printf("\n------- %s -------\n", gfile);
12671871Seric 	fflush(stdout);
12681501Seric 
12691438Seric 	/* create context for diff to run in */
12701433Seric 	if (pipe(pipev) < 0)
12711433Seric 	{
12721433Seric 		syserr("dodiff: pipe failed");
12731433Seric 		exit(EX_OSERR);
12741433Seric 	}
12751433Seric 	if ((pid = fork()) < 0)
12761433Seric 	{
12771433Seric 		syserr("dodiff: fork failed");
12781433Seric 		exit(EX_OSERR);
12791433Seric 	}
12801433Seric 	else if (pid > 0)
12811433Seric 	{
12821433Seric 		/* in parent; run get */
12831433Seric 		OutFile = pipev[1];
12841433Seric 		close(pipev[0]);
12851871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12861433Seric 		osig = signal(SIGINT, SIG_IGN);
12871433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
12881433Seric 			errno = 0;
12891433Seric 		signal(SIGINT, osig);
12901433Seric 		/* ignore result of diff */
12911433Seric 	}
12921433Seric 	else
12931433Seric 	{
12941433Seric 		/* in child, run diff */
12951433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
12961433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
12971433Seric 		{
12981433Seric 			syserr("dodiff: magic failed");
12991433Seric 			exit(EX_OSERR);
13001433Seric 		}
13011871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
13021433Seric 	}
13031433Seric 	return (rval);
13041433Seric }
13051433Seric 
13061433Seric /*
13071435Seric **  TAIL -- return tail of filename.
13081435Seric **
13091435Seric **	Parameters:
13101435Seric **		fn -- the filename.
13111435Seric **
13121435Seric **	Returns:
13131435Seric **		a pointer to the tail of the filename; e.g., given
13141435Seric **		"cmd/ls.c", "ls.c" is returned.
13151435Seric **
13161435Seric **	Side Effects:
13171435Seric **		none.
13181435Seric */
13191435Seric 
13201435Seric char *
13211435Seric tail(fn)
13221435Seric 	register char *fn;
13231435Seric {
13241435Seric 	register char *p;
13251435Seric 
13261435Seric 	for (p = fn; *p != 0; p++)
13271435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
13281435Seric 			fn = &p[1];
13291435Seric 	return (fn);
13301435Seric }
13311435Seric 
13321435Seric /*
13331822Seric **  GETPFENT -- get an entry from the p-file
1334396Seric **
1335396Seric **	Parameters:
1336396Seric **		pfp -- p-file file pointer
1337396Seric **
1338396Seric **	Returns:
1339396Seric **		pointer to p-file struct for next entry
1340396Seric **		NULL on EOF or error
1341396Seric **
1342396Seric **	Side Effects:
1343396Seric **		Each call wipes out results of previous call.
1344396Seric */
1345396Seric 
1346396Seric struct pfile *
13471822Seric getpfent(pfp)
1348396Seric 	FILE *pfp;
1349396Seric {
1350396Seric 	static struct pfile ent;
135110110Srrh 	static char buf[PFILELG];
1352396Seric 	register char *p;
1353396Seric 	extern char *nextfield();
1354396Seric 
1355396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1356396Seric 		return (NULL);
1357396Seric 
1358396Seric 	ent.p_osid = p = buf;
1359396Seric 	ent.p_nsid = p = nextfield(p);
1360396Seric 	ent.p_user = p = nextfield(p);
1361396Seric 	ent.p_date = p = nextfield(p);
1362396Seric 	ent.p_time = p = nextfield(p);
13632161Seric 	ent.p_aux = p = nextfield(p);
1364396Seric 
1365396Seric 	return (&ent);
1366396Seric }
1367396Seric 
1368396Seric 
1369396Seric char *
1370396Seric nextfield(p)
1371396Seric 	register char *p;
1372396Seric {
1373396Seric 	if (p == NULL || *p == '\0')
1374396Seric 		return (NULL);
1375396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1376396Seric 		p++;
1377396Seric 	if (*p == '\n' || *p == '\0')
1378396Seric 	{
1379396Seric 		*p = '\0';
1380396Seric 		return (NULL);
1381396Seric 	}
1382396Seric 	*p++ = '\0';
1383396Seric 	return (p);
1384396Seric }
13852161Seric /*
13862161Seric **  PUTPFENT -- output a p-file entry to a file
13872161Seric **
13882161Seric **	Parameters:
13892161Seric **		pf -- the p-file entry
13902161Seric **		f -- the file to put it on.
13912161Seric **
13922161Seric **	Returns:
13932161Seric **		none.
13942161Seric **
13952161Seric **	Side Effects:
13962161Seric **		pf is written onto file f.
13972161Seric */
13982161Seric 
13992161Seric putpfent(pf, f)
14002161Seric 	register struct pfile *pf;
14012161Seric 	register FILE *f;
14022161Seric {
14032161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
14042161Seric 		pf->p_user, pf->p_date, pf->p_time);
14052161Seric 	if (pf->p_aux != NULL)
14062161Seric 		fprintf(f, " %s", pf->p_aux);
14072161Seric 	else
14082161Seric 		fprintf(f, "\n");
14092161Seric }
14101432Seric 
14111432Seric /*
14121205Seric **  USRERR -- issue user-level error
14131205Seric **
14141205Seric **	Parameters:
14151205Seric **		f -- format string.
14161205Seric **		p1-p3 -- parameters to a printf.
14171205Seric **
14181205Seric **	Returns:
14191205Seric **		-1
14201205Seric **
14211205Seric **	Side Effects:
14221205Seric **		none.
14231205Seric */
14241205Seric 
14251738Seric /*VARARGS1*/
14261205Seric usrerr(f, p1, p2, p3)
14271205Seric 	char *f;
14281205Seric {
14291205Seric 	fprintf(stderr, "\n%s: ", MyName);
14301205Seric 	fprintf(stderr, f, p1, p2, p3);
14311205Seric 	fprintf(stderr, "\n");
14321205Seric 
14331205Seric 	return (-1);
14341205Seric }
14351432Seric 
14361432Seric /*
14371205Seric **  SYSERR -- print system-generated error.
14381205Seric **
14391205Seric **	Parameters:
14401205Seric **		f -- format string to a printf.
14411205Seric **		p1, p2, p3 -- parameters to f.
14421205Seric **
14431205Seric **	Returns:
14441205Seric **		never.
14451205Seric **
14461205Seric **	Side Effects:
14471205Seric **		none.
14481205Seric */
14491205Seric 
14501738Seric /*VARARGS1*/
14511205Seric syserr(f, p1, p2, p3)
14521205Seric 	char *f;
14531205Seric {
14541205Seric 	extern int errno;
14551205Seric 
14561205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
14571205Seric 	fprintf(stderr, f, p1, p2, p3);
14581205Seric 	fprintf(stderr, "\n");
14591205Seric 	if (errno == 0)
14601205Seric 		exit(EX_SOFTWARE);
14611205Seric 	else
14621205Seric 	{
14631738Seric 		perror(NULL);
14641205Seric 		exit(EX_OSERR);
14651205Seric 	}
14661205Seric }
14671864Seric /*
14681864Seric **  USERNAME -- return name of the current user
14691864Seric **
14701864Seric **	Parameters:
14711864Seric **		none
14721864Seric **
14731864Seric **	Returns:
14741864Seric **		name of current user
14751864Seric **
14761864Seric **	Side Effects:
14771864Seric **		none
14781864Seric */
14791864Seric 
14801864Seric char *
14811864Seric username()
14821864Seric {
14831864Seric # ifdef UIDUSER
14841864Seric 	extern struct passwd *getpwuid();
14851864Seric 	register struct passwd *pw;
14861864Seric 
14871864Seric 	pw = getpwuid(getuid());
14881864Seric 	if (pw == NULL)
14891864Seric 	{
14901864Seric 		syserr("who are you? (uid=%d)", getuid());
14911864Seric 		exit(EX_OSERR);
14921864Seric 	}
14931864Seric 	return (pw->pw_name);
14941864Seric # else
14951905Seric 	extern char *getlogin();
14966785Smckusick 	extern char *getenv();
14976785Smckusick 	register char *p;
14981905Seric 
14996785Smckusick 	p = getenv("USER");
15006785Smckusick 	if (p == NULL || p[0] == '\0')
15016785Smckusick 		p = getlogin();
15026785Smckusick 	return (p);
15031864Seric # endif UIDUSER
15041864Seric }
150510110Srrh 
150610110Srrh /*
150710110Srrh **	Guarded string manipulation routines; the last argument
150810110Srrh **	is the length of the buffer into which the strcpy or strcat
150910110Srrh **	is to be done.
151010110Srrh */
151110110Srrh char *gstrcat(to, from, length)
151210110Srrh 	char	*to, *from;
151310110Srrh 	int	length;
151410110Srrh {
151510110Srrh 	if (strlen(from) + strlen(to) >= length) {
151610110Srrh 		gstrbotch(to, from);
151710110Srrh 	}
151810110Srrh 	return(strcat(to, from));
151910110Srrh }
152010110Srrh 
152110110Srrh char *gstrncat(to, from, n, length)
152210110Srrh 	char	*to, *from;
152310110Srrh 	int	n;
152410110Srrh 	int	length;
152510110Srrh {
152610110Srrh 	if (n + strlen(to) >= length) {
152710110Srrh 		gstrbotch(to, from);
152810110Srrh 	}
152910110Srrh 	return(strncat(to, from, n));
153010110Srrh }
153110110Srrh 
153210110Srrh char *gstrcpy(to, from, length)
153310110Srrh 	char	*to, *from;
153410110Srrh 	int	length;
153510110Srrh {
153610110Srrh 	if (strlen(from) >= length) {
153710110Srrh 		gstrbotch(from, (char *)0);
153810110Srrh 	}
153910110Srrh 	return(strcpy(to, from));
154010110Srrh }
154110110Srrh gstrbotch(str1, str2)
154210110Srrh 	char	*str1, *str2;
154310110Srrh {
154410110Srrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
154510110Srrh }
1546