Mail Archives: djgpp-workers/2001/06/12/15:33:35
I've written a simple implementation of unsetenv(), as that's
part of the new Posix standard.
That standard also specifies setenv(), though with slightly
different semantics (specifies errno=EINVAL if the name
contains a '='). Okay to update this function and its docs
to match the spec and move it from src/libc/compat/v1 to
src/libc/posix/stdlib?
** unsetenv.c
/* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
#include <errno.h>
#include <stdlib.h>
#include <string.h>
extern char **environ;
int
unsetenv(const char *name)
{
char *name_with_equal_sign = NULL;
/* No environment == success */
if (environ == 0)
return 0;
/* Check for the failure conditions */
if (name == NULL || *name == '\0' || strchr (name, '=') != NULL)
{
errno = EINVAL;
return -1;
}
/* Let putenv() do the work */
name_with_assign = malloc (strlen (name) + 2);
strcpy (name_with_assign, name);
strcat (name_with_assign, "=");
putenv (name_with_assign);
free (name_with_assign);
return 0;
}
** unsetenv.txh
@node unsetenv, environment
@subheading Syntax
@example
#include <stdlib.h>
int unsetenv(const char *name);
@end example
@subheading Description
This function removes the environment variable @var{name} from the
environment. This will update the list of pointers to which the
@var{environ} variable points.
If the specified variable does not exist in the environment, the
environment is not modified and this function is considered to have
been sucessfully completed.
@subheading Return Value
If @var{name} is @code{NULL}, points to an empty string, or points to
a string containing a @samp{=}, this function returns -1 and sets
@var{errno} to @code{EINVAL}; otherwise it returns 0.
@subheading Portability
@portability !ansi, posix
@port-note Posix 1003.1-200x, not POSIX-1:1996
- Raw text -