-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecute.c
More file actions
117 lines (101 loc) · 2.12 KB
/
execute.c
File metadata and controls
117 lines (101 loc) · 2.12 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "shell.h"
/**
* execute - This function exxecutes a command
* @path: path of command passed
* @args: argument passed
*/
void execute(char *path, char **args)
{
pid_t pid = fork();
if (pid < 0)
{
exit(EXIT_FAILURE);
}
else if (pid == 0)
{
execve(path, args, environ);
perror("exec: error");
exit(EXIT_FAILURE);
}
else
{
waitpid(pid, NULL, 0);
}
}
/**
* error_message - This function displays error essage
* if command isn't able to run
* @args: repreesent argument passed
* @count: count commands
* @av: argument vector
*/
void error_message(char **args, int count, char **av)
{
int temp, mult = 1, len = 0;
write(STDERR_FILENO, av[0], _strlen(av[0]));
write(STDERR_FILENO, ": ", 2);
temp = count;
while (temp > 9)
{
temp /= 10;
mult *= 10;
++len;
}
while (len > 0)
{
if ((count / mult) < 10)
write_error((count / mult + '0'));
else
write_error(((count / mult) % 10 + '0'));
--len;
mult /= 10;
}
write_error(count % 10 + '0');
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, args[0], _strlen(args[0]));
write(STDERR_FILENO, ": not found\n", 12);
}
/**
* write_error - This function converts int to char
* @error: char to right
* Return: int (success)
*/
int write_error(char error)
{
return (write(STDERR_FILENO, &error, 1));
}
/**
* exit_errorMessage - This function handles errors from exit status
* @args: argument passed
* @count: number of cmd
* @av: argument vector
*/
void exit_errorMessage(char **args, int count, char **av)
{
int temp, len = 0, mult = 1;
write(STDERR_FILENO, av[0], _strlen(av[0]));
write(STDERR_FILENO, ": ", 2);
temp = count;
while (temp > 9)
{
temp /= 10;
mult *= 10;
++len;
}
while (len > 0)
{
if ((count / mult) < 10)
write_error((count / mult + '0'));
else
write_error(((count / mult) % 10 + '0'));
--len;
mult /= 10;
}
write_error((count % 10 + '0'));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, args[0], _strlen(args[0]));
write(STDERR_FILENO, ": ", 2);
write(STDERR_FILENO, "Illegal number: ", 16);
write(STDERR_FILENO, args[1], _strlen(args[1]));
_putchar('\n');
}