diff options
Diffstat (limited to 'src/lib/libc/stdlib/getenv.c')
| -rw-r--r-- | src/lib/libc/stdlib/getenv.c | 74 |
1 files changed, 36 insertions, 38 deletions
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c index 09d47f2149..72367b34e2 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.8 2005/08/08 08:05:36 espie 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,27 +28,10 @@ | |||
| 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";*/ | ||
| 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 */ | ||
| 38 | |||
| 39 | #include <stdlib.h> | 31 | #include <stdlib.h> |
| 40 | #include <string.h> | 32 | #include <string.h> |
| 41 | 33 | ||
| 42 | /* | 34 | 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 | 35 | ||
| 56 | /* | 36 | /* |
| 57 | * __findenv -- | 37 | * __findenv -- |
| @@ -63,20 +43,38 @@ getenv(name) | |||
| 63 | * This routine *should* be a static; don't use it. | 43 | * This routine *should* be a static; don't use it. |
| 64 | */ | 44 | */ |
| 65 | char * | 45 | char * |
| 66 | __findenv(name, offset) | 46 | __findenv(const char *name, int *offset) |
| 67 | register char *name; | ||
| 68 | int *offset; | ||
| 69 | { | 47 | { |
| 70 | extern char **environ; | 48 | extern char **environ; |
| 71 | register int len; | 49 | int len, i; |
| 72 | register char **P, *C; | 50 | const char *np; |
| 51 | char **p, *cp; | ||
| 52 | |||
| 53 | if (name == NULL || environ == NULL) | ||
| 54 | return (NULL); | ||
| 55 | for (np = name; *np && *np != '='; ++np) | ||
| 56 | ; | ||
| 57 | len = np - name; | ||
| 58 | for (p = environ; (cp = *p) != NULL; ++p) { | ||
| 59 | for (np = name, i = len; i && *cp; i--) | ||
| 60 | if (*cp++ != *np++) | ||
| 61 | break; | ||
| 62 | if (i == 0 && *cp++ == '=') { | ||
| 63 | *offset = p - environ; | ||
| 64 | return (cp); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return (NULL); | ||
| 68 | } | ||
| 69 | |||
| 70 | /* | ||
| 71 | * getenv -- | ||
| 72 | * Returns ptr to value associated with name, if any, else NULL. | ||
| 73 | */ | ||
| 74 | char * | ||
| 75 | getenv(const char *name) | ||
| 76 | { | ||
| 77 | int offset; | ||
| 73 | 78 | ||
| 74 | for (C = name, len = 0; *C && *C != '='; ++C, ++len); | 79 | 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 | } | 80 | } |
