Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdlib / getenv_r.c
blob6e51800ec3af33f66fc13402a3448b46d1f6e568
1 /*
2 FUNCTION
3 <<_getenv_r>>---look up environment variable
5 INDEX
6 _getenv_r
7 INDEX
8 environ
10 SYNOPSIS
11 #include <stdlib.h>
12 char *_getenv_r(struct _reent *<[reent_ptr]>, const char *<[name]>);
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.
20 RETURNS
21 A pointer to the (string) value of the environment variable, or
22 <<NULL>> if there is no such environment variable.
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.
29 <<_getenv_r>> requires a global pointer <<environ>>.
32 /* This file may have been modified by DJ Delorie (Jan 1991). If so,
33 ** these modifications are Copyright (C) 1991 DJ Delorie.
37 * Copyright (c) 1987 Regents of the University of California.
38 * All rights reserved.
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.
54 #include <stdlib.h>
55 #include <stddef.h>
56 #include <string.h>
57 #include "envlock.h"
59 extern char **environ;
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;
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).
72 * This routine *should* be a static; don't use it.
75 char *
76 _findenv_r (struct _reent *reent_ptr,
77 register const char *name,
78 int *offset)
80 register int len;
81 register char **p;
82 const char *c;
84 ENV_LOCK;
86 /* In some embedded systems, this does not get set. This protects
87 newlib from dereferencing a bad pointer. */
88 if (!*p_environ)
90 ENV_UNLOCK;
91 return NULL;
94 c = name;
95 while (*c && *c != '=') c++;
97 /* Identifiers may not contain an '=', so cannot match if does */
98 if(*c != '=')
100 len = c - name;
101 for (p = *p_environ; *p; ++p)
102 if (!strncmp (*p, name, len))
103 if (*(c = *p + len) == '=')
105 *offset = p - *p_environ;
106 ENV_UNLOCK;
107 return (char *) (++c);
110 ENV_UNLOCK;
111 return NULL;
115 * _getenv_r --
116 * Returns ptr to value associated with name, if any, else NULL.
119 char *
120 _getenv_r (struct _reent *reent_ptr,
121 const char *name)
123 int offset;
125 return _findenv_r (reent_ptr, name, &offset);