shithub: riscv

ref: 699d2e0ed94bd545bb3dd8c64a3922f56f273f0e
dir: /sys/src/libc/9sys/putenv.c/

View raw version
#include <u.h>
#include <libc.h>

int
putenv(char *name, char *val)
{
	char ename[100];
	int f, n;

	if(name[0]=='\0' || strcmp(name, ".")==0 || strcmp(name, "..")==0 || strchr(name, '/')!=nil
	|| strlen(name) >= sizeof(ename)-5){
		werrstr("bad env name: %s", name);
		return -1;
	}
	snprint(ename, sizeof(ename), "/env/%s", name);
	f = create(ename, OWRITE|OCEXEC, 0664);
	if(f < 0)
		return -1;
	n = strlen(val);
	if(write(f, val, n) != n){
		close(f);
		return -1;
	}
	close(f);
	return 0;
}