-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
182 lines (168 loc) · 3.05 KB
/
utils.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
#include "shell.h"
/**
* handle_builtin - Handle Builtin Command
* @cmd: Parsed Command
* @er:statue of last Excute
* Return: -1 Fail 0 Succes (Return :Excute Builtin)
*/
int handle_builtin(char **cmd, int er)
{
bul_t bil[] = {
{"cd", change_dir},
{"env", list_env},
{"help", show_help},
{"echo", echo_cmd},
{"setenv", _setenv},
{"unsetenv", _unsetenv},
{"history", show_history},
{NULL, NULL}
};
int i = 0;
while ((bil + i)->command)
{
if (_strcmp(cmd[0], (bil + i)->command) == 0)
{
return ((bil + i)->fun(cmd, er));
}
i++;
}
return (-1);
}
/**
* read_file - Read Command From File
* @filename:Filename
* @argv:Program Name
* Return: -1 or 0
*/
void read_file(char *filename, char **argv)
{
FILE *fp;
char *line = NULL;
size_t len = 0;
int counter = 0;
fp = fopen(filename, "r");
if (fp == NULL)
{
exit(EXIT_FAILURE);
}
while ((getline(&line, &len, fp)) != -1)
{
counter++;
handle_files(line, counter, fp, argv);
}
if (line)
free(line);
fclose(fp);
exit(0);
}
/**
* handle_files - run commands from a given file
* @line: Line From A File
* @counter:Error Counter
* @fd:File Descriptor
* @argv:Program Name
* Return : Excute A line void
*/
void handle_files(char *line, int counter, FILE *fd, char **argv)
{
char **cmd, *ch;
int st = 0;
ch = get_delim2(line);
if (ch != NULL && *ch != '\n')
{
chain_cmds(line, counter, argv, ch);
return;
}
cmd = get_tokens(line);
if (_strncmp(cmd[0], "exit", 4) == 0)
{
exit_for_file(cmd, line, fd);
}
else if (check_builtin(cmd) == 0)
{
st = handle_builtin(cmd, st);
free(cmd);
}
else
{
st = run_cmd(cmd, line, counter, argv);
free(cmd);
}
}
/**
* exit_for_file - Exit Shell when working with a File
* @line: Line From A File
* @cmd: Parsed Command
* @fd:File Descriptor
* Return : Case Of Exit in A File Line
*/
void exit_for_file(char **cmd, char *line, FILE *fd)
{
int statue, i = 0;
if (cmd[1] == NULL)
{
free(line);
free(cmd);
fclose(fd);
exit(errno);
}
while (cmd[1][i])
{
if (_isalpha(cmd[1][i++]) < 0)
{
perror("illegal number");
}
}
statue = _atoi(cmd[1]);
free(line);
free(cmd);
fclose(fd);
exit(statue);
}
/**
* run_cmd - Excutes command if it exists
*
* @cmd:Parsed Command
* @input: input from stdin or a file
* @c:Shell Excution Time Case of Command Not Found
* @argv:Program Name
* Return: 1 Case Command Null -1 Wrong Command 0 Command Excuted
*/
int run_cmd(char **cmd, char *input, int c, char **argv)
{
int status, check;
pid_t pid;
char *buff = NULL;
if (*cmd == NULL)
{
return (-1);
}
pid = fork();
if (pid == -1)
{
perror("Error");
return (-1);
}
if (pid == 0)
{
if (_strncmp(*cmd, "./", 2) != 0 && _strncmp(*cmd, "/", 1) != 0)
{
buff = _getpath(cmd[0]);
if (buff != NULL)
cmd[0] = _strdup(buff);
}
check = execve(*cmd, cmd, environ);
if (check == -1)
{
print_error(cmd[0], c, argv);
free_all(cmd, input);
exit(EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
free(buff);
wait(&status);
if (WIFEXITED(status))
return (WEXITSTATUS(status));
return (-1);
}