gboolean __scan_usb_procfs(void) { FILE *dev; gchar buffer[128]; gchar *tmp, *manuf = NULL, *product = NULL, *mxpwr; gint bus, level, port = 0, classid = 0, trash; gint vendor, prodid; gfloat ver, rev, speed; int n = 0; dev = fopen("/proc/bus/usb/devices", "r"); if (!dev) return 0; if (usb_list) { moreinfo_del_with_prefix("DEV:USB"); g_free(usb_list); } usb_list = g_strdup("[USB Devices]\n"); while (fgets(buffer, 128, dev)) { tmp = buffer; switch (*tmp) { case 'T': sscanf(tmp, "T: Bus=%d Lev=%d Prnt=%d Port=%d Cnt=%d Dev#=%d Spd=%f", &bus, &level, &trash, &port, &trash, &trash, &speed); break; case 'D': sscanf(tmp, "D: Ver=%f Cls=%x", &ver, &classid); break; case 'P': sscanf(tmp, "P: Vendor=%x ProdID=%x Rev=%f", &vendor, &prodid, &rev); break; case 'S': if (strstr(tmp, "Manufacturer=")) { manuf = g_strdup(strchr(tmp, '=') + 1); remove_linefeed(manuf); } else if (strstr(tmp, "Product=")) { product = g_strdup(strchr(tmp, '=') + 1); remove_linefeed(product); } break; case 'C': mxpwr = strstr(buffer, "MxPwr=") + 6; tmp = g_strdup_printf("USB%d", ++n); if (product && *product == '\0') { g_free(product); if (classid == 9) { product = g_strdup_printf("USB %.2f Hub", ver); } else { product = g_strdup_printf ("Unknown USB %.2f Device (class %d)", ver, classid); } } if (classid == 9) { /* hub */ usb_list = h_strdup_cprintf("[%s#%d]\n", usb_list, product, n); } else { /* everything else */ usb_list = h_strdup_cprintf("$%s$%s=\n", usb_list, tmp, product); const gchar *url = vendor_get_url(manuf); if (url) { gchar *tmp = g_strdup_printf("%s (%s)", vendor_get_name(manuf), url); g_free(manuf); manuf = tmp; } gchar *strhash = g_strdup_printf("[Device Information]\n" "Product=%s\n", product); if (manuf && strlen(manuf)) strhash = h_strdup_cprintf("Manufacturer=%s\n", strhash, manuf); strhash = h_strdup_cprintf("[Port #%d]\n" "Speed=%.2fMbit/s\n" "Max Current=%s\n" "[Misc]\n" "USB Version=%.2f\n" "Revision=%.2f\n" "Class=0x%x\n" "Vendor=0x%x\n" "Product ID=0x%x\n" "Bus=%d\n" "Level=%d\n", strhash, port, speed, mxpwr, ver, rev, classid, vendor, prodid, bus, level); moreinfo_add_with_prefix("DEV", tmp, strhash); g_free(tmp); } g_free(manuf); g_free(product); manuf = g_strdup(""); product = g_strdup(""); port = classid = 0; } } fclose(dev); return n > 0; }
static void read_sensor_labels(gchar * driver) { FILE *conf; gchar buf[256], *line, *p; gboolean lock = FALSE; gint i; sensor_labels = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); sensor_compute = g_hash_table_new(g_str_hash, g_str_equal); /* Try to open lm-sensors config file sensors3.conf */ conf = fopen("/etc/sensors3.conf", "r"); /* If it fails, try to open sensors.conf */ if (!conf) conf = fopen("/etc/sensors.conf", "r"); if (!conf) { /* Cannot open config file. */ return; } while (fgets(buf, 256, conf)) { line = buf; remove_linefeed(line); strend(line, '#'); if (*line == '\0') { continue; } else if (lock && strstr(line, "label")) { /* label lines */ gchar **names = g_strsplit(strstr(line, "label") + 5, " ", 0); gchar *name = NULL, *value = NULL; for (i = 0; names[i]; i++) { if (names[i][0] == '\0') continue; if (!name) name = g_strdup(names[i]); else if (!value) value = g_strdup(names[i]); else value = g_strconcat(value, " ", names[i], NULL); } remove_quotes(value); g_hash_table_insert(sensor_labels, name, value); g_strfreev(names); } else if (lock && strstr(line, "ignore")) { /* ignore lines */ p = strstr(line, "ignore") + 6; if (!strchr(p, ' ')) continue; while (*p == ' ') p++; g_hash_table_insert(sensor_labels, g_strdup(p), "ignore"); } else if (lock && strstr(line, "compute")) { /* compute lines */ gchar **formulas = g_strsplit(strstr(line, "compute") + 7, " ", 0); gchar *name = NULL, *formula = NULL; for (i = 0; formulas[i]; i++) { if (formulas[i][0] == '\0') continue; if (formulas[i][0] == ',') break; if (!name) name = g_strdup(formulas[i]); else if (!formula) formula = g_strdup(formulas[i]); else formula = g_strconcat(formula, formulas[i], NULL); } g_strfreev(formulas); g_hash_table_insert(sensor_compute, name, math_string_to_postfix(formula)); } else if (g_str_has_prefix(line, "chip")) { /* chip lines (delimiter) */ if (lock == FALSE) { gchar **chips = g_strsplit(line, " ", 0); for (i = 1; chips[i]; i++) { strend(chips[i], '*'); if (g_str_has_prefix(chips[i] + 1, driver)) { lock = TRUE; break; } } g_strfreev(chips); } else { break; } } } fclose(conf); }