-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.c
More file actions
48 lines (41 loc) · 847 Bytes
/
string.c
File metadata and controls
48 lines (41 loc) · 847 Bytes
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
#include "shell.h"
/**
* remove_newline - removes newline
* @str: string to modify
*/
void remove_newline(char *str)
{
int len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
str[len - 1] = '\0';
}
/**
* tokenize - tokenizes a string
* @str: string to tokenize
* @name: name of program
* Return: array of tokenized parts from str
*/
char **tokenize(char *str, char *name)
{
int i = 0;
char **args = NULL;
char *token;
char delim[] = " \t\n\r";
args = malloc(64 * sizeof(char *));
if (args == NULL)
{
error(name, 0, NULL, NULL, isatty(fileno(stdin)));
}
token = strtok(str, delim);
while (token != NULL)
{
if (token[0] == '"' || token[0] == '\'')
args[i] = strndup(token + 1, strlen(token) - 2);
else
args[i] = strdup(token);
token = strtok(NULL, delim);
i++;
}
args[i] = NULL;
return (args);
}