-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
118 lines (112 loc) · 2.1 KB
/
test.c
File metadata and controls
118 lines (112 loc) · 2.1 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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#define DELIM " \t\r\n\a\"\'"
struct command
{
const char **argv;
};
int spawn_proc (int in, int out, char *com)
{
pid_t pid;
char **args ;
args = segregate(com,DELIM);
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
// int tostart = getcurrdir(currdir,home);
// if(strcmp(args[0],"pwd")==0)
// printf("%s\n",currdir+tostart);
// else if(strcmp(args[0],"cd")==0)
// {
// if(!args[1])
// {
// args[1]=(char *)malloc((sizeof(char)*(3)));
// strcpy(args[1],"~");
// }
// changedir(args[1],home);
// }
// else if(strcmp(args[0],"exit")==0)
// _exit(-1);
// else if(strcmp(args[0],"echo")==0)
// echocall(args);
// else if(strcmp(args[0],"pinfo")==0)
// {
// pinfo(args,home);
// }
if ((pid = fork ()) == 0)
{
return execvp (args[0], args);
}
return pid;
}
int piping(char * comd)
{
int i;
int in, fd [2];
char * split;
char **pipedOnes ;
if(comd[strlen(comd)-1] == '\n')
{
comd[strlen(comd)-1] = '\0';
}
pipedOnes = segregate(comd,"|");
int len = 0;
while(pipedOnes[len] != NULL)
len++;
findInputFile(comd,dup(0));
in = dup(0);
for (i = 0; i < len-1; ++i)
{
pipe (fd);
spawn_proc (in, fd [1], pipedOnes[i]);
close (fd [1]);
in = fd [0];
}
// char *par[1000];
// char * Split_temp = strtok(com[i]," ");
// for(int i = 0; i < 1000; i++)
// {
// par[i] = Split_temp;
// Split_temp = strtok(NULL," ");
// if(par[i] == NULL) break;
// }
char **args = segregate(pipedOnes[i],DELIM);
findOutputFile(args,dup(1));
pid_t pid = fork();
// Error
if (pid == -1) {
char* error = strerror(errno);
printf("fork: %s\n", error);
return 1;
}
// Child process
else if (pid == 0) {
// execute execvp
if (in != 0)
dup2 (in, 0);
execvp(args[0], args);
char* error = strerror(errno);
printf("shell: %s: %s\n", args[0], error);
return 0;
}
// Parent process
else {
int childStatus;
waitpid(pid, &childStatus, 0);
return 1;
}
return 0;
}