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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents