Problems implementing popen()

R

Rob Hoelz

Hello everyone,

I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

FILE *mypopen(const char *file, const char **argv)
{
int pid;
int filedes[2];

if(pipe(filedes))
return NULL;

pid = fork();
if(pid == -1) {
return NULL;
} else if(! pid) {
close(filedes[1]);
dup2(filedes[0], fileno(stdout));
execvp(file, argv);
} else {
close(filedes[0]);
return fdopen(filedes[1], "r");
}
}

int main(int argc, char **argv)
{
FILE *file;
const char *args[3] = {"hg", "status", NULL};
int inchar;

file = mypopen("hg", args);
if(! file) {
return 1;
} else {
inchar = fgetc(file);
while(inchar != EOF) {
fputc(inchar, stdout);
inchar = fgetc(file);
}
fclose(file);
}
return 0;
}

Thanks,
Rob Hoelz
 
C

CBFalconer

Rob said:
I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

FILE *mypopen(const char *file, const char **argv)
{
int pid;
int filedes[2];

if(pipe(filedes))
return NULL;

pid = fork();

Wrong newsgroup. So far unistd.h, pipe(), fork() etc. are all
unknown to standard C. Try comp.unix.programmer. My guess is that
you can't write portable system functions.
 
R

Rob Hoelz

Rob Hoelz said:
Hello everyone,

I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

FILE *mypopen(const char *file, const char **argv)
{
int pid;
int filedes[2];

if(pipe(filedes))
return NULL;

pid = fork();
if(pid == -1) {
return NULL;
} else if(! pid) {
close(filedes[1]);
dup2(filedes[0], fileno(stdout));
execvp(file, argv);
} else {
close(filedes[0]);
return fdopen(filedes[1], "r");
}
}

int main(int argc, char **argv)
{
FILE *file;
const char *args[3] = {"hg", "status", NULL};
int inchar;

file = mypopen("hg", args);
if(! file) {
return 1;
} else {
inchar = fgetc(file);
while(inchar != EOF) {
fputc(inchar, stdout);
inchar = fgetc(file);
}
fclose(file);
}
return 0;
}

Thanks,
Rob Hoelz

Nevermind...I need to learn to read man pages more closely...
 
K

Keith Thompson

Rob Hoelz said:
I've been trying to roll my own popen(), but I've run into problems.
Would anyone care to look at my code and tell me what I'm doing wrong?

Here's my code:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>
[snip]


pipe(), fork(), et al are not defined by the C standard.
Try comp.unix.programmer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top