logo

Executing programs vs executing scripts in C

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main(int argc, char* argv[]) { int pid = fork(); if (pid == -1) { return 1; } if (pid == 0) { // This execlp doesn't work as intended int err = execlp("ping", "ping", "-c", "3", "google.com", "|", "grep", "\"rtt\"", NULL); if (err == -1) { printf("Could not find program to execute or other error ocurred\n"); return 2; } } else { int wstatus; wait(&wstatus); if (WIFEXITED(wstatus)) { int statusCode = WEXITSTATUS(wstatus); if (statusCode == 0) { printf("Success\n"); } else { printf("Failure with status code %d\n", statusCode); } } } return 0; }
Course thumb

The C programming language made simple