Skip to content

Instantly share code, notes, and snippets.

@rupertdev
Created February 9, 2016 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupertdev/c1f8bf1a481eb02fc8a0 to your computer and use it in GitHub Desktop.
Save rupertdev/c1f8bf1a481eb02fc8a0 to your computer and use it in GitHub Desktop.
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, char * argv[])
{
pid_t child;
child = fork();
if(child == 0){
char *file = argv[1];
char *arg;
char *args[10];
if(file == NULL){
printf("You must enter a file to be opened.");
exit(0);
}
FILE *fp;
fp=fopen(file, "r");
char *line[128];
while(fgets(line, sizeof (line), fp) != NULL){
if(!strstr(line, "#")){
printf("Command: %s\n", line );
arg = strtok(line, " ");
int i = 0;
while(arg != NULL){
printf("%s\n", arg);
arg = strtok(NULL, " ");
exelcvp(args[0], args);
perror("command failed to execute.");
args[i] = arg;
i++;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment