-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-execute_command.c
More file actions
138 lines (121 loc) · 3.02 KB
/
3-execute_command.c
File metadata and controls
138 lines (121 loc) · 3.02 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
#include <unistd.h>
#include "main.h"
#include <stdio.h>
/**
* execute - does the actual execution of the command written in the
* comment section of main
* @comm_path: path to the command to be executed
* @arr: array of arguments
* Definition - executes a command for child processes and exits 98 if
* exec function failed.
* description: Executes a command for child processes and exits code
* in case of a failed exec function
* Return: void
*/
void execute(char *comm_path, char **arr)
{
int exec_value;
exec_value = execve(comm_path, arr, environ);
if (exec_value == -1)
{
perror("Could not execute:");
}
/*_exit(0);*/
}
/**
* exec_chdir - changes the directory of the calling process to directory
* housing the command, and then, executes the command.
* @comm: single-word command to be executed
* @arr: array of arguments to the command, including the command itself as the
* first argument
* @dir: directory to be changed to
* Description - Is employed to run commands that are not specified as
* file paths.
* Return: void
*/
void exec_chdir(char *comm, char **arr, char *dir)
{
int exec_value;
int chdir_value;
chdir_value = chdir(dir);
if (chdir_value == 1)
perror("Could not change directory");
exec_value = execve(comm, arr, environ);
if (exec_value == -1)
perror("could not execute file");
}
/**
* create_command_path - creates a string path for a command
* @comm: command passed into function
* @dir: directory where command is located
*
* Description - creates a string path that can be passed as the first argument
* of the execve function. Works by joining string of path of directory
* location and string of command, to form a path.
* Return: created path, comm_path
*/
char *create_command_path(char *comm, char *dir)
{
int len_comm;
int len_dir;
int len_path;
char *comm_path;
int i;
int _i;
len_comm = _strlen(comm);
len_dir = _strlen(dir);
len_path = len_comm + len_dir;
comm_path = malloc(len_path + 2);
if (comm_path == NULL)
return (NULL);
for (i = 0; i < len_dir; i++)
{
comm_path[i] = dir[i];
}
_i = i;
comm_path[_i] = '/';
_i++;
for (i = 0; i < len_comm; i++)
{
comm_path[_i] = comm[i];
_i++;
}
comm_path[_i] = '\0';
return (comm_path);
}
/**
* create_new_array - creates a new argument array with command path created
* by create_command_path function.
* @comm_path: command path created by create_command_path function
* @arr: old array with first argument as non-path command
*
* Description - creates a new argument array that would be passed to another
* function for execution, by modifying old argument array
* Return: new array containing command path
*/
char **create_new_array(char *comm_path, char **arr)
{
int count;
int i;
char **new_arr;
i = 0;
count = 0;
while (arr[i] != NULL)
{
count++;
i++;
}
new_arr = malloc((count + 1) * sizeof(char *));
if (new_arr == NULL)
return (NULL);
new_arr[0] = comm_path;
i = 1;
while (arr[i] != NULL)
{
new_arr[i] = arr[i];
i++;
}
new_arr[i] = NULL;
/*free(arr);---*/
return (new_arr);
}