xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 30466)
121574Sdist /*
221574Sdist  * Copyright (c) 1980 Regents of the University of California.
321574Sdist  * All rights reserved.  The Berkeley software License Agreement
421574Sdist  * specifies the terms and conditions for redistribution.
521574Sdist  */
621574Sdist 
713622Ssam #ifndef lint
821574Sdist char copyright[] =
921574Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\
1021574Sdist  All rights reserved.\n";
1121574Sdist #endif not lint
1213622Ssam 
1321574Sdist #ifndef lint
14*30466Smckusick static char sccsid[] = "@(#)sccs.c	5.2 (Berkeley) 02/12/87";
1521574Sdist #endif not lint
1621574Sdist 
17148Seric # include <stdio.h>
186784Smckusick # include <sys/param.h>
19148Seric # include <sys/stat.h>
2013622Ssam # include <sys/dir.h>
211433Seric # include <errno.h>
221433Seric # include <signal.h>
23148Seric # include <sysexits.h>
242140Seric # include <pwd.h>
25148Seric 
26828Seric /*
27828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
28828Seric **
29828Seric **	Without trying to add any functionality to speak of, this
30828Seric **	program tries to make SCCS a little more accessible to human
31828Seric **	types.  The main thing it does is automatically put the
32828Seric **	string "SCCS/s." on the front of names.  Also, it has a
33828Seric **	couple of things that are designed to shorten frequent
34828Seric **	combinations, e.g., "delget" which expands to a "delta"
35828Seric **	and a "get".
36828Seric **
37828Seric **	This program can also function as a setuid front end.
38828Seric **	To do this, you should copy the source, renaming it to
39828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
40828Seric **	in the program (e.g., syssccs might default -d to
41828Seric **	"/usr/src/sys").  Then recompile and put the result
42828Seric **	as setuid to whomever you want.  In this mode, sccs
43828Seric **	knows to not run setuid for certain programs in order
44828Seric **	to preserve security, and so forth.
45828Seric **
46828Seric **	Usage:
47828Seric **		sccs [flags] command [args]
48828Seric **
49828Seric **	Flags:
50828Seric **		-d<dir>		<dir> represents a directory to search
51828Seric **				out of.  It should be a full pathname
52828Seric **				for general usage.  E.g., if <dir> is
53828Seric **				"/usr/src/sys", then a reference to the
54828Seric **				file "dev/bio.c" becomes a reference to
55828Seric **				"/usr/src/sys/dev/bio.c".
56828Seric **		-p<path>	prepends <path> to the final component
57828Seric **				of the pathname.  By default, this is
58828Seric **				"SCCS".  For example, in the -d example
59828Seric **				above, the path then gets modified to
60828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
61828Seric **				more common usage (without the -d flag),
62828Seric **				"prog.c" would get modified to
63828Seric **				"SCCS/s.prog.c".  In both cases, the
64828Seric **				"s." gets automatically prepended.
65828Seric **		-r		run as the real user.
66828Seric **
67828Seric **	Commands:
68828Seric **		admin,
69828Seric **		get,
70828Seric **		delta,
71828Seric **		rmdel,
72828Seric **		chghist,
73828Seric **		etc.		Straight out of SCCS; only difference
74828Seric **				is that pathnames get modified as
75828Seric **				described above.
76828Seric **		edit		Macro for "get -e".
77828Seric **		unedit		Removes a file being edited, knowing
78828Seric **				about p-files, etc.
79828Seric **		delget		Macro for "delta" followed by "get".
80828Seric **		deledit		Macro for "delta" followed by "get -e".
81828Seric **		info		Tell what files being edited.
82828Seric **		clean		Remove all files that can be
83828Seric **				regenerated from SCCS files.
841205Seric **		check		Like info, but return exit status, for
85828Seric **				use in makefiles.
86828Seric **		fix		Remove a top delta & reedit, but save
87828Seric **				the previous changes in that delta.
88828Seric **
89828Seric **	Compilation Flags:
90828Seric **		UIDUSER -- determine who the user is by looking at the
91828Seric **			uid rather than the login name -- for machines
92828Seric **			where SCCS gets the user in this way.
931270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
941205Seric **			this value.  This is so that the setuid
951205Seric **			aspects of this program cannot be abused.
961270Seric **			This flag also disables the -p flag.
971270Seric **		SCCSPATH -- the default for the -p flag.
981437Seric **		MYNAME -- the title this program should print when it
991437Seric **			gives error messages.
100828Seric **
101828Seric **	Compilation Instructions:
102828Seric **		cc -O -n -s sccs.c
1031437Seric **		The flags listed above can be -D defined to simplify
1041437Seric **			recompilation for variant versions.
105828Seric **
106828Seric **	Author:
107828Seric **		Eric Allman, UCB/INGRES
1081270Seric **		Copyright 1980 Regents of the University of California
109828Seric */
110155Seric 
1111432Seric 
1121270Seric /*******************  Configuration Information  ********************/
1131270Seric 
1141437Seric # ifndef SCCSPATH
1151432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1161437Seric # endif NOT SCCSPATH
117828Seric 
1181437Seric # ifndef MYNAME
1191437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1201437Seric # endif NOT MYNAME
1211270Seric 
1221432Seric # ifndef PROGPATH
1236784Smckusick # define PROGPATH(name)	"/usr/local/name"	/* place to find binaries */
1241432Seric # endif PROGPATH
1251432Seric 
1261270Seric /****************  End of Configuration Information  ****************/
1271432Seric 
128157Seric typedef char	bool;
129200Seric # define TRUE	1
130200Seric # define FALSE	0
131157Seric 
1321438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1331438Seric 
134148Seric struct sccsprog
135148Seric {
136148Seric 	char	*sccsname;	/* name of SCCS routine */
137200Seric 	short	sccsoper;	/* opcode, see below */
138200Seric 	short	sccsflags;	/* flags, see below */
139148Seric 	char	*sccspath;	/* pathname of binary implementing */
140148Seric };
141148Seric 
142200Seric /* values for sccsoper */
143200Seric # define PROG		0	/* call a program */
144201Seric # define CMACRO		1	/* command substitution macro */
145226Seric # define FIX		2	/* fix a delta */
146261Seric # define CLEAN		3	/* clean out recreatable files */
147396Seric # define UNEDIT		4	/* unedit a file */
1481431Seric # define SHELL		5	/* call a shell file (like PROG) */
1491433Seric # define DIFFS		6	/* diff between sccs & file out */
1501871Seric # define DODIFF		7	/* internal call to diff program */
15110104Srrh # define ENTER		8	/* enter new files */
152200Seric 
153157Seric /* bits for sccsflags */
154200Seric # define NO_SDOT	0001	/* no s. on front of args */
155200Seric # define REALUSER	0002	/* protected (e.g., admin) */
156148Seric 
157819Seric /* modes for the "clean", "info", "check" ops */
158819Seric # define CLEANC		0	/* clean command */
159819Seric # define INFOC		1	/* info command */
160819Seric # define CHECKC		2	/* check command */
1611730Seric # define TELLC		3	/* give list of files being edited */
162819Seric 
1631432Seric /*
1641432Seric **  Description of commands known to this program.
1651432Seric **	First argument puts the command into a class.  Second arg is
1661432Seric **	info regarding treatment of this command.  Third arg is a
1671432Seric **	list of flags this command accepts from macros, etc.  Fourth
1681432Seric **	arg is the pathname of the implementing program, or the
1691432Seric **	macro definition, or the arg to a sub-algorithm.
1701432Seric */
171202Seric 
172148Seric struct sccsprog SccsProg[] =
173148Seric {
1741864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1751864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1761864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1771864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1781864Seric 	"get",		PROG,	0,			PROGPATH(get),
1791864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1802136Seric 	"prs",		PROG,	0,			PROGPATH(prs),
1811864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1821864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1832136Seric 	"val",		PROG,	0,			PROGPATH(val),
1841864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1851864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1861864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1871864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1882138Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp -n/get:ixbskcl -e -t -g",
1891864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1901864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1911864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1921864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1931864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1941864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1951864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1961871Seric 	"-diff",	DODIFF,	NO_SDOT|REALUSER,	PROGPATH(bdiff),
1972137Seric 	"print",	CMACRO,	0,			"prt -e/get -p -m -s",
1982226Seric 	"branch",	CMACRO,	NO_SDOT,
1992226Seric 		"get:ixrc -e -b/delta: -s -n -ybranch-place-holder/get:pl -e -t -g",
20010104Srrh 	"enter",	ENTER,	NO_SDOT,		NULL,
20110104Srrh 	"create",	CMACRO,	NO_SDOT,		"enter/get:ixbeskcl -t",
2021864Seric 	NULL,		-1,	0,			NULL
203148Seric };
204148Seric 
2051432Seric /* one line from a p-file */
206396Seric struct pfile
207396Seric {
208396Seric 	char	*p_osid;	/* old SID */
209396Seric 	char	*p_nsid;	/* new SID */
210396Seric 	char	*p_user;	/* user who did edit */
211396Seric 	char	*p_date;	/* date of get */
212396Seric 	char	*p_time;	/* time of get */
2132161Seric 	char	*p_aux;		/* extra info at end */
214396Seric };
215396Seric 
2161270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2171270Seric # ifdef SCCSDIR
2181270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2191205Seric # else
2201270Seric char	*SccsDir = "";
2211205Seric # endif
2221437Seric char	MyName[] = MYNAME;	/* name used in messages */
2231433Seric int	OutFile = -1;		/* override output file for commands */
224157Seric bool	RealUser;		/* if set, running as real user */
225393Seric # ifdef DEBUG
226393Seric bool	Debug;			/* turn on tracing */
227393Seric # endif
2282139Seric # ifndef V6
2292139Seric extern char	*getenv();
2302139Seric # endif V6
23110110Srrh 
23210110Srrh char *gstrcat(), *strcat();
23310110Srrh char *gstrncat(), *strncat();
23410110Srrh char *gstrcpy(), *strcpy();
23510110Srrh #define	FBUFSIZ	BUFSIZ
23610110Srrh #define	PFILELG	120
2371432Seric 
238148Seric main(argc, argv)
239148Seric 	int argc;
240148Seric 	char **argv;
241148Seric {
242148Seric 	register char *p;
243262Seric 	extern struct sccsprog *lookup();
2441282Seric 	register int i;
2452139Seric # ifndef V6
2462139Seric # ifndef SCCSDIR
2472140Seric 	register struct passwd *pw;
2482140Seric 	extern struct passwd *getpwnam();
24910110Srrh 	char buf[FBUFSIZ];
2502140Seric 
2512139Seric 	/* pull "SccsDir" out of the environment (possibly) */
25210260Seric 	p = getenv("PROJECTDIR");
2532140Seric 	if (p != NULL && p[0] != '\0')
2542140Seric 	{
2552140Seric 		if (p[0] == '/')
2562140Seric 			SccsDir = p;
2572140Seric 		else
2582140Seric 		{
2592140Seric 			pw = getpwnam(p);
2602140Seric 			if (pw == NULL)
2612140Seric 			{
2622140Seric 				usrerr("user %s does not exist", p);
2632140Seric 				exit(EX_USAGE);
2642140Seric 			}
26510110Srrh 			gstrcpy(buf, pw->pw_dir, sizeof(buf));
26610110Srrh 			gstrcat(buf, "/src", sizeof(buf));
2672140Seric 			if (access(buf, 0) < 0)
2682140Seric 			{
26910110Srrh 				gstrcpy(buf, pw->pw_dir, sizeof(buf));
27010110Srrh 				gstrcat(buf, "/source", sizeof(buf));
2712140Seric 				if (access(buf, 0) < 0)
2722140Seric 				{
2732140Seric 					usrerr("project %s has no source!", p);
2742140Seric 					exit(EX_USAGE);
2752140Seric 				}
2762140Seric 			}
2772140Seric 			SccsDir = buf;
2782140Seric 		}
2792140Seric 	}
2802139Seric # endif SCCSDIR
2812139Seric # endif V6
2822139Seric 
283148Seric 	/*
284148Seric 	**  Detect and decode flags intended for this program.
285148Seric 	*/
286148Seric 
287200Seric 	if (argc < 2)
288148Seric 	{
2891205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
290200Seric 		exit(EX_USAGE);
291200Seric 	}
292200Seric 	argv[argc] = NULL;
293200Seric 
294262Seric 	if (lookup(argv[0]) == NULL)
295200Seric 	{
296262Seric 		while ((p = *++argv) != NULL)
297148Seric 		{
298262Seric 			if (*p != '-')
299262Seric 				break;
300262Seric 			switch (*++p)
301262Seric 			{
302262Seric 			  case 'r':		/* run as real user */
303262Seric 				setuid(getuid());
304262Seric 				RealUser++;
305262Seric 				break;
306148Seric 
3071270Seric # ifndef SCCSDIR
308262Seric 			  case 'p':		/* path of sccs files */
309262Seric 				SccsPath = ++p;
3102348Seric 				if (SccsPath[0] == '\0' && argv[1] != NULL)
3112348Seric 					SccsPath = *++argv;
312262Seric 				break;
313148Seric 
314588Seric 			  case 'd':		/* directory to search from */
315588Seric 				SccsDir = ++p;
3162348Seric 				if (SccsDir[0] == '\0' && argv[1] != NULL)
3172348Seric 					SccsDir = *++argv;
318588Seric 				break;
3191205Seric # endif
320588Seric 
321393Seric # ifdef DEBUG
322393Seric 			  case 'T':		/* trace */
323393Seric 				Debug++;
324393Seric 				break;
325393Seric # endif
326393Seric 
327262Seric 			  default:
3281205Seric 				usrerr("unknown option -%s", p);
329262Seric 				break;
330262Seric 			}
331148Seric 		}
332262Seric 		if (SccsPath[0] == '\0')
333262Seric 			SccsPath = ".";
334148Seric 	}
335148Seric 
3361737Seric 	i = command(argv, FALSE, "");
3371282Seric 	exit(i);
338200Seric }
3391432Seric 
3401432Seric /*
3411282Seric **  COMMAND -- look up and perform a command
3421282Seric **
3431282Seric **	This routine is the guts of this program.  Given an
3441282Seric **	argument vector, it looks up the "command" (argv[0])
3451282Seric **	in the configuration table and does the necessary stuff.
3461282Seric **
3471282Seric **	Parameters:
3481282Seric **		argv -- an argument vector to process.
3491282Seric **		forkflag -- if set, fork before executing the command.
3501316Seric **		editflag -- if set, only include flags listed in the
3511316Seric **			sccsklets field of the command descriptor.
3521316Seric **		arg0 -- a space-seperated list of arguments to insert
3531316Seric **			before argv.
3541282Seric **
3551282Seric **	Returns:
3561282Seric **		zero -- command executed ok.
3571282Seric **		else -- error status.
3581282Seric **
3591282Seric **	Side Effects:
3601282Seric **		none.
3611282Seric */
362157Seric 
3631737Seric command(argv, forkflag, arg0)
364200Seric 	char **argv;
365201Seric 	bool forkflag;
3661316Seric 	char *arg0;
367200Seric {
368200Seric 	register struct sccsprog *cmd;
369200Seric 	register char *p;
37010110Srrh 	char buf[FBUFSIZ];
371262Seric 	extern struct sccsprog *lookup();
3721316Seric 	char *nav[1000];
3731316Seric 	char **np;
3741431Seric 	register char **ap;
375585Seric 	register int i;
3761431Seric 	register char *q;
377585Seric 	extern bool unedit();
3781282Seric 	int rval = 0;
3791316Seric 	extern char *index();
3801316Seric 	extern char *makefile();
3811737Seric 	char *editchs;
3821435Seric 	extern char *tail();
383200Seric 
384393Seric # ifdef DEBUG
385393Seric 	if (Debug)
386393Seric 	{
3871316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3881316Seric 		for (np = argv; *np != NULL; np++)
3891316Seric 			printf("\t\"%s\"\n", *np);
390393Seric 	}
391393Seric # endif
392393Seric 
393157Seric 	/*
3941316Seric 	**  Copy arguments.
3951438Seric 	**	Copy from arg0 & if necessary at most one arg
3961438Seric 	**	from argv[0].
3971316Seric 	*/
3981316Seric 
3991431Seric 	np = ap = &nav[1];
4001737Seric 	editchs = NULL;
4011821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
4021316Seric 	{
4031316Seric 		*np++ = q;
4041316Seric 		while (*p == ' ')
4051316Seric 			p++;
4061737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
4071316Seric 			*q++ = *p++;
4081316Seric 		*q++ = '\0';
4091737Seric 		if (*p == ':')
4101737Seric 		{
4111737Seric 			editchs = q;
4121821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
4131737Seric 				*q++ = *p;
4141737Seric 			*q++ = '\0';
4151737Seric 		}
4161316Seric 	}
4171316Seric 	*np = NULL;
4181431Seric 	if (*ap == NULL)
4191316Seric 		*np++ = *argv++;
4201316Seric 
4211316Seric 	/*
422148Seric 	**  Look up command.
4231431Seric 	**	At this point, *ap is the command name.
424148Seric 	*/
425148Seric 
4261431Seric 	cmd = lookup(*ap);
427262Seric 	if (cmd == NULL)
428148Seric 	{
4291431Seric 		usrerr("Unknown command \"%s\"", *ap);
4301282Seric 		return (EX_USAGE);
431148Seric 	}
432148Seric 
433148Seric 	/*
4341316Seric 	**  Copy remaining arguments doing editing as appropriate.
4351316Seric 	*/
4361316Seric 
4371316Seric 	for (; *argv != NULL; argv++)
4381316Seric 	{
4391316Seric 		p = *argv;
4401316Seric 		if (*p == '-')
4411316Seric 		{
4421737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
4431316Seric 				*np++ = p;
4441316Seric 		}
4451316Seric 		else
4461316Seric 		{
4471316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
4481316Seric 				p = makefile(p);
4491316Seric 			if (p != NULL)
4501316Seric 				*np++ = p;
4511316Seric 		}
4521316Seric 	}
4531316Seric 	*np = NULL;
4541316Seric 
4551316Seric 	/*
456200Seric 	**  Interpret operation associated with this command.
457157Seric 	*/
458157Seric 
459200Seric 	switch (cmd->sccsoper)
460200Seric 	{
4611431Seric 	  case SHELL:		/* call a shell file */
4621431Seric 		*ap = cmd->sccspath;
4631431Seric 		*--ap = "sh";
4641431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4651431Seric 		break;
4661431Seric 
467200Seric 	  case PROG:		/* call an sccs prog */
4681431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
469201Seric 		break;
470201Seric 
471201Seric 	  case CMACRO:		/* command macro */
4721438Seric 		/* step through & execute each part of the macro */
473201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
474201Seric 		{
4751316Seric 			q = p;
4761316Seric 			while (*p != '\0' && *p != '/')
4771316Seric 				p++;
4781737Seric 			rval = command(&ap[1], *p != '\0', q);
4791282Seric 			if (rval != 0)
4801282Seric 				break;
481201Seric 		}
4821282Seric 		break;
483157Seric 
484226Seric 	  case FIX:		/* fix a delta */
4851431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
486226Seric 		{
4871205Seric 			usrerr("-r flag needed for fix command");
4881282Seric 			rval = EX_USAGE;
489226Seric 			break;
490226Seric 		}
4911438Seric 
4921438Seric 		/* get the version with all changes */
4931737Seric 		rval = command(&ap[1], TRUE, "get -k");
4941438Seric 
4951438Seric 		/* now remove that version from the s-file */
4961282Seric 		if (rval == 0)
4971737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4981438Seric 
4991438Seric 		/* and edit the old version (but don't clobber new vers) */
5001282Seric 		if (rval == 0)
5011737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
5021282Seric 		break;
503226Seric 
504261Seric 	  case CLEAN:
5051822Seric 		rval = clean((int) cmd->sccspath, ap);
506261Seric 		break;
507261Seric 
508396Seric 	  case UNEDIT:
5091431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
510585Seric 		{
5111316Seric 			if (unedit(*argv))
5121316Seric 				*np++ = *argv;
513585Seric 		}
5141316Seric 		*np = NULL;
5151438Seric 
5161438Seric 		/* get all the files that we unedited successfully */
5171738Seric 		if (np > &ap[1])
5181737Seric 			rval = command(&ap[1], FALSE, "get");
519396Seric 		break;
520396Seric 
5211433Seric 	  case DIFFS:		/* diff between s-file & edit file */
5221433Seric 		/* find the end of the flag arguments */
5231433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5241433Seric 			continue;
5251433Seric 		argv = np;
5261433Seric 
5271433Seric 		/* for each file, do the diff */
5281502Seric 		p = argv[1];
5291433Seric 		while (*np != NULL)
5301433Seric 		{
5311438Seric 			/* messy, but we need a null terminated argv */
5321433Seric 			*argv = *np++;
5331502Seric 			argv[1] = NULL;
5341435Seric 			i = dodiff(ap, tail(*argv));
5351433Seric 			if (rval == 0)
5361433Seric 				rval = i;
5371502Seric 			argv[1] = p;
5381433Seric 		}
5391433Seric 		break;
5401433Seric 
5411871Seric 	  case DODIFF:		/* internal diff call */
5421871Seric 		setuid(getuid());
5431871Seric 		for (np = ap; *np != NULL; np++)
5441871Seric 		{
5451871Seric 			if ((*np)[0] == '-' && (*np)[1] == 'C')
5461871Seric 				(*np)[1] = 'c';
5471871Seric 		}
5481871Seric 
5491871Seric 		/* insert "-" argument */
5501871Seric 		np[1] = NULL;
5511871Seric 		np[0] = np[-1];
5521871Seric 		np[-1] = "-";
5531871Seric 
5541871Seric 		/* execute the diff program of choice */
5551871Seric # ifndef V6
5561871Seric 		execvp("diff", ap);
5571871Seric # endif
5581871Seric 		execv(cmd->sccspath, argv);
5591871Seric 		syserr("cannot exec %s", cmd->sccspath);
5601871Seric 		exit(EX_OSERR);
5611871Seric 
56210104Srrh 	  case ENTER:		/* enter new sccs files */
5636785Smckusick 		/* skip over flag arguments */
5646785Smckusick 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
5656785Smckusick 			continue;
5666785Smckusick 		argv = np;
5676785Smckusick 
5686785Smckusick 		/* do an admin for each file */
5696785Smckusick 		p = argv[1];
5706785Smckusick 		while (*np != NULL)
5716785Smckusick 		{
5726785Smckusick 			printf("\n%s:\n", *np);
57310110Srrh 			strcpy(buf, "-i");
57410110Srrh 			gstrcat(buf, *np, sizeof(buf));
5756785Smckusick 			ap[0] = buf;
5766785Smckusick 			argv[0] = tail(*np);
5776785Smckusick 			argv[1] = NULL;
5786785Smckusick 			rval = command(ap, TRUE, "admin");
5796785Smckusick 			argv[1] = p;
5806785Smckusick 			if (rval == 0)
5816785Smckusick 			{
58210110Srrh 				strcpy(buf, ",");
58310110Srrh 				gstrcat(buf, tail(*np), sizeof(buf));
5846785Smckusick 				if (link(*np, buf) >= 0)
5856785Smckusick 					unlink(*np);
5866785Smckusick 			}
5876785Smckusick 			np++;
5886785Smckusick 		}
5896785Smckusick 		break;
5906785Smckusick 
591200Seric 	  default:
5921205Seric 		syserr("oper %d", cmd->sccsoper);
593200Seric 		exit(EX_SOFTWARE);
594200Seric 	}
5951282Seric # ifdef DEBUG
5961282Seric 	if (Debug)
5971282Seric 		printf("command: rval=%d\n", rval);
5981282Seric # endif
5991282Seric 	return (rval);
600200Seric }
6011432Seric 
6021432Seric /*
603262Seric **  LOOKUP -- look up an SCCS command name.
604262Seric **
605262Seric **	Parameters:
606262Seric **		name -- the name of the command to look up.
607262Seric **
608262Seric **	Returns:
609262Seric **		ptr to command descriptor for this command.
610262Seric **		NULL if no such entry.
611262Seric **
612262Seric **	Side Effects:
613262Seric **		none.
614262Seric */
615200Seric 
616262Seric struct sccsprog *
617262Seric lookup(name)
618262Seric 	char *name;
619262Seric {
620262Seric 	register struct sccsprog *cmd;
621226Seric 
622262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
623262Seric 	{
624262Seric 		if (strcmp(cmd->sccsname, name) == 0)
625262Seric 			return (cmd);
626262Seric 	}
627262Seric 	return (NULL);
628262Seric }
6291432Seric 
6301432Seric /*
6311282Seric **  CALLPROG -- call a program
6321282Seric **
6331316Seric **	Used to call the SCCS programs.
6341282Seric **
6351282Seric **	Parameters:
6361282Seric **		progpath -- pathname of the program to call.
6371282Seric **		flags -- status flags from the command descriptors.
6381282Seric **		argv -- an argument vector to pass to the program.
6391282Seric **		forkflag -- if true, fork before calling, else just
6401282Seric **			exec.
6411282Seric **
6421282Seric **	Returns:
6431282Seric **		The exit status of the program.
6441282Seric **		Nothing if forkflag == FALSE.
6451282Seric **
6461282Seric **	Side Effects:
6471282Seric **		Can exit if forkflag == FALSE.
6481282Seric */
649226Seric 
650200Seric callprog(progpath, flags, argv, forkflag)
651200Seric 	char *progpath;
652200Seric 	short flags;
653200Seric 	char **argv;
654200Seric 	bool forkflag;
655200Seric {
656200Seric 	register int i;
657201Seric 	auto int st;
658200Seric 
6591316Seric # ifdef DEBUG
6601316Seric 	if (Debug)
6611316Seric 	{
6621316Seric 		printf("callprog:\n");
6631316Seric 		for (i = 0; argv[i] != NULL; i++)
6641316Seric 			printf("\t\"%s\"\n", argv[i]);
6651316Seric 	}
6661316Seric # endif
6671316Seric 
668200Seric 	if (*argv == NULL)
669200Seric 		return (-1);
670200Seric 
671157Seric 	/*
672226Seric 	**  Fork if appropriate.
673148Seric 	*/
674148Seric 
675200Seric 	if (forkflag)
676200Seric 	{
677393Seric # ifdef DEBUG
678393Seric 		if (Debug)
679393Seric 			printf("Forking\n");
680393Seric # endif
681200Seric 		i = fork();
682200Seric 		if (i < 0)
683200Seric 		{
6841205Seric 			syserr("cannot fork");
685200Seric 			exit(EX_OSERR);
686200Seric 		}
687200Seric 		else if (i > 0)
688201Seric 		{
689201Seric 			wait(&st);
6901282Seric 			if ((st & 0377) == 0)
6911282Seric 				st = (st >> 8) & 0377;
6921433Seric 			if (OutFile >= 0)
6931433Seric 			{
6941433Seric 				close(OutFile);
6951433Seric 				OutFile = -1;
6961433Seric 			}
697201Seric 			return (st);
698201Seric 		}
699200Seric 	}
7001433Seric 	else if (OutFile >= 0)
7011433Seric 	{
7021433Seric 		syserr("callprog: setting stdout w/o forking");
7031433Seric 		exit(EX_SOFTWARE);
7041433Seric 	}
705200Seric 
7061433Seric 	/* set protection as appropriate */
707200Seric 	if (bitset(REALUSER, flags))
708200Seric 		setuid(getuid());
7091433Seric 
7101433Seric 	/* change standard input & output if needed */
7111433Seric 	if (OutFile >= 0)
7121433Seric 	{
7131433Seric 		close(1);
7141433Seric 		dup(OutFile);
7151433Seric 		close(OutFile);
7161433Seric 	}
717226Seric 
7181433Seric 	/* call real SCCS program */
719226Seric 	execv(progpath, argv);
7201205Seric 	syserr("cannot execute %s", progpath);
721148Seric 	exit(EX_UNAVAILABLE);
7221738Seric 	/*NOTREACHED*/
723148Seric }
7241432Seric 
7251432Seric /*
726586Seric **  MAKEFILE -- make filename of SCCS file
727586Seric **
728586Seric **	If the name passed is already the name of an SCCS file,
729586Seric **	just return it.  Otherwise, munge the name into the name
730586Seric **	of the actual SCCS file.
731586Seric **
732586Seric **	There are cases when it is not clear what you want to
733586Seric **	do.  For example, if SccsPath is an absolute pathname
734586Seric **	and the name given is also an absolute pathname, we go
735586Seric **	for SccsPath (& only use the last component of the name
736586Seric **	passed) -- this is important for security reasons (if
737586Seric **	sccs is being used as a setuid front end), but not
738586Seric **	particularly intuitive.
739586Seric **
740586Seric **	Parameters:
741586Seric **		name -- the file name to be munged.
742586Seric **
743586Seric **	Returns:
744586Seric **		The pathname of the sccs file.
745586Seric **		NULL on error.
746586Seric **
747586Seric **	Side Effects:
748586Seric **		none.
749586Seric */
750148Seric 
751148Seric char *
752148Seric makefile(name)
753148Seric 	char *name;
754148Seric {
755148Seric 	register char *p;
75610110Srrh 	char buf[3*FBUFSIZ];
757148Seric 	extern char *malloc();
758586Seric 	extern char *rindex();
759588Seric 	extern bool safepath();
760587Seric 	extern bool isdir();
761587Seric 	register char *q;
762148Seric 
763586Seric 	p = rindex(name, '/');
764586Seric 	if (p == NULL)
765586Seric 		p = name;
766586Seric 	else
767586Seric 		p++;
768586Seric 
769148Seric 	/*
770588Seric 	**  Check to see that the path is "safe", i.e., that we
771588Seric 	**  are not letting some nasty person use the setuid part
772588Seric 	**  of this program to look at or munge some presumably
773588Seric 	**  hidden files.
774148Seric 	*/
775148Seric 
776588Seric 	if (SccsDir[0] == '/' && !safepath(name))
777588Seric 		return (NULL);
778586Seric 
779586Seric 	/*
780588Seric 	**  Create the base pathname.
781586Seric 	*/
782586Seric 
7831438Seric 	/* first the directory part */
784588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
785148Seric 	{
78610110Srrh 		gstrcpy(buf, SccsDir, sizeof(buf));
78710110Srrh 		gstrcat(buf, "/", sizeof(buf));
788586Seric 	}
789586Seric 	else
79010110Srrh 		gstrcpy(buf, "", sizeof(buf));
7911438Seric 
7921438Seric 	/* then the head of the pathname */
79310110Srrh 	gstrncat(buf, name, p - name, sizeof(buf));
794587Seric 	q = &buf[strlen(buf)];
7951438Seric 
7961438Seric 	/* now copy the final part of the name, in case useful */
79710110Srrh 	gstrcpy(q, p, sizeof(buf));
7981438Seric 
7991438Seric 	/* so is it useful? */
800587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
801586Seric 	{
8021438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
80310110Srrh 		gstrcpy(q, SccsPath, sizeof(buf));
80410110Srrh 		gstrcat(buf, "/s.", sizeof(buf));
8051438Seric 
8061438Seric 		/* and now the end of the name */
80710110Srrh 		gstrcat(buf, p, sizeof(buf));
808586Seric 	}
809148Seric 
8101438Seric 	/* if i haven't changed it, why did I do all this? */
811588Seric 	if (strcmp(buf, name) == 0)
812588Seric 		p = name;
813588Seric 	else
814148Seric 	{
8151438Seric 		/* but if I have, squirrel it away */
816588Seric 		p = malloc(strlen(buf) + 1);
817588Seric 		if (p == NULL)
818588Seric 		{
819588Seric 			perror("Sccs: no mem");
820588Seric 			exit(EX_OSERR);
821588Seric 		}
822588Seric 		strcpy(p, buf);
823148Seric 	}
8241438Seric 
825148Seric 	return (p);
826148Seric }
8271432Seric 
8281432Seric /*
829587Seric **  ISDIR -- return true if the argument is a directory.
830587Seric **
831587Seric **	Parameters:
832587Seric **		name -- the pathname of the file to check.
833587Seric **
834587Seric **	Returns:
835587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
836587Seric **
837587Seric **	Side Effects:
838587Seric **		none.
839587Seric */
840587Seric 
841587Seric bool
842587Seric isdir(name)
843587Seric 	char *name;
844587Seric {
845587Seric 	struct stat stbuf;
846587Seric 
847587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
848587Seric }
8491432Seric 
8501432Seric /*
851586Seric **  SAFEPATH -- determine whether a pathname is "safe"
852586Seric **
853586Seric **	"Safe" pathnames only allow you to get deeper into the
854586Seric **	directory structure, i.e., full pathnames and ".." are
855586Seric **	not allowed.
856586Seric **
857586Seric **	Parameters:
858586Seric **		p -- the name to check.
859586Seric **
860586Seric **	Returns:
861586Seric **		TRUE -- if the path is safe.
862586Seric **		FALSE -- if the path is not safe.
863586Seric **
864586Seric **	Side Effects:
865586Seric **		Prints a message if the path is not safe.
866586Seric */
867586Seric 
868586Seric bool
869586Seric safepath(p)
870586Seric 	register char *p;
871586Seric {
872586Seric 	extern char *index();
873586Seric 
874586Seric 	if (*p != '/')
875586Seric 	{
876586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
877586Seric 		{
878586Seric 			p = index(p, '/');
879586Seric 			if (p == NULL)
880586Seric 				return (TRUE);
881586Seric 			p++;
882586Seric 		}
883586Seric 	}
884586Seric 
885586Seric 	printf("You may not use full pathnames or \"..\"\n");
886586Seric 	return (FALSE);
887586Seric }
8881432Seric 
8891432Seric /*
890261Seric **  CLEAN -- clean out recreatable files
891261Seric **
892261Seric **	Any file for which an "s." file exists but no "p." file
893261Seric **	exists in the current directory is purged.
894261Seric **
895261Seric **	Parameters:
8961822Seric **		mode -- tells whether this came from a "clean", "info", or
8971822Seric **			"check" command.
8981822Seric **		argv -- the rest of the argument vector.
899261Seric **
900261Seric **	Returns:
901261Seric **		none.
902261Seric **
903261Seric **	Side Effects:
904819Seric **		Removes files in the current directory.
905819Seric **		Prints information regarding files being edited.
906819Seric **		Exits if a "check" command.
907261Seric */
908261Seric 
9091822Seric clean(mode, argv)
910819Seric 	int mode;
9111822Seric 	char **argv;
912261Seric {
9136784Smckusick 	struct direct *dir;
91410110Srrh 	char buf[FBUFSIZ];
9152140Seric 	char *bufend;
916*30466Smckusick 	register DIR *dirp;
917346Seric 	register char *basefile;
918351Seric 	bool gotedit;
9191822Seric 	bool gotpfent;
920394Seric 	FILE *pfp;
9211822Seric 	bool nobranch = FALSE;
9221822Seric 	extern struct pfile *getpfent();
9231822Seric 	register struct pfile *pf;
9241822Seric 	register char **ap;
9251864Seric 	extern char *username();
9261864Seric 	char *usernm = NULL;
9272140Seric 	char *subdir = NULL;
9282140Seric 	char *cmdname;
929261Seric 
9301438Seric 	/*
9311822Seric 	**  Process the argv
9321822Seric 	*/
9331822Seric 
9342140Seric 	cmdname = *argv;
9352140Seric 	for (ap = argv; *++ap != NULL; )
9361822Seric 	{
9371864Seric 		if (**ap == '-')
9381864Seric 		{
9391864Seric 			/* we have a flag */
9401864Seric 			switch ((*ap)[1])
9411864Seric 			{
9421864Seric 			  case 'b':
9431864Seric 				nobranch = TRUE;
9441864Seric 				break;
9451864Seric 
9461864Seric 			  case 'u':
9471864Seric 				if ((*ap)[2] != '\0')
9481864Seric 					usernm = &(*ap)[2];
9491864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
9501864Seric 					usernm = *++ap;
9511864Seric 				else
9521864Seric 					usernm = username();
9531864Seric 				break;
9541864Seric 			}
9551864Seric 		}
9562140Seric 		else
9572140Seric 		{
9582140Seric 			if (subdir != NULL)
9592140Seric 				usrerr("too many args");
9602140Seric 			else
9612140Seric 				subdir = *ap;
9622140Seric 		}
9631822Seric 	}
9641822Seric 
9651822Seric 	/*
9661438Seric 	**  Find and open the SCCS directory.
9671438Seric 	*/
9681438Seric 
96910110Srrh 	gstrcpy(buf, SccsDir, sizeof(buf));
9701207Seric 	if (buf[0] != '\0')
97110110Srrh 		gstrcat(buf, "/", sizeof(buf));
9722140Seric 	if (subdir != NULL)
9732140Seric 	{
97410110Srrh 		gstrcat(buf, subdir, sizeof(buf));
97510110Srrh 		gstrcat(buf, "/", sizeof(buf));
9762140Seric 	}
97710110Srrh 	gstrcat(buf, SccsPath, sizeof(buf));
9782140Seric 	bufend = &buf[strlen(buf)];
9791438Seric 
980*30466Smckusick 	dirp = opendir(buf);
981*30466Smckusick 	if (dirp == NULL)
982261Seric 	{
9831207Seric 		usrerr("cannot open %s", buf);
9841282Seric 		return (EX_NOINPUT);
985261Seric 	}
986261Seric 
987261Seric 	/*
988261Seric 	**  Scan the SCCS directory looking for s. files.
9891438Seric 	**	gotedit tells whether we have tried to clean any
9901438Seric 	**		files that are being edited.
991261Seric 	*/
992261Seric 
993351Seric 	gotedit = FALSE;
994*30466Smckusick 	while (dir = readdir(dirp)) {
9956784Smckusick 		if (strncmp(dir->d_name, "s.", 2) != 0)
996261Seric 			continue;
997261Seric 
998261Seric 		/* got an s. file -- see if the p. file exists */
99910110Srrh 		gstrcpy(bufend, "/p.", sizeof(buf));
10002140Seric 		basefile = bufend + 3;
100110110Srrh 		gstrcpy(basefile, &dir->d_name[2], sizeof(buf));
10021822Seric 
10031822Seric 		/*
10041822Seric 		**  open and scan the p-file.
10051822Seric 		**	'gotpfent' tells if we have found a valid p-file
10061822Seric 		**		entry.
10071822Seric 		*/
10081822Seric 
1009394Seric 		pfp = fopen(buf, "r");
10101822Seric 		gotpfent = FALSE;
1011394Seric 		if (pfp != NULL)
1012346Seric 		{
10131438Seric 			/* the file exists -- report it's contents */
10141822Seric 			while ((pf = getpfent(pfp)) != NULL)
10151730Seric 			{
10161822Seric 				if (nobranch && isbranch(pf->p_nsid))
10171822Seric 					continue;
10181864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
10191864Seric 					continue;
10201822Seric 				gotedit = TRUE;
10211822Seric 				gotpfent = TRUE;
10221822Seric 				if (mode == TELLC)
10231822Seric 				{
10241822Seric 					printf("%s\n", basefile);
10251822Seric 					break;
10261822Seric 				}
10272161Seric 				printf("%12s: being edited: ", basefile);
10282161Seric 				putpfent(pf, stdout);
10291730Seric 			}
1030394Seric 			fclose(pfp);
10311822Seric 		}
1032261Seric 
1033261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
10341870Seric 		if (mode == CLEANC && !gotpfent)
1035346Seric 		{
103610110Srrh 			char	unlinkbuf[FBUFSIZ];
103710110Srrh 			gstrcpy(unlinkbuf, &dir->d_name[2], sizeof(unlinkbuf));
103810103Srrh 			unlink(unlinkbuf);
1039346Seric 		}
1040261Seric 	}
1041261Seric 
10421438Seric 	/* cleanup & report results */
1043*30466Smckusick 	closedir(dirp);
1044819Seric 	if (!gotedit && mode == INFOC)
10451864Seric 	{
10461864Seric 		printf("Nothing being edited");
10471864Seric 		if (nobranch)
10481864Seric 			printf(" (on trunk)");
10491864Seric 		if (usernm == NULL)
10501864Seric 			printf("\n");
10511864Seric 		else
10521864Seric 			printf(" by %s\n", usernm);
10531864Seric 	}
1054819Seric 	if (mode == CHECKC)
1055819Seric 		exit(gotedit);
10561282Seric 	return (EX_OK);
1057261Seric }
10581432Seric 
10591432Seric /*
10601822Seric **  ISBRANCH -- is the SID a branch?
10611822Seric **
10621822Seric **	Parameters:
10631822Seric **		sid -- the sid to check.
10641822Seric **
10651822Seric **	Returns:
10661822Seric **		TRUE if the sid represents a branch.
10671822Seric **		FALSE otherwise.
10681822Seric **
10691822Seric **	Side Effects:
10701822Seric **		none.
10711822Seric */
10721822Seric 
10731822Seric isbranch(sid)
10741822Seric 	char *sid;
10751822Seric {
10761822Seric 	register char *p;
10771822Seric 	int dots;
10781822Seric 
10791822Seric 	dots = 0;
10801822Seric 	for (p = sid; *p != '\0'; p++)
10811822Seric 	{
10821822Seric 		if (*p == '.')
10831822Seric 			dots++;
10841822Seric 		if (dots > 1)
10851822Seric 			return (TRUE);
10861822Seric 	}
10871822Seric 	return (FALSE);
10881822Seric }
10891822Seric 
10901822Seric /*
1091396Seric **  UNEDIT -- unedit a file
1092396Seric **
1093396Seric **	Checks to see that the current user is actually editting
1094396Seric **	the file and arranges that s/he is not editting it.
1095396Seric **
1096396Seric **	Parameters:
1097416Seric **		fn -- the name of the file to be unedited.
1098396Seric **
1099396Seric **	Returns:
1100585Seric **		TRUE -- if the file was successfully unedited.
1101585Seric **		FALSE -- if the file was not unedited for some
1102585Seric **			reason.
1103396Seric **
1104396Seric **	Side Effects:
1105396Seric **		fn is removed
1106396Seric **		entries are removed from pfile.
1107396Seric */
1108396Seric 
1109585Seric bool
1110396Seric unedit(fn)
1111396Seric 	char *fn;
1112396Seric {
1113396Seric 	register FILE *pfp;
111412153Ssam 	char *cp, *pfn;
1115396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1116396Seric 	FILE *tfp;
1117396Seric 	register char *q;
1118396Seric 	bool delete = FALSE;
1119396Seric 	bool others = FALSE;
1120396Seric 	char *myname;
11211864Seric 	extern char *username();
1122396Seric 	struct pfile *pent;
11231822Seric 	extern struct pfile *getpfent();
112410110Srrh 	char buf[PFILELG];
11251316Seric 	extern char *makefile();
1126396Seric 
1127396Seric 	/* make "s." filename & find the trailing component */
1128396Seric 	pfn = makefile(fn);
1129586Seric 	if (pfn == NULL)
1130586Seric 		return (FALSE);
1131586Seric 	q = rindex(pfn, '/');
1132586Seric 	if (q == NULL)
1133586Seric 		q = &pfn[-1];
1134586Seric 	if (q[1] != 's' || q[2] != '.')
1135396Seric 	{
11361205Seric 		usrerr("bad file name \"%s\"", fn);
1137585Seric 		return (FALSE);
1138396Seric 	}
1139396Seric 
11401438Seric 	/* turn "s." into "p." & try to open it */
1141396Seric 	*++q = 'p';
1142396Seric 
1143396Seric 	pfp = fopen(pfn, "r");
1144396Seric 	if (pfp == NULL)
1145396Seric 	{
1146416Seric 		printf("%12s: not being edited\n", fn);
1147585Seric 		return (FALSE);
1148396Seric 	}
1149396Seric 
11501438Seric 	/* create temp file for editing p-file */
1151396Seric 	mktemp(tfn);
1152396Seric 	tfp = fopen(tfn, "w");
1153396Seric 	if (tfp == NULL)
1154396Seric 	{
11551205Seric 		usrerr("cannot create \"%s\"", tfn);
1156396Seric 		exit(EX_OSERR);
1157396Seric 	}
1158396Seric 
11591438Seric 	/* figure out who I am */
11601864Seric 	myname = username();
11611438Seric 
11621438Seric 	/*
11631438Seric 	**  Copy p-file to temp file, doing deletions as needed.
11641438Seric 	*/
11651438Seric 
11661822Seric 	while ((pent = getpfent(pfp)) != NULL)
1167396Seric 	{
1168396Seric 		if (strcmp(pent->p_user, myname) == 0)
1169396Seric 		{
1170396Seric 			/* a match */
1171396Seric 			delete++;
1172396Seric 		}
1173396Seric 		else
1174396Seric 		{
11751438Seric 			/* output it again */
11762161Seric 			putpfent(pent, tfp);
1177396Seric 			others++;
1178396Seric 		}
1179396Seric 	}
1180396Seric 
118112153Ssam 	/*
118212153Ssam 	 * Before changing anything, make sure we can remove
118312153Ssam 	 * the file in question (assuming it exists).
118412153Ssam 	 */
118512153Ssam 	if (delete) {
118612153Ssam 		extern int errno;
118712153Ssam 
118812153Ssam 		cp = tail(fn);
118912153Ssam 		errno = 0;
119012153Ssam 		if (access(cp, 0) < 0 && errno != ENOENT)
119112153Ssam 			goto bad;
119212153Ssam 		if (errno == 0)
119312153Ssam 			/*
119412153Ssam 			 * This is wrong, but the rest of the program
119512153Ssam 			 * has built in assumptions about "." as well,
119612153Ssam 			 * so why make unedit a special case?
119712153Ssam 			 */
119812153Ssam 			if (access(".", 2) < 0) {
119912153Ssam 	bad:
120012153Ssam 				printf("%12s: can't remove\n", cp);
120112153Ssam 				fclose(tfp);
120212153Ssam 				fclose(pfp);
120312153Ssam 				unlink(tfn);
120412153Ssam 				return (FALSE);
120512153Ssam 			}
120612153Ssam 	}
1207396Seric 	/* do final cleanup */
1208396Seric 	if (others)
1209396Seric 	{
12101438Seric 		/* copy it back (perhaps it should be linked?) */
1211396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1212396Seric 		{
12131205Seric 			syserr("cannot reopen \"%s\"", tfn);
1214396Seric 			exit(EX_OSERR);
1215396Seric 		}
1216396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1217396Seric 		{
12181205Seric 			usrerr("cannot create \"%s\"", pfn);
1219585Seric 			return (FALSE);
1220396Seric 		}
1221396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1222396Seric 			fputs(buf, pfp);
1223396Seric 	}
1224396Seric 	else
1225396Seric 	{
12261438Seric 		/* it's empty -- remove it */
1227396Seric 		unlink(pfn);
1228396Seric 	}
1229396Seric 	fclose(tfp);
1230396Seric 	fclose(pfp);
1231396Seric 	unlink(tfn);
1232396Seric 
12331438Seric 	/* actually remove the g-file */
1234396Seric 	if (delete)
1235396Seric 	{
123612153Ssam 		/*
123712153Ssam 		 * Since we've checked above, we can
123812153Ssam 		 * use the return from unlink to
123912153Ssam 		 * determine if the file existed or not.
124012153Ssam 		 */
124112153Ssam 		if (unlink(cp) >= 0)
124212153Ssam 			printf("%12s: removed\n", cp);
1243585Seric 		return (TRUE);
1244396Seric 	}
1245396Seric 	else
1246396Seric 	{
1247416Seric 		printf("%12s: not being edited by you\n", fn);
1248585Seric 		return (FALSE);
1249396Seric 	}
1250396Seric }
12511432Seric 
12521432Seric /*
12531433Seric **  DODIFF -- diff an s-file against a g-file
12541433Seric **
12551433Seric **	Parameters:
12561433Seric **		getv -- argv for the 'get' command.
12571433Seric **		gfile -- name of the g-file to diff against.
12581433Seric **
12591433Seric **	Returns:
12601433Seric **		Result of get.
12611433Seric **
12621433Seric **	Side Effects:
12631433Seric **		none.
12641433Seric */
12651433Seric 
12661433Seric dodiff(getv, gfile)
12671433Seric 	char **getv;
12681433Seric 	char *gfile;
12691433Seric {
12701433Seric 	int pipev[2];
12711433Seric 	int rval;
12721433Seric 	register int i;
12731433Seric 	register int pid;
12741433Seric 	auto int st;
12751433Seric 	extern int errno;
12761433Seric 	int (*osig)();
12771433Seric 
12781905Seric 	printf("\n------- %s -------\n", gfile);
12791871Seric 	fflush(stdout);
12801501Seric 
12811438Seric 	/* create context for diff to run in */
12821433Seric 	if (pipe(pipev) < 0)
12831433Seric 	{
12841433Seric 		syserr("dodiff: pipe failed");
12851433Seric 		exit(EX_OSERR);
12861433Seric 	}
12871433Seric 	if ((pid = fork()) < 0)
12881433Seric 	{
12891433Seric 		syserr("dodiff: fork failed");
12901433Seric 		exit(EX_OSERR);
12911433Seric 	}
12921433Seric 	else if (pid > 0)
12931433Seric 	{
12941433Seric 		/* in parent; run get */
12951433Seric 		OutFile = pipev[1];
12961433Seric 		close(pipev[0]);
12971871Seric 		rval = command(&getv[1], TRUE, "get:rcixt -s -k -p");
12981433Seric 		osig = signal(SIGINT, SIG_IGN);
12991433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
13001433Seric 			errno = 0;
13011433Seric 		signal(SIGINT, osig);
13021433Seric 		/* ignore result of diff */
13031433Seric 	}
13041433Seric 	else
13051433Seric 	{
13061433Seric 		/* in child, run diff */
13071433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
13081433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
13091433Seric 		{
13101433Seric 			syserr("dodiff: magic failed");
13111433Seric 			exit(EX_OSERR);
13121433Seric 		}
13131871Seric 		command(&getv[1], FALSE, "-diff:elsfhbC");
13141433Seric 	}
13151433Seric 	return (rval);
13161433Seric }
13171433Seric 
13181433Seric /*
13191435Seric **  TAIL -- return tail of filename.
13201435Seric **
13211435Seric **	Parameters:
13221435Seric **		fn -- the filename.
13231435Seric **
13241435Seric **	Returns:
13251435Seric **		a pointer to the tail of the filename; e.g., given
13261435Seric **		"cmd/ls.c", "ls.c" is returned.
13271435Seric **
13281435Seric **	Side Effects:
13291435Seric **		none.
13301435Seric */
13311435Seric 
13321435Seric char *
13331435Seric tail(fn)
13341435Seric 	register char *fn;
13351435Seric {
13361435Seric 	register char *p;
13371435Seric 
13381435Seric 	for (p = fn; *p != 0; p++)
13391435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
13401435Seric 			fn = &p[1];
13411435Seric 	return (fn);
13421435Seric }
13431435Seric 
13441435Seric /*
13451822Seric **  GETPFENT -- get an entry from the p-file
1346396Seric **
1347396Seric **	Parameters:
1348396Seric **		pfp -- p-file file pointer
1349396Seric **
1350396Seric **	Returns:
1351396Seric **		pointer to p-file struct for next entry
1352396Seric **		NULL on EOF or error
1353396Seric **
1354396Seric **	Side Effects:
1355396Seric **		Each call wipes out results of previous call.
1356396Seric */
1357396Seric 
1358396Seric struct pfile *
13591822Seric getpfent(pfp)
1360396Seric 	FILE *pfp;
1361396Seric {
1362396Seric 	static struct pfile ent;
136310110Srrh 	static char buf[PFILELG];
1364396Seric 	register char *p;
1365396Seric 	extern char *nextfield();
1366396Seric 
1367396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1368396Seric 		return (NULL);
1369396Seric 
1370396Seric 	ent.p_osid = p = buf;
1371396Seric 	ent.p_nsid = p = nextfield(p);
1372396Seric 	ent.p_user = p = nextfield(p);
1373396Seric 	ent.p_date = p = nextfield(p);
1374396Seric 	ent.p_time = p = nextfield(p);
13752161Seric 	ent.p_aux = p = nextfield(p);
1376396Seric 
1377396Seric 	return (&ent);
1378396Seric }
1379396Seric 
1380396Seric 
1381396Seric char *
1382396Seric nextfield(p)
1383396Seric 	register char *p;
1384396Seric {
1385396Seric 	if (p == NULL || *p == '\0')
1386396Seric 		return (NULL);
1387396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1388396Seric 		p++;
1389396Seric 	if (*p == '\n' || *p == '\0')
1390396Seric 	{
1391396Seric 		*p = '\0';
1392396Seric 		return (NULL);
1393396Seric 	}
1394396Seric 	*p++ = '\0';
1395396Seric 	return (p);
1396396Seric }
13972161Seric /*
13982161Seric **  PUTPFENT -- output a p-file entry to a file
13992161Seric **
14002161Seric **	Parameters:
14012161Seric **		pf -- the p-file entry
14022161Seric **		f -- the file to put it on.
14032161Seric **
14042161Seric **	Returns:
14052161Seric **		none.
14062161Seric **
14072161Seric **	Side Effects:
14082161Seric **		pf is written onto file f.
14092161Seric */
14102161Seric 
14112161Seric putpfent(pf, f)
14122161Seric 	register struct pfile *pf;
14132161Seric 	register FILE *f;
14142161Seric {
14152161Seric 	fprintf(f, "%s %s %s %s %s", pf->p_osid, pf->p_nsid,
14162161Seric 		pf->p_user, pf->p_date, pf->p_time);
14172161Seric 	if (pf->p_aux != NULL)
14182161Seric 		fprintf(f, " %s", pf->p_aux);
14192161Seric 	else
14202161Seric 		fprintf(f, "\n");
14212161Seric }
14221432Seric 
14231432Seric /*
14241205Seric **  USRERR -- issue user-level error
14251205Seric **
14261205Seric **	Parameters:
14271205Seric **		f -- format string.
14281205Seric **		p1-p3 -- parameters to a printf.
14291205Seric **
14301205Seric **	Returns:
14311205Seric **		-1
14321205Seric **
14331205Seric **	Side Effects:
14341205Seric **		none.
14351205Seric */
14361205Seric 
14371738Seric /*VARARGS1*/
14381205Seric usrerr(f, p1, p2, p3)
14391205Seric 	char *f;
14401205Seric {
14411205Seric 	fprintf(stderr, "\n%s: ", MyName);
14421205Seric 	fprintf(stderr, f, p1, p2, p3);
14431205Seric 	fprintf(stderr, "\n");
14441205Seric 
14451205Seric 	return (-1);
14461205Seric }
14471432Seric 
14481432Seric /*
14491205Seric **  SYSERR -- print system-generated error.
14501205Seric **
14511205Seric **	Parameters:
14521205Seric **		f -- format string to a printf.
14531205Seric **		p1, p2, p3 -- parameters to f.
14541205Seric **
14551205Seric **	Returns:
14561205Seric **		never.
14571205Seric **
14581205Seric **	Side Effects:
14591205Seric **		none.
14601205Seric */
14611205Seric 
14621738Seric /*VARARGS1*/
14631205Seric syserr(f, p1, p2, p3)
14641205Seric 	char *f;
14651205Seric {
14661205Seric 	extern int errno;
14671205Seric 
14681205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
14691205Seric 	fprintf(stderr, f, p1, p2, p3);
14701205Seric 	fprintf(stderr, "\n");
14711205Seric 	if (errno == 0)
14721205Seric 		exit(EX_SOFTWARE);
14731205Seric 	else
14741205Seric 	{
14751738Seric 		perror(NULL);
14761205Seric 		exit(EX_OSERR);
14771205Seric 	}
14781205Seric }
14791864Seric /*
14801864Seric **  USERNAME -- return name of the current user
14811864Seric **
14821864Seric **	Parameters:
14831864Seric **		none
14841864Seric **
14851864Seric **	Returns:
14861864Seric **		name of current user
14871864Seric **
14881864Seric **	Side Effects:
14891864Seric **		none
14901864Seric */
14911864Seric 
14921864Seric char *
14931864Seric username()
14941864Seric {
14951864Seric # ifdef UIDUSER
14961864Seric 	extern struct passwd *getpwuid();
14971864Seric 	register struct passwd *pw;
14981864Seric 
14991864Seric 	pw = getpwuid(getuid());
15001864Seric 	if (pw == NULL)
15011864Seric 	{
15021864Seric 		syserr("who are you? (uid=%d)", getuid());
15031864Seric 		exit(EX_OSERR);
15041864Seric 	}
15051864Seric 	return (pw->pw_name);
15061864Seric # else
15071905Seric 	extern char *getlogin();
15086785Smckusick 	extern char *getenv();
15096785Smckusick 	register char *p;
15101905Seric 
15116785Smckusick 	p = getenv("USER");
15126785Smckusick 	if (p == NULL || p[0] == '\0')
15136785Smckusick 		p = getlogin();
15146785Smckusick 	return (p);
15151864Seric # endif UIDUSER
15161864Seric }
151710110Srrh 
151810110Srrh /*
151910110Srrh **	Guarded string manipulation routines; the last argument
152010110Srrh **	is the length of the buffer into which the strcpy or strcat
152110110Srrh **	is to be done.
152210110Srrh */
152310110Srrh char *gstrcat(to, from, length)
152410110Srrh 	char	*to, *from;
152510110Srrh 	int	length;
152610110Srrh {
152710110Srrh 	if (strlen(from) + strlen(to) >= length) {
152810110Srrh 		gstrbotch(to, from);
152910110Srrh 	}
153010110Srrh 	return(strcat(to, from));
153110110Srrh }
153210110Srrh 
153310110Srrh char *gstrncat(to, from, n, length)
153410110Srrh 	char	*to, *from;
153510110Srrh 	int	n;
153610110Srrh 	int	length;
153710110Srrh {
153810110Srrh 	if (n + strlen(to) >= length) {
153910110Srrh 		gstrbotch(to, from);
154010110Srrh 	}
154110110Srrh 	return(strncat(to, from, n));
154210110Srrh }
154310110Srrh 
154410110Srrh char *gstrcpy(to, from, length)
154510110Srrh 	char	*to, *from;
154610110Srrh 	int	length;
154710110Srrh {
154810110Srrh 	if (strlen(from) >= length) {
154910110Srrh 		gstrbotch(from, (char *)0);
155010110Srrh 	}
155110110Srrh 	return(strcpy(to, from));
155210110Srrh }
155310110Srrh gstrbotch(str1, str2)
155410110Srrh 	char	*str1, *str2;
155510110Srrh {
155610110Srrh 	usrerr("Filename(s) too long: %s %s", str1, str2);
155710110Srrh }
1558