Annotation of src/usr.bin/sudo/sudo_setenv.c, Revision 1.2
1.1 millert 1: /*
2: * Copyright (c) 1996, 1998, 1999 Todd C. Miller <Todd.Miller@courtesan.com>
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: *
9: * 1. Redistributions of source code must retain the above copyright
10: * notice, this list of conditions and the following disclaimer.
11: *
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * 3. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * 4. Products derived from this software may not be called "Sudo" nor
20: * may "Sudo" appear in their names without specific prior written
21: * permission from the author.
22: *
23: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
26: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33: */
34:
35: #include "config.h"
36:
37: #include <stdio.h>
38: #ifdef STDC_HEADERS
39: #include <stdlib.h>
40: #endif /* STDC_HEADERS */
41: #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
42: #include <malloc.h>
43: #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
44: #ifdef HAVE_UNISTD_H
45: #include <unistd.h>
46: #endif /* HAVE_UNISTD_H */
47: #include <sys/types.h>
48: #include <sys/param.h>
49:
50: #include "sudo.h"
51:
52: #ifndef STDC_HEADERS
53: #ifdef HAVE_PUTENV
54: extern int putenv __P((const char *));
55: #endif /* HAVE_PUTENV */
56: #ifdef HAVE_SETENV
57: extern int setenv __P((char *, char *, int));
58: #endif /* HAVE_SETENV */
59: #endif /* !STDC_HEADERS */
60:
61: #ifndef lint
1.2 ! millert 62: static const char rcsid[] = "$Sudo: sudo_setenv.c,v 1.41 2000/09/14 20:48:58 millert Exp $";
1.1 millert 63: #endif /* lint */
64:
65:
66: /*
1.2 ! millert 67: * Add a string of the form "var=val" to the environment. Exits if it is
! 68: * unable to expand the current environent.
1.1 millert 69: */
1.2 ! millert 70: void
1.1 millert 71: sudo_setenv(var, val)
72: char *var;
73: char *val;
74: {
75:
76: #ifdef HAVE_SETENV
1.2 ! millert 77: if (setenv(var, val, 1) == -1) {
! 78: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
! 79: exit(1);
! 80: }
1.1 millert 81: #else
82: char *envstring, *tmp;
83:
84: envstring = tmp = (char *) malloc(strlen(var) + strlen(val) + 2);
1.2 ! millert 85: if (envstring == NULL) {
! 86: (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
! 87: exit(1);
! 88: }
1.1 millert 89:
90: while ((*tmp++ = *var++))
91: ;
92:
93: *(tmp-1) = '=';
94:
95: while ((*tmp++ = *val++))
96: ;
97:
1.2 ! millert 98: putenv(envstring);
1.1 millert 99: #endif /* HAVE_SETENV */
100: }