summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/getenv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/getenv.c')
-rw-r--r--src/lib/libc/stdlib/getenv.c77
1 files changed, 38 insertions, 39 deletions
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c
index 09d47f2149..fd8482e9e3 100644
--- a/src/lib/libc/stdlib/getenv.c
+++ b/src/lib/libc/stdlib/getenv.c
@@ -1,6 +1,7 @@
1/* $OpenBSD: getenv.c,v 1.10 2010/08/23 22:31:50 millert Exp $ */
1/* 2/*
2 * Copyright (c) 1987 Regents of the University of California. 3 * Copyright (c) 1987, 1993
3 * All rights reserved. 4 * The Regents of the University of California. All rights reserved.
4 * 5 *
5 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
@@ -10,11 +11,7 @@
10 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software 14 * 3. Neither the name of the University nor the names of its contributors
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software 15 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission. 16 * without specific prior written permission.
20 * 17 *
@@ -31,52 +28,54 @@
31 * SUCH DAMAGE. 28 * SUCH DAMAGE.
32 */ 29 */
33 30
34#if defined(LIBC_SCCS) && !defined(lint)
35/*static char *sccsid = "from: @(#)getenv.c 5.8 (Berkeley) 2/23/91";*/
36static char *rcsid = "$Id: getenv.c,v 1.1.1.1 1995/10/18 08:42:17 deraadt Exp $";
37#endif /* LIBC_SCCS and not lint */
38
39#include <stdlib.h> 31#include <stdlib.h>
40#include <string.h> 32#include <string.h>
41 33
42/* 34char *__findenv(const char *name, int len, int *offset);
43 * getenv --
44 * Returns ptr to value associated with name, if any, else NULL.
45 */
46char *
47getenv(name)
48 const char *name;
49{
50 int offset;
51 char *__findenv();
52
53 return(__findenv(name, &offset));
54}
55 35
56/* 36/*
57 * __findenv -- 37 * __findenv --
58 * Returns pointer to value associated with name, if any, else NULL. 38 * Returns pointer to value associated with name, if any, else NULL.
39 * Starts searching within the environmental array at offset.
59 * Sets offset to be the offset of the name/value combination in the 40 * Sets offset to be the offset of the name/value combination in the
60 * environmental array, for use by setenv(3) and unsetenv(3). 41 * environmental array, for use by putenv(3), setenv(3) and unsetenv(3).
61 * Explicitly removes '=' in argument name. 42 * Explicitly removes '=' in argument name.
62 * 43 *
63 * This routine *should* be a static; don't use it. 44 * This routine *should* be a static; don't use it.
64 */ 45 */
65char * 46char *
66__findenv(name, offset) 47__findenv(const char *name, int len, int *offset)
67 register char *name;
68 int *offset;
69{ 48{
70 extern char **environ; 49 extern char **environ;
71 register int len; 50 int i;
72 register char **P, *C; 51 const char *np;
52 char **p, *cp;
53
54 if (name == NULL || environ == NULL)
55 return (NULL);
56 for (p = environ + *offset; (cp = *p) != NULL; ++p) {
57 for (np = name, i = len; i && *cp; i--)
58 if (*cp++ != *np++)
59 break;
60 if (i == 0 && *cp++ == '=') {
61 *offset = p - environ;
62 return (cp);
63 }
64 }
65 return (NULL);
66}
67
68/*
69 * getenv --
70 * Returns ptr to value associated with name, if any, else NULL.
71 */
72char *
73getenv(const char *name)
74{
75 int offset = 0;
76 const char *np;
73 77
74 for (C = name, len = 0; *C && *C != '='; ++C, ++len); 78 for (np = name; *np && *np != '='; ++np)
75 for (P = environ; *P; ++P) 79 ;
76 if (!strncmp(*P, name, len)) 80 return (__findenv(name, (int)(np - name), &offset));
77 if (*(C = *P + len) == '=') {
78 *offset = P - environ;
79 return(++C);
80 }
81 return(NULL);
82} 81}