1 /*
2  * Copyright (c) 2003, 2005, 2014 Tama Communications Corporation
3  *
4  * This file is part of GNU GLOBAL.
5  *
6  * This program 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 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #ifdef STDC_HEADERS
24 #include <stdlib.h>
25 #endif
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #else
29 #include <strings.h>
30 #endif
31 #ifdef HAVE_HOME_ETC_H
32 #include <home_etc.h>
33 #endif
34 
35 #include "conf.h"
36 #include "die.h"
37 #include "env.h"
38 #include "gparam.h"
39 #include "strbuf.h"
40 
41 extern char **environ;
42 
43 /**
44  * set_env: put environment variable.
45  *
46  *	@param[in]	var	environment variable
47  *	@param[in]	val	value
48  *
49  * Machine independent version of setenv(3).
50  */
51 void
set_env(const char * var,const char * val)52 set_env(const char *var, const char *val)
53 {
54 /*
55  * sparc-sun-solaris2.6 doesn't have setenv(3).
56  */
57 #ifdef HAVE_PUTENV
58 	STRBUF *sb = strbuf_open(0);
59 
60 	strbuf_sprintf(sb, "%s=%s", var, val);
61 	putenv(strbuf_value(sb));
62 	/* Don't free memory. putenv(3) require it. */
63 #else
64 	setenv(var, val, 1);
65 #endif
66 }
67 /**
68  * get_home_directory: get environment dependent home directory.
69  *
70  *	@return	home directory
71  */
72 char *
get_home_directory(void)73 get_home_directory(void)
74 {
75 #ifdef HAVE_HOME_ETC_H
76 	return _HEdir;
77 #else
78 	return getenv("HOME");
79 #endif
80 }
81 
82 /**
83  * env_size: calculate the size of area used by environment.
84  */
85 int
env_size(void)86 env_size(void)
87 {
88 	char **e;
89 	int size = 0;
90 
91 	for (e = environ; *e != NULL; e++)
92 		size += strlen(*e) + 1;
93 
94 	return size;
95 }
96 /**
97  * setenv_from_config
98  */
99 static const char *envname[] = {
100 	"GREP_COLOR",
101 	"GREP_COLORS",
102 	"GTAGSBLANKENCODE",
103 	"GTAGSCACHE",
104 	/*"GTAGSCONF",*/
105 	/*"GTAGSDBPATH",*/
106 	"GTAGSFORCECPP",
107 	"GTAGSGLOBAL",
108 	"GTAGSGTAGS",
109 	/*"GTAGSLABEL",*/
110 	"GTAGSLIBPATH",
111 	"GTAGSLOGGING",
112 	/*"GTAGSROOT",*/
113 	"GTAGSOBJDIR",
114 	"GTAGSOBJDIRPREFIX",
115 	"GTAGSTHROUGH",
116 	"GTAGS_OPTIONS",
117 	"HTAGS_OPTIONS",
118 	"MAKEOBJDIR",
119 	"MAKEOBJDIRPREFIX",
120 	"TMPDIR",
121 };
122 void
setenv_from_config(void)123 setenv_from_config(void)
124 {
125 	int i, lim = sizeof(envname) / sizeof(char *);
126 	STRBUF *sb = strbuf_open(0);
127 
128 	for (i = 0; i < lim; i++) {
129 		if (getenv(envname[i]) == NULL) {
130 			strbuf_reset(sb);
131 			if (getconfs(envname[i], sb))
132 				set_env(envname[i], strbuf_value(sb));
133 			else if (getconfb(envname[i]))
134 				set_env(envname[i], "");
135 		}
136 	}
137 	/*
138 	 * For upper compatibility.
139 	 * htags_options is deprecated.
140 	 */
141 	if (getenv("HTAGS_OPTIONS") == NULL) {
142 		strbuf_reset(sb);
143 		if (getconfs("htags_options", sb))
144 			set_env("HTAGS_OPTIONS", strbuf_value(sb));
145 	}
146 	strbuf_close(sb);
147 }
148