-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathline.c
120 lines (97 loc) · 1.91 KB
/
line.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
#include "shell.h"
/**
* splitstring - Splits a string and creates an array of pointers to words.
* @str: The string to be split.
* @delim: The delimiter.
* Return: Array of pointers to words.
*/
char **splitstring(char *str, const char *delim)
{
char **array = NULL, *token, *copy;
size_t wn = 2; /* Initial size of the array */
int i = 0;
copy = _strdup(str);
if (!copy || !(array = malloc(sizeof(char *) * wn)))
{
perror(_getenv("_"));
return (NULL);
}
token = strtok(copy, delim);
array[0] = _strdup(token);
while ((token = strtok(NULL, delim)))
{
array = _realloc(array, sizeof(char *) * (wn - 1), sizeof(char *) * wn);
array[i + 1] = _strdup(token);
i++;
wn++;
}
free(copy);
return (array);
}
/**
* execute - Executes a command.
* @argv: Array of arguments.
*/
void execute(char **argv)
{
int d, status;
if (!argv || !argv[0])
return;
d = fork();
if (d == -1)
{
perror(_getenv("_"));
return;
}
if (d == 0)
{
if (execve(argv[0], argv, environ) == -1)
{
perror(argv[0]);
exit(EXIT_FAILURE);
}
}
wait(&status);
}
/**
* _realloc - Reallocates memory block.
* @ptr: Previous pointer.
* @old_size: Old size of the previous pointer.
* @new_size: New size for the pointer.
* Return: New resized pointer.
*/
void *_realloc(void *ptr, size_t old_size, size_t new_size)
{
char *new, *old;
size_t i;
if (!ptr)
return (malloc(new_size));
if (new_size == old_size || (new_size == 0 && (free(ptr), 1)))
return (ptr);
new = malloc(new_size);
old = ptr;
if (!new)
return (NULL);
for (i = 0; i < (new_size < old_size ? new_size : old_size); i++)
new[i] = old[i];
free(ptr);
if (new_size > old_size)
{
for (; i < new_size; i++)
new[i] = '\0';
}
return (new);
}
/**
* freearv - Frees the array of pointers arv.
* @arv: Array of pointers.
*/
void freearv(char **arv)
{
int i;
if (!arv)
return;
for (i = 0; arv[i]; i++)
free(arv[i]);
free(arv);
}