-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredirection.c
More file actions
84 lines (69 loc) · 1.84 KB
/
Copy pathredirection.c
File metadata and controls
84 lines (69 loc) · 1.84 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
84
#include"headers.h"
int check_redirection(char* input){
for (int i=0;i<strlen(input);i++){
if (input[i]=='>'){
return 1;
}
}
return 0;
}
void redirect(char * input) {
int flag=0;
char cmm[1000];
char op_file[1000];
for (int i=0;i<strlen(input);i++){
if (input[i]=='>'){
cmm[i]='\0';
i+=1;
if (i==strlen(input)){
printf("specify output file name\n");
return;
}
else if (input[i]=='>'){
flag=1;
i+=1;
}
int k=0;
while(i<strlen(input)){
op_file[k++]=input[i++];
}
op_file[k]='\0';
break;
}
else{
cmm[i]=input[i];
}
}
pid_t pid = fork();
if (pid == -1) {
perror("Fork failed");
exit(EXIT_FAILURE);
} else if (pid == 0) {
int outputFile;
if (flag==0){
outputFile = open(op_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
}
else{
outputFile = open(op_file, O_WRONLY | O_CREAT | O_APPEND, 0644);
}
if (outputFile == -1) {
perror("Failed to open output file");
exit(EXIT_FAILURE);
}
dup2(outputFile, STDOUT_FILENO);
close(outputFile);
int execResult = execlp("/bin/sh", "sh", "-c", cmm, NULL);
if (execResult == -1) {
perror("Exec failed");
exit(EXIT_FAILURE);
}
} else {
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
printf("Command exited with status %d\n", WEXITSTATUS(status));
} else {
printf("Command did not exit normally\n");
}
}
}