-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec_command.c
121 lines (115 loc) · 2.08 KB
/
exec_command.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
#include "main.h"
/**
* print_err - print err
* @progname: prog name
* @arg: args[0]
* @msg: message
*
* Return: 127
*/
int print_err(char *progname, char *arg, char *msg)
{
_errputs(progname);
_errputs(": 1: ");
_errputs(arg);
_errputs(": ");
_errputs(msg);
return (127);
}
/**
* ignore_space - ignores spaces
* @line: line
* @i: index
* Return: char
*/
char ignore_space(char *line, int *i)
{
while (line[*i] == ' ')
(*i)++;
return (line[*i]);
}
/**
* exec_command - check and execute the command
* @line: input from getline in main
* @progname: argv[0] from main
* @envp: env from main
* @exit_status: exit
*
* Return: ay 7aga
*/
int exec_command(char *line, char *progname, char **envp, int exit_status)
{
char *args[1024], *token;
int arg_count = 0, allocated = 0, status, i = 0;
pid_t id;
if (!ignore_space(line, &i))
return (0);
token = strtok(&line[i], " ");
while (token)
{
args[arg_count++] = token;
token = strtok(NULL, " ");
}
args[arg_count] = NULL;
func(progname, args, arg_count, line, exit_status);
if (check_path(args[0]))
{
args[0] = get_path(envp, args[0]);
allocated = 1;
}
if (args[0] && (command_exists(args[0])))
id = fork();
else
return (exit_status = (print_err(progname, args[0], "not found\n")));
if (id == 0)
{
if (execve(args[0], args, envp) == -1)
{
free(line);
_exit(1);
}
} else
{
wait(&status);
if (WIFEXITED(status))
exit_status = WEXITSTATUS(status);
if (allocated)
free(args[0]);
}
return (exit_status);
}
/**
* func - asdlkfjla
* @progname: name
* @args: arguments
* @arg_count: args count
* @line: line
* @e: exit
*
* Return: Nothing
*/
void func(char *progname, char **args, int arg_count, char *line, int e)
{
int exit_arg = 0;
if (!_strcmp(args[0], "exit"))
{
if (arg_count == 1)
{
free(line);
_exit(e);
}
exit_arg = _errtoi(args[1]);
if (exit_arg == -1)
{
print_err(progname, args[0], "Illegal number: ");
_errputs(args[1]);
_errputchar('\n');
free(line);
_exit(2);
}
else if (exit_arg == 1000)
_exit(232);
else
_exit(exit_arg);
}
}