~ubuntu-branches/ubuntu/lucid/gnats/lucid

« back to all changes in this revision

Viewing changes to libiberty/setenv.c

  • Committer: Bazaar Package Importer
  • Author(s): Chad Walstrom
  • Date: 2005-03-07 17:56:31 UTC
  • mfrom: (1.1.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050307175631-agtm10dvjbemuc64
Tags: 4.1.0-0
* New upstream version
* debian/rules: now uses '--with-lispdir' option instead of environment
  variable overloading. Re-enabled optimization.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
2
 
   This file based on setenv.c in the GNU C Library.
3
 
 
4
 
   The GNU C Library is free software; you can redistribute it and/or
5
 
   modify it under the terms of the GNU Library General Public License as
6
 
   published by the Free Software Foundation; either version 2 of the
7
 
   License, or (at your option) any later version.
8
 
 
9
 
   The GNU C Library is distributed in the hope that it will be useful,
10
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 
   Library General Public License for more details.
13
 
 
14
 
   You should have received a copy of the GNU Library General Public
15
 
   License along with the GNU C Library; see the file COPYING.LIB.  If not,
16
 
   write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
 
   Boston, MA 02111-1307, USA.  */
18
 
 
19
 
#if HAVE_CONFIG_H
20
 
# include <config.h>
21
 
#endif
22
 
 
23
 
#include "ansidecl.h"
24
 
#include <sys/types.h> /* For `size_t' */
25
 
#include <stdio.h>     /* For `NULL' */
26
 
 
27
 
#include <errno.h>
28
 
#if !defined(errno) && !defined(HAVE_ERRNO_DECL)
29
 
extern int errno;
30
 
#endif
31
 
#define __set_errno(ev) ((errno) = (ev))
32
 
 
33
 
#if HAVE_STDLIB_H
34
 
# include <stdlib.h>
35
 
#endif
36
 
#if HAVE_STRING_H
37
 
# include <string.h>
38
 
#endif
39
 
#if HAVE_UNISTD_H
40
 
# include <unistd.h>
41
 
#endif
42
 
 
43
 
#define __environ       environ
44
 
#ifndef HAVE_ENVIRON_DECL
45
 
extern char **environ;
46
 
#endif
47
 
 
48
 
/* LOCK and UNLOCK are defined as no-ops.  This makes the libiberty
49
 
 * implementation MT-Unsafe. */
50
 
#define LOCK
51
 
#define UNLOCK
52
 
 
53
 
/* Below this point, it's verbatim code from the glibc-2.0 implementation */
54
 
 
55
 
/* If this variable is not a null pointer we allocated the current
56
 
   environment.  */
57
 
static char **last_environ;
58
 
 
59
 
 
60
 
int
61
 
setenv (name, value, replace)
62
 
     const char *name;
63
 
     const char *value;
64
 
     int replace;
65
 
{
66
 
  register char **ep = 0;
67
 
  register size_t size;
68
 
  const size_t namelen = strlen (name);
69
 
  const size_t vallen = strlen (value) + 1;
70
 
 
71
 
  LOCK;
72
 
 
73
 
  size = 0;
74
 
  if (__environ != NULL)
75
 
    {
76
 
      for (ep = __environ; *ep != NULL; ++ep)
77
 
        if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=')
78
 
          break;
79
 
        else
80
 
          ++size;
81
 
    }
82
 
 
83
 
  if (__environ == NULL || *ep == NULL)
84
 
    {
85
 
      char **new_environ;
86
 
      if (__environ == last_environ && __environ != NULL)
87
 
        /* We allocated this space; we can extend it.  */
88
 
        new_environ = (char **) realloc (last_environ,
89
 
                                         (size + 2) * sizeof (char *));
90
 
      else
91
 
        new_environ = (char **) malloc ((size + 2) * sizeof (char *));
92
 
 
93
 
      if (new_environ == NULL)
94
 
        {
95
 
          UNLOCK;
96
 
          return -1;
97
 
        }
98
 
 
99
 
      new_environ[size] = malloc (namelen + 1 + vallen);
100
 
      if (new_environ[size] == NULL)
101
 
        {
102
 
          free ((char *) new_environ);
103
 
          __set_errno (ENOMEM);
104
 
          UNLOCK;
105
 
          return -1;
106
 
        }
107
 
 
108
 
      if (__environ != last_environ)
109
 
        memcpy ((char *) new_environ, (char *) __environ,
110
 
                size * sizeof (char *));
111
 
 
112
 
      memcpy (new_environ[size], name, namelen);
113
 
      new_environ[size][namelen] = '=';
114
 
      memcpy (&new_environ[size][namelen + 1], value, vallen);
115
 
 
116
 
      new_environ[size + 1] = NULL;
117
 
 
118
 
      last_environ = __environ = new_environ;
119
 
    }
120
 
  else if (replace)
121
 
    {
122
 
      size_t len = strlen (*ep);
123
 
      if (len + 1 < namelen + 1 + vallen)
124
 
        {
125
 
          /* The existing string is too short; malloc a new one.  */
126
 
          char *new = malloc (namelen + 1 + vallen);
127
 
          if (new == NULL)
128
 
            {
129
 
              UNLOCK;
130
 
              return -1;
131
 
            }
132
 
          *ep = new;
133
 
        }
134
 
      memcpy (*ep, name, namelen);
135
 
      (*ep)[namelen] = '=';
136
 
      memcpy (&(*ep)[namelen + 1], value, vallen);
137
 
    }
138
 
 
139
 
  UNLOCK;
140
 
 
141
 
  return 0;
142
 
}
143
 
 
144
 
void
145
 
unsetenv (name)
146
 
     const char *name;
147
 
{
148
 
  const size_t len = strlen (name);
149
 
  char **ep;
150
 
 
151
 
  LOCK;
152
 
 
153
 
  for (ep = __environ; *ep; ++ep)
154
 
    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
155
 
      {
156
 
        /* Found it.  Remove this pointer by moving later ones back.  */
157
 
        char **dp = ep;
158
 
        do
159
 
          dp[0] = dp[1];
160
 
        while (*dp++);
161
 
        /* Continue the loop in case NAME appears again.  */
162
 
      }
163
 
 
164
 
  UNLOCK;
165
 
}