-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutility.c
95 lines (83 loc) · 1.44 KB
/
utility.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
#include "simishell.h"
/**
* envir - a function which excutes the /bin/ls program to list files
* @line: an array of command and arguments
*
* Return: returns 1 in success and -1 if it fails
*/
int envir(char *line[] __attribute__((unused)))
{
int i = 0;
while (environ[i] != NULL)
{
write(1, environ[i], strleng(environ[i]));
write(1, "\n", 2);
i++;
}
free(line);
return (1);
}
/**
* maker - a function which excutes the /bin/ls program to list files
* @line: an array of command and arguments
*
* Return: returns 1 in success and -1 if it fails
*/
int maker(char *line[])
{
int status;
pid_t waiter, child;
child = fork();
if (child == -1)
perror("Forking");
if (child == 0)
{
execve("/bin/mkdir", line, NULL);
}
else
{
waiter = wait(&status);
if (waiter == -1)
perror("Waiting");
}
free(line);
return (1);
}
/**
* stringer - a helper function to the string breaker
* @line: a string pointer
* @i: the index of the start of the string
* @str: a string pointer to the location
* @c: a character which specifys where to end the string
*
* Return: returns a string pointer
*/
char *stringer(char *line, int i, char *str, char c)
{
int k = 0;
char *tmp;
str = malloc(64);
if (str == NULL)
{
perror("Couldn't Allocate");
return (NULL);
}
while (line[i] != c && line[i] != '\0')
{
str[k] = line[i++];
k++;
if (k > 60)
{
tmp = realloc(str, (k + 8));
if (!tmp)
{
perror("Error");
return (NULL);
}
else
str = tmp;
}
}
str[k] = '\0';
return (str);
}