1104862Sru/* Copyright (C) 1991, 2001 Free Software Foundation, Inc.
275584SruThis file is part of the GNU C Library.
375584Sru
475584SruThe GNU C Library is free software; you can redistribute it and/or
575584Srumodify it under the terms of the GNU Library General Public License as
675584Srupublished by the Free Software Foundation; either version 2 of the
775584SruLicense, or (at your option) any later version.
875584Sru
975584SruThe GNU C Library is distributed in the hope that it will be useful,
1075584Srubut WITHOUT ANY WARRANTY; without even the implied warranty of
1175584SruMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1275584SruLibrary General Public License for more details.
1375584Sru
1475584SruYou should have received a copy of the GNU Library General Public
1575584SruLicense along with the GNU C Library; see the file COPYING.LIB.  If
1675584Srunot, write to the Free Software Foundation, Inc., 675 Mass Ave,
1775584SruCambridge, MA 02139, USA.  */
1875584Sru
1975584Sru/* Hacked slightly by jjc@jclark.com for groff. */
2075584Sru
21104862Sru#ifdef HAVE_CONFIG_H
22104862Sru#include <config.h>
23104862Sru#endif
24104862Sru
2575584Sru#include <string.h>
2675584Sru
2775584Sru#ifdef __STDC__
2875584Sru#include <stddef.h>
2975584Srutypedef void *PTR;
3075584Srutypedef size_t SIZE_T;
3175584Sru#else /* not __STDC__ */
3275584Srutypedef char *PTR;
3375584Srutypedef int SIZE_T;
3475584Sru#endif /* not __STDC__ */
3575584Sru
3675584Sru#ifdef HAVE_STDLIB_H
3775584Sru#include <stdlib.h>
3875584Sru#else /* not HAVE_STDLIB_H */
3975584SruPTR malloc();
4075584Sru#endif /* not HAVE_STDLIB_H */
4175584Sru
4275584Sru#ifndef NULL
4375584Sru#define NULL 0
4475584Sru#endif
4575584Sru
4675584Sruextern char **environ;
4775584Sru
4875584Sru/* Put STRING, which is of the form "NAME=VALUE", in the environment.  */
4975584Sru
5075584Sruint putenv(const char *string)
5175584Sru{
5275584Sru  char *name_end = strchr(string, '=');
5375584Sru  SIZE_T size;
5475584Sru  char **ep;
5575584Sru
5675584Sru  if (name_end == NULL)
5775584Sru    {
5875584Sru      /* Remove the variable from the environment.  */
5975584Sru      size = strlen(string);
6075584Sru      for (ep = environ; *ep != NULL; ++ep)
6175584Sru	if (!strncmp(*ep, string, size) && (*ep)[size] == '=')
6275584Sru	  {
6375584Sru	    while (ep[1] != NULL)
6475584Sru	      {
6575584Sru		ep[0] = ep[1];
6675584Sru		++ep;
6775584Sru	      }
6875584Sru	    *ep = NULL;
6975584Sru	    return 0;
7075584Sru	  }
7175584Sru    }
7275584Sru
7375584Sru  size = 0;
7475584Sru  for (ep = environ; *ep != NULL; ++ep)
7575584Sru    if (!strncmp(*ep, string, name_end - string)
7675584Sru	&& (*ep)[name_end - string] == '=')
7775584Sru      break;
7875584Sru    else
7975584Sru      ++size;
8075584Sru
8175584Sru  if (*ep == NULL)
8275584Sru    {
8375584Sru      static char **last_environ = NULL;
8475584Sru      char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
8575584Sru      if (new_environ == NULL)
8675584Sru	return -1;
8775584Sru      (void) memcpy((PTR) new_environ, (PTR) environ, size * sizeof(char *));
8875584Sru      new_environ[size] = (char *) string;
8975584Sru      new_environ[size + 1] = NULL;
9075584Sru      if (last_environ != NULL)
9175584Sru	free((PTR) last_environ);
9275584Sru      last_environ = new_environ;
9375584Sru      environ = new_environ;
9475584Sru    }
9575584Sru  else
9675584Sru    *ep = (char *) string;
9775584Sru
9875584Sru  return 0;
9975584Sru}
100