我们无法完成我们的课程代码。
在这个项目中,我们必须:
这是我们到目前为止所做的:
#include <stdio.h>
#include <memory.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
/* creation of a variable for the exec */
char chemin[5] = "/bin/";
char *cmd[argc - 1];
int i = 0;
int fils;
int mainPipe[2];
/* Creation of the pipe */
if (pipe(mainPipe) == -1) {
printf("pipe failed\n");
return 1;
}
/* Creation of the child process */
fils = fork();
if (fils < 0) {
printf("fork failed\n");
return 2;
}
if (fils == 0) {
/* Child Process */
/* We redirect the exit to the pipe*/
dup2(mainPipe[1], 1);
/* Formulation of the exec */
for (i = 0; i <= argc - 1; i++) {
cmd[i] = argv[i + 1];
}
strcat(chemin, argv[1]);
execv(chemin, cmd);
} else {
/* Parent process */
char *buffer[BUFSIZ];
/* Reception of the command */
/* We redirect to the pipe */
dup2(mainPipe[0],0);
/* We read the entry in the pipe and put it in the buffer */
/* Display of the Buffer */
while (read(mainPipe[0], buffer, BUFSIZ) != 0) {
write(1, buffer, BUFSIZ);
break;
}
}
return 0;
}