Ruin Keith's finely crafted ANSI C code to make it compile with PCC.
[unix-history] / usr / src / lib / libc / stdlib / getenv.c
CommitLineData
535b4307
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
019bea33 5 * %sccs.include.redist.c%
535b4307
KB
6 */
7
2ce81398 8#if defined(LIBC_SCCS) && !defined(lint)
019bea33 9static char sccsid[] = "@(#)getenv.c 5.7 (Berkeley) %G%";
535b4307 10#endif /* LIBC_SCCS and not lint */
b8f253e8 11
6a46fa84
KB
12#include <stdlib.h>
13#include <stddef.h>
834cc75f 14
24f434cb 15/*
535b4307 16 * getenv --
834cc75f 17 * Returns ptr to value associated with name, if any, else NULL.
24f434cb 18 */
24f434cb
BJ
19char *
20getenv(name)
834cc75f 21 char *name;
24f434cb 22{
535b4307
KB
23 int offset;
24 char *_findenv();
24f434cb 25
535b4307 26 return(_findenv(name, &offset));
24f434cb
BJ
27}
28
29/*
535b4307 30 * _findenv --
834cc75f
KB
31 * Returns pointer to value associated with name, if any, else NULL.
32 * Sets offset to be the offset of the name/value combination in the
33 * environmental array, for use by setenv(3) and unsetenv(3).
34 * Explicitly removes '=' in argument name.
35 *
36 * This routine *should* be a static; don't use it.
24f434cb 37 */
834cc75f 38char *
535b4307 39_findenv(name, offset)
834cc75f 40 register char *name;
535b4307 41 int *offset;
24f434cb 42{
535b4307
KB
43 extern char **environ;
44 register int len;
45 register char **P, *C;
24f434cb 46
535b4307
KB
47 for (C = name, len = 0; *C && *C != '='; ++C, ++len);
48 for (P = environ; *P; ++P)
49 if (!strncmp(*P, name, len))
834cc75f
KB
50 if (*(C = *P + len) == '=') {
51 *offset = P - environ;
52 return(++C);
53 }
24f434cb
BJ
54 return(NULL);
55}