diff options
author | Leandro A. F. Pereira <leandro@hardinfo.org> | 2009-02-21 12:59:39 -0300 |
---|---|---|
committer | Leandro A. F. Pereira <leandro@hardinfo.org> | 2009-02-21 12:59:39 -0300 |
commit | 6bc92795d0855cc08cf93e00a13f11595d36c7ec (patch) | |
tree | e9d06bfe56afa6ce0cc056011a657e064a915019 /hardinfo2/util.c | |
parent | bd76c170d0b57077574e2d3f75c5ae029f8a379e (diff) |
Don't rely on hardcoded paths anymore
Diffstat (limited to 'hardinfo2/util.c')
-rw-r--r-- | hardinfo2/util.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/hardinfo2/util.c b/hardinfo2/util.c index 9ef0c869..2e8facd4 100644 --- a/hardinfo2/util.c +++ b/hardinfo2/util.c @@ -45,6 +45,38 @@ #define MiB 1048576 #define GiB 1073741824 +gchar *find_program(gchar *program_name) +{ + int i; + char *temp; + static GHashTable *cache = NULL; + const char *path[] = { "/bin", "/sbin", + "/usr/bin", "/usr/sbin", + "/usr/local/bin", "/usr/local/sbin", + NULL }; + + if (!cache) { + cache = g_hash_table_new(g_str_hash, g_str_equal); + } + + if ((temp = g_hash_table_lookup(cache, program_name))) { + return g_strdup(temp); + } + + for (i = 0; path[i]; i++) { + temp = g_build_filename(path[i], program_name, NULL); + + if (g_file_test(temp, G_FILE_TEST_IS_EXECUTABLE)) { + g_hash_table_insert(cache, program_name, g_strdup(temp)); + return temp; + } + + g_free(temp); + } + + return NULL; +} + gchar *seconds_to_string(unsigned int seconds) { unsigned int hours, minutes, days; |