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