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

Subversion Repositories or1k

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 56 joel
/* This file may have been modified by DJ Delorie (Jan 1991).  If so,
2
** these modifications are Coyright (C) 1991 DJ Delorie, 24 Kirsten Ave,
3
** Rochester NH, 03867-2954, USA.
4
*/
5
 
6
/*
7
 * Copyright (c) 1987 Regents of the University of California.
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms are permitted
11
 * provided that: (1) source distributions retain this entire copyright
12
 * notice and comment, and (2) distributions including binaries display
13
 * the following acknowledgement:  ``This product includes software
14
 * developed by the University of California, Berkeley and its contributors''
15
 * in the documentation or other materials provided with the distribution
16
 * and in all advertising materials mentioning features or use of this
17
 * software. Neither the name of the University nor the names of its
18
 * contributors may be used to endorse or promote products derived
19
 * from this software without specific prior written permission.
20
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
21
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23
 */
24
 
25
#include <reent.h>
26
 
27
#include <stddef.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include "envlock.h"
31
 
32
/* _findenv_r is defined in getenv_r.c.  */
33
extern char *_findenv_r _PARAMS ((struct _reent *, const char *, int *));
34
 
35
/*
36
 * _setenv_r --
37
 *      Set the value of the environmental variable "name" to be
38
 *      "value".  If rewrite is set, replace any current value.
39
 */
40
 
41
int
42
_DEFUN (_setenv_r, (reent_ptr, name, value, rewrite),
43
        struct _reent *reent_ptr _AND
44
        _CONST char *name _AND
45
        _CONST char *value _AND
46
        int rewrite)
47
{
48
  extern char **environ;
49
  static int alloced;           /* if allocated space before */
50
  register char *C;
51
  int l_value, offset;
52
 
53
  ENV_LOCK;
54
 
55
  if (*value == '=')            /* no `=' in value */
56
    ++value;
57
  l_value = strlen (value);
58
  if ((C = _findenv_r (reent_ptr, name, &offset)))
59
    {                           /* find if already exists */
60
      if (!rewrite)
61
        {
62
          ENV_UNLOCK;
63
          return 0;
64
        }
65
      if (strlen (C) >= l_value)
66
        {                       /* old larger; copy over */
67
          while (*C++ = *value++);
68
          ENV_UNLOCK;
69
          return 0;
70
        }
71
    }
72
  else
73
    {                           /* create new slot */
74
      register int cnt;
75
      register char **P;
76
 
77
      for (P = environ, cnt = 0; *P; ++P, ++cnt);
78
      if (alloced)
79
        {                       /* just increase size */
80
          environ = (char **) realloc ((char *) environ,
81
                                       (size_t) (sizeof (char *) * (cnt + 2)));
82
          if (!environ)
83
            {
84
              ENV_UNLOCK;
85
              return -1;
86
            }
87
        }
88
      else
89
        {                       /* get new space */
90
          alloced = 1;          /* copy old entries into it */
91
          P = (char **) malloc ((size_t) (sizeof (char *) * (cnt + 2)));
92
          if (!P)
93
            {
94
              ENV_UNLOCK;
95
              return (-1);
96
            }
97
          bcopy ((char *) environ, (char *) P, cnt * sizeof (char *));
98
          environ = P;
99
        }
100
      environ[cnt + 1] = NULL;
101
      offset = cnt;
102
    }
103
  for (C = (char *) name; *C && *C != '='; ++C);        /* no `=' in name */
104
  if (!(environ[offset] =       /* name + `=' + value */
105
        malloc ((size_t) ((int) (C - name) + l_value + 2))))
106
    {
107
      ENV_UNLOCK;
108
      return -1;
109
    }
110
  for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
111
  for (*C++ = '='; *C++ = *value++;);
112
 
113
  ENV_UNLOCK;
114
 
115
  return 0;
116
}
117
 
118
/*
119
 * unsetenv_r(name) --
120
 *      Delete environmental variable "name".
121
 */
122
void
123
_DEFUN (unsetenv_r, (reent_ptr, name),
124
        struct _reent *reent_ptr _AND
125
        _CONST char *name)
126
{
127
  extern char **environ;
128
  register char **P;
129
  int offset;
130
 
131
  ENV_LOCK;
132
 
133
  while (_findenv_r (reent_ptr, name, &offset)) /* if set multiple times */
134
    for (P = &environ[offset];; ++P)
135
      if (!(*P = *(P + 1)))
136
        break;
137
 
138
  ENV_UNLOCK;
139
}

powered by: WebSVN 2.1.0

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