-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompat.c
More file actions
127 lines (104 loc) · 2.7 KB
/
compat.c
File metadata and controls
127 lines (104 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* compat.c - emulate functions that aren't available on some Unices -
*
* Copyright (C) 2001 Staf Wagemakers Belgie/Belgium
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "compat.h"
#include "xmalloc.h"
#include <stdlib.h>
#ifdef HAVE_SYS_SYSTEMINFO_H
#include <sys/systeminfo.h>
#endif
#ifndef HAVE_UNSETENV
void unsetenv(char *env_name) {
extern char **environ;
char **cc;
int l;
l=strlen(env_name);
for (cc=environ;*cc!=NULL;cc++) {
if (strncmp(env_name,*cc,l)==0 && ((*cc)[l]=='='||(*cc)[l]=='\0')) break;
}
for (; *cc != NULL; cc++) *cc=cc[1];
}
#endif
#ifndef HAVE_SETENV
int setenv(const char *name, const char *value, int overwrite) {
extern char **environ;
char **cc;
char *env_str;
int env_size=0;
static int is_my_env=0;
int name_len=strlen(name);
/*
* create the string to insert in the environ table
*/
env_str=xmalloc(name_len+strlen(value)+2);
strcpy(env_str,name);
strcat(env_str,"=");
strcat(env_str,value);
/*
* parse the environ table and find evriron length
* if the env name exists replace it if overwrite is true and exit
*/
for(cc=environ;*cc;cc++) {
if(!strncmp(*cc,name,name_len)) {
if(overwrite)
*cc=env_str;
else
free(env_str);
return 0;
}
env_size++;
}
/*
* do a new malloc/memcpy if is_my_env if false
* else realloc
*/
if (is_my_env) {
environ=realloc(environ,sizeof(char*)*(env_size+2));
}
else {
cc=malloc(sizeof(char*)*(env_size+2));
memcpy(cc,environ,sizeof(char*)*env_size);
environ=cc;
is_my_env=1;
}
/*
* add env_str to the environ table
*/
environ[env_size]=env_str;
environ[env_size+1]=NULL;
return 0;
}
#endif
#ifndef HAVE_GETDOMAINNAME
char *getdomainname() {
char *domain=(char *) xmalloc(100);
sysinfo(SI_SRPC_DOMAIN,domain,100);
domain=(char *)xrealloc(domain,strlen(domain)+1);
return(domain);
}
#endif
#ifndef HAVE_GETHOSTNAME
char *gethostname() {
char hostname=(char *) xmalloc(100);
sysinfo(SI_HOSTNAME,hostname,100);
hostname=(char *)xrealloc(hostname,strlen(hostname)+1);
return(hostname);
}
#endif