diff options
Diffstat (limited to 'src/lib/libc/stdlib/getenv.c')
| -rw-r--r-- | src/lib/libc/stdlib/getenv.c | 65 |
1 files changed, 36 insertions, 29 deletions
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c index 09d47f2149..4db86df915 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 |
| @@ -32,28 +32,13 @@ | |||
| 32 | */ | 32 | */ |
| 33 | 33 | ||
| 34 | #if defined(LIBC_SCCS) && !defined(lint) | 34 | #if defined(LIBC_SCCS) && !defined(lint) |
| 35 | /*static char *sccsid = "from: @(#)getenv.c 5.8 (Berkeley) 2/23/91";*/ | 35 | static char *rcsid = "$OpenBSD: getenv.c,v 1.4 1998/07/16 18:02:33 deraadt 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 */ | 36 | #endif /* LIBC_SCCS and not lint */ |
| 38 | 37 | ||
| 39 | #include <stdlib.h> | 38 | #include <stdlib.h> |
| 40 | #include <string.h> | 39 | #include <string.h> |
| 41 | 40 | ||
| 42 | /* | 41 | /* |
| 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 | |||
| 56 | /* | ||
| 57 | * __findenv -- | 42 | * __findenv -- |
| 58 | * Returns pointer to value associated with name, if any, else NULL. | 43 | * Returns pointer to value associated with name, if any, else NULL. |
| 59 | * Sets offset to be the offset of the name/value combination in the | 44 | * Sets offset to be the offset of the name/value combination in the |
| @@ -64,19 +49,41 @@ getenv(name) | |||
| 64 | */ | 49 | */ |
| 65 | char * | 50 | char * |
| 66 | __findenv(name, offset) | 51 | __findenv(name, offset) |
| 67 | register char *name; | 52 | register const char *name; |
| 68 | int *offset; | 53 | int *offset; |
| 69 | { | 54 | { |
| 70 | extern char **environ; | 55 | extern char **environ; |
| 71 | register int len; | 56 | register int len, i; |
| 72 | register char **P, *C; | 57 | register const char *np; |
| 58 | register char **p, *cp; | ||
| 73 | 59 | ||
| 74 | for (C = name, len = 0; *C && *C != '='; ++C, ++len); | 60 | if (name == NULL || environ == NULL) |
| 75 | for (P = environ; *P; ++P) | 61 | return (NULL); |
| 76 | if (!strncmp(*P, name, len)) | 62 | for (np = name; *np && *np != '='; ++np) |
| 77 | if (*(C = *P + len) == '=') { | 63 | ; |
| 78 | *offset = P - environ; | 64 | len = np - name; |
| 79 | return(++C); | 65 | for (p = environ; (cp = *p) != NULL; ++p) { |
| 80 | } | 66 | for (np = name, i = len; i && *cp; i--) |
| 81 | return(NULL); | 67 | if (*cp++ != *np++) |
| 68 | break; | ||
| 69 | if (i == 0 && *cp++ == '=') { | ||
| 70 | *offset = p - environ; | ||
| 71 | return (cp); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | return (NULL); | ||
| 75 | } | ||
| 76 | |||
| 77 | /* | ||
| 78 | * getenv -- | ||
| 79 | * Returns ptr to value associated with name, if any, else NULL. | ||
| 80 | */ | ||
| 81 | char * | ||
| 82 | getenv(name) | ||
| 83 | const char *name; | ||
| 84 | { | ||
| 85 | int offset; | ||
| 86 | char *__findenv(); | ||
| 87 | |||
| 88 | return(__findenv(name, &offset)); | ||
| 82 | } | 89 | } |
