-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
142 lines (119 loc) · 2.94 KB
/
Copy pathtest.c
File metadata and controls
142 lines (119 loc) · 2.94 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
int main(int argc, char *argv[])
{
shell_terminal = STDIN_FILENO;
while (tcgetpgrp (shell_terminal) != (shell_pgid = getpgrp ()))
kill (- shell_pgid, SIGTTIN);
/* Ignore interactive and job-control signals. */
signal (SIGINT, SIG_IGN);
signal (SIGQUIT, SIG_IGN);
signal (SIGTSTP, SIG_IGN);
/* Put ourselves in our own process group. */
shell_pgid = getpid ();
if (setpgid (shell_pgid, shell_pgid) < 0)
{
perror ("Couldn't put the shell in its own process group");
exit (1);
}
/* Grab control of the terminal. */
tcsetpgrp (shell_terminal, shell_pgid);
/* Save default terminal attributes for shell. */
tcgetattr (shell_terminal, &shell_tmodes);
int i, mode = NORMAL, cmdArgc;
size_t len = INPUT_STRING_SIZE;
char *cpt, *inputString, *supplement = NULL;
inputString = (char*)malloc(sizeof(char)*INPUT_STRING_SIZE);
char curDir[100];
home = (char *)malloc(100*sizeof(char));
getcwd(home,100);
while(1)
{
if(signal(SIGINT,sig_handler)==SIG_ERR)
perror("Signal not caught!!");
mode = NORMAL;
getcwd(curDir, 100);
proc temp3[1024];
proc temp4[1024];
int c=0;
int m=0;
for(c=0;c<no;c++)
{
if(kill(pro[c].id,0)==-1 && errno==ESRCH)
printf("Process %s exited whose id is %d\n",pro[c].process,pro[c].id);
else
{
temp3[m]=pro[c];
temp4[m]=jobs[c];
m++;
}
}
no=m;
for(c=0;c<no;c++)
{
pro[c]=temp3[c];
jobs[c]=temp4[c];
}
printf("%s@%s->", getlogin(),curDir);
getline( &inputString, &len, stdin);
if(strcmp(inputString, "quit\n") == 0)
exit(0);
char **args2;
args2=split(inputString,";");
char **afterpiping;
int piping=1;
char **afterredirect,**afterredirect2,**afterredirect3;
for(i=0;args2[i]!=NULL;i++)
{ int j;
char **args3;
int p[2];
pid_t pid4;
int fd_in = 0;
int i = 0;
char** cmd=split(args2[i],"|");
for(j=0;cmd[j]!=NULL;j++){
printf("%d\n",j );
if(j==1){
piping=0;
break;
}
}
if(piping==0)
{
for(j=0;cmd[j]!=NULL;j++){
char *cmdArgv[INPUT_STRING_SIZE];
cmdArgc = parse(cmd[j], cmdArgv, &supplement, &mode);
//fprintf(stderr, "%s %s %d %s\n",cmdArgv[0],cmdArgv[1],j,cmd[j] );
pipe(p);
if ((pid4 = fork()) == -1)
{
exit(1);
}
else if (pid4 == 0)
{ //fprintf(stderr, "%s %s\n","came here",cmd[j] );
dup2(fd_in, 0);
if (cmd[j+1] != NULL)
dup2(p[1], 1);
close(p[0]);
execute(cmdArgv, mode, &supplement,cmdArgc);
exit(2);
}
else
{
wait(NULL);
close(p[1]);
fd_in = p[0];
i++;
}
}
}else{
char *cmdArgv[INPUT_STRING_SIZE];
cmdArgc = parse(cmd[0], cmdArgv, &supplement, &mode);
printf("%s\n","pahaunch gye" );
printf("%s\n",cmd[0] );
execute(cmdArgv, mode, &supplement,cmdArgc);
}
}
//if(piping==1) // case of no piping
//execute(cmdArgv , 0 ,n,mode, &supplement,cmdArgc); /* otherwise, execute the command */
}
return 0;
}