-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_arg.c
146 lines (133 loc) · 2.41 KB
/
cmd_arg.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
#include "shell.h"
/**
* print_er127 - prints error 127
* @argv0: argument for printing.
* @st: verifier
* @line: line from user.
*/
void print_er127(char *argv0, struct stat *st, char *line)
{
int len;
len = my_strlen(argv0);
/**perror(av[0]);*/
write(STDERR_FILENO, "./hsh: 1: ", 10);
write(STDERR_FILENO, argv0, len);
write(STDERR_FILENO, ": not found\n", 12);
if (!isatty(STDIN_FILENO))
{
free(line);
free(st);
exit(127);
}
/*[./hsh: 1: ls: not found*/
}
/**
* exec - executes commnd
* @argv: argument for command.
* @st: program verifier.
* @env: environment variable.
* @line: line from user.
*/
void exec(char **argv, struct stat *st, char **env, char *line)
{
int status;
pid_t pid;
if (stat(argv[0], st) == 0)
{
pid = fork();
if (pid == -1)
exit(EXIT_FAILURE);
if (pid == 0)
{
if (execve(argv[0], argv, env) == -1)
exit(EXIT_FAILURE);
}
else
{
wait(&status);
if (status != 0)
exit(2);
}
/* free(st);*/
}
else
{
print_er127(argv[0], st, line);
}
}
/**
* parsenexec - parses line from user and call exec.
* @argv: argument for command.
* @st: program verifier.
* @line: lne of text to be parsed
* @env: environment variable.
*/
void parsenexec(char **argv, struct stat *st, char *line, char **env)
{
char *tok;
int ar;
tok = strtok(line, " \n\t\b\r\v");
argv[0] = tok;
ar = 0;
while (argv[ar] != NULL)
{
ar++;
argv[ar] = strtok(NULL, " \n\t\b\r\v");
}
if (argv[0] != NULL)
{
exec(argv, st, env, line);
}
}
/**
* freeinits - frees line and st.
* @st: program verifier.
* @line: lne of text to be parsed
*/
void freeinits(char *line, struct stat *st)
{
free(line);
free(st);
}
/**
* main - program entry point.
* @argc: number of arguments.
* @av: array of arguments.
* @env: environment variable.
*
* Return: status
*/
int main(int argc, char *av[], char **env)
{
size_t n;
struct stat *st = malloc(sizeof(struct stat));
char *line = NULL, *buf = "$ ";
char *argv[50];
(void)av;
(void)argc;
while (1) /** Handle Arguments**/
{
if (isatty(STDIN_FILENO) != 0)
write(STDOUT_FILENO, buf, 2);
if (getline(&line, &n, stdin) == -1)
{
freeinits(line, st);
exit(0);
}
if (my_strcmp(line, "env\n") == 0)
{
print_environment(env);
}
else if (my_strcmp(line, "exit\n") == 0)
{
freeinits(line, st);
exit(0);
}
else
{
parsenexec(argv, st, line, env);
}
}
freeinits(line, st);
exit(EXIT_SUCCESS);
}