BSD 4_3_Tahoe release
[unix-history] / usr / src / lib / libc / gen / getenv.c
index 0e0689c..e71c31e 100644 (file)
@@ -1,41 +1,64 @@
-/* @(#)getenv.c        4.1 (Berkeley) 12/21/80 */
 /*
 /*
- *     getenv(name)
- *     returns ptr to value associated with name, if any, else NULL
+ * Copyright (c) 1987 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
  */
-#define NULL   0
-extern char **environ;
-char   *nvmatch();
 
 
+#if defined(LIBC_SCCS) && !defined(lint)
+static char sccsid[] = "@(#)getenv.c   5.5 (Berkeley) 6/27/88";
+#endif /* LIBC_SCCS and not lint */
+
+#include <stdio.h>
+
+/*
+ * getenv --
+ *     Returns ptr to value associated with name, if any, else NULL.
+ */
 char *
 getenv(name)
 char *
 getenv(name)
-register char *name;
+       char *name;
 {
 {
-       register char **p = environ;
-       register char *v;
+       int offset;
+       char *_findenv();
 
 
-       while (*p != NULL)
-               if ((v = nvmatch(name, *p++)) != NULL)
-                       return(v);
-       return(NULL);
+       return(_findenv(name, &offset));
 }
 
 /*
 }
 
 /*
- *     s1 is either name, or name=value
- *     s2 is name=value
- *     if names match, return value of s2, else NULL
- *     used for environment searching: see getenv
+ * _findenv --
+ *     Returns pointer to value associated with name, if any, else NULL.
+ *     Sets offset to be the offset of the name/value combination in the
+ *     environmental array, for use by setenv(3) and unsetenv(3).
+ *     Explicitly removes '=' in argument name.
+ *
+ *     This routine *should* be a static; don't use it.
  */
  */
-
-static char *
-nvmatch(s1, s2)
-register char *s1, *s2;
+char *
+_findenv(name, offset)
+       register char *name;
+       int *offset;
 {
 {
+       extern char **environ;
+       register int len;
+       register char **P, *C;
 
 
-       while (*s1 == *s2++)
-               if (*s1++ == '=')
-                       return(s2);
-       if (*s1 == '\0' && *(s2-1) == '=')
-               return(s2);
+       for (C = name, len = 0; *C && *C != '='; ++C, ++len);
+       for (P = environ; *P; ++P)
+               if (!strncmp(*P, name, len))
+                       if (*(C = *P + len) == '=') {
+                               *offset = P - environ;
+                               return(++C);
+                       }
        return(NULL);
 }
        return(NULL);
 }