-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1a.c
executable file
·229 lines (218 loc) · 4.79 KB
/
lab1a.c
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include <getopt.h>
#include <libgen.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include <termios.h>
#include <signal.h>
#include <pthread.h>
struct termios original_attributes;
int childPid;
int controlD;
int input_pipe[2];
int output_pipe[2];
struct arg_struct {
int src;
int dst;
};
void orig_mode(void)
{
tcsetattr(STDIN_FILENO, TCSANOW, &original_attributes);
}
void handler(int sig)
{
kill(childPid,SIGINT);
}
void sigPipehandler()
{
exit(1);
}
void shell_status(void){
pid_t checkID;
int status;
checkID = waitpid(childPid, &status, WNOHANG|WUNTRACED);
if (checkID == -1) {
perror("waitpid error");
}
else if (checkID == 0) {
printf("Parent waiting for child");
}
else if (checkID == childPid) {
if (WIFEXITED(status)){
printf("Shells exit status : %d",WEXITSTATUS(status));
printf("\n");
}
else if (WIFSIGNALED(status)){
printf("Child ended abnormally \n");
}
else if (WIFSTOPPED(status)){
printf("Child process stopped \n ");
}
}
}
void no_echo_mode(void)
{
struct termios tattributes;
//Save the original attributes for restoring to original mode later
if (tcgetattr (STDIN_FILENO, &original_attributes) < 0)
perror("tcsetattr()");
// tcgetattr (STDIN_FILENO, &original_attributes);
atexit(orig_mode);
//checking to see if stdin is a terminal
if(isatty (STDIN_FILENO)==0)
{
fprintf (stderr, "ERROR:stdin not a terminal.\n");
exit (EXIT_FAILURE);
}
//Setting the new terminal modes
tcgetattr (STDIN_FILENO, &tattributes);
tattributes.c_lflag = tattributes.c_lflag & ~(ICANON|ECHO);
tattributes.c_cc[VMIN] = 1;
tattributes.c_cc[VTIME] = 0;
tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattributes);
}
void *read_all(void *arguments) {
signal(SIGINT, handler);
signal(SIGPIPE, sigPipehandler);
struct arg_struct *args = (struct arg_struct *)arguments;
char c;int x;
while(1){
if(args->dst>args->src){
x=read(args->src, &c, 1);
if(x==0){
break;
}
if(c=='\004'){
close(input_pipe[1]);
close(output_pipe[0]);
fflush(stdout);
controlD=1;
kill(childPid,SIGHUP);
break;
}
else{
if(c=='\r' || c== '\n'){
char temp ='\n';
write(args->dst, &temp, 1);
temp='\r';
write(STDOUT_FILENO, &temp, 1);
temp='\n';
write(STDOUT_FILENO, &temp, 1);
}
else{
write(args->dst, &c, 1);
write(STDOUT_FILENO, &c, 1);
}
}
}
else{
x=read(args->src, &c, 1);
if(x==0){
if(controlD==1){
break;
}
exit(1);
}
if(c==EOF){
exit(1);
}
else{
write(args->dst, &c, 1);
}
}
}
}
int main(int argc, char** argv) {
int shell = 0;
int opt=0;
int longIndex=0;
static const char *optString = "s";
static struct option long_options[] = {
{"shell", no_argument, NULL, 's'},
{NULL, no_argument, NULL, 0}
};
opt = getopt_long( argc, argv, optString, long_options, &longIndex );
while( opt != -1 ) {
switch( opt ) {
case 's':
shell=1;
break;
default:
exit(4);
break;
}
opt = getopt_long( argc, argv, optString, long_options, &longIndex );
}
if(shell==1){
no_echo_mode();
atexit(shell_status);
pipe(input_pipe);
pipe(output_pipe);
pid_t rc = fork();
if (rc < 0) { // fork failed; exit
fprintf(stderr, "fork error\n");
exit(1);
}
else if (rc == 0) {
// child process
close(input_pipe[1]);
close(output_pipe[0]);
dup2(input_pipe[0], STDIN_FILENO);
dup2(output_pipe[1], STDOUT_FILENO);
dup2(output_pipe[1], STDERR_FILENO);
// exec the given program
char *myargs[2];
myargs[0] = strdup("/bin/bash");
myargs[1]=NULL;
if (execvp(myargs[0] ,myargs ) == -1) {
perror("failed to start subprocess");
return EXIT_FAILURE;
}
}
childPid=rc;
pthread_t tid1,tid2;
struct arg_struct args1;
struct arg_struct args2;
args1.src=STDIN_FILENO;
args1.dst=input_pipe[1];
args2.src=output_pipe[0];
args2.dst=STDOUT_FILENO;
// parent process
close(input_pipe[0]);
close(output_pipe[1]);
pthread_create(&tid1, NULL, &read_all, (void *)&args1);
pthread_create(&tid2, NULL, &read_all, (void *)&args2);
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
close(input_pipe[1]);
fflush(stdout);
close(output_pipe[0]);
exit(0);
}
else{
char c;
no_echo_mode();
while(1)
{
read (STDIN_FILENO, &c, 1);
if (c == '\004'){
break;
}
else {
if(c=='\r' || c== '\n'){
char tempChar = '\r';
write(STDOUT_FILENO, &tempChar, 1);
tempChar = '\n';
write(STDOUT_FILENO, &tempChar, 1);
}
else{
write(STDOUT_FILENO, &c, 1);
}
}
}
exit(0);
}
}