9 typedef struct sProperty
15 typedef struct sDevice
23 int parse_property(char *str, int size, Property *prop)
28 for(i=0; (equals<0 && i<size); ++i)
35 prop->name = malloc(equals+1);
36 strncpy(prop->name, str, equals);
37 prop->name[equals] = 0;
39 prop->value = malloc(size-equals);
40 strncpy(prop->value, str+equals+1, size-equals-1);
41 prop->value[size-equals-1] = 0;
46 Property *get_device_properties(char *node)
51 /*printf("Examining device %s\n", node);*/
61 execl("/sbin/udevadm", "udevadm", "info", "-q", "property", "-n", node, NULL);
69 Property *props = NULL;
84 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos);
93 for(i=0; (newline<0 && i<pos); ++i)
100 if(parse_property(buf, newline, &prop)==0)
102 /*printf("Got property '%s' = '%s'\n", prop.name, prop.value);*/
104 props = (Property *)realloc(props, (n_props+2)*sizeof(Property));
105 props[n_props] = prop;
108 memmove(buf, buf+newline+1, pos-newline-1);
115 props[n_props].name = NULL;
116 props[n_props].value = NULL;
118 waitpid(pid, NULL, 0);
132 char *get_property_value(Property *props, char *name)
135 for(i=0; props[i].name; ++i)
136 if(strcmp(props[i].name, name)==0)
137 return props[i].value;
141 int match_property_value(Property *props, char *name, char *value)
143 char *v = get_property_value(props, name);
146 return strcmp(v, value)==0;
149 void free_properties(Property *props)
154 for(i=0; props[i].name; ++i)
157 free(props[i].value);
162 Device *get_devices(void)
167 Device *devices = NULL;
170 dir = opendir("/dev/disk/by-id");
174 while((de = readdir(dir)))
176 if(de->d_name[0]=='.' && (de->d_name[1]==0 || (de->d_name[1]=='.' && de->d_name[2]==0)))
179 snprintf(fnbuf, sizeof(fnbuf), "/dev/disk/by-id/%s", de->d_name);
180 Property *props = get_device_properties(fnbuf);
181 if(match_property_value(props, "ID_BUS", "usb") && match_property_value(props, "DEVTYPE", "partition"))
190 /*printf("Using device %s\n", fnbuf);*/
192 label = get_property_value(props, "ID_FS_LABEL");
194 label = get_property_value(props, "ID_FS_UUID");
199 label = get_property_value(props, "DEVNAME");
200 for(ptr=label; *ptr; ++ptr)
204 vendor = get_property_value(props, "ID_VENDOR");
205 model = get_property_value(props, "ID_MODEL");
207 pos = snprintf(buf, sizeof(buf), "%s", label);
209 pos += snprintf(buf+pos, sizeof(buf)-pos, " (%s %s)", vendor, model);
213 devices = (Device *)realloc(devices, (n_devices+2)*sizeof(Device));
214 devices[n_devices].node = strdup(fnbuf);
215 devices[n_devices].label = strdup(label);
216 devices[n_devices].description = strdup(buf);
217 devices[n_devices].time = st.st_mtime;
220 free_properties(props);
227 devices[n_devices].node = NULL;
228 devices[n_devices].label = NULL;
234 void free_devices(Device *devices)
239 for(i=0; devices[i].node; ++i)
241 free(devices[i].node);
242 free(devices[i].label);
247 void row_activated(GtkTreeView *list, GtkTreePath *path, GtkTreeViewColumn *column, gpointer user_data)
252 model = gtk_tree_view_get_model(list);
254 if(gtk_tree_model_get_iter(model, &iter, path))
260 gtk_tree_model_get(model, &iter, 1, &device, -1);
270 execl("/usr/bin/pmount", "pmount", device->node, device->label, NULL);
285 len = read(pipe_fd[0], buf+pos, sizeof(buf)-pos-1);
293 waitpid(pid, &status, 0);
294 if(!WIFEXITED(status) || WEXITSTATUS(status))
298 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s", buf);
299 g_signal_connect(dialog, "response", >k_main_quit, NULL);
300 gtk_widget_show_all(dialog);
314 int main(int argc, char **argv)
320 GtkTreeSelection *selection;
326 gtk_init(&argc, &argv);
328 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
329 gtk_container_set_border_width(GTK_CONTAINER(window), 5);
330 g_signal_connect(window, "destroy", >k_main_quit, NULL);
332 viewport = gtk_viewport_new(NULL, NULL);
333 gtk_viewport_set_shadow_type(GTK_VIEWPORT(viewport), GTK_SHADOW_IN);
334 gtk_container_add(GTK_CONTAINER(window), viewport);
336 list = gtk_tree_view_new();
337 gtk_container_add(GTK_CONTAINER(viewport), list);
338 g_signal_connect(list, "row-activated", (GCallback)&row_activated, NULL);
340 store = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
341 gtk_tree_view_set_model(GTK_TREE_VIEW(list), GTK_TREE_MODEL(store));
342 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(list),
343 -1, "Device", gtk_cell_renderer_text_new(), "text", 0, NULL);
345 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
347 devices = get_devices();
351 for(i=0; devices[i].node; ++i)
353 gtk_list_store_append(store, &iter);
354 gtk_list_store_set(store, &iter, 0, devices[i].description, 1, &devices[i], -1);
355 if(devices[i].time>latest)
357 latest = devices[i].time;
358 gtk_tree_selection_select_iter(selection, &iter);
362 gtk_widget_show_all(window);
368 dialog = gtk_message_dialog_new(NULL, 0, GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "No devices found");
369 g_signal_connect(dialog, "response", >k_main_quit, NULL);
370 gtk_widget_show_all(dialog);
375 free_devices(devices);