-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimpleShell.c
96 lines (80 loc) · 1.36 KB
/
simpleShell.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
#include "simishell.h"
/**
* copyarray - a function to copy string array to another string array variable
* @line: a string array to be copied
*
* Return: returns the copied array
*/
char **copyarray(char **line)
{
char **array;
int i = 1;
array = malloc(64);
if (!array)
return (NULL);
while (line[i] != NULL)
{
array[(i - 1)] = malloc(32);
if (!array[(i - 1)])
return (NULL);
strcopy(line[i], array[(i - 1)]);
i++;
}
return (array);
}
/**
* sigintHandler - a function to handle the ctrl-c signal
* @sig_num: an integer signal indicator
*
* Return: void function
*/
void sigintHandler(int sig_num __attribute__((unused)))
{
signal(SIGINT, sigintHandler);
write(1, "\n", 2);
printprompt(0);
fflush(stdout);
}
/**
* main - a the main function of the shell
* @argc: the number of arguments given
* @argv: an array of given argument strings
*
* Return: returns an integer
*/
int main(int argc __attribute__((unused)), char **argv)
{
char *line;
line = malloc(256);
if (!line)
{
perror("Allocation");
exit(1);
}
if (!isatty(STDIN_FILENO))
{
if (getstr(line) == (-1))
{
write(1, "\n", 2);
exit(1);
}
if (shellprocessor(strbrk(line, ' '), argv) == -1)
{
perror("Error");
}
exit(0);
}
do {
printprompt(0);
if (getstr(line) == (-1))
{
write(1, "\n", 2);
exit(0);
}
if ((shellprocessor(strbrk(line, ' '), argv)) == -1)
{
perror("Error");
}
} while (1);
return (0);
}