www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/06/12/15:33:35

From: "Tim Van Holder" <tim DOT van DOT holder AT pandora DOT be>
To: "DJGPP-Workers" <djgpp-workers AT delorie DOT com>
Subject: (un)setenv()
Date: Tue, 12 Jun 2001 21:34:15 +0200
Message-ID: <CAEGKOHJKAAFPKOCLHDIOECICEAA.tim.van.holder@pandora.be>
MIME-Version: 1.0
X-Priority: 3 (Normal)
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2910.0)
Importance: Normal
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Reply-To: djgpp-workers AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019