diff options
| author | Burt P <pburt0@gmail.com> | 2017-07-06 21:58:37 -0500 | 
|---|---|---|
| committer | Leandro Pereira <leandro@hardinfo.org> | 2017-07-09 11:54:46 -0700 | 
| commit | 425b4b20edeb741cd6114cc1c40bd198a9908407 (patch) | |
| tree | eadfe7b25374b5ecc65aaef54684ae09b6766f7f /modules | |
| parent | 889fc87a59f1814cd56c0d18a5b5e04a0ea09be5 (diff) | |
Use ldconfig to detect C library implementation and version
Signed-off-by: Burt P <pburt0@gmail.com>
Diffstat (limited to 'modules')
| -rw-r--r-- | modules/computer/os.c | 68 | 
1 files changed, 40 insertions, 28 deletions
| diff --git a/modules/computer/os.c b/modules/computer/os.c index 264f718a..50fa2bb4 100644 --- a/modules/computer/os.c +++ b/modules/computer/os.c @@ -24,41 +24,53 @@  static gchar *  get_libc_version(void)  { -    FILE *libc; -    gchar buf[256], *tmp, *p, *ret = NULL; -    char *libc_paths[] = { -        "/lib/ld-uClibc.so.0", "/lib64/ld-uClibc.so.0", -        "/lib/libc.so.6", "/lib64/libc.so.6", "/lib/x86_64-linux-gnu/libc.so.6", +    FILE *testp; +    gchar buf[256], *p, *ver_str = NULL, *ret = NULL; + +    struct { +        const char *test_cmd; +        const char *match_str; +        const char *lib_name; +    } libs[] = { +        { .test_cmd = "ldconfig -V", .match_str = "GLIBC", .lib_name = "GNU C Library" }, +        { .test_cmd = "ldconfig -v", .match_str = "uClibc", .lib_name = "uClibc or uClibc-ng" }, +        { NULL, NULL, NULL },      }; -    int i; - -    for (i=0; i < 4; i++) { -        if (g_file_test(libc_paths[i], G_FILE_TEST_EXISTS)) break; +    int i = 0; +    while (libs[i].test_cmd != NULL) { +        memset(buf, 0, 256); +        testp = popen(libs[i].test_cmd, "r"); +        if (testp) { +            (void)fgets(buf, 255, testp); +            pclose(testp); + +            /* limit to first line */ +            p = strstr(buf, "\n"); if (p) *p = 0; + +            if ( strstr(buf, libs[i].match_str) ) { +                ver_str = strstr(buf, " "); /* skip the first word, likely "ldconfig" */ +                if (ver_str) ver_str++; +                break; +            } +        } +        i++;      } +      switch (i) { -        case 0: case 1: -            ret = g_strdup("uClibc Library"); +        case 0: /* GLIBC */ +            if (ver_str) +                ret = g_strdup_printf("%s / %s", libs[i].lib_name, ver_str ); +            else +                ret = g_strdup(libs[i].lib_name);              break; -        case 2: case 3: case 4: -            ret = g_strdup("GNU C Library (GLIBC)"); -            /* fall through and try to do better */ -        default: -            memset(buf, 0, 256); -            libc = popen("ldconfig -V", "r"); -            if (!libc) goto err; -            (void)fgets(buf, 255, libc); -            if (pclose(libc)) goto err; -            tmp = strstr(buf, " "); -            if (!tmp) goto err; -            p = strstr(buf, "\n"); -            if (p) *p = 0; -            g_free(ret); -            ret = g_strdup_printf("%s", tmp + 1); +        case 1: /* uClibc */ +            ret = g_strdup(libs[i].lib_name);              break; +        default: +            ret = g_strdup(_("Unknown"));      } -err: -    return (ret) ? ret : g_strdup(_("Unknown")); +    return ret;  }  #include <gdk/gdkx.h> | 
