-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshell.c
More file actions
99 lines (96 loc) · 1.72 KB
/
Copy pathshell.c
File metadata and controls
99 lines (96 loc) · 1.72 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
#include "shell.h"
/**
* sighand - function to handle ctrl+c
* @signum: signal number
*/
void sighand(int signum)
{
(void)signum;
write(1, "\n$ ", 3);
}
/**
* main - simple shell program
* Return: 0 always success
*/
int main(void)
{
/*unsigned int pid, ppid;*/
ssize_t read = 0;
char *buff = NULL, **arr;
size_t size = 0;
int result, tok_size = 0, len = 0, line_counter = 0;
int ex_flag = 0;
while (1)
{
size = 0;
buff = NULL;
if (isatty(0))
write(STDIN_FILENO, "$ ", 2);
signal(SIGINT, sighand);
read = getline(&buff, &size, stdin);
line_counter++;
if (read == -1)
{
if (isatty(0))
write(STDIN_FILENO, "\n", 1);
free(buff);
return (0);
}
if (read == 0)
{
if (isatty(0))
{
write(STDIN_FILENO, "\n", 1);
continue;
}
}
if (buff && buff[0] != '\n')
{
/* count length of buffer input */
len = _strlen(buff);
if (buff[len - 1] == '\n')
buff[len - 1] = '\0';
/* count number of tokens in buffer */
tok_size = toksize(buff);
if (tok_size == -1)
break;
if (tok_size == 0)
continue;
/* put tokens inside array */
arr = tokenize(buff);
/* checks if 'env', 'exit', or '.' is entered */
result = str_comp(arr, tok_size);
if (result == 0)
{
free(arr);
free(buff);
exit(ex_flag);
}
ex_flag = 0;
if (result == 2)
{
free(arr);
free(buff);
continue;
}
else if (result == 1)
{
print_env();
free(arr);
free(buff);
continue;
}
/* send input to path function to check */
/*if it exists, permissions, and if it can execute */
ex_flag = path(arr, line_counter);
}
if (buff && buff[0] == '\n')
{
free(buff);
continue;
}
free(buff);
free(arr);
}
return (0);
}