-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringproc.c
More file actions
68 lines (67 loc) · 1.5 KB
/
stringproc.c
File metadata and controls
68 lines (67 loc) · 1.5 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
#include <stdlib.h>
#include <unistd.h>
#include "shell.h"
#include <stdio.h>
/**
* process_string - process the next command into string tokens
* @params: parameters
*
* Return: number of tokens
*/
int process_string(param_t *params)
{
char *token = NULL, *state = NULL;
char *alias = NULL, *state_2 = NULL, *val;
list_t *node;
token = _strtok(params->nextCommand, " \n\t", &state);
if (!token)
{
params->tokCount = 0;
return (0);
}
node = get_node(params->alias_head, token);
if (node != NULL)
{
free(token);
token = NULL;
alias = _strdup(node->val);
if (!alias)
{
write(STDERR_FILENO,
"alias expansion malloc error\n", 18);
free_params(params);
exit(-1);
}
val = _strtok(alias, " \n\t", &state_2);
(params->args)[(params->tokCount)++] = val;
while (val)
{
val = _strtok(alias, " ", &state_2);
(params->args)[(params->tokCount)++] = val;
}
free(alias);
}
else
(params->args)[(params->tokCount)++] = token;
token = _strtok(params->nextCommand, " \n\t", &state);
params->args[params->tokCount++] = token;
while (token)
{
token = _strtok(params->nextCommand, " \n\t", &state);
(params->args)[(params->tokCount)++] = token;
if (params->tokCount == params->argsCap)
{
params->argsCap += 10;
params->args = _realloc(params->args,
params->argsCap - 10,
params->argsCap);
if (!(params->args))
{
write(STDERR_FILENO, "realloc error\n", 14);
free_params(params);
exit(-1);
}
}
}
return (params->tokCount - 1);
}