-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPunto 3
129 lines (116 loc) · 3.23 KB
/
Punto 3
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#define READ 0
#define WRITE 1
int main(int argc, char *argv[]){
if(strcmp(argv[3],"|") != 0 && strcmp(argv[3],">") != 0){
char lim[] = " ";
char *argsT[3];
char *tok = strtok(argv[1],lim);
int z=0;
if(tok != NULL){
while(tok != NULL){
argsT[z]= tok;
tok = strtok(NULL, lim);
z++;
}
}
argsT[2]=NULL;
execvp(argsT[0], argsT);
}//cierre de minishell con un comando
else {//Inicia minishell si se paso pipe
if(strcmp(argv[3],"|") == 0){
pid_t pid;
int fd[2];
char delimitador[] = " ";
if (pipe(fd) == -1) {
perror("Creating pipe");
exit(EXIT_FAILURE);
}
switch(pid = fork()) {
case 0:
// The child process will execute wc.
// Close the pipe write descriptor.
close(fd[WRITE]);
// Redirect STDIN to read from the pipe.
dup2(fd[READ], STDIN_FILENO);
//split the arguments of argv
char *myargs2[3];
char *token2 = strtok(argv[2], delimitador);
int k=0;
if(token2 != NULL){
while(token2 != NULL){
// Sólo en la primera pasamos la cadena; en las siguientes pasamos NULL
myargs2[k]= token2;
token2 = strtok(NULL, delimitador);
k++;
}
}
myargs2[2]= NULL;
// Execute wc -l
execvp(myargs2[0],myargs2);
case -1:
perror("fork() failed)");
exit(EXIT_FAILURE);
default:
// The parent process will execute ls.
// Close the pipe read descriptor.
close(fd[READ]);
// Redirect STDOUT to write to the pipe.
dup2(fd[WRITE], STDOUT_FILENO);
//split the first string (command) to execute.
char *myargs[3];
char *token = strtok(argv[1], delimitador);
int j=0;
if(token != NULL){
while(token != NULL){
// Sólo en la primera pasamos la cadena; en las siguientes pasamos NULL
myargs[j]= token;
token = strtok(NULL, delimitador);
j++;
}
}
myargs[2]= NULL;
//printf("%s" myar);
// cat p4.c
execvp(myargs[0], myargs);
}
}//cierra if del caso minishell pipe
else{ if(strcmp(argv[3],">") == 0){
char limite[] = " ";
char *argsU[3];
char *toke = strtok(argv[1],limite);
int x=0;
if(toke != NULL){
while(toke != NULL){
argsU[x]= toke;
toke = strtok(NULL, limite);
x++;
}
}
argsU[2]=NULL;
int rc = fork();
if (rc < 0) {
// fork failed; exit
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
// child: redirect standard output to a file
close(STDOUT_FILENO);
open(argv[2], O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);
// now exec "comando"...
execlp(strcat("./", argsU[0]), argsU[0], argsU[1], NULL);
} else {
// parent goes down this path (original process)
int wc = wait(NULL);
assert(wc >= 0);
}
}//cierra if caso redireccion minishell
}
}//cierra else despues del primer caso de minishell
return 0;
}//Cierre del main