diff options
Diffstat (limited to 'src/lib/libc/stdlib/getenv.c')
| -rw-r--r-- | src/lib/libc/stdlib/getenv.c | 66 |
1 files changed, 36 insertions, 30 deletions
diff --git a/src/lib/libc/stdlib/getenv.c b/src/lib/libc/stdlib/getenv.c index 09d47f2149..934f10928f 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,26 +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.5 2002/12/10 22:44:12 mickey 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 | 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 | 42 | ||
| 56 | /* | 43 | /* |
| 57 | * __findenv -- | 44 | * __findenv -- |
| @@ -63,20 +50,39 @@ getenv(name) | |||
| 63 | * This routine *should* be a static; don't use it. | 50 | * This routine *should* be a static; don't use it. |
| 64 | */ | 51 | */ |
| 65 | char * | 52 | char * |
| 66 | __findenv(name, offset) | 53 | __findenv(const char *name, int *offset) |
| 67 | register char *name; | ||
| 68 | 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; | ||
| 59 | |||
| 60 | if (name == NULL || environ == NULL) | ||
| 61 | return (NULL); | ||
| 62 | for (np = name; *np && *np != '='; ++np) | ||
| 63 | ; | ||
| 64 | len = np - name; | ||
| 65 | for (p = environ; (cp = *p) != NULL; ++p) { | ||
| 66 | for (np = name, i = len; i && *cp; i--) | ||
| 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; | ||
| 73 | 86 | ||
| 74 | for (C = name, len = 0; *C && *C != '='; ++C, ++len); | 87 | 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 | } | 88 | } |
