From 8c390406d7d76810a12a2374f1acf7953cb88f09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 3 Nov 2004 15:00:08 +0000 Subject: [PATCH] Added missing putenv() implementation. Chosed to limit env names to an arbitrary length of 64 bytes. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@9768 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/stdlib/env.c | 45 +++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/kernel/libroot/posix/stdlib/env.c b/src/kernel/libroot/posix/stdlib/env.c index f206b36beb..8fd6501496 100644 --- a/src/kernel/libroot/posix/stdlib/env.c +++ b/src/kernel/libroot/posix/stdlib/env.c @@ -1,21 +1,41 @@ /* +** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the Haiku License. +** ** Copyright 2001, Travis Geiselbrecht. All rights reserved. ** Distributed under the terms of the NewOS License. */ + + #include + #include +#include +#include + + +#define RETURN_AND_SET_ERRNO(err) \ + if (err < 0) { \ + errno = err; \ + return -1; \ + } \ + return err; char **environ = NULL; -int setenv(const char *name, const char *value, int overwrite) +int +setenv(const char *name, const char *value, int overwrite) { - return sys_setenv(name, value, overwrite); + status_t status = sys_setenv(name, value, overwrite); + + RETURN_AND_SET_ERRNO(status); } -char *getenv(const char *name) +char * +getenv(const char *name) { char *value; int rc; @@ -25,3 +45,22 @@ char *getenv(const char *name) return NULL; return value; } + + +int +putenv(const char *string) +{ + char name[64]; + char *value = strchr(string, '='); + + if (value == NULL || value - string >= (int)sizeof(name)) { + errno = EINVAL; + return -1; + } + + strlcpy(name, string, value - string); + value++; + + return setenv(name, value, true); +} +