BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file getenv.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | * |
||
29 | * @section DESCRIPTION |
||
30 | * |
||
31 | * Synopsis: |
||
32 | * getenv(string name) |
||
33 | * |
||
34 | * Variables: |
||
35 | * string (empty) - if the environment value exists, its value |
||
36 | * string exists - "true" if the variable exists, "false" if not |
||
37 | */ |
||
38 | |||
39 | #include <stdlib.h> |
||
40 | |||
41 | #include <misc/strdup.h> |
||
42 | |||
43 | #include <ncd/module_common.h> |
||
44 | |||
45 | #include <generated/blog_channel_ncd_getenv.h> |
||
46 | |||
47 | struct instance { |
||
48 | NCDModuleInst *i; |
||
49 | char *value; |
||
50 | }; |
||
51 | |||
52 | enum {STRING_EXISTS}; |
||
53 | |||
54 | static const char *strings[] = {"exists", NULL}; |
||
55 | |||
56 | static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params) |
||
57 | { |
||
58 | struct instance *o = vo; |
||
59 | o->i = i; |
||
60 | |||
61 | // check arguments |
||
62 | NCDValRef name_arg; |
||
63 | if (!NCDVal_ListRead(params->args, 1, &name_arg)) { |
||
64 | ModuleLog(i, BLOG_ERROR, "wrong arity"); |
||
65 | goto fail0; |
||
66 | } |
||
67 | if (!NCDVal_IsString(name_arg)) { |
||
68 | ModuleLog(i, BLOG_ERROR, "wrong type"); |
||
69 | goto fail0; |
||
70 | } |
||
71 | |||
72 | o->value = NULL; |
||
73 | |||
74 | if (NCDVal_StringHasNulls(name_arg)) { |
||
75 | goto out; |
||
76 | } |
||
77 | |||
78 | NCDValNullTermString nts; |
||
79 | if (!NCDVal_StringNullTerminate(name_arg, &nts)) { |
||
80 | ModuleLog(i, BLOG_ERROR, "NCDVal_StringNullTerminate failed"); |
||
81 | goto fail0; |
||
82 | } |
||
83 | |||
84 | char *result = getenv(nts.data); |
||
85 | NCDValNullTermString_Free(&nts); |
||
86 | if (!result) { |
||
87 | goto out; |
||
88 | } |
||
89 | |||
90 | o->value = b_strdup(result); |
||
91 | if (!o->value) { |
||
92 | ModuleLog(i, BLOG_ERROR, "b_strdup failed"); |
||
93 | goto fail0; |
||
94 | } |
||
95 | |||
96 | out: |
||
97 | // signal up |
||
98 | NCDModuleInst_Backend_Up(i); |
||
99 | return; |
||
100 | |||
101 | fail0: |
||
102 | NCDModuleInst_Backend_DeadError(i); |
||
103 | } |
||
104 | |||
105 | static void func_die (void *vo) |
||
106 | { |
||
107 | struct instance *o = vo; |
||
108 | |||
109 | // free value |
||
110 | if (o->value) { |
||
111 | BFree(o->value); |
||
112 | } |
||
113 | |||
114 | NCDModuleInst_Backend_Dead(o->i); |
||
115 | } |
||
116 | |||
117 | static int func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) |
||
118 | { |
||
119 | struct instance *o = vo; |
||
120 | |||
121 | if (name == NCD_STRING_EMPTY && o->value) { |
||
122 | *out = NCDVal_NewString(mem, o->value); |
||
123 | return 1; |
||
124 | } |
||
125 | |||
126 | if (name == ModuleString(o->i, STRING_EXISTS)) { |
||
127 | *out = ncd_make_boolean(mem, !!o->value); |
||
128 | return 1; |
||
129 | } |
||
130 | |||
131 | return 0; |
||
132 | } |
||
133 | |||
134 | static struct NCDModule modules[] = { |
||
135 | { |
||
136 | .type = "getenv", |
||
137 | .func_new2 = func_new, |
||
138 | .func_die = func_die, |
||
139 | .func_getvar2 = func_getvar, |
||
140 | .alloc_size = sizeof(struct instance) |
||
141 | }, { |
||
142 | .type = NULL |
||
143 | } |
||
144 | }; |
||
145 | |||
146 | const struct NCDModuleGroup ncdmodule_getenv = { |
||
147 | .modules = modules, |
||
148 | .strings = strings |
||
149 | }; |