1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <sys/stat.h>
#include <errno.h>
#if HAVE_FCNTL_H
#include <fcntl.h>
#endif
#include "libhdr.h"
#define STDIN 0
#define STDOUT 1
#define READ 0
#define WRITE 1
static pid_t childpid = 0;
static void dupto (const int from,const int to,const char *msg)
{
int err;
if(from!=to) {
if(errno) perror("aa:");
err=close(to);
if (err<0 && errno != EBADF) {
perror("dupto(): Cannot close file descriptor");
exit(EXIT_FAILURE);
}
if(errno) perror("AA:");
err=dup(from);
if(errno) perror("BB:");
if (err!=to) {
(void)fprintf(stderr,"dupto(): Cannot dup %s\n",msg);
exit(EXIT_FAILURE);
}
if(close(from)<0) {
(void)fprintf(stderr,"dupto(): cannot close file descriptor\n");
exit(EXIT_FAILURE);
}
if(errno) perror("CC:");
}
}
int child_open1(const int read_flag,const char *fname,const char *filterprog,const char *arg)
{
int ppipe[2]={-1,-1},fd= -1,fd1;
struct stat sbuf;
if(!fname) return fd;
if(read_flag==READ) if(stat(fname,&sbuf)) return fd;
if(pipe(ppipe)<0) {
(void)fprintf(stderr,"child_open(): Can't open pipe\n");
return fd;
}
childpid=fork();
if(childpid<0) {
(void)fprintf(stderr, "child_open(): cannot fork\n");
return fd;
}
if(childpid>0) {
if(read_flag==READ) {
fd=ppipe[READ];
if(close(ppipe[WRITE])<0) {
(void)fprintf(stderr, "child_open(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
} else {
fd=ppipe[WRITE];
if(close(ppipe[READ])<0) {
(void)fprintf(stderr, "child_open(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
}
} else {
if(read_flag==READ) {
dupto(ppipe[WRITE], STDOUT,"(child) pipr to stdout");
if(close(ppipe[READ])<0) {
(void)fprintf(stderr, "child_open(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
if(fname) {
fd1=open(fname,O_RDONLY,0666);
if(fd1<0) {
(void)fprintf(stderr, "child_open(): cannot open file %s\n",fname);
exit(EXIT_FAILURE);
}
dupto(fd1,STDIN,"file to stdin");
}
} else {
dupto (ppipe[READ], STDIN, "(child) pipe to stdin");
if(close(ppipe[WRITE])<0) {
(void)fprintf(stderr, "child_open(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
if(fname) {
fd1=creat(fname,0666);
if(fd1<0) {
(void)fprintf(stderr, "child_open(): cannot open file %s\n",fname);
exit(EXIT_FAILURE);
}
dupto(fd1,STDOUT,"file to stdout");
}
}
if(read_flag==READ) (void)execlp(filterprog,filterprog,arg,(char *)0);
else (void)execlp(filterprog,filterprog,arg,(char *) 0);
(void)fprintf(stderr, "child_open(): cannot exec %s\n",filterprog);
_exit(EXIT_FAILURE);
}
return fd;
}
int child_open(const int read_flag,const char *fname,const char *filterprog)
{
int fd;
if(read_flag==READ) fd=child_open1(read_flag,fname,filterprog,"-d");
else fd=child_open1(read_flag,fname,filterprog,0);
return fd;
}
#if !HAVE_POPEN
FILE *popen(const char *command,const char *type)
{
int ppipe[2]={-1,-1},read_flag,fd;
FILE *fptr=0;
if(!command) return fptr;
if(!strcmp("r",type)) read_flag=READ;
else if(!strcmp("w",type)) read_flag=WRITE;
else return fptr;
if(pipe(ppipe)<0) {
(void)fprintf(stderr,"lk_popen(): Can't open pipe\n");
return fptr;
}
childpid=fork();
if(childpid<0) {
(void)fprintf(stderr, "lk_popen(): cannot fork\n");
return fptr;
}
if(childpid>0) {
if (read_flag==READ) {
fd = ppipe[READ];
if(close(ppipe[WRITE])<0) {
(void)fprintf(stderr, "lk_popen(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
} else {
fd = ppipe[WRITE];
if(close(ppipe[READ])<0) {
(void)fprintf(stderr, "lk_popen(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
}
fptr=fdopen(fd,type);
if(!fptr) {
(void)fprintf(stderr, "lk_popen(): fdopen() failed\n");
exit(EXIT_FAILURE);
}
} else {
if(read_flag==READ) {
dupto (ppipe[WRITE], STDOUT, "(child) pipe to stdout");
if(close(ppipe[READ])<0) {
(void)fprintf(stderr, "lk_popen(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
} else {
dupto (ppipe[READ], STDIN, "(child) pipe to stdin");
if(close(ppipe[WRITE])<0) {
(void)fprintf(stderr, "lk_popen(): cannot close pipe\n");
exit(EXIT_FAILURE);
}
}
(int)execl("/bin/sh","sh","-c",command,(char *)0);
(void)fprintf(stderr, "lk_popen(): cannot exec %s\n",command);
_exit(EXIT_FAILURE);
}
return fptr;
}
#endif
|