1 #include <string.h>
2 #include "str.h"
3 #include "envstr.h"
4
5 /** Put an assignment, in \c NAME=value format, into the environment
6 * string.
7 * \note Unlike putenv, a copy of the assignment is made instead of
8 * keeping a copy of the given pointer. */
envstr_put(struct str * env,const char * asgn,int overwrite)9 int envstr_put(struct str* env, const char* asgn, int overwrite)
10 {
11 long varlen;
12 const char* found;
13 found = strchr(asgn, '=');
14 varlen = (found == 0) ? (long)strlen(asgn) : found - asgn;
15 if ((found = envstr_find(env, asgn, varlen)) != 0) {
16 if (!overwrite)
17 return 1;
18 str_spliceb(env, found - env->s, strlen(found) + 1, 0, 0);
19 }
20 return str_cats(env, asgn)
21 && str_catc(env, 0);
22 }
23
24 #ifdef SELFTEST_MAIN
25 MAIN
26 {
27 static str env;
28 debugstrfn(envstr_put(&env, "A=4", 0), &env);
29 debugstrfn(envstr_put(&env, "A=5", 0), &env);
30 debugstrfn(envstr_put(&env, "A=6", 1), &env);
31 }
32 #endif
33 #ifdef SELFTEST_EXP
34 result=1 len=4 size=16 s=A=4^@
35 result=1 len=4 size=16 s=A=4^@
36 result=1 len=4 size=16 s=A=6^@
37 #endif
38