-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell_helper1.c
More file actions
102 lines (96 loc) · 1.77 KB
/
shell_helper1.c
File metadata and controls
102 lines (96 loc) · 1.77 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
#include "holberton.h"
/**
* signalhandler - handles the ctrl-c key to keep looping
* @sig: signal
*/
void signalhandler(int sig)
{
(void) sig;
write(STDOUT_FILENO, "\n\r$ ", 4);
}
/**
* _strcmp - compares 2 strings
* @s1: string1 to compare
* @s2: string2 to compare
* Return: -1,0,1 if string1 <, =, > string2
*/
int _strcmp(char *s1, char *s2)
{
int i = 0, diff = 0;
while (*(s1 + i) != 0 && *(s2 + i) != 0 && diff == 0)
{
diff = (*(s1 + i) - *(s2 + i));
i++;
}
if (diff != 0)
{
return (diff);
}
else
{
if (*(s1 + i) == 0 && *(s2 + i) == 0)
return (0);
else
return (*(s1 + i) - *(s2 + i));
}
}
/**
* _strdup - duplicates a string
* @str: string to duplicated
* Return: pointer to new string
*/
char *_strdup(char *str)
{
char *s;
int i = 0, j;
if (!str)
{
_exit(1);
}
while (*(str + i))
i++;
i++;
s = malloc(sizeof(char) * i);
if (s == NULL)
{
_exit(1);
}
for (j = 0; j < i; j++)
s[j] = str[j];
return (s);
}
/**
* create_arg_list - takes the input buffer and creates argument list
* @buff_tk: pointer to where the argument lis will be stored
* @buff: buffer to get argument list from
* @delim: delimiter to use to delimit buffer
* Return: pointer to pointer of argument list
*/
char **create_arg_list(char **buff_tk, char *buff, const char *delim)
{
int count = 0;
char *toprint, *buffdup;
buffdup = _strdup(buff);
toprint = strtok(buffdup, delim);
while (toprint)
{
toprint = strtok(NULL, delim);
count++;
}
free(buffdup);
if (!count)
return (NULL);
buff_tk = (char **) malloc((count + 1) * sizeof(char *));
if (!buff_tk)
_exit(1);
toprint = strtok(buff, delim);
count = 0;
while (toprint)
{
buff_tk[count] = toprint;
toprint = strtok(NULL, delim);
count++;
}
buff_tk[count] = NULL;
return (buff_tk);
}