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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdlib/] [setenv_r.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/* 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 "envlock.h"
30
 
31
extern char **environ;
32
 
33
/* Only deal with a pointer to environ, to work around subtle bugs with shared
34
   libraries and/or small data systems where the user declares his own
35
   'environ'.  */
36
static char ***p_environ = &environ;
37
 
38
/* _findenv_r is defined in getenv_r.c.  */
39
extern char *_findenv_r _PARAMS ((struct _reent *, const char *, int *));
40
 
41
/*
42
 * _setenv_r --
43
 *      Set the value of the environmental variable "name" to be
44
 *      "value".  If rewrite is set, replace any current value.
45
 */
46
 
47
int
48
_DEFUN (_setenv_r, (reent_ptr, name, value, rewrite),
49
        struct _reent *reent_ptr _AND
50
        _CONST char *name _AND
51
        _CONST char *value _AND
52
        int rewrite)
53
{
54
  static int alloced;           /* if allocated space before */
55
  register char *C;
56
  int l_value, offset;
57
 
58
  ENV_LOCK;
59
 
60
  if (*value == '=')            /* no `=' in value */
61
    ++value;
62
  l_value = strlen (value);
63
  if ((C = _findenv_r (reent_ptr, name, &offset)))
64
    {                           /* find if already exists */
65
      if (!rewrite)
66
        {
67
          ENV_UNLOCK;
68
          return 0;
69
        }
70
      if (strlen (C) >= l_value)
71
        {                       /* old larger; copy over */
72
         while ((*C++ = *value++) != 0);
73
          ENV_UNLOCK;
74
          return 0;
75
        }
76
    }
77
  else
78
    {                           /* create new slot */
79
      register int cnt;
80
      register char **P;
81
 
82
      for (P = *p_environ, cnt = 0; *P; ++P, ++cnt);
83
      if (alloced)
84
        {                       /* just increase size */
85
          *p_environ = (char **) _realloc_r (reent_ptr, (char *) environ,
86
                                             (size_t) (sizeof (char *) * (cnt + 2)));
87
          if (!*p_environ)
88
            {
89
              ENV_UNLOCK;
90
              return -1;
91
            }
92
        }
93
      else
94
        {                       /* get new space */
95
          alloced = 1;          /* copy old entries into it */
96
          P = (char **) _malloc_r (reent_ptr, (size_t) (sizeof (char *) * (cnt + 2)));
97
          if (!P)
98
            {
99
              ENV_UNLOCK;
100
              return (-1);
101
            }
102
          bcopy ((char *) *p_environ, (char *) P, cnt * sizeof (char *));
103
          *p_environ = P;
104
        }
105
      (*p_environ)[cnt + 1] = NULL;
106
      offset = cnt;
107
    }
108
  for (C = (char *) name; *C && *C != '='; ++C);        /* no `=' in name */
109
  if (!((*p_environ)[offset] =  /* name + `=' + value */
110
        _malloc_r (reent_ptr, (size_t) ((int) (C - name) + l_value + 2))))
111
    {
112
      ENV_UNLOCK;
113
      return -1;
114
    }
115
  for (C = (*p_environ)[offset]; (*C = *name++) && *C != '='; ++C);
116
  for (*C++ = '='; (*C++ = *value++) != 0;);
117
 
118
  ENV_UNLOCK;
119
 
120
  return 0;
121
}
122
 
123
/*
124
 * _unsetenv_r(name) --
125
 *      Delete environmental variable "name".
126
 */
127
void
128
_DEFUN (_unsetenv_r, (reent_ptr, name),
129
        struct _reent *reent_ptr _AND
130
        _CONST char *name)
131
{
132
  register char **P;
133
  int offset;
134
 
135
  ENV_LOCK;
136
 
137
  while (_findenv_r (reent_ptr, name, &offset)) /* if set multiple times */
138
    for (P = &(*p_environ)[offset];; ++P)
139
      if (!(*P = *(P + 1)))
140
        break;
141
 
142
  ENV_UNLOCK;
143
}

powered by: WebSVN 2.1.0

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