aboutsummaryrefslogtreecommitdiff
path: root/tests/envtest.c
blob: 7c65f2acab9b722a8482a3435ac61f9dae7a0a65 (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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* Environment test program for GNU Pies.
   Copyright (C) 2019-2021 Sergey Poznyakoff

   GNU Pies is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Pies is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Pies.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <envop.h>
#include <wordsplit.h>

static int
envcmp (void const *a, void const *b)
{
  char const *as = *(char const **)a;
  char const *bs = *(char const **)b;
  int c;
  
  while (*as && *bs)
    {
      c = *as - *bs;
      if (c || *as == '=' || *bs == '=')
	break;
      as++;
      bs++;
    }
  return c;
}

static void
sortenv (char **env)
{
  size_t n;
  for (n = 0; env[n]; n++)
    ;
  qsort (env, n, sizeof (env[0]), envcmp);
}

static void
printenv (char **env)
{
  size_t i;
  for (i = 0; env[i]; i++)
    {
      char *p = env[i];
      while (*p)
	{
	  fputc (*p, stdout);
	  if (*p++ == '=')
	    break;
	}
      if (*p)
	{
	  int c;
	  fputc ('"', stdout);
	  while ((c = *p++) != 0)
	    {
	      int c1;
	      if (isascii (c) && isprint (c) && c != '\\' && c != '"')
		fputc (c, stdout);
	      else if ((c1 = wordsplit_c_quote_char (c)))
		{
		  fputc ('\\', stdout);
		  fputc (c1, stdout);
		}
	    }
	  fputc ('"', stdout);
	}
      fputc ('\n', stdout);
    }
}

char *defenv[] = {
  "PATH=/usr/local/bin:/usr/bin:/bin",
  "HOME=/home/user",
  "USER=user",
  "LOGIN=user",
  "PWD=/home",
  "LC_ALL=C",
  "LC_CTYPE=C",
  "LC_MESSAGES=C",
  "LC_NUMERIC=C",
  NULL
};

extern char **environ;

int
main (int argc, char **argv)
{
  envop_t *envop = NULL;
  int opcode = envop_set;
  environ_t *env = NULL;

  if (argc > 1)
    {
      if (strcmp (argv[1], "-clone") == 0)
	{
	  env = environ_create (environ);
	  argc--;
	  argv++;
	}
      else if (strcmp (argv[1], "-null") == 0)
	{
	  env = environ_create (NULL);
	  argc--;
	  argv++;
	}
    }

  if (!env)
    env = environ_create (defenv);

  if (!env)
    {
      perror ("environ_create");
      abort ();
    }
  
  while (--argc)
    {
      char *a = *++argv;

      if (strcmp (a, "-set") == 0)
	opcode = envop_set;
      else if (strcmp (a, "-unset") == 0)
	opcode = envop_unset;
      else if (strcmp (a, "-keep") == 0)
	opcode = envop_keep;
      else if (strcmp (a, "-clear") == 0)
	{
	  int rc = envop_entry_add (&envop, envop_clear, NULL, NULL);
	  if (rc)
	    {
	      perror ("envop_entry_add");
	      return 1;
	    }
	}
      else if (strcmp (a, "-exec") == 0)
	{
	  --argc;
	  ++argv;
	  if (argc == 0)
	    {
	      fprintf (stderr, "program name required after -exec\n");
	      return 1;
	    }
	  break;
	}
      else if (a[0] == '-')
	{
	  fprintf (stderr, "unrecognized option: %s\n", a);
	  return 1;
	}
      else
	{
          int rc;
	  char *p = strchr (a, '=');

	  if (p)
	    *p++ = 0;
	  rc = envop_entry_add (&envop, opcode, a, p);

	  if (rc)
	    {
	      perror ("envop_entry_add");
	      return 1;
	    }
	}
    }

  if (envop_exec (envop, env))
    {
      perror ("envop_exec");
      return 1;
    }

  if (argc)
    {
      environ = environ_ptr (env);
      execvp (argv[0], argv);
      perror ("execvp");
      abort ();
    }
  
  sortenv (env->env_base);
  printenv (env->env_base);
  return 0;
}
		  

  

Return to:

Send suggestions and report system problems to the System administrator.