-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrite_array.c
72 lines (59 loc) · 1.56 KB
/
write_array.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
#include <builtins.h>
#ifndef EXECUTION_FAILURE
#include <shell.h>
#endif
#include <builtins/bashgetopt.h> // for internal_getopt(), etc.
#include <builtins/common.h> // for no_options()
#include <stdio.h>
void replace_array(const char *name)
{
SHELL_VAR *svar;
svar = find_variable(name);
if (svar)
{
ARRAY *array = array_cell(svar);
array_flush(array);
array_insert(array, 0, "un");
array_insert(array, 1, "deux");
array_insert(array, 2, "trois");
array_insert(array, 3, "quatre");
array_insert(array, 4, "cinq");
array_insert(array, 5, "six");
}
}
static int write_array(WORD_LIST *list)
{
// There should be no options, recognized as arguments
// that begin with a hyphen:
if (no_options(list))
return EX_USAGE;
list = loptend;
if (list == 0)
{
builtin_usage();
return EX_USAGE;
}
WORD_LIST *lptr = list;
while (lptr != NULL)
{
const char *word = lptr->word->word;
if (legal_identifier(word))
replace_array(word);
else
printf("Name ref '%s' is not a legal identifier.\n", word);
lptr = lptr->next;
}
return EXECUTION_SUCCESS;
}
static char *desc_write_array[] = {
"This function will accept the name of an array variable,",
"which will be cleared out and filled with new content."
};
struct builtin write_array_struct = {
.name = "write_array",
.function = write_array,
.flags = BUILTIN_ENABLED,
.long_doc = desc_write_array,
.short_doc = "write_array array_name",
.handle = 0
};