comparison cinst/mozilla.c @ 188:a3bde2aaabd9

merged.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 26 Mar 2014 09:12:10 +0100
parents bea93c8651b7
children d4e97c9b199f
comparison
equal deleted inserted replaced
187:0c06a608e15f 188:a3bde2aaabd9
46 * 46 *
47 * Mozilla also accepts the ini file on Windows even if it is UTF-16 47 * Mozilla also accepts the ini file on Windows even if it is UTF-16
48 * encoded. 48 * encoded.
49 * */ 49 * */
50 50
51 #include <dirent.h>
51 #include <stdbool.h> 52 #include <stdbool.h>
52 #include <stdio.h> 53 #include <stdio.h>
53 #include <stdlib.h> 54 #include <stdlib.h>
54 #include <string.h> 55 #include <string.h>
55 56 #include <sys/types.h>
57
58 #include <debug.h>
56 #include <errorcodes.h> 59 #include <errorcodes.h>
57 #include <portpath.h> 60 #include <portpath.h>
58 #include <strhelp.h> 61 #include <strhelp.h>
59 62
60 #ifndef _WIN32 63 #ifndef _WIN32
61 #define UNIX 1 64 #define LINUX 1
62 #else 65 #else
63 #define UNIX 0 66 #define LINUX 0
64 #endif 67 #endif
65 68
66 #define LINEBUFLEN 1000 69 #define LINEBUFLEN 1000
67 70
68 /** 71 /**
72 * error code on fatal errors and to and warning code on non-fatal 75 * error code on fatal errors and to and warning code on non-fatal
73 * errors. In case of mor than one warning the warning codes will be 76 * errors. In case of mor than one warning the warning codes will be
74 * ORed together. 77 * ORed together.
75 */ 78 */
76 int return_code = 0; 79 int return_code = 0;
80
81 /**
82 * @brief Return users home path, on windows including drive letter
83 * @returns a pointer to a string containing the home path, it should
84 * be freed by the caller.
85 */
86 static char *
87 get_home()
88 {
89 char *home, *homevar, *fqhome;
90 char *homedrive = NULL;
91
92 size_t len;
93 if (LINUX)
94 homevar = "HOME";
95 else
96 homevar = "HOMEPATH";
97
98 if ((home = getenv(homevar)) == NULL)
99 {
100 DEBUGFPRINT("DEBUG: FATAL! No HOME in environment.\n");
101 exit(ERR_MOZ_HOMELESS);
102 }
103
104 len = strlen(home);
105 if (!LINUX)
106 homedrive = getenv("HOMEDRIVE");
107
108 if (homedrive != NULL)
109 len += strlen(homedrive);
110
111 len++; /* Room for \0 */
112 fqhome = xmalloc(len);
113
114 if (homedrive == NULL)
115 snprintf(fqhome, len, "%s", home);
116 else
117 snprintf(fqhome, len, "%s%s", homedrive, home);
118
119 return fqhome;
120 }
77 121
78 /** 122 /**
79 * @brief Get a list of all mozilla profile directories 123 * @brief Get a list of all mozilla profile directories
80 * 124 *
81 * Read the profiles.ini and extract all profile paths from that. 125 * Read the profiles.ini and extract all profile paths from that.
99 bool inprofile = false; 143 bool inprofile = false;
100 bool relative_path = false; 144 bool relative_path = false;
101 145
102 if ((inifile = fopen(inifile_name, "r")) != NULL) 146 if ((inifile = fopen(inifile_name, "r")) != NULL)
103 { 147 {
148 DEBUGFPRINT("DEBUG: Searching for profile paths in: '%s'\n", inifile_name);
149
104 inifile_dirname = port_dirname(inifile_name); 150 inifile_dirname = port_dirname(inifile_name);
105 while (fgets(line, LINEBUFLEN, inifile) != NULL) 151 while (fgets(line, LINEBUFLEN, inifile) != NULL)
106 { 152 {
107 /* Determine if we are in an profile section */ 153 /* Determine if we are in an profile section */
108 if (str_starts_with(line, "[Profile")) 154 if (str_starts_with(line, "[Profile"))
114 inprofile = false; 160 inprofile = false;
115 161
116 /* If we are in a profile parse path related stuff */ 162 /* If we are in a profile parse path related stuff */
117 if (inprofile) 163 if (inprofile)
118 { 164 {
119 value = line; 165 key = strtok(line, "=");
120 key = strsep(&value, "="); 166 value = strtok(NULL, "=");
121 str_trim(&value); 167 str_trim(&value);
122 if (str_equal(key, "Path")) 168 if (str_equal(key, "Path"))
123 { 169 {
124 if (relative_path) 170 if (relative_path)
125 snprintf(path, LINEBUFLEN, "%s/%s", inifile_dirname, value); 171 snprintf(path, LINEBUFLEN, "%s/%s", inifile_dirname, value);
126 else 172 else
127 strncpy(path, value, LINEBUFLEN); 173 strncpy(path, value, LINEBUFLEN);
128 if ((fqpath = port_realpath(path)) != NULL) 174 if ((fqpath = port_realpath(path)) != NULL)
129 { 175 {
176 DEBUGFPRINT("DEBUG: Found profile path: '%s'\n", fqpath);
130 strv_append(&dirs, fqpath, strlen(fqpath)); 177 strv_append(&dirs, fqpath, strlen(fqpath));
131 free (fqpath); 178 free (fqpath);
132 } 179 }
133 else 180 else
134 return_code |= WARN_MOZ_PROFILE_DOES_NOT_EXIST; 181 {
182 DEBUGFPRINT("DEBUG: WARN! Non existent profile path: '%s'\n", path);
183 return_code |= WARN_MOZ_PROFILE_DOES_NOT_EXIST;
184 }
135 } 185 }
136 else if (str_equal(key, "IsRelative") && 186 else if (str_equal(key, "IsRelative") &&
137 str_starts_with(value, "1")) 187 str_starts_with(value, "1"))
138 relative_path = true; 188 relative_path = true;
139 } 189 }
140 } 190 }
141 } 191 fclose(inifile);
142 else 192 }
143 { 193 else
194 {
195 DEBUGFPRINT("DEBUG: WARN! Could not open ini file: '%s'\n", inifile_name);
144 return_code |= WARN_MOZ_FAILED_TO_OPEN_INI; 196 return_code |= WARN_MOZ_FAILED_TO_OPEN_INI;
145 } 197 }
146 return dirs; 198 return dirs;
147 } 199 }
148 200
201 /**
202 * @brief Search for mozilla profiles.ini files
203 *
204 * Use well known paths and heuristics to find the current users
205 * profiles.ini files on GNU/Linux and Windows systems.
206 *
207 * @return NULL terminated array of strings containing the absolute
208 * path of the profiles.ini files. The array needs to be freed by the
209 * caller.
210 */
211 static char **
212 get_profile_inis ()
213 {
214 char **inis = NULL;
215 char path[LINEBUFLEN];
216 char *fqpath;
217 char *mozdirname;
218 DIR *mozdir;
219 struct dirent *mozdirent;
220 char *home = get_home();
221
222 if (LINUX)
223 {
224 mozdirname = ".mozilla";
225 }
226 else
227 {
228 mozdirname = "AppData/Roaming/Mozilla";
229 }
230
231 snprintf(path, LINEBUFLEN, "%s/%s", home, mozdirname);
232 if ((mozdir = opendir(path)) != NULL)
233 {
234 while ((mozdirent = readdir(mozdir)) != NULL)
235 {
236 snprintf(path, LINEBUFLEN, "%s/%s/%s",
237 home,
238 mozdirname,
239 mozdirent->d_name);
240 if (port_isdir(path))
241 {
242 snprintf(path, LINEBUFLEN, "%s/%s/%s/%s",
243 home,
244 mozdirname,
245 mozdirent->d_name,
246 "profiles.ini");
247 DEBUGFPRINT("DEBUG: checking for %s...\n", path);
248 if ((fqpath = port_realpath(path)) != NULL)
249 {
250 strv_append(&inis, fqpath, strlen(fqpath));
251 DEBUGFPRINT("DEBUG: Found mozilla ini file: '%s'\n", fqpath);
252 free(fqpath);
253 }
254 }
255 }
256 closedir(mozdir);
257 }
258 else
259 {
260 DEBUGFPRINT("DEBUG: Could not open %s/%s\n", home, mozdirname);
261 exit(WARN_MOZ_NO_PROFILES);
262 }
263 return inis;
264 }
265
266
149 int 267 int
150 main (int argc, char *argv[]) 268 main ()
151 { 269 {
152 int x = 0; 270 int x = 0;
153 if (argc == 2) 271 int y = 0;
154 { 272 char **mozinis, **pdirs;
155 char **pdirs = 273 if ((mozinis = get_profile_inis()) != NULL)
156 get_profile_dirs(argv[1]); 274 while (mozinis[y] != NULL)
157 if (pdirs != NULL) 275 {
158 { 276 pdirs =
159 while (pdirs[x] != NULL) 277 get_profile_dirs(mozinis[y++]);
160 puts(pdirs[x++]); 278 if (pdirs != NULL)
161 strv_free(pdirs); 279 {
162 } 280 while (pdirs[x] != NULL)
163 } 281 puts(pdirs[x++]);
282 strv_free(pdirs);
283 }
284 }
164 exit(return_code); 285 exit(return_code);
165 } 286 }

http://wald.intevation.org/projects/trustbridge/