shithub: riscv

Download patch

ref: 84c930a07894e30804469c32d84377b15868bfce
parent: eb9de925c63990f6b19494698e4db1eb9682e46d
author: cinap_lenrek <[email protected]>
date: Mon Dec 3 01:44:30 EST 2012

ape: fix putenv()

writing /env in putenv() doesnt work. exec will create
new enviroment anyway. we have to modify environ array!

--- a/sys/include/ape/bsd.h
+++ b/sys/include/ape/bsd.h
@@ -36,7 +36,6 @@
 extern int	rcmd(char**, int, char*, char*, char*, int*);
 extern char*	strdup(char*);
 extern int	strcasecmp(char*, char*);
-extern int 	putenv(char*);
 extern int	strncasecmp(char*, char*,int);
 extern void* memccpy(void*, void*, int, size_t);
 
--- a/sys/include/ape/stdlib.h
+++ b/sys/include/ape/stdlib.h
@@ -35,6 +35,7 @@
 extern int atexit(void (*func)(void));
 extern void exit(int);
 extern char *getenv(const char *);
+extern int putenv(char *);
 extern int system(const char *);
 extern void *bsearch(const void *, const void *, size_t, size_t, int (*)(const void *, const void *));
 extern void qsort(void *, size_t, size_t, int (*)(const void *, const void *));
--- a/sys/src/ape/lib/ap/gen/mkfile
+++ b/sys/src/ape/lib/ap/gen/mkfile
@@ -17,6 +17,7 @@
 	difftime.$O\
 	div.$O\
 	getenv.$O\
+	putenv.$O\
 	isalnum.$O\
 	itoa.$O\
 	itol.$O\
--- a/sys/src/ape/lib/ap/plan9/_envsetup.c
+++ b/sys/src/ape/lib/ap/plan9/_envsetup.c
@@ -45,11 +45,8 @@
 	fdinited = 0;
 	cnt = 0;
 	dfd = _OPEN("/env", 0);
-	if(dfd < 0) {
-		static char **emptyenvp = 0;
-		environ = emptyenvp;
-		return;
-	}
+	if(dfd < 0)
+		goto done;
 	psize = Envhunk;
 	ps = p = malloc(psize);
 	nd = _dirreadall(dfd, &d9a);
@@ -92,6 +89,7 @@
 	free(d9a);
 	if(!fdinited)
 		_fdinit(0, 0);
+done:
 	environ = pp = malloc((1+cnt)*sizeof(char *));
 	p = ps;
 	for(i = 0; i < cnt; i++) {
--- a/sys/src/ape/lib/bsd/mkfile
+++ b/sys/src/ape/lib/bsd/mkfile
@@ -29,7 +29,6 @@
 	ntohl.$O\
 	nptohl.$O\
 	popen.$O\
-	putenv.$O\
 	rcmd.$O\
 	readv.$O\
 	rresvport.$O\
--- a/sys/src/ape/lib/bsd/putenv.c
+++ /dev/null
@@ -1,32 +1,0 @@
-#include <sys/types.h>
-#include <unistd.h>
-#include <fcntl.h>
-#include <string.h>
-
-int
-putenv(char *s)
-{
-	int f, n;
-	char *value;
-	char buf[300];
-
-	value = strchr(s, '=');
-	if (value) {
-		n = value-s;
-		if(n<=0 || n > sizeof(buf)-6)
-			return -1;
-		strcpy(buf, "/env/");
-		strncpy(buf+5, s, n);
-		buf[n+5] = 0;
-		f = creat(buf, 0666);
-		if(f < 0)
-			return 1;
-		value++;
-		n = strlen(value);
-		if(write(f, value, n) != n)
-			return -1;
-		close(f);
-		return 0;
-	} else
-		return -1;
-}