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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdlib/] [getenv_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
/*
2
FUNCTION
3
<<_getenv_r>>---look up environment variable
4
 
5
INDEX
6
        _getenv_r
7
INDEX
8
        environ
9
 
10
ANSI_SYNOPSIS
11
        #include <stdlib.h>
12
        char *_getenv_r(struct _reent *<[reent_ptr]>, const char *<[name]>);
13
 
14
TRAD_SYNOPSIS
15
        #include <stdlib.h>
16
        char *_getenv_r(<[reent_ptr]>, <[name]>)
17
        struct _reent *<[reent_ptr]>;
18
        char *<[name]>;
19
 
20
DESCRIPTION
21
<<_getenv_r>> searches the list of environment variable names and values
22
(using the global pointer ``<<char **environ>>'') for a variable whose
23
name matches the string at <[name]>.  If a variable name matches,
24
<<_getenv_r>> returns a pointer to the associated value.
25
 
26
RETURNS
27
A pointer to the (string) value of the environment variable, or
28
<<NULL>> if there is no such environment variable.
29
 
30
PORTABILITY
31
<<_getenv_r>> is not ANSI; the rules for properly forming names of environment
32
variables vary from one system to another.
33
 
34
<<_getenv_r>> requires a global pointer <<environ>>.
35
*/
36
 
37
/* This file may have been modified by DJ Delorie (Jan 1991).  If so,
38
** these modifications are Copyright (C) 1991 DJ Delorie
39
*/
40
 
41
/*
42
 * Copyright (c) 1987 Regents of the University of California.
43
 * All rights reserved.
44
 *
45
 * Redistribution and use in source and binary forms are permitted
46
 * provided that: (1) source distributions retain this entire copyright
47
 * notice and comment, and (2) distributions including binaries display
48
 * the following acknowledgement:  ``This product includes software
49
 * developed by the University of California, Berkeley and its contributors''
50
 * in the documentation or other materials provided with the distribution
51
 * and in all advertising materials mentioning features or use of this
52
 * software. Neither the name of the University nor the names of its
53
 * contributors may be used to endorse or promote products derived
54
 * from this software without specific prior written permission.
55
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
56
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
57
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
58
 */
59
 
60
#include <stdlib.h>
61
#include <stddef.h>
62
#include <string.h>
63
#include "envlock.h"
64
 
65
extern char **environ;
66
 
67
/* Only deal with a pointer to environ, to work around subtle bugs with shared
68
   libraries and/or small data systems where the user declares his own
69
   'environ'.  */
70
static char ***p_environ = &environ;
71
 
72
/*
73
 * _findenv --
74
 *      Returns pointer to value associated with name, if any, else NULL.
75
 *      Sets offset to be the offset of the name/value combination in the
76
 *      environmental array, for use by setenv(3) and unsetenv(3).
77
 *      Explicitly removes '=' in argument name.
78
 *
79
 *      This routine *should* be a static; don't use it.
80
 */
81
 
82
char *
83
_DEFUN (_findenv_r, (reent_ptr, name, offset),
84
        struct _reent *reent_ptr   _AND
85
        register _CONST char *name _AND
86
        int *offset)
87
{
88
  register int len;
89
  register char **p;
90
  _CONST char *c;
91
 
92
  ENV_LOCK;
93
 
94
  /* In some embedded systems, this does not get set.  This protects
95
     newlib from dereferencing a bad pointer.  */
96
  if (!*p_environ)
97
    return NULL;
98
 
99
  c = name;
100
  len = 0;
101
  while (*c && *c != '=')
102
    {
103
      c++;
104
      len++;
105
    }
106
 
107
  for (p = *p_environ; *p; ++p)
108
    if (!strncmp (*p, name, len))
109
      if (*(c = *p + len) == '=')
110
        {
111
          *offset = p - *p_environ;
112
          ENV_UNLOCK;
113
          return (char *) (++c);
114
        }
115
  ENV_UNLOCK;
116
  return NULL;
117
}
118
 
119
/*
120
 * _getenv_r --
121
 *      Returns ptr to value associated with name, if any, else NULL.
122
 */
123
 
124
char *
125
_DEFUN (_getenv_r, (reent_ptr, name),
126
        struct _reent *reent_ptr _AND
127
        _CONST char *name)
128
{
129
  int offset;
130
  char *_findenv_r ();
131
 
132
  return _findenv_r (reent_ptr, name, &offset);
133
}

powered by: WebSVN 2.1.0

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