-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt.c
More file actions
89 lines (82 loc) · 1.78 KB
/
prompt.c
File metadata and controls
89 lines (82 loc) · 1.78 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
#include "shellby.h"
/**
* inter_shellby - intercactve shell
*
* Return: 0 on success.
*/
int inter_shellby(void)
{
size_t line_size = 0;
ssize_t line = 0;
char *buffer = NULL;
int error = 0, count = 1;
write(STDOUT_FILENO, "shellby~$ ", 10);
while ((line = getline(&buffer, &line_size, stdin)))
{
if (line == EOF)/*check end of file*/
{
free(buffer);
write(STDOUT_FILENO, "\n", 1);
exit(0);
}
if (*buffer == '\n')
{
write(STDOUT_FILENO, "shellby~$ ", 10);
count++;
continue;
}
error = process_line(&buffer, &line_size, &count);
write(STDOUT_FILENO, "shellby~$ ", 10);
}
return (error);
}
/**
* process_line - interpretates line obtained by getline.
* @buffer: double pointer to the buffer obtained from getline.
* @line_size: pointer to the size of the buffer.
* @count: count of the number of lines executed in the shellby.
*
* Return: error status which should be 0 if everything went ok.
*/
int process_line(char **buffer, size_t *line_size, int *count)
{
char *token = NULL, **commands = NULL;
char *token_o = NULL, *heap_token = NULL;
int status, error = 0;
pid_t child_pid;
token = strtok(*buffer, " \n\t\r");
if (token != NULL)
{
if (check_built_ins(*buffer, token) == 1)
{
free(*buffer);
*buffer = NULL;
*line_size = 0;
return (0);
}
token_o = token;
heap_token = look_inPATH(&token);
commands = input_tokens(token, *buffer);
if (commands == NULL)
return (0);
child_pid = fork();
if (child_pid == 0)
child_exe(commands, token_o, *count);
else if (child_pid == -1)
{
perror("Error");
exit(1);
}
else
{
wait(&status);
if (WIFEXITED(status))
error = WEXITSTATUS(status);
}
free_all(*buffer, commands, heap_token);
*line_size = 0;
*buffer = NULL;
}
(*count)++;
return (error);
}