1 /*
2 FUNCTION
3 <<_getenv_r>>---look up environment variable
4
5 INDEX
6 _getenv_r
7 INDEX
8 environ
9
10 SYNOPSIS
11 #include <stdlib.h>
12 char *_getenv_r(struct _reent *<[reent_ptr]>, const char *<[name]>);
13
14 DESCRIPTION
15 <<_getenv_r>> searches the list of environment variable names and values
16 (using the global pointer ``<<char **environ>>'') for a variable whose
17 name matches the string at <[name]>. If a variable name matches,
18 <<_getenv_r>> returns a pointer to the associated value.
19
20 RETURNS
21 A pointer to the (string) value of the environment variable, or
22 <<NULL>> if there is no such environment variable.
23
24 PORTABILITY
25 <<_getenv_r>> is not ANSI; the rules for properly forming names of environment
26 variables vary from one system to another. This implementation does not
27 permit '=' 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 * Neither the name of the University nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
52 */
53
54 #include <stdlib.h>
55 #include <stddef.h>
56 #include <string.h>
57 #include "envlock.h"
58
59 extern char **environ;
60
61 /* Only deal with a pointer to environ, to work around subtle bugs with shared
62 libraries and/or small data systems where the user declares his own
63 'environ'. */
64 static char ***p_environ = &environ;
65
66 /*
67 * _findenv --
68 * Returns pointer to value associated with name, if any, else NULL.
69 * Sets offset to be the offset of the name/value combination in the
70 * environmental array, for use by setenv(3) and unsetenv(3).
71 *
72 * This routine *should* be a static; don't use it.
73 */
74
75 char *
_findenv(register const char * name,int * offset)76 _findenv (
77 register const char *name,
78 int *offset)
79 {
80 register int len;
81 register char **p;
82 const char *c;
83
84 ENV_LOCK;
85
86 /* In some embedded systems, this does not get set. This protects
87 newlib from dereferencing a bad pointer. */
88 if (!*p_environ)
89 {
90 ENV_UNLOCK;
91 return NULL;
92 }
93
94 c = name;
95 while (*c && *c != '=') c++;
96
97 /* Identifiers may not contain an '=', so cannot match if does */
98 if(*c != '=')
99 {
100 len = c - name;
101 for (p = *p_environ; *p; ++p)
102 if (!strncmp (*p, name, len))
103 if (*(c = *p + len) == '=')
104 {
105 *offset = p - *p_environ;
106 ENV_UNLOCK;
107 return (char *) (++c);
108 }
109 }
110 ENV_UNLOCK;
111 return NULL;
112 }
113