[svn-inject] Applying Debian modifications to trunk
[manu/suphp.git] / src / compat.c
1 /*
2     suPHP - (c)2002-2004 Sebastian Marsching <sebastian@marsching.com>
3     
4     This file is part of suPHP.
5
6     suPHP is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     suPHP is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with suPHP; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "suphp.h"
25
26 int suphp_setenv(const char *name, const char *value, int overwrite)
27 {
28  char *putstr = NULL;
29  char *temp = NULL;
30  int retval = 0;
31  if (!overwrite && getenv(name) != NULL)
32   return 0;
33  temp = NULL;
34  putstr = malloc(strlen(name) + strlen(value) + 2);
35  if (putstr == NULL)
36   error_sysmsg_exit(ERRCODE_MEMORY, "Could not allocate memory", __FILE__, __LINE__);
37  strcpy(putstr, name);
38  temp = putstr + strlen(name);
39  *temp = '=';
40  temp++;
41  strcpy(temp, value);
42  retval = putenv(putstr);
43  return retval;
44 }
45
46 int suphp_unsetenv(const char *name)
47 {
48  char *putstr = malloc(strlen(name) + 2);
49  char *temp = NULL;
50  int retval = 0;
51  if (putstr == NULL)
52   error_sysmsg_exit(ERRCODE_MEMORY, "Could not allocate memory", __FILE__, __LINE__);
53  strcpy(putstr, name);
54  temp = putstr + strlen(name);
55  *temp = '=';
56  temp++;
57  *temp = 0;
58  retval = putenv(putstr);
59  return retval;
60 }
61
62 char** suphp_copyenv(const char **old_env)
63 {
64  char** new_env = NULL;
65  char** element = NULL;
66  char** new_element = NULL;
67  char* firsteqchr = NULL;
68  int num_elements = 0;
69  
70  element = (char **) old_env;
71  while (*element != NULL)
72  {
73   firsteqchr = strchr(*element, '='); 
74   if (*(firsteqchr + 1) != 0)
75   {
76    num_elements++;
77   }
78   element++;
79  }
80
81  new_env = (char**) malloc(sizeof(char*) * (num_elements + 1));
82  if (new_env == NULL)
83  {
84   error_sysmsg_exit(ERRCODE_MEMORY, "Could not allocate memory", __FILE__, __LINE__);
85  }
86  new_element = new_env;
87  element = (char **) old_env;
88  while (*element != NULL)
89  {
90   firsteqchr = strchr(*element, '=');
91   if (*(firsteqchr + 1) != 0)
92   {
93    *new_element = malloc(strlen(*element) + 1);
94    if (*new_element == NULL)
95    {
96     error_sysmsg_exit(ERRCODE_MEMORY, "Could not allocate memory", __FILE__, __LINE__);
97    }
98    strcpy(*new_element, *element);
99    new_element++;
100   }
101   element++;
102  }
103  *new_element = NULL;
104  return new_env;
105 }