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 | * 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 | |
---|
60 | extern 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'. */ |
---|
65 | static 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 | |
---|
76 | char * |
---|
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 | |
---|
120 | char * |
---|
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 | } |
---|