xref: /csrg-svn/usr.bin/sccs/sccs.c (revision 1870)
1148Seric # include <stdio.h>
2148Seric # include <sys/types.h>
3148Seric # include <sys/stat.h>
4261Seric # include <sys/dir.h>
51433Seric # include <errno.h>
61433Seric # include <signal.h>
7148Seric # include <sysexits.h>
8202Seric # include <whoami.h>
9148Seric 
10828Seric /*
11828Seric **  SCCS.C -- human-oriented front end to the SCCS system.
12828Seric **
13828Seric **	Without trying to add any functionality to speak of, this
14828Seric **	program tries to make SCCS a little more accessible to human
15828Seric **	types.  The main thing it does is automatically put the
16828Seric **	string "SCCS/s." on the front of names.  Also, it has a
17828Seric **	couple of things that are designed to shorten frequent
18828Seric **	combinations, e.g., "delget" which expands to a "delta"
19828Seric **	and a "get".
20828Seric **
21828Seric **	This program can also function as a setuid front end.
22828Seric **	To do this, you should copy the source, renaming it to
23828Seric **	whatever you want, e.g., "syssccs".  Change any defaults
24828Seric **	in the program (e.g., syssccs might default -d to
25828Seric **	"/usr/src/sys").  Then recompile and put the result
26828Seric **	as setuid to whomever you want.  In this mode, sccs
27828Seric **	knows to not run setuid for certain programs in order
28828Seric **	to preserve security, and so forth.
29828Seric **
30828Seric **	Usage:
31828Seric **		sccs [flags] command [args]
32828Seric **
33828Seric **	Flags:
34828Seric **		-d<dir>		<dir> represents a directory to search
35828Seric **				out of.  It should be a full pathname
36828Seric **				for general usage.  E.g., if <dir> is
37828Seric **				"/usr/src/sys", then a reference to the
38828Seric **				file "dev/bio.c" becomes a reference to
39828Seric **				"/usr/src/sys/dev/bio.c".
40828Seric **		-p<path>	prepends <path> to the final component
41828Seric **				of the pathname.  By default, this is
42828Seric **				"SCCS".  For example, in the -d example
43828Seric **				above, the path then gets modified to
44828Seric **				"/usr/src/sys/dev/SCCS/s.bio.c".  In
45828Seric **				more common usage (without the -d flag),
46828Seric **				"prog.c" would get modified to
47828Seric **				"SCCS/s.prog.c".  In both cases, the
48828Seric **				"s." gets automatically prepended.
49828Seric **		-r		run as the real user.
50828Seric **
51828Seric **	Commands:
52828Seric **		admin,
53828Seric **		get,
54828Seric **		delta,
55828Seric **		rmdel,
56828Seric **		chghist,
57828Seric **		etc.		Straight out of SCCS; only difference
58828Seric **				is that pathnames get modified as
59828Seric **				described above.
60828Seric **		edit		Macro for "get -e".
61828Seric **		unedit		Removes a file being edited, knowing
62828Seric **				about p-files, etc.
63828Seric **		delget		Macro for "delta" followed by "get".
64828Seric **		deledit		Macro for "delta" followed by "get -e".
65828Seric **		info		Tell what files being edited.
66828Seric **		clean		Remove all files that can be
67828Seric **				regenerated from SCCS files.
681205Seric **		check		Like info, but return exit status, for
69828Seric **				use in makefiles.
70828Seric **		fix		Remove a top delta & reedit, but save
71828Seric **				the previous changes in that delta.
72828Seric **
73828Seric **	Compilation Flags:
74828Seric **		UIDUSER -- determine who the user is by looking at the
75828Seric **			uid rather than the login name -- for machines
76828Seric **			where SCCS gets the user in this way.
771270Seric **		SCCSDIR -- if defined, forces the -d flag to take on
781205Seric **			this value.  This is so that the setuid
791205Seric **			aspects of this program cannot be abused.
801270Seric **			This flag also disables the -p flag.
811270Seric **		SCCSPATH -- the default for the -p flag.
821437Seric **		MYNAME -- the title this program should print when it
831437Seric **			gives error messages.
84828Seric **
85828Seric **	Compilation Instructions:
86828Seric **		cc -O -n -s sccs.c
871437Seric **		The flags listed above can be -D defined to simplify
881437Seric **			recompilation for variant versions.
89828Seric **
90828Seric **	Author:
91828Seric **		Eric Allman, UCB/INGRES
921270Seric **		Copyright 1980 Regents of the University of California
93828Seric */
94155Seric 
95*1870Seric static char SccsId[] = "@(#)sccs.c	1.51 12/05/80";
961432Seric 
971270Seric /*******************  Configuration Information  ********************/
981270Seric 
991437Seric /* special defines for local berkeley systems */
1001437Seric # include <whoami.h>
1011437Seric 
102828Seric # ifdef CSVAX
103828Seric # define UIDUSER
1041270Seric # define PROGPATH(name)	"/usr/local/name"
1051270Seric # endif CSVAX
106828Seric 
1071618Seric # ifdef INGVAX
1081618Seric # define PROGPATH(name)	"/usr/local/name"
1091618Seric # endif INGVAX
1101618Seric 
1111867Seric # ifdef CORY
1121867Seric # define PROGPATH(name)	"/usr/eecs/bin/name"
1131867Seric # endif CORY
1141867Seric 
1151437Seric /* end of berkeley systems defines */
1161437Seric 
1171437Seric # ifndef SCCSPATH
1181432Seric # define SCCSPATH	"SCCS"	/* pathname in which to find s-files */
1191437Seric # endif NOT SCCSPATH
120828Seric 
1211437Seric # ifndef MYNAME
1221437Seric # define MYNAME		"sccs"	/* name used for printing errors */
1231437Seric # endif NOT MYNAME
1241270Seric 
1251432Seric # ifndef PROGPATH
1261432Seric # define PROGPATH(name)	"/usr/sccs/name"	/* place to find binaries */
1271432Seric # endif PROGPATH
1281432Seric 
1291270Seric /****************  End of Configuration Information  ****************/
1301432Seric 
131157Seric typedef char	bool;
132200Seric # define TRUE	1
133200Seric # define FALSE	0
134157Seric 
1351438Seric # define bitset(bit, word)	((bool) ((bit) & (word)))
1361438Seric 
137828Seric # ifdef UIDUSER
138828Seric # include <pwd.h>
139828Seric # endif UIDUSER
140828Seric 
141148Seric struct sccsprog
142148Seric {
143148Seric 	char	*sccsname;	/* name of SCCS routine */
144200Seric 	short	sccsoper;	/* opcode, see below */
145200Seric 	short	sccsflags;	/* flags, see below */
146148Seric 	char	*sccspath;	/* pathname of binary implementing */
147148Seric };
148148Seric 
149200Seric /* values for sccsoper */
150200Seric # define PROG		0	/* call a program */
151201Seric # define CMACRO		1	/* command substitution macro */
152226Seric # define FIX		2	/* fix a delta */
153261Seric # define CLEAN		3	/* clean out recreatable files */
154396Seric # define UNEDIT		4	/* unedit a file */
1551431Seric # define SHELL		5	/* call a shell file (like PROG) */
1561433Seric # define DIFFS		6	/* diff between sccs & file out */
157200Seric 
158157Seric /* bits for sccsflags */
159200Seric # define NO_SDOT	0001	/* no s. on front of args */
160200Seric # define REALUSER	0002	/* protected (e.g., admin) */
161148Seric 
162819Seric /* modes for the "clean", "info", "check" ops */
163819Seric # define CLEANC		0	/* clean command */
164819Seric # define INFOC		1	/* info command */
165819Seric # define CHECKC		2	/* check command */
1661730Seric # define TELLC		3	/* give list of files being edited */
167819Seric 
1681432Seric /*
1691432Seric **  Description of commands known to this program.
1701432Seric **	First argument puts the command into a class.  Second arg is
1711432Seric **	info regarding treatment of this command.  Third arg is a
1721432Seric **	list of flags this command accepts from macros, etc.  Fourth
1731432Seric **	arg is the pathname of the implementing program, or the
1741432Seric **	macro definition, or the arg to a sub-algorithm.
1751432Seric */
176202Seric 
177148Seric struct sccsprog SccsProg[] =
178148Seric {
1791864Seric 	"admin",	PROG,	REALUSER,		PROGPATH(admin),
1801864Seric 	"chghist",	PROG,	0,			PROGPATH(rmdel),
1811864Seric 	"comb",		PROG,	0,			PROGPATH(comb),
1821864Seric 	"delta",	PROG,	0,			PROGPATH(delta),
1831864Seric 	"get",		PROG,	0,			PROGPATH(get),
1841864Seric 	"help",		PROG,	NO_SDOT,		PROGPATH(help),
1851864Seric 	"prt",		PROG,	0,			PROGPATH(prt),
1861864Seric 	"rmdel",	PROG,	REALUSER,		PROGPATH(rmdel),
1871864Seric 	"what",		PROG,	NO_SDOT,		PROGPATH(what),
1881864Seric 	"sccsdiff",	SHELL,	REALUSER,		PROGPATH(sccsdiff),
1891864Seric 	"edit",		CMACRO,	NO_SDOT,		"get -e",
1901864Seric 	"delget",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbeskcl -t",
1911864Seric 	"deledit",	CMACRO,	NO_SDOT,		"delta:mysrp/get:ixbskcl -e -t",
1921864Seric 	"fix",		FIX,	NO_SDOT,		NULL,
1931864Seric 	"clean",	CLEAN,	REALUSER|NO_SDOT,	(char *) CLEANC,
1941864Seric 	"info",		CLEAN,	REALUSER|NO_SDOT,	(char *) INFOC,
1951864Seric 	"check",	CLEAN,	REALUSER|NO_SDOT,	(char *) CHECKC,
1961864Seric 	"tell",		CLEAN,	REALUSER|NO_SDOT,	(char *) TELLC,
1971864Seric 	"unedit",	UNEDIT,	NO_SDOT,		NULL,
1981864Seric 	"diffs",	DIFFS,	NO_SDOT|REALUSER,	NULL,
1991864Seric 	NULL,		-1,	0,			NULL
200148Seric };
201148Seric 
2021432Seric /* one line from a p-file */
203396Seric struct pfile
204396Seric {
205396Seric 	char	*p_osid;	/* old SID */
206396Seric 	char	*p_nsid;	/* new SID */
207396Seric 	char	*p_user;	/* user who did edit */
208396Seric 	char	*p_date;	/* date of get */
209396Seric 	char	*p_time;	/* time of get */
210396Seric };
211396Seric 
2121270Seric char	*SccsPath = SCCSPATH;	/* pathname of SCCS files */
2131270Seric # ifdef SCCSDIR
2141270Seric char	*SccsDir = SCCSDIR;	/* directory to begin search from */
2151205Seric # else
2161270Seric char	*SccsDir = "";
2171205Seric # endif
2181437Seric char	MyName[] = MYNAME;	/* name used in messages */
2191433Seric int	OutFile = -1;		/* override output file for commands */
220157Seric bool	RealUser;		/* if set, running as real user */
221393Seric # ifdef DEBUG
222393Seric bool	Debug;			/* turn on tracing */
223393Seric # endif
2241432Seric 
225148Seric main(argc, argv)
226148Seric 	int argc;
227148Seric 	char **argv;
228148Seric {
229148Seric 	register char *p;
230262Seric 	extern struct sccsprog *lookup();
2311282Seric 	register int i;
232148Seric 
233148Seric 	/*
234148Seric 	**  Detect and decode flags intended for this program.
235148Seric 	*/
236148Seric 
237200Seric 	if (argc < 2)
238148Seric 	{
2391205Seric 		fprintf(stderr, "Usage: %s [flags] command [flags]\n", MyName);
240200Seric 		exit(EX_USAGE);
241200Seric 	}
242200Seric 	argv[argc] = NULL;
243200Seric 
244262Seric 	if (lookup(argv[0]) == NULL)
245200Seric 	{
246262Seric 		while ((p = *++argv) != NULL)
247148Seric 		{
248262Seric 			if (*p != '-')
249262Seric 				break;
250262Seric 			switch (*++p)
251262Seric 			{
252262Seric 			  case 'r':		/* run as real user */
253262Seric 				setuid(getuid());
254262Seric 				RealUser++;
255262Seric 				break;
256148Seric 
2571270Seric # ifndef SCCSDIR
258262Seric 			  case 'p':		/* path of sccs files */
259262Seric 				SccsPath = ++p;
260262Seric 				break;
261148Seric 
262588Seric 			  case 'd':		/* directory to search from */
263588Seric 				SccsDir = ++p;
264588Seric 				break;
2651205Seric # endif
266588Seric 
267393Seric # ifdef DEBUG
268393Seric 			  case 'T':		/* trace */
269393Seric 				Debug++;
270393Seric 				break;
271393Seric # endif
272393Seric 
273262Seric 			  default:
2741205Seric 				usrerr("unknown option -%s", p);
275262Seric 				break;
276262Seric 			}
277148Seric 		}
278262Seric 		if (SccsPath[0] == '\0')
279262Seric 			SccsPath = ".";
280148Seric 	}
281148Seric 
2821737Seric 	i = command(argv, FALSE, "");
2831282Seric 	exit(i);
284200Seric }
2851432Seric 
2861432Seric /*
2871282Seric **  COMMAND -- look up and perform a command
2881282Seric **
2891282Seric **	This routine is the guts of this program.  Given an
2901282Seric **	argument vector, it looks up the "command" (argv[0])
2911282Seric **	in the configuration table and does the necessary stuff.
2921282Seric **
2931282Seric **	Parameters:
2941282Seric **		argv -- an argument vector to process.
2951282Seric **		forkflag -- if set, fork before executing the command.
2961316Seric **		editflag -- if set, only include flags listed in the
2971316Seric **			sccsklets field of the command descriptor.
2981316Seric **		arg0 -- a space-seperated list of arguments to insert
2991316Seric **			before argv.
3001282Seric **
3011282Seric **	Returns:
3021282Seric **		zero -- command executed ok.
3031282Seric **		else -- error status.
3041282Seric **
3051282Seric **	Side Effects:
3061282Seric **		none.
3071282Seric */
308157Seric 
3091737Seric command(argv, forkflag, arg0)
310200Seric 	char **argv;
311201Seric 	bool forkflag;
3121316Seric 	char *arg0;
313200Seric {
314200Seric 	register struct sccsprog *cmd;
315200Seric 	register char *p;
316201Seric 	char buf[40];
317262Seric 	extern struct sccsprog *lookup();
3181316Seric 	char *nav[1000];
3191316Seric 	char **np;
3201431Seric 	register char **ap;
321585Seric 	register int i;
3221431Seric 	register char *q;
323585Seric 	extern bool unedit();
3241282Seric 	int rval = 0;
3251316Seric 	extern char *index();
3261316Seric 	extern char *makefile();
3271737Seric 	char *editchs;
3281435Seric 	extern char *tail();
329200Seric 
330393Seric # ifdef DEBUG
331393Seric 	if (Debug)
332393Seric 	{
3331316Seric 		printf("command:\n\t\"%s\"\n", arg0);
3341316Seric 		for (np = argv; *np != NULL; np++)
3351316Seric 			printf("\t\"%s\"\n", *np);
336393Seric 	}
337393Seric # endif
338393Seric 
339157Seric 	/*
3401316Seric 	**  Copy arguments.
3411438Seric 	**	Copy from arg0 & if necessary at most one arg
3421438Seric 	**	from argv[0].
3431316Seric 	*/
3441316Seric 
3451431Seric 	np = ap = &nav[1];
3461737Seric 	editchs = NULL;
3471821Seric 	for (p = arg0, q = buf; *p != '\0' && *p != '/'; )
3481316Seric 	{
3491316Seric 		*np++ = q;
3501316Seric 		while (*p == ' ')
3511316Seric 			p++;
3521737Seric 		while (*p != ' ' && *p != '\0' && *p != '/' && *p != ':')
3531316Seric 			*q++ = *p++;
3541316Seric 		*q++ = '\0';
3551737Seric 		if (*p == ':')
3561737Seric 		{
3571737Seric 			editchs = q;
3581821Seric 			while (*++p != '\0' && *p != '/' && *p != ' ')
3591737Seric 				*q++ = *p;
3601737Seric 			*q++ = '\0';
3611737Seric 		}
3621316Seric 	}
3631316Seric 	*np = NULL;
3641431Seric 	if (*ap == NULL)
3651316Seric 		*np++ = *argv++;
3661316Seric 
3671316Seric 	/*
368148Seric 	**  Look up command.
3691431Seric 	**	At this point, *ap is the command name.
370148Seric 	*/
371148Seric 
3721431Seric 	cmd = lookup(*ap);
373262Seric 	if (cmd == NULL)
374148Seric 	{
3751431Seric 		usrerr("Unknown command \"%s\"", *ap);
3761282Seric 		return (EX_USAGE);
377148Seric 	}
378148Seric 
379148Seric 	/*
3801316Seric 	**  Copy remaining arguments doing editing as appropriate.
3811316Seric 	*/
3821316Seric 
3831316Seric 	for (; *argv != NULL; argv++)
3841316Seric 	{
3851316Seric 		p = *argv;
3861316Seric 		if (*p == '-')
3871316Seric 		{
3881737Seric 			if (p[1] == '\0' || editchs == NULL || index(editchs, p[1]) != NULL)
3891316Seric 				*np++ = p;
3901316Seric 		}
3911316Seric 		else
3921316Seric 		{
3931316Seric 			if (!bitset(NO_SDOT, cmd->sccsflags))
3941316Seric 				p = makefile(p);
3951316Seric 			if (p != NULL)
3961316Seric 				*np++ = p;
3971316Seric 		}
3981316Seric 	}
3991316Seric 	*np = NULL;
4001316Seric 
4011316Seric 	/*
402200Seric 	**  Interpret operation associated with this command.
403157Seric 	*/
404157Seric 
405200Seric 	switch (cmd->sccsoper)
406200Seric 	{
4071431Seric 	  case SHELL:		/* call a shell file */
4081431Seric 		*ap = cmd->sccspath;
4091431Seric 		*--ap = "sh";
4101431Seric 		rval = callprog("/bin/sh", cmd->sccsflags, ap, forkflag);
4111431Seric 		break;
4121431Seric 
413200Seric 	  case PROG:		/* call an sccs prog */
4141431Seric 		rval = callprog(cmd->sccspath, cmd->sccsflags, ap, forkflag);
415201Seric 		break;
416201Seric 
417201Seric 	  case CMACRO:		/* command macro */
4181438Seric 		/* step through & execute each part of the macro */
419201Seric 		for (p = cmd->sccspath; *p != '\0'; p++)
420201Seric 		{
4211316Seric 			q = p;
4221316Seric 			while (*p != '\0' && *p != '/')
4231316Seric 				p++;
4241737Seric 			rval = command(&ap[1], *p != '\0', q);
4251282Seric 			if (rval != 0)
4261282Seric 				break;
427201Seric 		}
4281282Seric 		break;
429157Seric 
430226Seric 	  case FIX:		/* fix a delta */
4311431Seric 		if (strncmp(ap[1], "-r", 2) != 0)
432226Seric 		{
4331205Seric 			usrerr("-r flag needed for fix command");
4341282Seric 			rval = EX_USAGE;
435226Seric 			break;
436226Seric 		}
4371438Seric 
4381438Seric 		/* get the version with all changes */
4391737Seric 		rval = command(&ap[1], TRUE, "get -k");
4401438Seric 
4411438Seric 		/* now remove that version from the s-file */
4421282Seric 		if (rval == 0)
4431737Seric 			rval = command(&ap[1], TRUE, "rmdel:r");
4441438Seric 
4451438Seric 		/* and edit the old version (but don't clobber new vers) */
4461282Seric 		if (rval == 0)
4471737Seric 			rval = command(&ap[2], FALSE, "get -e -g");
4481282Seric 		break;
449226Seric 
450261Seric 	  case CLEAN:
4511822Seric 		rval = clean((int) cmd->sccspath, ap);
452261Seric 		break;
453261Seric 
454396Seric 	  case UNEDIT:
4551431Seric 		for (argv = np = &ap[1]; *argv != NULL; argv++)
456585Seric 		{
4571316Seric 			if (unedit(*argv))
4581316Seric 				*np++ = *argv;
459585Seric 		}
4601316Seric 		*np = NULL;
4611438Seric 
4621438Seric 		/* get all the files that we unedited successfully */
4631738Seric 		if (np > &ap[1])
4641737Seric 			rval = command(&ap[1], FALSE, "get");
465396Seric 		break;
466396Seric 
4671433Seric 	  case DIFFS:		/* diff between s-file & edit file */
4681433Seric 		/* find the end of the flag arguments */
4691433Seric 		for (np = &ap[1]; *np != NULL && **np == '-'; np++)
4701433Seric 			continue;
4711433Seric 		argv = np;
4721433Seric 
4731433Seric 		/* for each file, do the diff */
4741502Seric 		p = argv[1];
4751433Seric 		while (*np != NULL)
4761433Seric 		{
4771438Seric 			/* messy, but we need a null terminated argv */
4781433Seric 			*argv = *np++;
4791502Seric 			argv[1] = NULL;
4801435Seric 			i = dodiff(ap, tail(*argv));
4811433Seric 			if (rval == 0)
4821433Seric 				rval = i;
4831502Seric 			argv[1] = p;
4841433Seric 		}
4851433Seric 		break;
4861433Seric 
487200Seric 	  default:
4881205Seric 		syserr("oper %d", cmd->sccsoper);
489200Seric 		exit(EX_SOFTWARE);
490200Seric 	}
4911282Seric # ifdef DEBUG
4921282Seric 	if (Debug)
4931282Seric 		printf("command: rval=%d\n", rval);
4941282Seric # endif
4951282Seric 	return (rval);
496200Seric }
4971432Seric 
4981432Seric /*
499262Seric **  LOOKUP -- look up an SCCS command name.
500262Seric **
501262Seric **	Parameters:
502262Seric **		name -- the name of the command to look up.
503262Seric **
504262Seric **	Returns:
505262Seric **		ptr to command descriptor for this command.
506262Seric **		NULL if no such entry.
507262Seric **
508262Seric **	Side Effects:
509262Seric **		none.
510262Seric */
511200Seric 
512262Seric struct sccsprog *
513262Seric lookup(name)
514262Seric 	char *name;
515262Seric {
516262Seric 	register struct sccsprog *cmd;
517226Seric 
518262Seric 	for (cmd = SccsProg; cmd->sccsname != NULL; cmd++)
519262Seric 	{
520262Seric 		if (strcmp(cmd->sccsname, name) == 0)
521262Seric 			return (cmd);
522262Seric 	}
523262Seric 	return (NULL);
524262Seric }
5251432Seric 
5261432Seric /*
5271282Seric **  CALLPROG -- call a program
5281282Seric **
5291316Seric **	Used to call the SCCS programs.
5301282Seric **
5311282Seric **	Parameters:
5321282Seric **		progpath -- pathname of the program to call.
5331282Seric **		flags -- status flags from the command descriptors.
5341282Seric **		argv -- an argument vector to pass to the program.
5351282Seric **		forkflag -- if true, fork before calling, else just
5361282Seric **			exec.
5371282Seric **
5381282Seric **	Returns:
5391282Seric **		The exit status of the program.
5401282Seric **		Nothing if forkflag == FALSE.
5411282Seric **
5421282Seric **	Side Effects:
5431282Seric **		Can exit if forkflag == FALSE.
5441282Seric */
545226Seric 
546200Seric callprog(progpath, flags, argv, forkflag)
547200Seric 	char *progpath;
548200Seric 	short flags;
549200Seric 	char **argv;
550200Seric 	bool forkflag;
551200Seric {
552200Seric 	register int i;
553201Seric 	auto int st;
554200Seric 
5551316Seric # ifdef DEBUG
5561316Seric 	if (Debug)
5571316Seric 	{
5581316Seric 		printf("callprog:\n");
5591316Seric 		for (i = 0; argv[i] != NULL; i++)
5601316Seric 			printf("\t\"%s\"\n", argv[i]);
5611316Seric 	}
5621316Seric # endif
5631316Seric 
564200Seric 	if (*argv == NULL)
565200Seric 		return (-1);
566200Seric 
567157Seric 	/*
568226Seric 	**  Fork if appropriate.
569148Seric 	*/
570148Seric 
571200Seric 	if (forkflag)
572200Seric 	{
573393Seric # ifdef DEBUG
574393Seric 		if (Debug)
575393Seric 			printf("Forking\n");
576393Seric # endif
577200Seric 		i = fork();
578200Seric 		if (i < 0)
579200Seric 		{
5801205Seric 			syserr("cannot fork");
581200Seric 			exit(EX_OSERR);
582200Seric 		}
583200Seric 		else if (i > 0)
584201Seric 		{
585201Seric 			wait(&st);
5861282Seric 			if ((st & 0377) == 0)
5871282Seric 				st = (st >> 8) & 0377;
5881433Seric 			if (OutFile >= 0)
5891433Seric 			{
5901433Seric 				close(OutFile);
5911433Seric 				OutFile = -1;
5921433Seric 			}
593201Seric 			return (st);
594201Seric 		}
595200Seric 	}
5961433Seric 	else if (OutFile >= 0)
5971433Seric 	{
5981433Seric 		syserr("callprog: setting stdout w/o forking");
5991433Seric 		exit(EX_SOFTWARE);
6001433Seric 	}
601200Seric 
6021433Seric 	/* set protection as appropriate */
603200Seric 	if (bitset(REALUSER, flags))
604200Seric 		setuid(getuid());
6051433Seric 
6061433Seric 	/* change standard input & output if needed */
6071433Seric 	if (OutFile >= 0)
6081433Seric 	{
6091433Seric 		close(1);
6101433Seric 		dup(OutFile);
6111433Seric 		close(OutFile);
6121433Seric 	}
613226Seric 
6141433Seric 	/* call real SCCS program */
615226Seric 	execv(progpath, argv);
6161205Seric 	syserr("cannot execute %s", progpath);
617148Seric 	exit(EX_UNAVAILABLE);
6181738Seric 	/*NOTREACHED*/
619148Seric }
6201432Seric 
6211432Seric /*
622586Seric **  MAKEFILE -- make filename of SCCS file
623586Seric **
624586Seric **	If the name passed is already the name of an SCCS file,
625586Seric **	just return it.  Otherwise, munge the name into the name
626586Seric **	of the actual SCCS file.
627586Seric **
628586Seric **	There are cases when it is not clear what you want to
629586Seric **	do.  For example, if SccsPath is an absolute pathname
630586Seric **	and the name given is also an absolute pathname, we go
631586Seric **	for SccsPath (& only use the last component of the name
632586Seric **	passed) -- this is important for security reasons (if
633586Seric **	sccs is being used as a setuid front end), but not
634586Seric **	particularly intuitive.
635586Seric **
636586Seric **	Parameters:
637586Seric **		name -- the file name to be munged.
638586Seric **
639586Seric **	Returns:
640586Seric **		The pathname of the sccs file.
641586Seric **		NULL on error.
642586Seric **
643586Seric **	Side Effects:
644586Seric **		none.
645586Seric */
646148Seric 
647148Seric char *
648148Seric makefile(name)
649148Seric 	char *name;
650148Seric {
651148Seric 	register char *p;
652148Seric 	char buf[512];
653148Seric 	extern char *malloc();
654586Seric 	extern char *rindex();
655588Seric 	extern bool safepath();
656587Seric 	extern bool isdir();
657587Seric 	register char *q;
658148Seric 
659586Seric 	p = rindex(name, '/');
660586Seric 	if (p == NULL)
661586Seric 		p = name;
662586Seric 	else
663586Seric 		p++;
664586Seric 
665148Seric 	/*
666588Seric 	**  Check to see that the path is "safe", i.e., that we
667588Seric 	**  are not letting some nasty person use the setuid part
668588Seric 	**  of this program to look at or munge some presumably
669588Seric 	**  hidden files.
670148Seric 	*/
671148Seric 
672588Seric 	if (SccsDir[0] == '/' && !safepath(name))
673588Seric 		return (NULL);
674586Seric 
675586Seric 	/*
676588Seric 	**  Create the base pathname.
677586Seric 	*/
678586Seric 
6791438Seric 	/* first the directory part */
680588Seric 	if (SccsDir[0] != '\0' && name[0] != '/' && strncmp(name, "./", 2) != 0)
681148Seric 	{
682588Seric 		strcpy(buf, SccsDir);
683586Seric 		strcat(buf, "/");
684586Seric 	}
685586Seric 	else
686586Seric 		strcpy(buf, "");
6871438Seric 
6881438Seric 	/* then the head of the pathname */
689587Seric 	strncat(buf, name, p - name);
690587Seric 	q = &buf[strlen(buf)];
6911438Seric 
6921438Seric 	/* now copy the final part of the name, in case useful */
693587Seric 	strcpy(q, p);
6941438Seric 
6951438Seric 	/* so is it useful? */
696587Seric 	if (strncmp(p, "s.", 2) != 0 && !isdir(buf))
697586Seric 	{
6981438Seric 		/* sorry, no; copy the SCCS pathname & the "s." */
699588Seric 		strcpy(q, SccsPath);
700588Seric 		strcat(buf, "/s.");
7011438Seric 
7021438Seric 		/* and now the end of the name */
703586Seric 		strcat(buf, p);
704586Seric 	}
705148Seric 
7061438Seric 	/* if i haven't changed it, why did I do all this? */
707588Seric 	if (strcmp(buf, name) == 0)
708588Seric 		p = name;
709588Seric 	else
710148Seric 	{
7111438Seric 		/* but if I have, squirrel it away */
712588Seric 		p = malloc(strlen(buf) + 1);
713588Seric 		if (p == NULL)
714588Seric 		{
715588Seric 			perror("Sccs: no mem");
716588Seric 			exit(EX_OSERR);
717588Seric 		}
718588Seric 		strcpy(p, buf);
719148Seric 	}
7201438Seric 
721148Seric 	return (p);
722148Seric }
7231432Seric 
7241432Seric /*
725587Seric **  ISDIR -- return true if the argument is a directory.
726587Seric **
727587Seric **	Parameters:
728587Seric **		name -- the pathname of the file to check.
729587Seric **
730587Seric **	Returns:
731587Seric **		TRUE if 'name' is a directory, FALSE otherwise.
732587Seric **
733587Seric **	Side Effects:
734587Seric **		none.
735587Seric */
736587Seric 
737587Seric bool
738587Seric isdir(name)
739587Seric 	char *name;
740587Seric {
741587Seric 	struct stat stbuf;
742587Seric 
743587Seric 	return (stat(name, &stbuf) >= 0 && (stbuf.st_mode & S_IFMT) == S_IFDIR);
744587Seric }
7451432Seric 
7461432Seric /*
747586Seric **  SAFEPATH -- determine whether a pathname is "safe"
748586Seric **
749586Seric **	"Safe" pathnames only allow you to get deeper into the
750586Seric **	directory structure, i.e., full pathnames and ".." are
751586Seric **	not allowed.
752586Seric **
753586Seric **	Parameters:
754586Seric **		p -- the name to check.
755586Seric **
756586Seric **	Returns:
757586Seric **		TRUE -- if the path is safe.
758586Seric **		FALSE -- if the path is not safe.
759586Seric **
760586Seric **	Side Effects:
761586Seric **		Prints a message if the path is not safe.
762586Seric */
763586Seric 
764586Seric bool
765586Seric safepath(p)
766586Seric 	register char *p;
767586Seric {
768586Seric 	extern char *index();
769586Seric 
770586Seric 	if (*p != '/')
771586Seric 	{
772586Seric 		while (strncmp(p, "../", 3) != 0 && strcmp(p, "..") != 0)
773586Seric 		{
774586Seric 			p = index(p, '/');
775586Seric 			if (p == NULL)
776586Seric 				return (TRUE);
777586Seric 			p++;
778586Seric 		}
779586Seric 	}
780586Seric 
781586Seric 	printf("You may not use full pathnames or \"..\"\n");
782586Seric 	return (FALSE);
783586Seric }
7841432Seric 
7851432Seric /*
786261Seric **  CLEAN -- clean out recreatable files
787261Seric **
788261Seric **	Any file for which an "s." file exists but no "p." file
789261Seric **	exists in the current directory is purged.
790261Seric **
791261Seric **	Parameters:
7921822Seric **		mode -- tells whether this came from a "clean", "info", or
7931822Seric **			"check" command.
7941822Seric **		argv -- the rest of the argument vector.
795261Seric **
796261Seric **	Returns:
797261Seric **		none.
798261Seric **
799261Seric **	Side Effects:
800819Seric **		Removes files in the current directory.
801819Seric **		Prints information regarding files being edited.
802819Seric **		Exits if a "check" command.
803261Seric */
804261Seric 
8051822Seric clean(mode, argv)
806819Seric 	int mode;
8071822Seric 	char **argv;
808261Seric {
809261Seric 	struct direct dir;
810261Seric 	char buf[100];
811346Seric 	register FILE *dirfd;
812346Seric 	register char *basefile;
813351Seric 	bool gotedit;
8141822Seric 	bool gotpfent;
815394Seric 	FILE *pfp;
8161822Seric 	bool nobranch = FALSE;
8171822Seric 	extern struct pfile *getpfent();
8181822Seric 	register struct pfile *pf;
8191822Seric 	register char **ap;
8201864Seric 	extern char *username();
8211864Seric 	char *usernm = NULL;
822261Seric 
8231438Seric 	/*
8241822Seric 	**  Process the argv
8251822Seric 	*/
8261822Seric 
8271822Seric 	for (ap = argv; *ap != NULL; ap++)
8281822Seric 	{
8291864Seric 		if (**ap == '-')
8301864Seric 		{
8311864Seric 			/* we have a flag */
8321864Seric 			switch ((*ap)[1])
8331864Seric 			{
8341864Seric 			  case 'b':
8351864Seric 				nobranch = TRUE;
8361864Seric 				break;
8371864Seric 
8381864Seric 			  case 'u':
8391864Seric 				if ((*ap)[2] != '\0')
8401864Seric 					usernm = &(*ap)[2];
8411864Seric 				else if (ap[1] != NULL && ap[1][0] != '-')
8421864Seric 					usernm = *++ap;
8431864Seric 				else
8441864Seric 					usernm = username();
8451864Seric 				break;
8461864Seric 			}
8471864Seric 		}
8481822Seric 	}
8491822Seric 
8501822Seric 	/*
8511438Seric 	**  Find and open the SCCS directory.
8521438Seric 	*/
8531438Seric 
8541207Seric 	strcpy(buf, SccsDir);
8551207Seric 	if (buf[0] != '\0')
8561207Seric 		strcat(buf, "/");
8571207Seric 	strcat(buf, SccsPath);
8581438Seric 
8591207Seric 	dirfd = fopen(buf, "r");
860261Seric 	if (dirfd == NULL)
861261Seric 	{
8621207Seric 		usrerr("cannot open %s", buf);
8631282Seric 		return (EX_NOINPUT);
864261Seric 	}
865261Seric 
866261Seric 	/*
867261Seric 	**  Scan the SCCS directory looking for s. files.
8681438Seric 	**	gotedit tells whether we have tried to clean any
8691438Seric 	**		files that are being edited.
870261Seric 	*/
871261Seric 
872351Seric 	gotedit = FALSE;
8731738Seric 	while (fread((char *)&dir, sizeof dir, 1, dirfd) != NULL)
874261Seric 	{
875568Seric 		if (dir.d_ino == 0 || strncmp(dir.d_name, "s.", 2) != 0)
876261Seric 			continue;
877261Seric 
878261Seric 		/* got an s. file -- see if the p. file exists */
8791207Seric 		strcpy(buf, SccsDir);
8801207Seric 		if (buf[0] != '\0')
8811207Seric 			strcat(buf, "/");
8821207Seric 		strcat(buf, SccsPath);
883261Seric 		strcat(buf, "/p.");
884346Seric 		basefile = &buf[strlen(buf)];
885568Seric 		strncpy(basefile, &dir.d_name[2], sizeof dir.d_name - 2);
886346Seric 		basefile[sizeof dir.d_name - 2] = '\0';
8871822Seric 
8881822Seric 		/*
8891822Seric 		**  open and scan the p-file.
8901822Seric 		**	'gotpfent' tells if we have found a valid p-file
8911822Seric 		**		entry.
8921822Seric 		*/
8931822Seric 
894394Seric 		pfp = fopen(buf, "r");
8951822Seric 		gotpfent = FALSE;
896394Seric 		if (pfp != NULL)
897346Seric 		{
8981438Seric 			/* the file exists -- report it's contents */
8991822Seric 			while ((pf = getpfent(pfp)) != NULL)
9001730Seric 			{
9011822Seric 				if (nobranch && isbranch(pf->p_nsid))
9021822Seric 					continue;
9031864Seric 				if (usernm != NULL && strcmp(usernm, pf->p_user) != 0 && mode != CLEANC)
9041864Seric 					continue;
9051822Seric 				gotedit = TRUE;
9061822Seric 				gotpfent = TRUE;
9071822Seric 				if (mode == TELLC)
9081822Seric 				{
9091822Seric 					printf("%s\n", basefile);
9101822Seric 					break;
9111822Seric 				}
9121822Seric 				printf("%12s: being edited: %s %s %s %s %s\n",
9131822Seric 				       basefile, pf->p_osid, pf->p_nsid,
9141822Seric 				       pf->p_user, pf->p_date, pf->p_time);
9151730Seric 			}
916394Seric 			fclose(pfp);
9171822Seric 		}
918261Seric 
919261Seric 		/* the s. file exists and no p. file exists -- unlink the g-file */
920*1870Seric 		if (mode == CLEANC && !gotpfent)
921346Seric 		{
922568Seric 			strncpy(buf, &dir.d_name[2], sizeof dir.d_name - 2);
923346Seric 			buf[sizeof dir.d_name - 2] = '\0';
924346Seric 			unlink(buf);
925346Seric 		}
926261Seric 	}
927261Seric 
9281438Seric 	/* cleanup & report results */
929261Seric 	fclose(dirfd);
930819Seric 	if (!gotedit && mode == INFOC)
9311864Seric 	{
9321864Seric 		printf("Nothing being edited");
9331864Seric 		if (nobranch)
9341864Seric 			printf(" (on trunk)");
9351864Seric 		if (usernm == NULL)
9361864Seric 			printf("\n");
9371864Seric 		else
9381864Seric 			printf(" by %s\n", usernm);
9391864Seric 	}
940819Seric 	if (mode == CHECKC)
941819Seric 		exit(gotedit);
9421282Seric 	return (EX_OK);
943261Seric }
9441432Seric 
9451432Seric /*
9461822Seric **  ISBRANCH -- is the SID a branch?
9471822Seric **
9481822Seric **	Parameters:
9491822Seric **		sid -- the sid to check.
9501822Seric **
9511822Seric **	Returns:
9521822Seric **		TRUE if the sid represents a branch.
9531822Seric **		FALSE otherwise.
9541822Seric **
9551822Seric **	Side Effects:
9561822Seric **		none.
9571822Seric */
9581822Seric 
9591822Seric isbranch(sid)
9601822Seric 	char *sid;
9611822Seric {
9621822Seric 	register char *p;
9631822Seric 	int dots;
9641822Seric 
9651822Seric 	dots = 0;
9661822Seric 	for (p = sid; *p != '\0'; p++)
9671822Seric 	{
9681822Seric 		if (*p == '.')
9691822Seric 			dots++;
9701822Seric 		if (dots > 1)
9711822Seric 			return (TRUE);
9721822Seric 	}
9731822Seric 	return (FALSE);
9741822Seric }
9751822Seric 
9761822Seric /*
977396Seric **  UNEDIT -- unedit a file
978396Seric **
979396Seric **	Checks to see that the current user is actually editting
980396Seric **	the file and arranges that s/he is not editting it.
981396Seric **
982396Seric **	Parameters:
983416Seric **		fn -- the name of the file to be unedited.
984396Seric **
985396Seric **	Returns:
986585Seric **		TRUE -- if the file was successfully unedited.
987585Seric **		FALSE -- if the file was not unedited for some
988585Seric **			reason.
989396Seric **
990396Seric **	Side Effects:
991396Seric **		fn is removed
992396Seric **		entries are removed from pfile.
993396Seric */
994396Seric 
995585Seric bool
996396Seric unedit(fn)
997396Seric 	char *fn;
998396Seric {
999396Seric 	register FILE *pfp;
1000396Seric 	char *pfn;
1001396Seric 	static char tfn[] = "/tmp/sccsXXXXX";
1002396Seric 	FILE *tfp;
1003396Seric 	register char *q;
1004396Seric 	bool delete = FALSE;
1005396Seric 	bool others = FALSE;
1006396Seric 	char *myname;
10071864Seric 	extern char *username();
1008396Seric 	struct pfile *pent;
10091822Seric 	extern struct pfile *getpfent();
1010396Seric 	char buf[120];
10111316Seric 	extern char *makefile();
1012396Seric 
1013396Seric 	/* make "s." filename & find the trailing component */
1014396Seric 	pfn = makefile(fn);
1015586Seric 	if (pfn == NULL)
1016586Seric 		return (FALSE);
1017586Seric 	q = rindex(pfn, '/');
1018586Seric 	if (q == NULL)
1019586Seric 		q = &pfn[-1];
1020586Seric 	if (q[1] != 's' || q[2] != '.')
1021396Seric 	{
10221205Seric 		usrerr("bad file name \"%s\"", fn);
1023585Seric 		return (FALSE);
1024396Seric 	}
1025396Seric 
10261438Seric 	/* turn "s." into "p." & try to open it */
1027396Seric 	*++q = 'p';
1028396Seric 
1029396Seric 	pfp = fopen(pfn, "r");
1030396Seric 	if (pfp == NULL)
1031396Seric 	{
1032416Seric 		printf("%12s: not being edited\n", fn);
1033585Seric 		return (FALSE);
1034396Seric 	}
1035396Seric 
10361438Seric 	/* create temp file for editing p-file */
1037396Seric 	mktemp(tfn);
1038396Seric 	tfp = fopen(tfn, "w");
1039396Seric 	if (tfp == NULL)
1040396Seric 	{
10411205Seric 		usrerr("cannot create \"%s\"", tfn);
1042396Seric 		exit(EX_OSERR);
1043396Seric 	}
1044396Seric 
10451438Seric 	/* figure out who I am */
10461864Seric 	myname = username();
10471438Seric 
10481438Seric 	/*
10491438Seric 	**  Copy p-file to temp file, doing deletions as needed.
10501438Seric 	*/
10511438Seric 
10521822Seric 	while ((pent = getpfent(pfp)) != NULL)
1053396Seric 	{
1054396Seric 		if (strcmp(pent->p_user, myname) == 0)
1055396Seric 		{
1056396Seric 			/* a match */
1057396Seric 			delete++;
1058396Seric 		}
1059396Seric 		else
1060396Seric 		{
10611438Seric 			/* output it again */
1062396Seric 			fprintf(tfp, "%s %s %s %s %s\n", pent->p_osid,
1063396Seric 			    pent->p_nsid, pent->p_user, pent->p_date,
1064396Seric 			    pent->p_time);
1065396Seric 			others++;
1066396Seric 		}
1067396Seric 	}
1068396Seric 
1069396Seric 	/* do final cleanup */
1070396Seric 	if (others)
1071396Seric 	{
10721438Seric 		/* copy it back (perhaps it should be linked?) */
1073396Seric 		if (freopen(tfn, "r", tfp) == NULL)
1074396Seric 		{
10751205Seric 			syserr("cannot reopen \"%s\"", tfn);
1076396Seric 			exit(EX_OSERR);
1077396Seric 		}
1078396Seric 		if (freopen(pfn, "w", pfp) == NULL)
1079396Seric 		{
10801205Seric 			usrerr("cannot create \"%s\"", pfn);
1081585Seric 			return (FALSE);
1082396Seric 		}
1083396Seric 		while (fgets(buf, sizeof buf, tfp) != NULL)
1084396Seric 			fputs(buf, pfp);
1085396Seric 	}
1086396Seric 	else
1087396Seric 	{
10881438Seric 		/* it's empty -- remove it */
1089396Seric 		unlink(pfn);
1090396Seric 	}
1091396Seric 	fclose(tfp);
1092396Seric 	fclose(pfp);
1093396Seric 	unlink(tfn);
1094396Seric 
10951438Seric 	/* actually remove the g-file */
1096396Seric 	if (delete)
1097396Seric 	{
10981435Seric 		unlink(tail(fn));
10991435Seric 		printf("%12s: removed\n", tail(fn));
1100585Seric 		return (TRUE);
1101396Seric 	}
1102396Seric 	else
1103396Seric 	{
1104416Seric 		printf("%12s: not being edited by you\n", fn);
1105585Seric 		return (FALSE);
1106396Seric 	}
1107396Seric }
11081432Seric 
11091432Seric /*
11101433Seric **  DODIFF -- diff an s-file against a g-file
11111433Seric **
11121433Seric **	Parameters:
11131433Seric **		getv -- argv for the 'get' command.
11141433Seric **		gfile -- name of the g-file to diff against.
11151433Seric **
11161433Seric **	Returns:
11171433Seric **		Result of get.
11181433Seric **
11191433Seric **	Side Effects:
11201433Seric **		none.
11211433Seric */
11221433Seric 
11231433Seric dodiff(getv, gfile)
11241433Seric 	char **getv;
11251433Seric 	char *gfile;
11261433Seric {
11271433Seric 	int pipev[2];
11281433Seric 	int rval;
11291433Seric 	register int i;
11301433Seric 	register int pid;
11311433Seric 	auto int st;
11321433Seric 	extern int errno;
11331433Seric 	int (*osig)();
11341433Seric 
11351501Seric 	printf("\n------- %s -------\n", gfile);
11361501Seric 
11371438Seric 	/* create context for diff to run in */
11381433Seric 	if (pipe(pipev) < 0)
11391433Seric 	{
11401433Seric 		syserr("dodiff: pipe failed");
11411433Seric 		exit(EX_OSERR);
11421433Seric 	}
11431433Seric 	if ((pid = fork()) < 0)
11441433Seric 	{
11451433Seric 		syserr("dodiff: fork failed");
11461433Seric 		exit(EX_OSERR);
11471433Seric 	}
11481433Seric 	else if (pid > 0)
11491433Seric 	{
11501433Seric 		/* in parent; run get */
11511433Seric 		OutFile = pipev[1];
11521433Seric 		close(pipev[0]);
11531737Seric 		rval = command(&getv[1], TRUE, "get -s -k -p");
11541433Seric 		osig = signal(SIGINT, SIG_IGN);
11551433Seric 		while (((i = wait(&st)) >= 0 && i != pid) || errno == EINTR)
11561433Seric 			errno = 0;
11571433Seric 		signal(SIGINT, osig);
11581433Seric 		/* ignore result of diff */
11591433Seric 	}
11601433Seric 	else
11611433Seric 	{
11621433Seric 		/* in child, run diff */
11631433Seric 		if (close(pipev[1]) < 0 || close(0) < 0 ||
11641433Seric 		    dup(pipev[0]) != 0 || close(pipev[0]) < 0)
11651433Seric 		{
11661433Seric 			syserr("dodiff: magic failed");
11671433Seric 			exit(EX_OSERR);
11681433Seric 		}
11691433Seric 		execl(PROGPATH(bdiff), "bdiff", "-", gfile, NULL);
11701433Seric # ifndef V6
11711433Seric 		execlp("bdiff", "bdiff", "-", gfile, NULL);
11721433Seric 		execlp("diff", "diff", "-", gfile, NULL);
11731433Seric # endif NOT V6
11741433Seric 		syserr("bdiff: cannot execute");
11751433Seric 		exit(EX_OSERR);
11761433Seric 	}
11771433Seric 	return (rval);
11781433Seric }
11791433Seric 
11801433Seric /*
11811435Seric **  TAIL -- return tail of filename.
11821435Seric **
11831435Seric **	Parameters:
11841435Seric **		fn -- the filename.
11851435Seric **
11861435Seric **	Returns:
11871435Seric **		a pointer to the tail of the filename; e.g., given
11881435Seric **		"cmd/ls.c", "ls.c" is returned.
11891435Seric **
11901435Seric **	Side Effects:
11911435Seric **		none.
11921435Seric */
11931435Seric 
11941435Seric char *
11951435Seric tail(fn)
11961435Seric 	register char *fn;
11971435Seric {
11981435Seric 	register char *p;
11991435Seric 
12001435Seric 	for (p = fn; *p != 0; p++)
12011435Seric 		if (*p == '/' && p[1] != '\0' && p[1] != '/')
12021435Seric 			fn = &p[1];
12031435Seric 	return (fn);
12041435Seric }
12051435Seric 
12061435Seric /*
12071822Seric **  GETPFENT -- get an entry from the p-file
1208396Seric **
1209396Seric **	Parameters:
1210396Seric **		pfp -- p-file file pointer
1211396Seric **
1212396Seric **	Returns:
1213396Seric **		pointer to p-file struct for next entry
1214396Seric **		NULL on EOF or error
1215396Seric **
1216396Seric **	Side Effects:
1217396Seric **		Each call wipes out results of previous call.
1218396Seric */
1219396Seric 
1220396Seric struct pfile *
12211822Seric getpfent(pfp)
1222396Seric 	FILE *pfp;
1223396Seric {
1224396Seric 	static struct pfile ent;
1225396Seric 	static char buf[120];
1226396Seric 	register char *p;
1227396Seric 	extern char *nextfield();
1228396Seric 
1229396Seric 	if (fgets(buf, sizeof buf, pfp) == NULL)
1230396Seric 		return (NULL);
1231396Seric 
1232396Seric 	ent.p_osid = p = buf;
1233396Seric 	ent.p_nsid = p = nextfield(p);
1234396Seric 	ent.p_user = p = nextfield(p);
1235396Seric 	ent.p_date = p = nextfield(p);
1236396Seric 	ent.p_time = p = nextfield(p);
1237396Seric 	if (p == NULL || nextfield(p) != NULL)
1238396Seric 		return (NULL);
1239396Seric 
1240396Seric 	return (&ent);
1241396Seric }
1242396Seric 
1243396Seric 
1244396Seric char *
1245396Seric nextfield(p)
1246396Seric 	register char *p;
1247396Seric {
1248396Seric 	if (p == NULL || *p == '\0')
1249396Seric 		return (NULL);
1250396Seric 	while (*p != ' ' && *p != '\n' && *p != '\0')
1251396Seric 		p++;
1252396Seric 	if (*p == '\n' || *p == '\0')
1253396Seric 	{
1254396Seric 		*p = '\0';
1255396Seric 		return (NULL);
1256396Seric 	}
1257396Seric 	*p++ = '\0';
1258396Seric 	return (p);
1259396Seric }
12601432Seric 
12611432Seric /*
12621205Seric **  USRERR -- issue user-level error
12631205Seric **
12641205Seric **	Parameters:
12651205Seric **		f -- format string.
12661205Seric **		p1-p3 -- parameters to a printf.
12671205Seric **
12681205Seric **	Returns:
12691205Seric **		-1
12701205Seric **
12711205Seric **	Side Effects:
12721205Seric **		none.
12731205Seric */
12741205Seric 
12751738Seric /*VARARGS1*/
12761205Seric usrerr(f, p1, p2, p3)
12771205Seric 	char *f;
12781205Seric {
12791205Seric 	fprintf(stderr, "\n%s: ", MyName);
12801205Seric 	fprintf(stderr, f, p1, p2, p3);
12811205Seric 	fprintf(stderr, "\n");
12821205Seric 
12831205Seric 	return (-1);
12841205Seric }
12851432Seric 
12861432Seric /*
12871205Seric **  SYSERR -- print system-generated error.
12881205Seric **
12891205Seric **	Parameters:
12901205Seric **		f -- format string to a printf.
12911205Seric **		p1, p2, p3 -- parameters to f.
12921205Seric **
12931205Seric **	Returns:
12941205Seric **		never.
12951205Seric **
12961205Seric **	Side Effects:
12971205Seric **		none.
12981205Seric */
12991205Seric 
13001738Seric /*VARARGS1*/
13011205Seric syserr(f, p1, p2, p3)
13021205Seric 	char *f;
13031205Seric {
13041205Seric 	extern int errno;
13051205Seric 
13061205Seric 	fprintf(stderr, "\n%s SYSERR: ", MyName);
13071205Seric 	fprintf(stderr, f, p1, p2, p3);
13081205Seric 	fprintf(stderr, "\n");
13091205Seric 	if (errno == 0)
13101205Seric 		exit(EX_SOFTWARE);
13111205Seric 	else
13121205Seric 	{
13131738Seric 		perror(NULL);
13141205Seric 		exit(EX_OSERR);
13151205Seric 	}
13161205Seric }
13171864Seric /*
13181864Seric **  USERNAME -- return name of the current user
13191864Seric **
13201864Seric **	Parameters:
13211864Seric **		none
13221864Seric **
13231864Seric **	Returns:
13241864Seric **		name of current user
13251864Seric **
13261864Seric **	Side Effects:
13271864Seric **		none
13281864Seric */
13291864Seric 
13301864Seric char *
13311864Seric username()
13321864Seric {
13331864Seric # ifdef UIDUSER
13341864Seric 	extern struct passwd *getpwuid();
13351864Seric 	register struct passwd *pw;
13361864Seric 
13371864Seric 	pw = getpwuid(getuid());
13381864Seric 	if (pw == NULL)
13391864Seric 	{
13401864Seric 		syserr("who are you? (uid=%d)", getuid());
13411864Seric 		exit(EX_OSERR);
13421864Seric 	}
13431864Seric 	return (pw->pw_name);
13441864Seric # else
13451864Seric 	return (getlogin());
13461864Seric # endif UIDUSER
13471864Seric }
1348