-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_process.c
170 lines (164 loc) · 2.91 KB
/
create_process.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "shell.h"
/**
* create_process - create a new process for a full path
* @args: array of strings that contains the command and its flags
* Return: 1 if sucess, 0 otherwise.
*/
int create_process(char **args)
{
pid_t pid;
int status;
pid = fork();
if (pid == 0)
{
if (execve(args[0], args, NULL) == -1)
{
perror("error in create_process: child process");
exit(EXIT_FAILURE);
}
}
else if (pid < 0)
{
perror("error in create_process: forking");
return (0);
}
else
{
if (waitpid(pid, &status, 0) == -1)
{
perror("error in waitpid");
return (0);
}
}
return (1);
}
/**
* handle_path_commands - handles commands not given as full paths
* @args: array of commands
* Return: returns 1 on success 0 on failure
*/
int handle_path_commands(char **args)
{
int i;
char *path, **paths, *path_command;
path = get_path();
if (path == NULL)
{
return (0);
}
paths = get_tokens(path, ":");
if (paths == NULL)
{
for (i = 0; paths[i] != NULL; i++)
{
free(paths[i]);
}
free(path);
return (0);
}
path_command = check_builtin(paths, args[0]);
if (path_command == NULL)
{
return (0);
}
for (i = 0; paths[i] != NULL; i++)
{
free(paths[i]);
}
free(paths);
if (handle_path_commands2(path_command, args) == 1)
{
return (1);
}
return (0);
}
/**
* handle_path_commands2 - continuation of the previous function
* @path_command: full comand
* @args: array of commands
* Return: returns 1 on success 0 on failure
*/
int handle_path_commands2(char *path_command, char **args)
{
pid_t pid;
int status;
if (path_command != NULL)
{
pid = fork();
if (pid == 0)
{
if (execve(path_command, args, NULL) == -1)
{
free(path_command);
exit(EXIT_FAILURE);
}
}
else if (pid < 0)
{
free(path_command);
return (0);
}
else
{
if (waitpid(pid, &status, 0) == -1)
{
perror("error in waitpid");
return (0);
}
}
free(path_command);
return (1);
}
perror(args[0]);
return (0);
}
/**
* checker - checks whether a command is a full path
* @args: the array of commands/ command tokens
* Return: 1 on successfull execution or 0 if no execution
*
*/
int checker(char **args)
{
if (**args == '/')
{
if (create_process(args) == 0)
{
perror("Create process:");
}
return (1);
}
else if (handle_builtin_commands(args) == 1)
{
return (1);
}
else
{
if (handle_path_commands(args) == 0)
{
return (0);
}
}
return (0);
}
/**
* path_concat - appends the command to the directory name
* @directory: the directory
* @command: the command to be appended
* Return: returns the full path string
*/
char *path_concat(char *directory, char *command)
{
int length;
char *full_path;
length = _strlen(directory) + _strlen(command) + 2;
full_path = malloc(length * sizeof(char));
if (full_path == NULL)
{
return (NULL);
}
_strcpy(full_path, directory);
_strcat(full_path, "/");
_strcat(full_path, command);
return (full_path);
}