xref: /csrg-svn/usr.bin/uucp/port/getwd.c (revision 17835)
113656Ssam #ifndef lint
2*17835Sralph static char sccsid[] = "@(#)getwd.c	5.3 (Berkeley) 01/22/85";
313656Ssam #endif
413656Ssam 
513656Ssam #include "uucp.h"
613656Ssam 
7*17835Sralph /*
8*17835Sralph  *	get working directory
913656Ssam  *
10*17835Sralph  *	return codes  SUCCESS | FAIL
1113656Ssam  */
1213656Ssam 
1313656Ssam gwd(wkdir)
1413656Ssam register char *wkdir;
1513656Ssam {
1613656Ssam 	register FILE *fp;
1713656Ssam 	extern FILE *rpopen();
1813656Ssam 	extern int rpclose();
1913656Ssam 	register char *c;
2013656Ssam 
21*17835Sralph #ifdef BSD4_2
22*17835Sralph 	if (getwd(wkdir) == 0)
23*17835Sralph 		return FAIL;
24*17835Sralph #else !BSD4_2
25*17835Sralph # ifdef VMS
26*17835Sralph 	getwd(wkdir);	/* Call Eunice C library version instead */
27*17835Sralph #else !VMS
2813656Ssam 	*wkdir = '\0';
29*17835Sralph 	if ((fp = rpopen("PATH=/bin:/usr/bin:/usr/ucb;pwd 2>&-", "r")) == NULL)
30*17835Sralph 		return FAIL;
31*17835Sralph 	if (fgets(wkdir, 100, fp) == NULL) {
32*17835Sralph 		rpclose(fp);
33*17835Sralph 		return FAIL;
3413656Ssam 	}
3513656Ssam 	if (*(c = wkdir + strlen(wkdir) - 1) == '\n')
3613656Ssam 		*c = '\0';
3713656Ssam 	rpclose(fp);
38*17835Sralph # endif !VMS
39*17835Sralph #endif !BSD4_2
40*17835Sralph 	return SUCCESS;
4113656Ssam }
4213656Ssam 
4313656Ssam /*
44*17835Sralph  * gwd uses 'reverting' version of popen
4513656Ssam  * which runs process with permissions of real gid/uid
4613656Ssam  * rather than the effective gid/uid.
4713656Ssam  */
4813656Ssam #include <signal.h>
4913656Ssam #define	tst(a,b)	(*mode == 'r'? (b) : (a))
5013656Ssam #define	RDR	0
5113656Ssam #define	WTR	1
5213656Ssam static	int	popen_pid[20];
5313656Ssam 
5413656Ssam FILE *
5513656Ssam rpopen(cmd,mode)
5613656Ssam char	*cmd;
5713656Ssam char	*mode;
5813656Ssam {
5913656Ssam 	int p[2];
6013656Ssam 	register myside, hisside, pid;
6113656Ssam 
6213656Ssam 	if(pipe(p) < 0)
6313656Ssam 		return NULL;
6413656Ssam 	myside = tst(p[WTR], p[RDR]);
6513656Ssam 	hisside = tst(p[RDR], p[WTR]);
6613656Ssam 	if((pid = fork()) == 0) {
6713656Ssam 		/* myside and hisside reverse roles in child */
6813656Ssam 		close(myside);
6913656Ssam 		dup2(hisside, tst(0, 1));
7013656Ssam 		close(hisside);
7113656Ssam 		/* revert permissions */
7213656Ssam 		setgid(getgid());
7313656Ssam 		setuid(getuid());
7413656Ssam 		execl("/bin/sh", "sh", "-c", cmd, (char *)0);
7513656Ssam 		_exit(1);
7613656Ssam 	}
7713656Ssam 	if(pid == -1)
7813656Ssam 		return NULL;
7913656Ssam 	popen_pid[myside] = pid;
8013656Ssam 	close(hisside);
8113656Ssam 	return(fdopen(myside, mode));
8213656Ssam }
8313656Ssam 
8413656Ssam rpclose(ptr)
8513656Ssam FILE *ptr;
8613656Ssam {
8713656Ssam 	register f, r, (*hstat)(), (*istat)(), (*qstat)();
8813656Ssam 	int status;
8913656Ssam 
9013656Ssam 	f = fileno(ptr);
9113656Ssam 	fclose(ptr);
9213656Ssam 	istat = signal(SIGINT, SIG_IGN);
9313656Ssam 	qstat = signal(SIGQUIT, SIG_IGN);
9413656Ssam 	hstat = signal(SIGHUP, SIG_IGN);
9513656Ssam 	while((r = wait(&status)) != popen_pid[f] && r != -1)
9613656Ssam 		;
9713656Ssam 	if(r == -1)
9813656Ssam 		status = -1;
9913656Ssam 	signal(SIGINT, istat);
10013656Ssam 	signal(SIGQUIT, qstat);
10113656Ssam 	signal(SIGHUP, hstat);
102*17835Sralph 	return status;
10313656Ssam }
104