File: env_set.c

package info (click to toggle)
bglibs 2.04%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,500 kB
  • sloc: ansic: 15,824; perl: 674; sh: 63; makefile: 29
file content (33 lines) | stat: -rw-r--r-- 836 bytes parent folder | download | duplicates (4)
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
#include <string.h>
#include "str.h"
#include "envstr.h"

/** Set the named variable to the given value in the environment string. */
int envstr_set(struct str* env, const char* var, const char* val, int overwrite)
{
  const char* found;
  if ((found = envstr_find(env, var, strlen(var))) != 0) {
    if (!overwrite)
      return 1;
    str_spliceb(env, found - env->s, strlen(found) + 1, 0, 0);
  }
  return str_cats(env, var)
    && str_catc(env, '=')
    && str_cats(env, val)
    && str_catc(env, 0);
}

#ifdef SELFTEST_MAIN
MAIN
{
  static str env;
  debugstrfn(envstr_set(&env, "A", "4", 0), &env);
  debugstrfn(envstr_set(&env, "A", "5", 0), &env);
  debugstrfn(envstr_set(&env, "A", "6", 1), &env);
}
#endif
#ifdef SELFTEST_EXP
result=1 len=4 size=16 s=A=4^@
result=1 len=4 size=16 s=A=4^@
result=1 len=4 size=16 s=A=6^@
#endif