Remove non-portable functions
authorColin Clark <colin.clark@cclark.uk>
Sat, 2 Oct 2021 09:52:48 +0000 (10:52 +0100)
committerColin Clark <colin.clark@cclark.uk>
Sat, 2 Oct 2021 09:52:48 +0000 (10:52 +0100)
The functions /proc/cpuinfo and /proc/self/exe are not portable.
Replace them with other solutions.

src/main.c
src/misc.c

index e7f6b69..3bfdedd 100644 (file)
@@ -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, "");
index c117b68..c1f338f 100644 (file)
@@ -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)