-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchild_exe.c
51 lines (46 loc) · 1001 Bytes
/
child_exe.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
#include "shellby.h"
/**
* child_exe - executes a command in a child process.
* @cmnds: NULL terminated pointer array to the command and flags if there are
* @argv: string holding the name of the current executable.
* @count: a counting of errors so far.
*
* Return: void.
*/
void child_exe(char **cmnds, char *argv, int count)
{
if (execve(cmnds[0], cmnds, environ) == -1)
{
write(STDERR_FILENO, argv, _strlen(argv));
write(STDERR_FILENO, ": ", 2);
print_number(count);
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, cmnds[0], _strlen(cmnds[0]));
write(STDERR_FILENO, ": not found\n", 12);
exit(EXIT_FAILURE);
}
}
/**
* print_number - prints an integer
* @n: input number to be printed
*
* Return: void
*/
void print_number(int n)
{
int res = n, fact = 1;
char digit = '0';
while (res / 10)
{
fact *= 10;
res /= 10;
}
while (fact)
{
res = n / fact;
digit = res + '0';
write(STDERR_FILENO, &digit, 1);
n = n - (res * fact);
fact /= 10;
}
}