~peter-pearse/ubuntu/natty/diffutils/prop001

« back to all changes in this revision

Viewing changes to gnulib-tests/putenv.c

  • Committer: Bazaar Package Importer
  • Author(s): Santiago Vila
  • Date: 2010-05-04 20:38:00 UTC
  • mfrom: (2.1.7 sid)
  • Revision ID: james.westby@ubuntu.com-20100504203800-f67xd9rsa9xl9qqj
Tags: 1:3.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- buffer-read-only: t -*- vi: set ro: */
 
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
 
3
/* Copyright (C) 1991, 1994, 1997-1998, 2000, 2003-2010 Free Software
 
4
   Foundation, Inc.
 
5
 
 
6
   NOTE: The canonical source of this file is maintained with the GNU C
 
7
   Library.  Bugs can be reported to bug-glibc@prep.ai.mit.edu.
 
8
 
 
9
   This program is free software: you can redistribute it and/or modify it
 
10
   under the terms of the GNU General Public License as published by the
 
11
   Free Software Foundation; either version 3 of the License, or any
 
12
   later version.
 
13
 
 
14
   This program is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
21
 
 
22
#include <config.h>
 
23
 
 
24
/* Specification.  */
 
25
#include <stdlib.h>
 
26
 
 
27
#include <stddef.h>
 
28
 
 
29
/* Include errno.h *after* sys/types.h to work around header problems
 
30
   on AIX 3.2.5.  */
 
31
#include <errno.h>
 
32
#ifndef __set_errno
 
33
# define __set_errno(ev) ((errno) = (ev))
 
34
#endif
 
35
 
 
36
#include <string.h>
 
37
#include <unistd.h>
 
38
 
 
39
#if HAVE_GNU_LD
 
40
# define environ __environ
 
41
#else
 
42
extern char **environ;
 
43
#endif
 
44
 
 
45
#if _LIBC
 
46
/* This lock protects against simultaneous modifications of `environ'.  */
 
47
# include <bits/libc-lock.h>
 
48
__libc_lock_define_initialized (static, envlock)
 
49
# define LOCK   __libc_lock_lock (envlock)
 
50
# define UNLOCK __libc_lock_unlock (envlock)
 
51
#else
 
52
# define LOCK
 
53
# define UNLOCK
 
54
#endif
 
55
 
 
56
static int
 
57
_unsetenv (const char *name)
 
58
{
 
59
  size_t len;
 
60
  char **ep;
 
61
 
 
62
  if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
 
63
    {
 
64
      __set_errno (EINVAL);
 
65
      return -1;
 
66
    }
 
67
 
 
68
  len = strlen (name);
 
69
 
 
70
  LOCK;
 
71
 
 
72
  ep = environ;
 
73
  while (*ep != NULL)
 
74
    if (!strncmp (*ep, name, len) && (*ep)[len] == '=')
 
75
      {
 
76
        /* Found it.  Remove this pointer by moving later ones back.  */
 
77
        char **dp = ep;
 
78
 
 
79
        do
 
80
          dp[0] = dp[1];
 
81
        while (*dp++);
 
82
        /* Continue the loop in case NAME appears again.  */
 
83
      }
 
84
    else
 
85
      ++ep;
 
86
 
 
87
  UNLOCK;
 
88
 
 
89
  return 0;
 
90
}
 
91
 
 
92
 
 
93
/* Put STRING, which is of the form "NAME=VALUE", in the environment.
 
94
   If STRING contains no `=', then remove STRING from the environment.  */
 
95
int
 
96
putenv (char *string)
 
97
{
 
98
  const char *const name_end = strchr (string, '=');
 
99
  register size_t size;
 
100
  register char **ep;
 
101
 
 
102
  if (name_end == NULL)
 
103
    {
 
104
      /* Remove the variable from the environment.  */
 
105
      return _unsetenv (string);
 
106
    }
 
107
 
 
108
  size = 0;
 
109
  for (ep = environ; *ep != NULL; ++ep)
 
110
    if (!strncmp (*ep, string, name_end - string) &&
 
111
        (*ep)[name_end - string] == '=')
 
112
      break;
 
113
    else
 
114
      ++size;
 
115
 
 
116
  if (*ep == NULL)
 
117
    {
 
118
      static char **last_environ = NULL;
 
119
      char **new_environ = (char **) malloc ((size + 2) * sizeof (char *));
 
120
      if (new_environ == NULL)
 
121
        return -1;
 
122
      (void) memcpy ((void *) new_environ, (void *) environ,
 
123
                     size * sizeof (char *));
 
124
      new_environ[size] = (char *) string;
 
125
      new_environ[size + 1] = NULL;
 
126
      free (last_environ);
 
127
      last_environ = new_environ;
 
128
      environ = new_environ;
 
129
    }
 
130
  else
 
131
    *ep = string;
 
132
 
 
133
  return 0;
 
134
}