summaryrefslogtreecommitdiff
path: root/src/skaembutils/s6-env.c
blob: ce91b94f7c896d33b3bcc72048086c77f5da1dbc (plain)
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
/* ISC license. */

#include <string.h>
#include <errno.h>
#include <skalibs/sgetopt.h>
#include <skalibs/strerr2.h>
#include <skalibs/env.h>
#include <skalibs/djbunix.h>
#include <s6-portable-utils/config.h>

#define USAGE "s6-env [ -i ] [ name=value... ] prog..."

int main (int argc, char const *const *argv, char const *const *envp)
{
  stralloc modifs = STRALLOC_ZERO ;
  char const *arg_zero[2] = { S6_PORTABLE_UTILS_BINPREFIX "s6-printenv", 0 } ;
  char const *env_zero[1] = { 0 } ;
  PROG = "s6-env" ;
  {
    subgetopt_t l = SUBGETOPT_ZERO ;
    for (;;)
    {
      int opt = subgetopt_r(argc, argv, "i", &l) ;
      if (opt == -1) break ;
      switch (opt)
      {
        case 'i': envp = env_zero ; break ;
        default : strerr_dieusage(100, USAGE) ;
      }
    }
    argc -= l.ind ; argv += l.ind ;
  }
  for (; argc ; argc--, argv++)
  {
    if (!strchr(*argv, '=')) break ;
    if (!stralloc_cats(&modifs, *argv) || !stralloc_0(&modifs))
      strerr_diefu1sys(111, "stralloc_cats") ;
  }
  if (!argc) argv = arg_zero ;
  xpathexec_r(argv, envp, env_len(envp), modifs.s, modifs.len) ;
}