Run process
suggest change#define _POSIX_C_SOURCE 200809L /* declare popen/pclose in strict ISO modes */
#include <stdio.h>
void print_all(FILE *stream)
{
int c;
while ((c = getc(stream)) != EOF)
putchar(c);
}
int main(void)
{
FILE *stream;
/* call netstat command. netstat is available for Windows and Linux */
if ((stream = popen("netstat", "r")) == NULL)
return 1;
print_all(stream);
pclose(stream);
return 0;
}
This program runs netstat through popen(), reads the process’s standard output, and writes it to the program’s standard output. pclose should be checked as well: it returns -1 if it could not collect the process status.
Note: popen() is not part of the ISO C library. It is a POSIX function. The "netstat" command must exist on the host.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents