source: trunk/libs/newlib/src/newlib/libc/stdlib/getenv_r.c @ 577

Last change on this file since 577 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

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