xref: /plan9/sys/src/ape/lib/bsd/putenv.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include <sys/types.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <string.h>
5 
6 int
putenv(char * s)7 putenv(char *s)
8 {
9 	int f, n;
10 	char *value;
11 	char buf[300];
12 
13 	value = strchr(s, '=');
14 	if (value) {
15 		n = value-s;
16 		if(n<=0 || n > sizeof(buf)-6)
17 			return -1;
18 		strcpy(buf, "/env/");
19 		strncpy(buf+5, s, n);
20 		buf[n+5] = 0;
21 		f = creat(buf, 0666);
22 		if(f < 0)
23 			return 1;
24 		value++;
25 		n = strlen(value);
26 		if(write(f, value, n) != n)
27 			return -1;
28 		close(f);
29 		return 0;
30 	} else
31 		return -1;
32 }
33