| 1 | #ifndef lint |
| 2 | static char sccsid[] = "@(#)getwd.c 5.3 (Berkeley) %G%"; |
| 3 | #endif |
| 4 | |
| 5 | #include "uucp.h" |
| 6 | |
| 7 | /* |
| 8 | * get working directory |
| 9 | * |
| 10 | * return codes SUCCESS | FAIL |
| 11 | */ |
| 12 | |
| 13 | gwd(wkdir) |
| 14 | register char *wkdir; |
| 15 | { |
| 16 | register FILE *fp; |
| 17 | extern FILE *rpopen(); |
| 18 | extern int rpclose(); |
| 19 | register char *c; |
| 20 | |
| 21 | #ifdef BSD4_2 |
| 22 | if (getwd(wkdir) == 0) |
| 23 | return FAIL; |
| 24 | #else !BSD4_2 |
| 25 | # ifdef VMS |
| 26 | getwd(wkdir); /* Call Eunice C library version instead */ |
| 27 | #else !VMS |
| 28 | *wkdir = '\0'; |
| 29 | if ((fp = rpopen("PATH=/bin:/usr/bin:/usr/ucb;pwd 2>&-", "r")) == NULL) |
| 30 | return FAIL; |
| 31 | if (fgets(wkdir, 100, fp) == NULL) { |
| 32 | rpclose(fp); |
| 33 | return FAIL; |
| 34 | } |
| 35 | if (*(c = wkdir + strlen(wkdir) - 1) == '\n') |
| 36 | *c = '\0'; |
| 37 | rpclose(fp); |
| 38 | # endif !VMS |
| 39 | #endif !BSD4_2 |
| 40 | return SUCCESS; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * gwd uses 'reverting' version of popen |
| 45 | * which runs process with permissions of real gid/uid |
| 46 | * rather than the effective gid/uid. |
| 47 | */ |
| 48 | #include <signal.h> |
| 49 | #define tst(a,b) (*mode == 'r'? (b) : (a)) |
| 50 | #define RDR 0 |
| 51 | #define WTR 1 |
| 52 | static int popen_pid[20]; |
| 53 | |
| 54 | FILE * |
| 55 | rpopen(cmd,mode) |
| 56 | char *cmd; |
| 57 | char *mode; |
| 58 | { |
| 59 | int p[2]; |
| 60 | register myside, hisside, pid; |
| 61 | |
| 62 | if(pipe(p) < 0) |
| 63 | return NULL; |
| 64 | myside = tst(p[WTR], p[RDR]); |
| 65 | hisside = tst(p[RDR], p[WTR]); |
| 66 | if((pid = fork()) == 0) { |
| 67 | /* myside and hisside reverse roles in child */ |
| 68 | close(myside); |
| 69 | dup2(hisside, tst(0, 1)); |
| 70 | close(hisside); |
| 71 | /* revert permissions */ |
| 72 | setgid(getgid()); |
| 73 | setuid(getuid()); |
| 74 | execl("/bin/sh", "sh", "-c", cmd, (char *)0); |
| 75 | _exit(1); |
| 76 | } |
| 77 | if(pid == -1) |
| 78 | return NULL; |
| 79 | popen_pid[myside] = pid; |
| 80 | close(hisside); |
| 81 | return(fdopen(myside, mode)); |
| 82 | } |
| 83 | |
| 84 | rpclose(ptr) |
| 85 | FILE *ptr; |
| 86 | { |
| 87 | register f, r, (*hstat)(), (*istat)(), (*qstat)(); |
| 88 | int status; |
| 89 | |
| 90 | f = fileno(ptr); |
| 91 | fclose(ptr); |
| 92 | istat = signal(SIGINT, SIG_IGN); |
| 93 | qstat = signal(SIGQUIT, SIG_IGN); |
| 94 | hstat = signal(SIGHUP, SIG_IGN); |
| 95 | while((r = wait(&status)) != popen_pid[f] && r != -1) |
| 96 | ; |
| 97 | if(r == -1) |
| 98 | status = -1; |
| 99 | signal(SIGINT, istat); |
| 100 | signal(SIGQUIT, qstat); |
| 101 | signal(SIGHUP, hstat); |
| 102 | return status; |
| 103 | } |