-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadditionalFunctions_0.c
105 lines (100 loc) · 1.56 KB
/
additionalFunctions_0.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
#include "library.h"
/**
* cantCmds - function to count commands typed
* @buff: buffer
* Return: amount of commands
*/
int cantCmds(char *buff)
{
int cC = 0, i = 0;
while (buff[i] != '\0')
{
if (buff[i] == '\n' || buff[i] == '\t' || buff[i] == ' ')
cC++, i++;
else
i++;
}
return (cC);
}
/**
* parse - this parse strings
* @buffer: buffer
* Return: double pointer
*/
char **parse(char *buffer)
{
char **cmds, *cmd, *delim;
int i = 0, cC;
delim = "\n\t \r\a";
cC = cantCmds(buffer);
cmds = alloc(sizeof(char *) * (cC + 1));
cmd = _strtok(buffer, delim);
while (cmd != NULL)
{
cmds[i] = cmd;
if (cmds[i][0] == '#')
break;
cmd = _strtok(NULL, delim);
i++;
}
cmds[i] = NULL;
return (cmds);
}
/**
* constructor - this function calls the execute
* @cmds: double pointer
*/
void constructor(char **cmds)
{
if (cmds[0][0] == '/')
execute(cmds);
else
{
cmds = findpath(cmds);
if (cmds != NULL)
execute(cmds);
}
}
/**
* print_number - Entry point
* @n: char variable
*/
void print_number(int n)
{
unsigned int i = n;
char loop;
if (i / 10)
{
print_number(i / 10);
}
loop = ('0' + (i % 10));
write(STDERR_FILENO, &loop, 1);
}
/**
* execute - this function proceed to execute
* @cmds: double pointer
*/
void execute(char **cmds)
{
pid_t child;
int status = 0;
child = fork();
if (child == -1)
{
perror("Error");
_exit(-1);
}
if (child == 0)
{
execve(cmds[0], cmds, environ);
_puts("./hsh: ");
print_number(cantLoops);
_puts(": ");
_puts(cmds[0]);
_puts(": not found\n");
free(cmds);
_exit(1);
}
else
wait(&status);
}