diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 154 |
1 files changed, 132 insertions, 22 deletions
@@ -1,6 +1,6 @@ /* * HardInfo - Displays System Information - * Copyright (C) 2003-2007 Leandro A. F. Pereira <leandro@linuxmag.com.br> + * Copyright (C) 2003-2007 Leandro A. F. Pereira <leandro@hardinfo.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -45,6 +45,43 @@ #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 }; + + /* we don't need to call stat() every time: cache the results */ + if (!cache) { + cache = g_hash_table_new(g_str_hash, g_str_equal); + } else 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); + } + + /* our search has failed; use GLib's search (which uses $PATH env var) */ + if ((temp = g_find_program_in_path(program_name))) { + g_hash_table_insert(cache, program_name, g_strdup(temp)); + return temp; + } + + return NULL; +} + gchar *seconds_to_string(unsigned int seconds) { unsigned int hours, minutes, days; @@ -86,14 +123,16 @@ inline gchar *size_human_readable(gfloat size) return g_strdup_printf("%.1f GiB", size / GiB); } -inline void strend(gchar * str, gchar chr) +inline char *strend(gchar * str, gchar chr) { if (!str) - return; + return NULL; char *p; if ((p = strchr(str, chr))) *p = 0; + + return str; } inline void remove_quotes(gchar * str) @@ -116,9 +155,11 @@ void widget_set_cursor(GtkWidget * widget, GdkCursorType cursor_type) { GdkCursor *cursor; - cursor = gdk_cursor_new(cursor_type); - gdk_window_set_cursor(GDK_WINDOW(widget->window), cursor); - gdk_cursor_unref(cursor); + if ((cursor = gdk_cursor_new(cursor_type))) { + gdk_window_set_cursor(GDK_WINDOW(widget->window), cursor); + gdk_display_flush(gtk_widget_get_display(widget)); + gdk_cursor_unref(cursor); + } while (gtk_events_pending()) gtk_main_iteration(); @@ -387,6 +428,7 @@ void parameters_init(int *argc, char ***argv, ProgramParameters * param) param->list_modules = list_modules; param->use_modules = use_modules; param->autoload_deps = autoload_deps; + param->argv0 = *(argv)[0]; if (report_format && g_str_equal(report_format, "html")) param->report_format = REPORT_FORMAT_HTML; @@ -419,10 +461,15 @@ void open_url(gchar * url) "opera", "konqueror", "netscape", "links -g", NULL }; - gint i; - - for (i = 0; browsers[i]; i++) { - gchar *cmdline = g_strdup_printf("%s '%s'", browsers[i], url); + gint i = 0; + gchar *browser = (gchar *)g_getenv("BROWSER"); + + if (!browser || *browser == '\0') { + browser = (gchar *)browsers[i++]; + } + + do { + gchar *cmdline = g_strdup_printf("%s '%s'", browser, url); if (g_spawn_command_line_async(cmdline, NULL)) { g_free(cmdline); @@ -430,7 +477,9 @@ void open_url(gchar * url) } g_free(cmdline); - } + + browser = (gchar *)browsers[i++]; + } while (browser); g_warning("Couldn't find a Web browser to open URL %s.", url); } @@ -459,9 +508,9 @@ static void module_register_methods(ShellModule * module) if (g_module_symbol (module->dll, "hi_exported_methods", (gpointer) & get_methods)) { - ShellModuleMethod *methods = get_methods(); - - while (TRUE) { + ShellModuleMethod *methods; + + for (methods = get_methods(); methods->name; methods++) { ShellModuleMethod method = *methods; gchar *name = g_path_get_basename(g_module_name(module->dll)); @@ -471,9 +520,6 @@ static void module_register_methods(ShellModule * module) g_hash_table_insert(__module_methods, method_name, method.function); g_free(name); - - if (!(*(++methods)).name) - break; } } @@ -492,6 +538,20 @@ gchar *module_call_method(gchar * method) g_strdup_printf("{Unknown method: \"%s\"}", method); } +/* FIXME: varargs? */ +gchar *module_call_method_param(gchar * method, gchar * parameter) +{ + gchar *(*function) (gchar *param); + + if (__module_methods == NULL) { + return NULL; + } + + function = g_hash_table_lookup(__module_methods, method); + return function ? g_strdup(function(parameter)) : + g_strdup_printf("{Unknown method: \"%s\"}", method); +} + static ShellModule *module_load(gchar * filename) { ShellModule *module; @@ -618,7 +678,7 @@ static void module_entry_free(gpointer data, gpointer user_data) } } -static void module_free(ShellModule * module) +void module_free(ShellModule * module) { g_free(module->name); g_object_unref(module->icon); @@ -705,8 +765,8 @@ static GSList *modules_check_deps(GSList * modules) modules = g_slist_prepend(modules, mod); modules = modules_check_deps(modules); /* re-check dependencies */ } else { - modules = g_slist_remove(modules, module); - module_free(module); + g_error("HardInfo cannot run without loading the additional module."); + exit(1); } gtk_widget_destroy(dialog); @@ -896,8 +956,6 @@ void tree_view_save_image(gchar * filename) static gboolean __idle_free_do(gpointer ptr) { - DEBUG("bla %p", ptr); - if (ptr) { g_free(ptr); } @@ -1048,3 +1106,55 @@ h_hash_table_remove_all(GHashTable *hash_table) h_hash_table_remove_all_true, NULL); } + +gfloat +h_sysfs_read_float(gchar *endpoint, gchar *entry) +{ + gchar *tmp, *buffer; + gfloat return_value = 0.0f; + + tmp = g_build_filename(endpoint, entry, NULL); + if (g_file_get_contents(tmp, &buffer, NULL, NULL)) + return_value = atof(buffer); + + g_free(tmp); + g_free(buffer); + + return return_value; +} + +gint +h_sysfs_read_int(gchar *endpoint, gchar *entry) +{ + gchar *tmp, *buffer; + gint return_value = 0.0f; + + tmp = g_build_filename(endpoint, entry, NULL); + if (g_file_get_contents(tmp, &buffer, NULL, NULL)) + return_value = atoi(buffer); + + g_free(tmp); + g_free(buffer); + + return return_value; +} + +gchar * +h_sysfs_read_string(gchar *endpoint, gchar *entry) +{ + gchar *tmp, *return_value; + + tmp = g_build_filename(endpoint, entry, NULL); + if (!g_file_get_contents(tmp, &return_value, NULL, NULL)) { + g_free(return_value); + + return_value = NULL; + } else { + return_value = g_strstrip(return_value); + } + + g_free(tmp); + + return return_value; +} + |