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 | |
parent | bd76c170d0b57077574e2d3f75c5ae029f8a379e (diff) |
Don't rely on hardcoded paths anymore
Diffstat (limited to 'hardinfo2')
-rw-r--r-- | hardinfo2/arch/linux/common/modules.h | 10 | ||||
-rw-r--r-- | hardinfo2/arch/linux/common/pci.h | 15 | ||||
-rwxr-xr-x | hardinfo2/configure | 19 | ||||
-rw-r--r-- | hardinfo2/hardinfo.h | 2 | ||||
-rw-r--r-- | hardinfo2/network.c | 140 | ||||
-rw-r--r-- | hardinfo2/util.c | 32 |
6 files changed, 136 insertions, 82 deletions
diff --git a/hardinfo2/arch/linux/common/modules.h b/hardinfo2/arch/linux/common/modules.h index 32c9c0d6..7ea238b7 100644 --- a/hardinfo2/arch/linux/common/modules.h +++ b/hardinfo2/arch/linux/common/modules.h @@ -36,6 +36,7 @@ scan_modules_do(void) { FILE *lsmod; gchar buffer[1024]; + gchar *lsmod_path; if (!_module_hash_table) { _module_hash_table = g_hash_table_new(g_str_hash, g_str_equal); @@ -48,9 +49,12 @@ scan_modules_do(void) module_list = NULL; g_hash_table_foreach_remove(moreinfo, remove_module_devices, NULL); - lsmod = popen("/sbin/lsmod", "r"); - if (!lsmod) + lsmod_path = find_program("lsmod"); + lsmod = popen(lsmod_path, "r"); + if (!lsmod) { + g_free(lsmod_path); return; + } fgets(buffer, 1024, lsmod); /* Discards the first line */ @@ -160,4 +164,6 @@ scan_modules_do(void) g_free(filename); } pclose(lsmod); + + g_free(lsmod_path); } diff --git a/hardinfo2/arch/linux/common/pci.h b/hardinfo2/arch/linux/common/pci.h index 7358090a..a1ea1021 100644 --- a/hardinfo2/arch/linux/common/pci.h +++ b/hardinfo2/arch/linux/common/pci.h @@ -33,9 +33,15 @@ __scan_pci(void) { FILE *lspci; gchar buffer[256], *buf, *strhash = NULL, *strdevice = NULL; - gchar *category = NULL, *name = NULL, *icon; + gchar *category = NULL, *name = NULL, *icon, *lspci_path, *command_line; gint n = 0, x = 0; + if ((lspci_path = find_program("lspci")) == NULL) { + goto pci_error; + } else { + command_line = g_strdup_printf("%s -v", lspci_path); + } + if (!_pci_devices) { _pci_devices = g_hash_table_new(g_str_hash, g_str_equal); } @@ -44,13 +50,13 @@ __scan_pci(void) if (!g_file_test(buf, G_FILE_TEST_EXISTS)) { DEBUG("using system-provided PCI IDs"); g_free(buf); - if (!(lspci = popen(LSPCI, "r"))) { + if (!(lspci = popen(command_line, "r"))) { goto pci_error; } } else { gchar *tmp; - tmp = g_strdup_printf("%s -i '%s'", LSPCI, buf); + tmp = g_strdup_printf("%s -i '%s'", command_line, buf); g_free(buf); buf = tmp; @@ -232,4 +238,7 @@ pci_error: g_free(category); g_free(name); } + + g_free(lspci_path); + g_free(command_line); } diff --git a/hardinfo2/configure b/hardinfo2/configure index d8113727..c3d14c04 100755 --- a/hardinfo2/configure +++ b/hardinfo2/configure @@ -104,24 +104,6 @@ echo "" # --------------------------------------------------------------------------- -echo -n "Checking for lspci... " -LSPCIPATH="/sbin/lspci /usr/sbin/lspci /bin/lspci /usr/bin/lspci `which lspci 2>/dev/null`" -for i in $LSPCIPATH; do - if [ -x "$i" ]; then - LSPCI=$i - break - fi -done - -if [ -e "$LSPCI" ]; then - echo $LSPCI -else - echo "lspci cannot be found" - exit -fi - -# --------------------------------------------------------------------------- - GTK2=-1 MIN_VERSION="2.6.0" echo -n "Checking for GTK version >= ${MIN_VERSION}... " @@ -201,7 +183,6 @@ echo -e "#ifndef __CONFIG_H__\n#define __CONFIG_H__\n" > config.h echo "#define VERSION \"$VERSION\"" >> config.h -echo "#define LSPCI \"$LSPCI -v\"" >> config.h echo "#define $ARCH" >> config.h echo "#define ARCH \"$ARCH\"" >> config.h diff --git a/hardinfo2/hardinfo.h b/hardinfo2/hardinfo.h index 63a5cfa8..9f52f633 100644 --- a/hardinfo2/hardinfo.h +++ b/hardinfo2/hardinfo.h @@ -89,6 +89,8 @@ gpointer __idle_free(gpointer ptr, gchar *f, gint l); #define idle_free(p) __idle_free(p, __FILE__, __LINE__) #endif /* RELEASE == 1 */ + +gchar *find_program(gchar *program_name); inline gchar *size_human_readable(gfloat size); void nonblock_sleep(guint msec); void open_url(gchar *url); diff --git a/hardinfo2/network.c b/hardinfo2/network.c index 71d20ea1..52029598 100644 --- a/hardinfo2/network.c +++ b/hardinfo2/network.c @@ -80,42 +80,50 @@ void scan_statistics(gboolean reload) { FILE *netstat; gchar buffer[256]; + gchar *netstat_path; SCAN_START(); g_free(__statistics); __statistics = g_strdup(""); - if ((netstat = popen("netstat -s", "r"))) { - while (fgets(buffer, 256, netstat)) { - if (!isspace(buffer[0]) && strchr(buffer, ':')) { - gchar *tmp; - - tmp = g_ascii_strup(strend(buffer, ':'), -1); - - __statistics = h_strdup_cprintf("[%s]\n", - __statistics, - tmp); - - g_free(tmp); - } else if (isdigit(buffer[4])) { - gchar *tmp1 = buffer + 4, - *tmp2 = tmp1; - - while (*tmp2 && !isspace(*tmp2)) tmp2++; - *tmp2 = 0; - tmp2++; - - *tmp2 = toupper(*tmp2); - - __statistics = h_strdup_cprintf("%s=%s\n", - __statistics, - g_strstrip(tmp1), - g_strstrip(tmp2)); + if ((netstat_path = find_program("netstat"))) { + gchar *command_line = g_strdup_printf("%s -s", netstat_path); + + if ((netstat = popen(command_line, "r"))) { + while (fgets(buffer, 256, netstat)) { + if (!isspace(buffer[0]) && strchr(buffer, ':')) { + gchar *tmp; + + tmp = g_ascii_strup(strend(buffer, ':'), -1); + + __statistics = h_strdup_cprintf("[%s]\n", + __statistics, + tmp); + + g_free(tmp); + } else if (isdigit(buffer[4])) { + gchar *tmp1 = buffer + 4, + *tmp2 = tmp1; + + while (*tmp2 && !isspace(*tmp2)) tmp2++; + *tmp2 = 0; + tmp2++; + + *tmp2 = toupper(*tmp2); + + __statistics = h_strdup_cprintf("%s=%s\n", + __statistics, + g_strstrip(tmp1), + g_strstrip(tmp2)); + } } + + pclose(netstat); } - pclose(netstat); + g_free(command_line); + g_free(netstat_path); } SCAN_END(); @@ -158,32 +166,40 @@ void scan_route(gboolean reload) { FILE *route; gchar buffer[256]; + gchar *route_path; SCAN_START(); g_free(__routing_table); __routing_table = g_strdup(""); - if ((route = popen("route -n", "r"))) { - /* eat first two lines */ - fgets(buffer, 256, route); - fgets(buffer, 256, route); - - while (fgets(buffer, 256, route)) { - buffer[15] = '\0'; - buffer[31] = '\0'; - buffer[47] = '\0'; - buffer[53] = '\0'; + if ((route_path = find_program("route"))) { + gchar *command_line = g_strdup_printf("%s -n", route_path); + + if ((route = popen(command_line, "r"))) { + /* eat first two lines */ + fgets(buffer, 256, route); + fgets(buffer, 256, route); + + while (fgets(buffer, 256, route)) { + buffer[15] = '\0'; + buffer[31] = '\0'; + buffer[47] = '\0'; + buffer[53] = '\0'; + + __routing_table = h_strdup_cprintf("%s / %s=%s|%s|%s\n", + __routing_table, + g_strstrip(buffer), g_strstrip(buffer + 16), + g_strstrip(buffer + 72), + g_strstrip(buffer + 48), + g_strstrip(buffer + 32)); + } - __routing_table = h_strdup_cprintf("%s / %s=%s|%s|%s\n", - __routing_table, - g_strstrip(buffer), g_strstrip(buffer + 16), - g_strstrip(buffer + 72), - g_strstrip(buffer + 48), - g_strstrip(buffer + 32)); + pclose(route); } - pclose(route); + g_free(command_line); + g_free(route_path); } SCAN_END(); @@ -226,29 +242,37 @@ void scan_connections(gboolean reload) { FILE *netstat; gchar buffer[256]; + gchar *netstat_path; SCAN_START(); g_free(__connections); __connections = g_strdup(""); - if ((netstat = popen("netstat -an", "r"))) { - while (fgets(buffer, 256, netstat)) { - buffer[6] = '\0'; - buffer[43] = '\0'; - buffer[67] = '\0'; - - if (g_str_has_prefix(buffer, "tcp") || g_str_has_prefix(buffer, "udp")) { - __connections = h_strdup_cprintf("%s=%s|%s|%s\n", - __connections, - g_strstrip(buffer + 20), /* local address */ - g_strstrip(buffer), /* protocol */ - g_strstrip(buffer + 44), /* foreign address */ - g_strstrip(buffer + 68)); /* state */ + if ((netstat_path = find_program("netstat"))) { + gchar *command_line = g_strdup_printf("%s -an", netstat_path); + + if ((netstat = popen("netstat -an", "r"))) { + while (fgets(buffer, 256, netstat)) { + buffer[6] = '\0'; + buffer[43] = '\0'; + buffer[67] = '\0'; + + if (g_str_has_prefix(buffer, "tcp") || g_str_has_prefix(buffer, "udp")) { + __connections = h_strdup_cprintf("%s=%s|%s|%s\n", + __connections, + g_strstrip(buffer + 20), /* local address */ + g_strstrip(buffer), /* protocol */ + g_strstrip(buffer + 44), /* foreign address */ + g_strstrip(buffer + 68)); /* state */ + } } + + pclose(netstat); } - pclose(netstat); + g_free(command_line); + g_free(netstat_path); } SCAN_END(); 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; |