-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshutility.c
More file actions
178 lines (154 loc) · 4.36 KB
/
shutility.c
File metadata and controls
178 lines (154 loc) · 4.36 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <stdio.h>
#include <pwd.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <wait.h>
#define max_hname 128
#define max_path 256
#define max_cwd 256
#define READ_END 0
#define WRITE_END 1
void display_info()
{
struct passwd *p;
char hostname[max_hname] = {'\0'};
char cwd[max_cwd] = {'\0'};
char *token;
const char delimiter[2] = "/";
getcwd(cwd, sizeof(cwd));
/* get the first token */
token = strtok(cwd, delimiter);
/* walk through other tokens */
while ( token != NULL ) {
token = strtok(NULL, delimiter);
if (token != NULL)
strcpy(cwd,token);
}
p = getpwuid(getuid());
gethostname(hostname, max_hname);
printf("[%s@%s %s]# ",p->pw_name, hostname, cwd);
}
void redirection_input(char *word1, char *word2)
{
pid_t cpid;
int rtrnstatus;
cpid = fork();
if (cpid < 0) {
perror("fork failed");
return;
}
else if (cpid == 0) {
execlp(word1, word1, word2, NULL);
}
else {
/* Wait for child process to finish */
waitpid(cpid, &rtrnstatus, 0);
if (WIFEXITED(rtrnstatus))
printf("Child's exit code %d\n", WEXITSTATUS(rtrnstatus));
else
printf("Child did not terminate with exit\n");
}
}
void redirection_output(char *word1, char *argument, char *word2)
{
pid_t cpid;
int rtrnstatus;
int outpt;
int save_out;
outpt = open(word2, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
if (-1 == outpt) {
perror(word2);
return;
}
/* Save standard output */
save_out = dup(fileno(stdout));
if (-1 == dup2(outpt, fileno(stdout))) {
perror("Cannot redirect stdout");
return;
}
cpid=fork();
if (cpid < 0 ) {
perror("Fork failed");
return;
}
else if (cpid == 0) {
if (strcmp(argument, "\0") == 0)
execlp(word1, word1, NULL);
else
execlp(word1, word1, argument, NULL);
}
else {
/* Wait for child process to finish */
waitpid(cpid, &rtrnstatus, 0);
/* Restore standard output */
dup2(save_out, fileno(stdout));
close(save_out);
close(outpt);
if (WIFEXITED(rtrnstatus))
printf("Child's exit code %d\n", WEXITSTATUS(rtrnstatus));
else
printf("Child did not terminate with exit\n");
}
}
void handle_pipe(char *word1, char *pipearg1, char *word2, char *pipearg2)
{
int pipefd[2];
int status;
pid_t pid1, pid2;
/* Create Pipe */
if (pipe(pipefd) == -1) {
perror("pipe");
_exit(0);
}
/* First fork */
pid1 = fork();
if (pid1 == -1) {
perror("fork");
_exit(0);
}
/* CHILD 1 */
if (pid1 == 0) {
if (dup2(pipefd[WRITE_END], STDOUT_FILENO) == -1) {
perror("dup2");
_exit(0);
}
/* Don't need pipe READ_END */
close(pipefd[READ_END]);
if (pipefd[WRITE_END] != STDOUT_FILENO)
close(pipefd[WRITE_END]);
/* Execute depending on argument */
if(strcmp(pipearg1, "\0") == 0)
execlp(word1, word1, NULL);
else
execlp(word1, word1, pipearg1, NULL);
}
close(pipefd[WRITE_END]);
/* Second fork */
pid2 = fork();
if (pid2 == -1) {
perror("fork");
kill(pid1, SIGTERM);
_exit(0);
}
/* CHILD 2 */
if (pid2 == 0) {
if (dup2(pipefd[READ_END], STDIN_FILENO) == -1) {
perror("dup2");
/* Kill first process */
kill(pid1, SIGTERM);
_exit(0);
}
if (pipefd[READ_END] != STDIN_FILENO)
close(pipefd[READ_END]);
/* Execute depending on argument */
if(strcmp(pipearg2, "\0") == 0)
execlp(word2, word2, NULL);
else
execlp(word2, word2, pipearg2, NULL);
}
close (pipefd[READ_END]);
/* Wait for childs */
waitpid(pid1, NULL, 0);
waitpid(pid2, &status, 0);
}