/[baselayout]/trunk/src/env-update.c
Gentoo

Diff of /trunk/src/env-update.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 2576 Revision 2577
3 Create /etc/profile.env (sh), /etc/csh.env from /etc/env.d 3 Create /etc/profile.env (sh), /etc/csh.env from /etc/env.d
4 Run ldconfig as required 4 Run ldconfig as required
5 5
6 Copyright 2007 Gentoo Foundation 6 Copyright 2007 Gentoo Foundation
7 Released under the GPLv2 7 Released under the GPLv2
8 */ 8
9*/
9 10
10#include <errno.h> 11#include <errno.h>
11#include <limits.h> 12#include <limits.h>
12#include <stdbool.h> 13#include <stdbool.h>
13#include <stdio.h> 14#include <stdio.h>
24#define PROFILE_ENV "/etc/profile.env" 25#define PROFILE_ENV "/etc/profile.env"
25#define CSH_ENV "/etc/csh.env" 26#define CSH_ENV "/etc/csh.env"
26#define LDSOCONF "/etc/ld.so.conf" 27#define LDSOCONF "/etc/ld.so.conf"
27 28
28#define NOTICE "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n" \ 29#define NOTICE "# THIS FILE IS AUTOMATICALLY GENERATED BY env-update.\n" \
29 "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n" \ 30 "# DO NOT EDIT THIS FILE. CHANGES TO STARTUP PROFILES\n" \
30"# GO INTO %s NOT %s\n\n" 31 "# GO INTO %s NOT %s\n\n"
31 32
32#define LDNOTICE "# ld.so.conf autogenerated by env-update; make all\n" \ 33#define LDNOTICE "# ld.so.conf autogenerated by env-update; make all\n" \
33 "# changes to contents of /etc/env.d directory\n" 34 "# changes to contents of /etc/env.d directory\n"
34 35
35static const char *specials[] = 36static const char *specials[] = {
36{
37 "ADA_INCLUDE_PATH", 37 "ADA_INCLUDE_PATH",
38 "ADA_OBJECTS_PATH", 38 "ADA_OBJECTS_PATH",
39 "CLASSPATH", 39 "CLASSPATH",
40 "INFOPATH", 40 "INFOPATH",
41 "KDEDIRS", 41 "KDEDIRS",
42 "LDPATH", 42 "LDPATH",
43 "MANPATH", 43 "MANPATH",
44 "PATH", 44 "PATH",
45 "PKG_CONFIG_PATH", 45 "PKG_CONFIG_PATH",
46 "PRELINK_PATH", 46 "PRELINK_PATH",
47 "PRELINK_PATH_MASK", 47 "PRELINK_PATH_MASK",
48 "PYTHONPATH", 48 "PYTHONPATH",
49 "ROOTPATH", 49 "ROOTPATH",
50 NULL 50 NULL
51}; 51};
52 52
53static const char *special_spaces[] = 53static const char *special_spaces[] = {
54{
55 "CONFIG_PROTECT", 54 "CONFIG_PROTECT",
56 "CONFIG_PROTECT_MASK", 55 "CONFIG_PROTECT_MASK",
57 NULL, 56 NULL,
58}; 57};
59 58
60static char *applet = NULL; 59static char *applet = NULL;
61 60
62int main (int argc, char **argv) 61int main (int argc, char **argv)
63{ 62{
64 char **files = rc_ls_dir (NULL, ENVDIR, 0); 63 char **files = rc_ls_dir (NULL, ENVDIR, 0);
65 char *file; 64 char *file;
66 char **envs = NULL; 65 char **envs = NULL;
67 char *env; 66 char *env;
68 int i = 0; 67 int i = 0;
69 FILE *fp; 68 FILE *fp;
70 bool ld = true; 69 bool ld = true;
71 char *ldent; 70 char *ldent;
72 char **ldents = NULL; 71 char **ldents = NULL;
73 int nents = 0; 72 int nents = 0;
74 73
75 applet = argv[0]; 74 applet = argv[0];
76 75
77 if (! files) 76 if (! files)
78 eerrorx ("%s: no files in " ENVDIR " to process", applet); 77 eerrorx ("%s: no files in " ENVDIR " to process", applet);
79 78
80 STRLIST_FOREACH (files, file, i) 79 STRLIST_FOREACH (files, file, i) {
81 {
82 char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL); 80 char *path = rc_strcatpaths (ENVDIR, file, (char *) NULL);
83 char **entries = NULL; 81 char **entries = NULL;
84 char *entry; 82 char *entry;
85 int j; 83 int j;
86 84
87 if (! rc_is_dir (path)) 85 if (! rc_is_dir (path))
88 entries = rc_get_config (NULL, path); 86 entries = rc_get_config (NULL, path);
89 free (path); 87 free (path);
90 88
91 STRLIST_FOREACH (entries, entry, j) 89 STRLIST_FOREACH (entries, entry, j) {
92 {
93 char *tmpent = rc_xstrdup (entry); 90 char *tmpent = rc_xstrdup (entry);
94 char *value = tmpent; 91 char *value = tmpent;
95 char *var = strsep (&value, "="); 92 char *var = strsep (&value, "=");
96 int k; 93 int k;
97 bool isspecial = false; 94 bool isspecial = false;
98 bool isspecial_spaced = false; 95 bool isspecial_spaced = false;
99 bool replaced = false; 96 bool replaced = false;
100 97
101 for (k = 0; special_spaces[k]; k++) 98 for (k = 0; special_spaces[k]; k++)
102 if (strcmp (special_spaces[k], var) == 0) 99 if (strcmp (special_spaces[k], var) == 0) {
103 { 100 isspecial = true;
104 isspecial = true; 101 isspecial_spaced = true;
105 isspecial_spaced = true; 102 break;
106 break; 103 }
107 }
108 104
109 if (! isspecial) 105 if (! isspecial) {
110 {
111 for (k = 0; specials[k]; k++) 106 for (k = 0; specials[k]; k++)
112 if (strcmp (specials[k], var) == 0) 107 if (strcmp (specials[k], var) == 0) {
113 { 108 isspecial = true;
114 isspecial = true; 109 break;
115 break; 110 }
116 } 111 }
117 }
118 112
119 /* Skip blank vars */ 113 /* Skip blank vars */
120 if (isspecial && 114 if (isspecial &&
121 (! value || strlen (value)) == 0) 115 (! value || strlen (value)) == 0)
122 { 116 {
123 free (tmpent); 117 free (tmpent);
124 continue; 118 continue;
125 } 119 }
126 120
127 STRLIST_FOREACH (envs, env, k) 121 STRLIST_FOREACH (envs, env, k) {
128 {
129 char *tmpenv = rc_xstrdup (env); 122 char *tmpenv = rc_xstrdup (env);
130 char *tmpvalue = tmpenv; 123 char *tmpvalue = tmpenv;
131 char *tmpentry = strsep (&tmpvalue, "="); 124 char *tmpentry = strsep (&tmpvalue, "=");
132 125
133 if (strcmp (tmpentry, var) == 0) 126 if (strcmp (tmpentry, var) == 0) {
134 { 127 if (isspecial) {
135 if (isspecial)
136 {
137 int len = strlen (envs[k - 1]) + strlen (entry) + 1; 128 int len = strlen (envs[k - 1]) + strlen (entry) + 1;
138 envs[k - 1] = rc_xrealloc (envs[k - 1], len); 129 envs[k - 1] = rc_xrealloc (envs[k - 1], len);
139 snprintf (envs[k - 1] + strlen (envs[k - 1]), len, 130 snprintf (envs[k - 1] + strlen (envs[k - 1]), len,
140 "%s%s", isspecial_spaced ? " " : ":", value); 131 "%s%s", isspecial_spaced ? " " : ":", value);
141 } 132 } else {
142 else 133 free (envs[k - 1]);
143 { 134 envs[k - 1] = strdup (entry);
144 free (envs[k - 1]); 135 }
145 envs[k - 1] = strdup (entry); 136 replaced = true;
146 } 137 }
147 replaced = true; 138 free (tmpenv);
148 }
149 free (tmpenv);
150 139
151 if (replaced) 140 if (replaced)
152 break; 141 break;
153 } 142 }
154 143
155 if (! replaced) 144 if (! replaced)
156 envs = rc_strlist_addsort (envs, entry); 145 envs = rc_strlist_addsort (envs, entry);
157 146
158 free (tmpent); 147 free (tmpent);
159 } 148 }
160 } 149 }
161 150
162 if ((fp = fopen (PROFILE_ENV, "w")) == NULL) 151 if ((fp = fopen (PROFILE_ENV, "w")) == NULL)
163 eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno)); 152 eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
164 fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV); 153 fprintf (fp, NOTICE, "/etc/profile", PROFILE_ENV);
154
165 STRLIST_FOREACH (envs, env, i) 155 STRLIST_FOREACH (envs, env, i) {
166 {
167 char *tmpent = rc_xstrdup (env); 156 char *tmpent = rc_xstrdup (env);
168 char *value = tmpent; 157 char *value = tmpent;
169 char *var = strsep (&value, "="); 158 char *var = strsep (&value, "=");
170 if (strcmp (var, "LDPATH") != 0) 159 if (strcmp (var, "LDPATH") != 0)
171 fprintf (fp, "export %s='%s'\n", var, value); 160 fprintf (fp, "export %s='%s'\n", var, value);
172 free (tmpent); 161 free (tmpent);
173 } 162 }
174 fclose (fp); 163 fclose (fp);
175 164
176 if ((fp = fopen (CSH_ENV, "w")) == NULL) 165 if ((fp = fopen (CSH_ENV, "w")) == NULL)
177 eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno)); 166 eerrorx ("%s: fopen `%s': %s", applet, PROFILE_ENV, strerror (errno));
178 fprintf (fp, NOTICE, "/etc/csh.cshrc", PROFILE_ENV); 167 fprintf (fp, NOTICE, "/etc/csh.cshrc", PROFILE_ENV);
168
179 STRLIST_FOREACH (envs, env, i) 169 STRLIST_FOREACH (envs, env, i) {
180 {
181 char *tmpent = rc_xstrdup (env); 170 char *tmpent = rc_xstrdup (env);
182 char *value = tmpent; 171 char *value = tmpent;
183 char *var = strsep (&value, "="); 172 char *var = strsep (&value, "=");
184 if (strcmp (var, "LDPATH") != 0) 173 if (strcmp (var, "LDPATH") != 0)
185 fprintf (fp, "setenv %s '%s'\n", var, value); 174 fprintf (fp, "setenv %s '%s'\n", var, value);
186 free (tmpent); 175 free (tmpent);
187 } 176 }
188 fclose (fp); 177 fclose (fp);
189 178
190 ldent = rc_get_config_entry (envs, "LDPATH"); 179 ldent = rc_get_config_entry (envs, "LDPATH");
191 180
192 if (! ldent || 181 if (! ldent ||
193 (argc > 1 && argv[1] && strcmp (argv[1], "--no-ldconfig") == 0)) 182 (argc > 1 && argv[1] && strcmp (argv[1], "--no-ldconfig") == 0))
194 { 183 {
195 free (envs); 184 free (envs);
196 return (EXIT_SUCCESS); 185 return (EXIT_SUCCESS);
197 } 186 }
198 187
199 while ((file = strsep (&ldent, ":"))) 188 while ((file = strsep (&ldent, ":"))) {
200 {
201 if (strlen (file) == 0) 189 if (strlen (file) == 0)
202 continue; 190 continue;
203 191
204 ldents = rc_strlist_add (ldents, file); 192 ldents = rc_strlist_add (ldents, file);
205 nents++; 193 nents++;
206 } 194 }
207 195
208 /* Update ld.so.conf only if different */ 196 /* Update ld.so.conf only if different */
209 if (rc_exists (LDSOCONF)) 197 if (rc_exists (LDSOCONF)) {
210 {
211 char **lines = rc_get_list (NULL, LDSOCONF); 198 char **lines = rc_get_list (NULL, LDSOCONF);
212 char *line; 199 char *line;
213 ld = false; 200 ld = false;
214 STRLIST_FOREACH (lines, line, i) 201 STRLIST_FOREACH (lines, line, i)
215 if (i > nents || strcmp (line, ldents[i - 1]) != 0) 202 if (i > nents || strcmp (line, ldents[i - 1]) != 0)
216 { 203 {
217 ld = true; 204 ld = true;
218 break; 205 break;
219 } 206 }
220 if (i - 1 != nents) 207 if (i - 1 != nents)
221 ld = true; 208 ld = true;
222 } 209 }
223 210
224 if (ld) 211 if (ld) {
225 {
226 int retval = 0; 212 int retval = 0;
227 213
228 if ((fp = fopen (LDSOCONF, "w")) == NULL) 214 if ((fp = fopen (LDSOCONF, "w")) == NULL)
229 eerrorx ("%s: fopen `%s': %s", applet, LDSOCONF, strerror (errno)); 215 eerrorx ("%s: fopen `%s': %s", applet, LDSOCONF, strerror (errno));
230 fprintf (fp, LDNOTICE); 216 fprintf (fp, LDNOTICE);
231 STRLIST_FOREACH (ldents, ldent, i) 217 STRLIST_FOREACH (ldents, ldent, i)
232 fprintf (fp, "%s\n", ldent); 218 fprintf (fp, "%s\n", ldent);
233 fclose (fp); 219 fclose (fp);
234 220
235#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) 221#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__)
236 ebegin ("Regenerating /var/run/ld-elf.so.hints"); 222 ebegin ("Regenerating /var/run/ld-elf.so.hints");
237 retval = system ("/sbin/ldconfig -elf -i '" LDSOCONF "'"); 223 retval = system ("/sbin/ldconfig -elf -i '" LDSOCONF "'");
238#else 224#else
239 ebegin ("Regenerating /etc/ld.so.cache"); 225 ebegin ("Regenerating /etc/ld.so.cache");
240 retval = system ("/sbin/ldconfig"); 226 retval = system ("/sbin/ldconfig");
241#endif 227#endif
242 eend (retval, NULL); 228 eend (retval, NULL);
243 } 229 }
244 230
245 return(EXIT_SUCCESS); 231 return(EXIT_SUCCESS);
246} 232}

Legend:
Removed from v.2576  
changed lines
  Added in v.2577

  ViewVC Help
Powered by ViewVC 1.1.20