# HG changeset patch # User Sascha Wilde # Date 1395678277 -3600 # Node ID fc9af77b06b97088dfc082647dbd3ba2dcfe0fc9 # Parent 306e4db1176134858b64b421157fbf1d3e0506f8 Completed profile.ini parser. Debug main() allows to give an mozilla ini file as argument, and outputs extracted profile paths. diff -r 306e4db11761 -r fc9af77b06b9 cinst/mozilla.c --- a/cinst/mozilla.c Mon Mar 24 17:23:06 2014 +0100 +++ b/cinst/mozilla.c Mon Mar 24 17:24:37 2014 +0100 @@ -54,6 +54,7 @@ #include #include +#include #include #ifndef _WIN32 @@ -62,6 +63,8 @@ #define UNIX 0 #endif +#define LINEBUFLEN 1000 + /** * @brief Global Return Code * @@ -86,39 +89,77 @@ get_profile_dirs (char *inifile_name) { char **dirs = NULL; + char *inifile_dirname; FILE *inifile; - char line[1000]; + char line[LINEBUFLEN]; + char *key; + char *value; + char path[LINEBUFLEN]; + char *fqpath; bool inprofile = false; + bool relative_path = false; if ((inifile = fopen(inifile_name, "r")) != NULL) { - while (fgets(line, 1000, inifile) != NULL) + inifile_dirname = port_dirname(inifile_name); + while (fgets(line, LINEBUFLEN, inifile) != NULL) { - if (strncmp(line, "[Profile", 8) == 0) - inprofile = true; + /* Determine if we are in an profile section */ + if (str_starts_with(line, "[Profile")) + { + relative_path = false; + inprofile = true; + } else if (line[0] == '[') inprofile = false; - if (inprofile && - (strncmp(line, "Path=", 5) == 0)) - strv_append(&dirs, line, strlen(line)); + + /* If we are in a profile parse path related stuff */ + if (inprofile) + { + value = line; + key = strsep(&value, "="); + str_trim(&value); + if (str_equal(key, "Path")) + { + if (relative_path) + snprintf(path, LINEBUFLEN, "%s/%s", inifile_dirname, value); + else + strncpy(path, value, LINEBUFLEN); + if ((fqpath = port_realpath(path)) != NULL) + { + strv_append(&dirs, fqpath, strlen(fqpath)); + free (fqpath); + } + else + return_code |= WARN_MOZ_PROFILE_DOES_NOT_EXIST; + } + else if (str_equal(key, "IsRelative") && + str_starts_with(value, "1")) + relative_path = true; + } } } else { return_code |= WARN_MOZ_FAILED_TO_OPEN_INI; } - return dirs; } int -main () +main (int argc, char *argv[]) { int x = 0; - char **pdirs = - get_profile_dirs("/home/wilde/.mozilla/firefox/profiles.ini"); - while (pdirs[x] != NULL) - puts(pdirs[x++]); - strv_free(pdirs); + if (argc == 2) + { + char **pdirs = + get_profile_dirs(argv[1]); + if (pdirs != NULL) + { + while (pdirs[x] != NULL) + puts(pdirs[x++]); + strv_free(pdirs); + } + } exit(return_code); } diff -r 306e4db11761 -r fc9af77b06b9 common/errorcodes.h --- a/common/errorcodes.h Mon Mar 24 17:23:06 2014 +0100 +++ b/common/errorcodes.h Mon Mar 24 17:24:37 2014 +0100 @@ -27,5 +27,7 @@ /* Warning: Failed to read profile.ini */ #define WARN_MOZ_FAILED_TO_OPEN_INI 0x0091 +/* Warning: Some profile paths from profile.ini don't exist */ +#define WARN_MOZ_PROFILE_DOES_NOT_EXIST 0x0092 #endif