Sync-to-go: update copyright for 2015
[s-roff.git] / src / lib-roff / putenv.c
blob38cfdccfee6b2dff901c70af0af660aa746022e2
1 /*@
2 * Copyright (c) 2014 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
4 * Copyright (C) 1991, 2001, 2006 Free Software Foundation, Inc.
5 * This file is part of the GNU C Library.
7 * The GNU C Library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * The GNU C Library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with the GNU C Library; see the file COPYING.LIB. If
19 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
20 * Cambridge, MA 02139, USA.
23 /* Hacked slightly by jjc@jclark.com for groff. */
25 #include "config.h"
27 #include <stdlib.h>
28 #include <string.h>
30 typedef void *PTR;
31 typedef size_t SIZE_T;
33 extern char **environ;
35 /* Put STRING, which is of the form `NAME=VALUE', in the environment. */
37 int putenv(const char *string)
39 char *name_end = strchr(string, '=');
40 SIZE_T size;
41 char **ep;
43 if (name_end == NULL)
45 /* Remove the variable from the environment. */
46 size = strlen(string);
47 for (ep = environ; *ep != NULL; ++ep)
48 if (!strncmp(*ep, string, size) && (*ep)[size] == '=')
50 while (ep[1] != NULL)
52 ep[0] = ep[1];
53 ++ep;
55 *ep = NULL;
56 return 0;
60 size = 0;
61 for (ep = environ; *ep != NULL; ++ep)
62 if (!strncmp(*ep, string, name_end - string)
63 && (*ep)[name_end - string] == '=')
64 break;
65 else
66 ++size;
68 if (*ep == NULL)
70 static char **last_environ = NULL;
71 char **new_environ = (char **) malloc((size + 2) * sizeof(char *));
72 if (new_environ == NULL)
73 return -1;
74 (void) memcpy((PTR) new_environ, (PTR) environ, size * sizeof(char *));
75 new_environ[size] = (char *) string;
76 new_environ[size + 1] = NULL;
77 if (last_environ != NULL)
78 free((PTR) last_environ);
79 last_environ = new_environ;
80 environ = new_environ;
82 else
83 *ep = (char *) string;
85 return 0;
88 // s-it2-mode