question about popen in C

does popen print out the executed string result in stdout, or just evaluate it and not print the result?

I'm not sure what you're even asking. You fread() the output of the program from the handle it gives you if that's what you mean.

if making a function popen using pipes/forks, then when calling that function giving "r" and the string, that means that the result of the string will go to the write end of the pipe, and a pointer to the read end of the pipe will be returned?

Also if given "w" instead of "r", then where does the result of the string go? I think that its supposed to return a pointer to the write end of the pipe... But I don't understand where the string output goes...

Ah, yes.

If you open it with "r", you're given the read-end of a pipe connected to its stdout.

if you open it with "w", you're given the write-end of a pipe connected to its stdin. I think the output is discarded (but aren't 100% sure -- it may go to stdout). Stuff written to stdout isn't a 'string' by the way -- string means something specific and that's not it.

Note that this is not quite the usual file-handle. You have to call pclose() on it, not fclose(), and nonstandard operations on it like fileno() may not do quite what you expect.

I'm not sure if this is right...
for the 'w' part i am not doing execl since i dont know where it goes

You don't have to write your own popen(), stdio already has one.

Have you actually tried this code? It'll crash, because

return &fd[0]; 

That doesn't make a FILE pointer, just an integer pointer, since fd isn't a FILE. You convert a file number into a FILE * with a command like fdopen(fd, "r");

You also forgot to close the end of the pipe you're not using again.

This doesn't work either:

(*type == "r")

You don't compare strings that way.

if(strcmp(type, "r") == 0)
{
...
}

...and you have no way to wait for the process on close now, because you didn't store the PID anywhere. stdio popen() can do that and remember, but you can't just from a FILE *.

Honestly I think some review of the C language would do you good before you try to tackle multiprocessing.

so the returns statements are like this: (unless i did something wrong again)
return fdopen(fd[0], "r");
return fdopen(fd[1], "w");
respectively

and just to be clear again, when given 'w', the output of the string gets discarded or printed to stdout?

Yes, that looks more like it.

I don't see that you've redirected stdout for the "w" case, so it'd go to the terminal as usual.

It depends on the caller's file descriptors and their flags.

So, as usual with fork/exec, the child will inherit all of its parents file descriptors that are not marked close-on-exec (FD_CLOEXEC). Additionally, there's the possibility of redirections in the command passed to "sh".

Regards,
Alister

you are comparing a character(typically an integer) with a string (char *) . This is illegal, you should better try

Note the difference b/w "r" and 'r'.

There are other problems yet with the code.

Thanks,
Gaurav.

i have another question
this is what i have
When given "r", the child process sets fd[1] to stdout and writes the result of the string to that end of the pipe, and closes both ends of the pipe, while the parent closes the write end and sends the read end back

But when given "w", i know that the result of the string gets printed to stdout, but i dont understand what to close in the child process... The child is just writing the result to stdout, so do I need a dup2 there? Can i close both ends of the pipe for the child? For the parent i think i just have to close the read end and return the write end. but i'm confused as to what to close for the child process...

You asked many questions but I don't know what are you trying.
Do you try to open a program and redirect both stdin and stdout?

I've got a function that does that pretty well, so if you are trying that ask me. If not, try to explain what are your objective.

trying to make a function that does what popen does

Spoiler:

Koders Code Search: popen.c - C - BSD

Check that, it might help you.

i made a function similar to peopen, how can i test if it works?

    FILE *file_pointer; char output[512];
    file_pointer = popen_new(COMMAND, "r");
    size_t bytes = read(fileno(file_pointer), output, 512); 
    printf(output);
    pclose_new(file_pointer);

this gave me the expected output

    file_pointer = popen_new(TEST_COMMAND, "w");
    write(fileno(file_pointer), output, bytes); 
    pclose_new(file_pointer);

this one i also get expected results, but i am still not sure if its right, the output was the result of the COMMAND string, but what can i do after writing to see if it worked, i cant read it since its in write mode...

Redirect its output into a file?

Or just leave its stdout unredirected, to write to console?

the output is coming to stdout and i can see it, does this mean it works exactly like what popen does with a "w" argument?, i get no warnings or errors, and the code is pretty much what you saw before. except i changed it from structures to
arrays, because i know i will at most have 20 streams

so i have a int array, a int variable, and a file pointer array

int count = 0;
FILE *fp_array[25];
pid_t pid_array[25];

these are global

fp_array[count] = open_file;
pid_array[count\+\+] = pid;

this happens when i use popen_new

and then when i pclose_new
i loop theough fp_array and see if the value matched the given fp, and if so i have access to its pid, so i can wait for the process to finish using a while loop and waitpid, also close it, and then return the status variable since thats what i code i posted up before did.

this is my pclose_new
also when getting the exit code using pclose_new for the popen_new using "r" i get the return to be 141 but when used by "w" i get 0.....
why am i getting 141

also can a pointer to a file ever be the value 0 and can a pid value ever be 0?

Huh. Actually, you can count on a global variable being NULL or zero when the program starts.

You should probably use WEXITSTATUS(status).

I think it works like it does with popen. You might want to actually try popen to be sure.

what about

also when getting the exit code using pclose_new for the popen_new using "r" i get the return to be 141 but when used by "w" i get 0.....
why am i getting 141

also can a pointer to a file ever be the value 0 and can a pid value ever be 0?

I can't tell from here. Please post your current code and exactly what you did with it.

No. Then you wouldn't be able to tell it apart from NULL.

No.