OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [newlib-1.17.0/] [newlib/] [libc/] [stdlib/] [setenv_r.c] - Blame information for rev 825

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 148 jeremybenn
/* This file may have been modified by DJ Delorie (Jan 1991).  If so,
2
** these modifications are Copyright (C) 1991 DJ Delorie.
3
*/
4
 
5
/*
6
 * Copyright (c) 1987 Regents of the University of California.
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms are permitted
10
 * provided that: (1) source distributions retain this entire copyright
11
 * notice and comment, and (2) distributions including binaries display
12
 * the following acknowledgement:  ``This product includes software
13
 * developed by the University of California, Berkeley and its contributors''
14
 * in the documentation or other materials provided with the distribution
15
 * and in all advertising materials mentioning features or use of this
16
 * software. Neither the name of the University nor the names of its
17
 * contributors may be used to endorse or promote products derived
18
 * from this software without specific prior written permission.
19
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22
 */
23
 
24
#include <reent.h>
25
 
26
#include <stddef.h>
27
#include <stdlib.h>
28
#include <string.h>
29
#include <time.h>
30
#include <errno.h>
31
#include "envlock.h"
32
 
33
extern char **environ;
34
 
35
/* Only deal with a pointer to environ, to work around subtle bugs with shared
36
   libraries and/or small data systems where the user declares his own
37
   'environ'.  */
38
static char ***p_environ = &environ;
39
 
40
/* _findenv_r is defined in getenv_r.c.  */
41
extern char *_findenv_r _PARAMS ((struct _reent *, const char *, int *));
42
 
43
/*
44
 * _setenv_r --
45
 *      Set the value of the environmental variable "name" to be
46
 *      "value".  If rewrite is set, replace any current value.
47
 *      If "name" contains equal sign, -1 is returned, and errno is
48
 *      set to EINVAL;
49
 */
50
 
51
int
52
_DEFUN (_setenv_r, (reent_ptr, name, value, rewrite),
53
        struct _reent *reent_ptr _AND
54
        _CONST char *name _AND
55
        _CONST char *value _AND
56
        int rewrite)
57
{
58
  static int alloced;           /* if allocated space before */
59
  register char *C;
60
  int l_value, offset;
61
 
62
  if (strchr(name, '='))
63
    {
64
      errno = EINVAL;
65
      return -1;
66
    }
67
 
68
  ENV_LOCK;
69
 
70
  l_value = strlen (value);
71
  if ((C = _findenv_r (reent_ptr, name, &offset)))
72
    {                           /* find if already exists */
73
      if (!rewrite)
74
        {
75
          ENV_UNLOCK;
76
          return 0;
77
        }
78
      if (strlen (C) >= l_value)
79
        {                       /* old larger; copy over */
80
          while ((*C++ = *value++) != 0);
81
          ENV_UNLOCK;
82
          /* if we are changing the TZ environment variable, update timezone info */
83
          if (strcmp (name, "TZ") == 0)
84
            tzset ();
85
          return 0;
86
        }
87
    }
88
  else
89
    {                           /* create new slot */
90
      register int cnt;
91
      register char **P;
92
 
93
      for (P = *p_environ, cnt = 0; *P; ++P, ++cnt);
94
      if (alloced)
95
        {                       /* just increase size */
96
          *p_environ = (char **) _realloc_r (reent_ptr, (char *) environ,
97
                                             (size_t) (sizeof (char *) * (cnt + 2)));
98
          if (!*p_environ)
99
            {
100
              ENV_UNLOCK;
101
              return -1;
102
            }
103
        }
104
      else
105
        {                       /* get new space */
106
          alloced = 1;          /* copy old entries into it */
107
          P = (char **) _malloc_r (reent_ptr, (size_t) (sizeof (char *) * (cnt + 2)));
108
          if (!P)
109
            {
110
              ENV_UNLOCK;
111
              return (-1);
112
            }
113
          bcopy ((char *) *p_environ, (char *) P, cnt * sizeof (char *));
114
          *p_environ = P;
115
        }
116
      (*p_environ)[cnt + 1] = NULL;
117
      offset = cnt;
118
    }
119
  for (C = (char *) name; *C && *C != '='; ++C);        /* no `=' in name */
120
  if (!((*p_environ)[offset] =  /* name + `=' + value */
121
        _malloc_r (reent_ptr, (size_t) ((int) (C - name) + l_value + 2))))
122
    {
123
      ENV_UNLOCK;
124
      return -1;
125
    }
126
  for (C = (*p_environ)[offset]; (*C = *name++) && *C != '='; ++C);
127
  for (*C++ = '='; (*C++ = *value++) != 0;);
128
 
129
  ENV_UNLOCK;
130
 
131
  /* if we are setting the TZ environment variable, update timezone info */
132
  if (strncmp ((*p_environ)[offset], "TZ=", 3) == 0)
133
    tzset ();
134
 
135
  return 0;
136
}
137
 
138
/*
139
 * _unsetenv_r(name) --
140
 *      Delete environmental variable "name".
141
 */
142
int
143
_DEFUN (_unsetenv_r, (reent_ptr, name),
144
        struct _reent *reent_ptr _AND
145
        _CONST char *name)
146
{
147
  register char **P;
148
  int offset;
149
 
150
  /* Name cannot be NULL, empty, or contain an equal sign.  */
151
  if (name == NULL || name[0] == '\0' || strchr(name, '='))
152
    {
153
      errno = EINVAL;
154
      return -1;
155
    }
156
 
157
  ENV_LOCK;
158
 
159
  while (_findenv_r (reent_ptr, name, &offset)) /* if set multiple times */
160
    {
161
      for (P = &(*p_environ)[offset];; ++P)
162
        if (!(*P = *(P + 1)))
163
          break;
164
    }
165
 
166
  ENV_UNLOCK;
167
  return 0;
168
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.