-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.c
More file actions
83 lines (66 loc) · 1.38 KB
/
Copy pathprogram.c
File metadata and controls
83 lines (66 loc) · 1.38 KB
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
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
char* executar(int fe,char* linha,char* resultado){
int p[2], p2[2];
pipe(p);
pipe(p2);
char buf[2000]=" ";
printf(">>>\n");
char** comando = agrupaComando(linha);
if(fork()==0){
dup2(p[1],1);
close(p[1]);
close(p[0]);
execvp(comando[0],comando);
_exit(-1);
}
close(p[1]);
read(p[0],buf,sizeof(buf));
printf("%s\n", buf);
wait(NULL);
printf("<<<\n");
resultado = strdup(buf);
return resultado;
}
void lerFicheiro(int fe, int fs) {
int i;
char buffer[6][200];
char* resultado=malloc(sizeof(resultado));
for(i=0;i<6;i++) {
fazEcho(fe,i,buffer[i]);
}
dup2(fs,1);
close(fs);
for(i=0;i<6;i++) {
printf("%s\n", buffer[i]);
if(buffer[i][0] == '$') {
resultado=strdup(executar(fe,buffer[i],resultado));
}
}
}
int main(int argc, char const *argv[]){
int fo, fe, fs;
fo=open("DocumentoTeste",O_RDONLY);
fs=open("DocumentodeSaida",O_CREAT | O_WRONLY | O_TRUNC, 0666);
fe=open("Erros.txt", O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (fe==-1) {
perror("Erro na criação do ficheiro error.txt\n");
_exit(-1);
}
dup2(fe,2);
close(fe);
if (fo==-1) {
perror("Erro ao abrir ficheiro leitura");
_exit(-1);
}
if (fs==-1) {
perror("Erro ao abrir ficheiro escrita");
_exit(-1);
}
lerFicheiro(fo,fs);
}