-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
86 lines (64 loc) · 1.54 KB
/
utils.c
File metadata and controls
86 lines (64 loc) · 1.54 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
// SPDX-License-Identifier: BSD-3-Clause
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "utils.h"
/**
* Concatenate parts of the word to obtain the command.
*/
char *get_word(word_t *s)
{
char *string = NULL;
int string_length = 0;
const char *substring = NULL;
int substring_length = 0;
while (s != NULL) {
if (s->expand == true) {
substring = getenv(s->string);
/* Prevents strlen from failing. */
if (substring == NULL)
substring = "";
} else {
substring = s->string;
}
substring_length = strlen(substring);
string = realloc(string, string_length + substring_length + 1);
DIE(string == NULL, "Error allocating word string.");
string[string_length] = '\0';
strcat(string, substring);
string_length += substring_length;
s = s->next_part;
}
return string;
}
/**
* Concatenate command arguments in a NULL terminated list in order to pass
* them directly to execv.
*/
char **get_argv(simple_command_t *command, int *size)
{
char **argv;
int argc;
word_t *param;
argc = 1;
/* Get parameters number. */
param = command->params;
while (param != NULL) {
param = param->next_word;
argc++;
}
argv = calloc(argc + 1, sizeof(char *));
DIE(argv == NULL, "Error allocating argv.");
argv[0] = get_word(command->verb);
DIE(argv[0] == NULL, "Error retrieving word.");
param = command->params;
argc = 1;
while (param != NULL) {
argv[argc] = get_word(param);
DIE(argv[argc] == NULL, "Error retrieving word.");
param = param->next_word;
argc++;
}
*size = argc;
return argv;
}