-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintoHsh.c
More file actions
143 lines (134 loc) · 2.7 KB
/
Copy pathintoHsh.c
File metadata and controls
143 lines (134 loc) · 2.7 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
#include "shell.h"
int _Wait(char **argv, char *command, int QExecutes);
/**
* intoHsh - Start the shell, process, loop of instructions
* @env: array enviroment
* @argv: array arguments
* Authors - Carlos Garcia - Ivan Dario Lasso - Cohort 10 - Cali
**/
void intoHsh(char **env, char **argv)
{
size_t sizebuf;
char *command = NULL;
pid_t pid;
int indBuilt = 0;
static int status;
static int Qexecutes = 1;
ssize_t QcharComm;
command = NULL;
_prompt();
while ((QcharComm = getline(&command, &sizebuf, stdin)) != EOF)
{
if (_strcmp(command, "\n") != 0)
{
indBuilt = Builtin(command, env, status);
if (indBuilt == CHANGE_DIR)
{ free(command);
command = NULL;
continue;
};
if (indBuilt == EXIT_SHELL)
{ free(command);
exit(0);
}
pid = fork();
if (pid > 0)
{
status = _Wait(argv, command, Qexecutes);
Qexecutes++;
}
else if (pid == 0)
{
execute(command, env);
}
if (pid == -1)
perror("Error fork");
}
else
{ free(command);
command = NULL;
}
_prompt();
}
free(command);
if (status != 0)
exit(status);
}
/**
* execute - execute command whith enviroment
* @command: take a command
* @env: enviroment
* Return: void
* Authors - Carlos Garcia - Ivan Dario Lasso - Cohort 10 - Cali
**/
void execute(char *command, char **env)
{
char **param;
int indEx = 0;
char *comm;
comm = strdup(command);
removeSpaces(comm);
if (_strlen(comm) == 1)
{
free(comm);
_exit(0);
}
free(comm);
param = ParseCommand(command, " ");
if (param != NULL)
{
if (_strcmp(param[0], "env") == 0)
{
_printenv(env);
/*free(param); VALGRIND*/
} else
{
indEx = _exec(param, env);
if (indEx == 1)
{
free(command);
free(param);
_exit(127);
}
else if (indEx == 2)
{
free(command);
free(param);
_exit(126);
}
}
free(command);
}
free(param); /*valgrind*/
_exit(0);
}
/**
* _Wait - Wait for de child process and eval status
* @argv: line command args to print name exe shell
* @command: command to execute
* @Qex: Executions Quantity
* Return: return status process
* Authors - Carlos Garcia - Ivan Dario Lasso - Cohort 10 - Cali
**/
int _Wait(char **argv, char *command, int Qex)
{
int status;
char **ParsedCom;
wait(&status);
if (WIFEXITED(status))
{
ParsedCom = ParseCommand(command, " ");
if (WEXITSTATUS(status) == 126)
errors(argv[0], ParsedCom[0], PERM_DENIED, Qex);
if (WEXITSTATUS(status) == 127)
errors(argv[0], ParsedCom[0], NOT_FOUND, Qex);
/*unknow errors*/
if (WEXITSTATUS(status) != 127 &&
WEXITSTATUS(status) != 126 &&
WEXITSTATUS(status) != 2 &&
WEXITSTATUS(status) != 0)
perror("");
free(ParsedCom);
}
return (WEXITSTATUS(status));
}