]> git.donarmstrong.com Git - lilypond.git/blob - guile18/libguile/putenv.c
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / libguile / putenv.c
1 /* Copyright (C) 1991, 2000, 2001, 2004, 2005, 2006 Free Software Foundation, Inc.
2
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  */
17
18
19 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "libguile/scmconfig.h"
24
25 #include <sys/types.h>
26 #include <errno.h>
27
28 /* Don't include stdlib.h for non-GNU C libraries because some of them
29    contain conflicting prototypes for getopt.
30    This needs to come after some library #include
31    to get __GNU_LIBRARY__ defined.  */
32 #ifdef  __GNU_LIBRARY__
33 #include <stdlib.h>
34 #else
35 char *malloc ();
36 #endif  /* GNU C library.  */
37
38 #if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
39 #include <string.h>
40 #else
41 #include <strings.h>
42 #ifndef strchr
43 #define strchr index
44 #endif
45 #ifndef memcpy
46 #define memcpy(d, s, n) bcopy((s), (d), (n))
47 #endif
48 #endif
49
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53
54 #if HAVE_CRT_EXTERNS_H
55 #include <crt_externs.h>  /* for Darwin _NSGetEnviron */
56 #endif
57
58 #ifndef NULL
59 #define NULL 0
60 #endif
61
62 extern char **environ;
63
64 /* On Apple Darwin in a shared library there's no "environ" to access
65    directly, instead the address of that variable must be obtained with
66    _NSGetEnviron().  */
67 #if HAVE__NSGETENVIRON && defined (PIC)
68 #define environ (*_NSGetEnviron())
69 #endif
70
71 /* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
72 int
73 putenv (const char *string)
74 {
75   char *name_end = strchr (string, '=');
76   register size_t size;
77   register char **ep;
78
79   if (name_end == NULL)
80     {
81       /* Remove the variable from the environment.  */
82       size = strlen (string);
83       for (ep = environ; *ep != NULL; ++ep)
84         if (!strncmp (*ep, string, size) && (*ep)[size] == '=')
85           {
86             while (ep[1] != NULL)
87               {
88                 ep[0] = ep[1];
89                 ++ep;
90               }
91             *ep = NULL;
92             return 0;
93           }
94     }
95
96   size = 0;
97   for (ep = environ; *ep != NULL; ++ep)
98     if (!strncmp (*ep, string, name_end - string) &&
99         (*ep)[name_end - string] == '=')
100       break;
101     else
102       ++size;
103
104   if (*ep == NULL)
105     {
106       static char **last_environ = NULL;
107       char **new_environ = (char **) scm_malloc ((size + 2) * sizeof (char *));
108       memcpy ((char *) new_environ, (char *) environ, size * sizeof (char *));
109       new_environ[size] = (char *) string;
110       new_environ[size + 1] = NULL;
111       if (last_environ != NULL)
112         free ((char *) last_environ);
113       last_environ = new_environ;
114       environ = new_environ;
115     }
116   else
117     *ep = (char *) string;
118
119   return 0;
120 }
121
122 /*
123   Local Variables:
124   c-file-style: "gnu"
125   End:
126 */