
Untitled
By: a guest on
May 21st, 2012 | syntax:
None | size: 2.00 KB | hits: 11 | expires: Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main(void) {
char input[110];
char *str1;
char *array1[20];
char *arr1, *arr2, *argv1[10],*argv2[10];
int pid,j;
int x = 1;
int fds[2];
system("clear");
do {
printf("AE > ");
fgets(input, 110, stdin);
if(strchr(input,'|') != NULL) {
str1 = strtok(input, "|");
arr1 = str1;
printf("Array 1: %s\n",arr1);
// extract string from string sequence
str1 = strtok(NULL, "|");
arr2=str1;
printf("array 2: %s\n", arr2);
x=0;
str1 = strtok(arr1, " \n");
// loop until finishied
while (str1!=NULL) {
argv1[x]=str1; x++;
// extract string from string sequence
str1 = strtok(NULL, " \n");
}
argv1[x] = NULL;
for(j=0;j<x;j++)
printf("argv1[%d]=%s\n",j,argv1[j]);
str1 = strtok(arr2, " \n");
x=0;
// loop until finishied
while (str1!=NULL) {
argv2[x]=str1; x++;
// extract string from string sequence
str1 = strtok(NULL, " \n");
}
argv2[x] = NULL;
for(j=0;j<x;j++)
printf("argv2[%d]=%s\n",j,argv2[j]);
pipe(fds);
if(fork()==0) {
close(fds[0]);
dup2(fds[1],STDOUT_FILENO);
close(fds[1]);
execvp(argv1[0], argv1);
} else {
if(fork()==0) {
close(fds[1]);
dup2(fds[0],STDIN_FILENO);
close(fds[0]);
execvp(argv2[0], argv2);
} else {
close(fds[0]);
close(fds[1]);
wait();
wait();
}
}
} else {
char *token, *arr1, *arr2, *argv1[10], *argv2[10];
int i, a, b, n;
int fds[2];
i=0 , a=0, b=0;
token = strtok(input," \n");
argv1[a] = token; a++;
while(token!=NULL) {
token = strtok(NULL," \n");
argv1[a] = token; a++;
}
for(n=0; n < a; n++)
printf("argv1[%d] = %s\n", n, argv1[n]);
pipe(fds);
if(fork() == 0) {
execvp(argv1[0],argv1);
exit(0);
} else {
close(fds[1]);
wait(NULL);
}
}
} while (!feof(stdin));
return 0;
}