xref: /onnv-gate/usr/src/lib/libwrap/setenv.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate 
8*0Sstevel@tonic-gate  /*
9*0Sstevel@tonic-gate   * Some systems do not have setenv(). This one is modeled after 4.4 BSD, but
10*0Sstevel@tonic-gate   * is implemented in terms of portable primitives only: getenv(), putenv()
11*0Sstevel@tonic-gate   * and malloc(). It should therefore be safe to use on every UNIX system.
12*0Sstevel@tonic-gate   *
13*0Sstevel@tonic-gate   * If clobber == 0, do not overwrite an existing variable.
14*0Sstevel@tonic-gate   *
15*0Sstevel@tonic-gate   * Returns nonzero if memory allocation fails.
16*0Sstevel@tonic-gate   *
17*0Sstevel@tonic-gate   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
18*0Sstevel@tonic-gate   */
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate #ifndef lint
21*0Sstevel@tonic-gate static char sccsid[] = "@(#) setenv.c 1.1 93/03/07 22:47:58";
22*0Sstevel@tonic-gate #endif
23*0Sstevel@tonic-gate 
24*0Sstevel@tonic-gate #include <stdio.h>
25*0Sstevel@tonic-gate #include <stdlib.h>
26*0Sstevel@tonic-gate #include <unistd.h>
27*0Sstevel@tonic-gate #include <string.h>
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* setenv - update or insert environment (name,value) pair */
30*0Sstevel@tonic-gate 
setenv(name,value,clobber)31*0Sstevel@tonic-gate int     setenv(name, value, clobber)
32*0Sstevel@tonic-gate char   *name;
33*0Sstevel@tonic-gate char   *value;
34*0Sstevel@tonic-gate int     clobber;
35*0Sstevel@tonic-gate {
36*0Sstevel@tonic-gate     char   *cp;
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate     if (clobber == 0 && getenv(name) != 0)
39*0Sstevel@tonic-gate 	return (0);
40*0Sstevel@tonic-gate     if ((cp = malloc(strlen(name) + strlen(value) + 2)) == 0)
41*0Sstevel@tonic-gate 	return (1);
42*0Sstevel@tonic-gate     sprintf(cp, "%s=%s", name, value);
43*0Sstevel@tonic-gate     return (putenv(cp));
44*0Sstevel@tonic-gate }
45