-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecute_commands.c
More file actions
139 lines (125 loc) · 2.86 KB
/
execute_commands.c
File metadata and controls
139 lines (125 loc) · 2.86 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "main.h"
/**
* int_to_str - converts int to str
* @num: int
* @str: str
*
* Description: Converts an integer to a string.
* The result is stored in the provided character array.
*/
void int_to_str(int num, char *str)
{
char temp;
int i;
int len = 0;
/* Convert integer to string */
while (num > 0)
{
str[len++] = '0' + (num % 10);
num /= 10;
}
/* Reverse the string */
for (i = 0; i < len / 2; i++)
{
temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
/**
* handle_error - handles errors
* @error_msg: error message
* @command: command
*
* Description: Prints an error message to stderr in a specific format
* and exits with failure status.
*/
void handle_error(const char *error_msg, char *command)
{
char error_statement[100];
char exit_status_str[10];
int exit_status = errno, len = 0, i;
/* Convert exit status to string */
int_to_str(exit_status, exit_status_str);
/* Construct error statement */
error_statement[len++] = '.';
error_statement[len++] = '/';
error_statement[len++] = 'h';
error_statement[len++] = 's';
error_statement[len++] = 'h';
error_statement[len++] = ':';
error_statement[len++] = ' ';
for (i = 0; exit_status_str[i] != '\0'; i++)
{
error_statement[len++] = exit_status_str[i];
}
error_statement[len++] = ':';
error_statement[len++] = ' ';
for (i = 0; command[i] != '\0'; i++)
{
error_statement[len++] = command[i];
}
error_statement[len++] = ':';
error_statement[len++] = ' ';
for (i = 0; error_msg[i] != '\0'; i++)
{
error_statement[len++] = error_msg[i];
}
error_statement[len++] = '\n';
/* Print error statement to stderr */
write(STDERR_FILENO, error_statement, len);
/* Exit with failure status */
exit(EXIT_FAILURE);
}
/**
* execute_command - executes commands
* @command: command
* @args: arguments
*
* Description: Executes the provided command with the given arguments.
* If the command does not exist or has no execute permission,
* an error message is displayed.
*/
void execute_command(char *command, char **args)
{
pid_t pid;
int exit_status = 0;
struct stat st;
/* Check if command exists */
if (stat(command, &st) == 0)
{
/* Check if command has execute permission */
if (access(command, X_OK) == 0)
{
/* Get variables */
get_variables(args, &exit_status);
/* Fork process */
pid = fork();
if (pid < 0)
{
perror("fork failed");
exit(EXIT_FAILURE);
}
else if (pid == 0) /* Child process */
{
/* Execute command */
if (execve(command, args, environ) == -1)
{
handle_error(strerror(errno), command);
}
}
else /* Parent process */
{
wait(NULL);
}
}
else /* No execute permission */
{
handle_error("permission denied", command);
}
}
else /* Command not found */
{
handle_error("no such file or directory", command);
}
}