1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
| int main(int argc, char * argv[])
{ decouper(getenv("PATH"), ":", dirs, MaxDirs);
for(printf(PROMPT); fgets(ligne, sizeof ligne, stdin) != 0; printf(PROMPT))
{ decouper(ligne, " \t\n", mot, MaxMot);
if (mot[0] == 0)
continue;
if (strcmp(mot[0], "moncd") == 0)
{ if (mot[1] == NULL)
{ dir = getenv("HOME");
if (dir == 0)
{ dir = "/tmp";
}
}
else if (mot[2] != NULL)
{ fprintf(stderr, "usage: %s [dir]\n", mot[0]);
continue;
}
else dir = mot[1];
t = chdir(dir);
if (t < 0)
{ perror(dir);
}
continue;
}
for (cmp = 0; mot[cmp]; cmp++)
{ if(mot[cmp] != NULL && strcmp(mot[cmp], tube) == 0)
{ mot[cmp] = NULL;
mot2[0] = mot[cmp + 1];
mot2[1] = NULL;
if (pipe(pfd) == -1)
{ perror("pipe");
continue;
}
if ((tmp = fork()) < 0)
{ perror("fork");
continue;
}
if (tmp == 0)
{ close(STDOUT_FILENO);
dup(pfd[1]);
close(pfd[0]);
close(pfd[1]);
for(i = 0; dirs[i] != 0; i++)
{ snprintf(pathname, sizeof pathname, "%s/%s", dirs[i], mot[0]);
execv(pathname, mot);
}
}
else
{ close(STDIN_FILENO);
dup(pfd[0]);
close(pfd[0]);
close(pfd[1]);
for(i = 0; dirs[i] != 0; i++)
{ snprintf(pathname, sizeof pathname, "%s/%s", dirs[i], mot2[0]);
execv(pathname, mot2);
}
}
}
}
verif_esperluette();
verif_redirection();
tmp = fork();
if (tmp < 0)
{ perror("fork");
continue;
}
if ((tmp != 0) && (test == 1))
{ while(wait(0) != tmp);
continue;
}
if((tmp != 0) && (test == 0))
{ continue;
}
if (fichier_1 == 1)
{ out = freopen(path_out, "w+", stdout);
}
if ((fichier_1 == 1) && (fichier_2 == 1))
{ in = freopen(path_in, "r", stdin);
out = freopen(path_out, "w+", stdout);
}
if (fichier_2 == 1)
{ in = freopen(path_in, "r", stdin);
}
for(i = 0; dirs[i] != 0; i++)
{ snprintf(pathname, sizeof pathname, "%s/%s", dirs[i], mot[0]);
execv(pathname, mot);
}
fprintf(stderr, "%s: not found\n", mot[0]);
exit(1);
}
printf("Bye\n");
return 0;
} |
Partager