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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20