Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell if popen() was successful?

Tags:

c++

c

shell

popen

How can you know if popen() succeeded/done running the shell or failed?

like image 424
user1104856 Avatar asked Dec 22 '22 04:12

user1104856


2 Answers

popen return value:

Upon successful completion, popen() shall return a pointer to an open stream that can be used to read or write to the pipe. Otherwise, it shall return a null pointer and may set errno to indicate the error.

fp = popen("ls *", "r");
if (fp == NULL)
    /* Handle error */;
like image 193
Igor Avatar answered Dec 23 '22 17:12

Igor


Assuming you mean running popen in C code, you can check the man page. It says that popen will return NULL if it fails, and it returns a file stream otherwise.

like image 29
Derek Avatar answered Dec 23 '22 16:12

Derek