-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec.c
More file actions
119 lines (103 loc) · 1.82 KB
/
Copy pathexec.c
File metadata and controls
119 lines (103 loc) · 1.82 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
#include "header.h"
/**
* _exec - Funtion to execute program.
*
* @token: Command from the user imput.
* @array: arguments for the program to execute.
* @count_w: holis.
*
* Return: Always return 0.
*/
int _exec(int count_w, char *token, char *array[])
{
struct stat st;
pid_t child = 0;
if (stat(token, &st) == 0)
{
child = fork();
if (child == -1)
free(token),
perror("Error"), exit(0);
if (child == 0)
{
if (execve(token, array, NULL) == -1)
perror("Error Execve"), exit(0);
}
else
{
wait(NULL);
if (token != array[0])
free(token);
_sfree(array);
}
}
else
{
_error(count_w, array);
_sfree(array);
}
return (0);
}
/**
* _print_env - Function to print env
* @buffer: string from buffer to compare
* Return: 1 for success or -1 for error
*/
int _print_env(char *buffer)
{
int i = 0, j = 0;
char *key_word[] = {"env", NULL};
char *env_token = NULL;
if (buffer[0] == ' ')
env_token = _strtok(buffer, " ");
else
{
env_token = buffer;
}
if (_strcmp(env_token, key_word[0]) == 0)
{
while (environ[i])
{
for (j = 0; environ[i][j]; j++)
;
write(1, environ[i], j);
write(1, "\n", 1);
i++;
}
return (1);
}
return (-1);
}
/**
*_ctrl_c - function to capture interruption signal
*@sig: signal variable
*Return: void
*/
void _ctrl_c(int sig)
{
(void)sig;
write(STDERR_FILENO, "\n", 1);
write(STDOUT_FILENO, "\033[94mminishell$: \033[0m", 16);
}
/**
* _getenv - Function to get de enviroment
* @name:string from buffer
* Return: PATH to succes or NULL for error
*/
char *_getenv(char *name)
{
int i = 0, j = 0;
char *null = "(null)";
char *path = NULL;
for (j = 0; name[j]; j++)
;
for (i = 0; environ[i] != NULL; i++)
{
if (!_strcmp(environ[i], name))
{
path = (environ[i] + (j + 1));
return (path);
}
}
return (null);
}