forked from erikaosgue/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfamily.c
More file actions
69 lines (67 loc) · 1.56 KB
/
Copy pathfamily.c
File metadata and controls
69 lines (67 loc) · 1.56 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
#include "shell.h"
/**
* access_to_family - the fork process
* @args: Double pointer to tokens like: {"ls","-l" NULL}
* @buffer: It's the pointer from getline function from stdin
* @av: argument vector or array of arguments
* @count: The counter for every shell prompt display
* Return: 0 if succes, other number otherwise
*/
int access_to_family(char **args, char *buffer, char **av, int count)
{
char *pathname = NULL;
if (_strcmp(args[0], "..") == 0)
{
errores(args, av, count, 3);
return (127);
}
pathname = _which(args[0]);
if (pathname == NULL)
{
errores(args, av, count, 1);
return (127);
}
if (access(pathname, X_OK) != 0)
{
errores(args, av, count, 3);
free(pathname);
return (126);
}
return (family(pathname, args, buffer, av, count));
}
/**
* family - the fork process
* @pathname: It's the pointer to the pathname of the file
* @args: Double pointer to tokens like: {"ls","-l" NULL}
* @buffer: It's the pointer from getline function from stdin
* @av: argument vector or array of arguments
* @count: The counter for every shell prompt display
* Return: 0 if succes, other number otherwise
*/
int family(char *pathname, char **args, char *buffer, char **av, int count)
{
pid_t pid;
int status = 0, exit_ = 0;
pid = fork();
if (pid == -1)
{
perror("Error:");
return (1);
}
if (pid == 0)
{
if (execve(pathname, args, environ) == -1)
{
errores(args, av, count, 3);
fredom(args, buffer, pathname, 1);
exit(126);
}
}
else
{
waitpid(pid, &status, 0);
exit_ = (WEXITSTATUS(status));
}
free(pathname);
return (exit_);
}