ソースを参照

changed environment handling to decrease namespace pollution

eck 34 年 前
コミット
e7dc9ed7f6

+ 1 - 0
lang/cem/libcc.ansi/misc/.distr

@@ -11,6 +11,7 @@ getw.c
 opendir.c
 popen.c
 putenv.c
+environ.c
 putw.c
 readdir.c
 rewinddir.c

+ 3 - 2
lang/cem/libcc.ansi/misc/Makefile

@@ -1,4 +1,5 @@
 clean:
 	rm -f getgrent.o getopt.o getpass.o getpw.o getw.o putw.o putenv.o \
-		popen.o sleep.o termcap.o fdopen.o closedir.o getdents.o \
-		opendir.o readdir.o rewinddir.o seekdir.o telldir.o OLIST
+		environ.o popen.o sleep.o termcap.o fdopen.o closedir.o \
+		getdents.o opendir.o readdir.o rewinddir.o seekdir.o \
+		telldir.o OLIST

+ 20 - 0
lang/cem/libcc.ansi/misc/environ.c

@@ -0,0 +1,20 @@
+/*
+ * environ.c - define the variable environ
+ */
+/* $Header$ */
+/*
+ * This file defines the variable environ and initializes it with a magic
+ * value.  The C run-time start-off routine tests whether the variable
+ * environ is initialized with this value.  If it is not, it is assumed
+ * that it is defined by the user.  Only two bytes are tested, since we
+ * don't know the endian-ness and alignment restrictions of the machine.
+ * This means that the low-order two-bytes should be equal to the
+ * high-order two-bytes on machines with four-byte pointers.  In fact, all
+ * the bytes in the pointer are the same, just in case.
+ */
+
+#if _EM_PSIZE==2
+char **environ = (char **) 0x5353;
+#else
+char **environ = (char **) 0x53535353;
+#endif

+ 11 - 9
lang/cem/libcc.ansi/misc/putenv.c

@@ -10,17 +10,18 @@
 #define	ENTRY_INC	10
 #define	rounded(x)	(((x / ENTRY_INC) + 1) * ENTRY_INC)
 
-extern const char **environ;
+extern const char **_envp;
+extern const char **environ;	/* environ is a shadow name for _envp */
 
 int
 putenv(char *name)
 {
-	register const char **v = environ;
+	register const char **v = _envp;
 	register char *r;
 	static int size = 0;
 	/* When size != 0, it contains the number of entries in the
 	 * table (including the final NULL pointer). This means that the
-	 * last non-null entry  is environ[size - 2].
+	 * last non-null entry  is _envp[size - 2].
 	 */
 
 	if (!name) return 0;
@@ -47,7 +48,7 @@ putenv(char *name)
 			}
 		}
 		*r = '=';
-		v = environ;
+		v = _envp;
 	}
 
 	if (!size) {
@@ -61,17 +62,18 @@ putenv(char *name)
 		if (!(v = malloc(rounded(i) * sizeof(char **))))
 			return 1;
 		size = i;
-		p = environ;
-		environ = v;
+		p = _envp;
+		_envp = v;
 		while (*v++ = *p++);		/* copy the environment */
-		v = environ;
+		v = _envp;
 	} else if (!(size % ENTRY_INC)) {
-		if (!(v = realloc(environ, rounded(size) * sizeof(char **))))
+		if (!(v = realloc(_envp, rounded(size) * sizeof(char **))))
 			return 1;
-		environ = v;
+		_envp = v;
 	}
 	v[size - 1] = name;
 	v[size] = NULL;
 	size++;
+	environ = _envp;
 	return 0;
 }