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