From d7d2db8b4cb3be207b14c562b6fc1cf4fc894063 Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Sat, 2 Oct 2021 10:52:48 +0100 Subject: [PATCH] Remove non-portable functions The functions /proc/cpuinfo and /proc/self/exe are not portable. Replace them with other solutions. --- src/main.c | 24 +++++++++++++----------- src/misc.c | 24 +----------------------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/src/main.c b/src/main.c index e7f6b69f..3bfdedde 100644 --- a/src/main.c +++ b/src/main.c @@ -1247,23 +1247,24 @@ static void setup_sigbus_handler(void) * They are now variables, all defined relative to one level above the * directory that the executable is run from. */ -static void create_application_paths() +static void create_application_paths(gchar *argv[]) { - gchar buf[1024]; gchar *dirname; gchar *tmp; + gchar **env; - memset(buf, 0, sizeof(buf)); - if (readlink("/proc/self/exe", buf, sizeof(buf) - 1) < 0) + env = g_get_environ(); + + if (argv[0][0] == G_DIR_SEPARATOR) + { + gq_executable_path = g_strdup(argv[0]); + } + else { - /* There was an error. Perhaps the path does not exist - * or the buffer is not big enough. */ - log_printf("Can't get path from /proc/self/exe"); - exit(1); + gq_executable_path = g_build_filename(g_environ_getenv(env, "PWD"), argv[0], NULL); } - gq_executable_path = g_strdup(buf); - dirname = g_path_get_dirname(buf); // default is /usr/bin/ + dirname = g_path_get_dirname(gq_executable_path); // default is /usr/bin/ gq_prefix = g_path_get_dirname(dirname); gq_localedir = g_build_filename(gq_prefix, "share", "locale", NULL); @@ -1276,6 +1277,7 @@ static void create_application_paths() g_free(tmp); g_free(dirname); + g_strfreev(env); } gint main(gint argc, gchar *argv[]) @@ -1299,7 +1301,7 @@ gint main(gint argc, gchar *argv[]) /* init execution time counter (debug only) */ init_exec_time(); - create_application_paths(); + create_application_paths(argv); /* setup locale, i18n */ setlocale(LC_ALL, ""); diff --git a/src/misc.c b/src/misc.c index c117b681..c1f338f4 100644 --- a/src/misc.c +++ b/src/misc.c @@ -379,29 +379,7 @@ gchar *get_symbolic_link(const gchar *path_utf8) gint get_cpu_cores(void) { - FILE *cpuinfo = fopen("/proc/cpuinfo", "rb"); - char *arg = 0; - size_t size = 0; - int cores = 1; - gchar *siblings_line; - gchar *siblings_str; - - while(getline(&arg, &size, cpuinfo) != -1) - { - siblings_line = g_strrstr(arg, "siblings"); - if (siblings_line) - { - siblings_str = g_strrstr(siblings_line, ":"); - if (siblings_str) - { - cores = g_ascii_strtoll(siblings_str + 1, NULL, 0); - } - } - } - free(arg); - fclose(cpuinfo); - - return cores; + return sysconf(_SC_NPROCESSORS_ONLN); } void tree_path_free_wrapper(void *data, void *useradata) -- 2.20.1