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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib/] [newlib/] [libc/] [stdlib/] [setenv.c] - Blame information for rev 39

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

Line No. Rev Author Line
1 39 lampret
/* 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 <stddef.h>
26
#include <stdlib.h>
27
#include <string.h>
28
 
29
/* _findenv is defined in getenv.c.  */
30
 
31
extern char *_findenv _PARAMS ((const char *, int *));
32
 
33
/*
34
 * setenv --
35
 *      Set the value of the environmental variable "name" to be
36
 *      "value".  If rewrite is set, replace any current value.
37
 */
38
 
39
int
40
_DEFUN (setenv, (name, value, rewrite),
41
        _CONST char *name _AND
42
        _CONST char *value _AND
43
        int rewrite)
44
{
45
  extern char **environ;
46
  static int alloced;           /* if allocated space before */
47
  register char *C;
48
  int l_value, offset;
49
 
50
  if (*value == '=')            /* no `=' in value */
51
    ++value;
52
  l_value = strlen (value);
53
  if ((C = _findenv (name, &offset)))
54
    {                           /* find if already exists */
55
      if (!rewrite)
56
        return 0;
57
      if (strlen (C) >= l_value)
58
        {                       /* old larger; copy over */
59
          while (*C++ = *value++);
60
          return 0;
61
        }
62
    }
63
  else
64
    {                           /* create new slot */
65
      register int cnt;
66
      register char **P;
67
 
68
      for (P = environ, cnt = 0; *P; ++P, ++cnt);
69
      if (alloced)
70
        {                       /* just increase size */
71
          environ = (char **) realloc ((char *) environ,
72
                                       (size_t) (sizeof (char *) * (cnt + 2)));
73
          if (!environ)
74
            return -1;
75
        }
76
      else
77
        {                       /* get new space */
78
          alloced = 1;          /* copy old entries into it */
79
          P = (char **) malloc ((size_t) (sizeof (char *) * (cnt + 2)));
80
          if (!P)
81
            return (-1);
82
          bcopy ((char *) environ, (char *) P, cnt * sizeof (char *));
83
          environ = P;
84
        }
85
      environ[cnt + 1] = NULL;
86
      offset = cnt;
87
    }
88
  for (C = (char *) name; *C && *C != '='; ++C);        /* no `=' in name */
89
  if (!(environ[offset] =       /* name + `=' + value */
90
        malloc ((size_t) ((int) (C - name) + l_value + 2))))
91
    return -1;
92
  for (C = environ[offset]; (*C = *name++) && *C != '='; ++C);
93
  for (*C++ = '='; *C++ = *value++;);
94
  return 0;
95
}
96
 
97
/*
98
 * unsetenv(name) --
99
 *      Delete environmental variable "name".
100
 */
101
void
102
_DEFUN (unsetenv, (name),
103
        _CONST char *name)
104
{
105
  extern char **environ;
106
  register char **P;
107
  int offset;
108
 
109
  while (_findenv (name, &offset))      /* if set multiple times */
110
    for (P = &environ[offset];; ++P)
111
      if (!(*P = *(P + 1)))
112
        break;
113
}

powered by: WebSVN 2.1.0

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