1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "sysfs-show.h"
29 static bool device_has_tag(struct udev_device *d, const char *tag) {
30 struct udev_list_entry *first, *item;
36 udev_device_get_is_initialized(d);
38 first = udev_device_get_tags_list_entry(d);
39 udev_list_entry_foreach(item, first)
40 if (streq(udev_list_entry_get_name(item), tag))
46 static int show_sysfs_one(
49 struct udev_list_entry **item,
60 struct udev_list_entry *next, *lookahead;
61 struct udev_device *d;
62 const char *sn, *id, *name, *sysfs, *subsystem, *sysname;
64 sysfs = udev_list_entry_get_name(*item);
65 if (!path_startswith(sysfs, sub))
68 d = udev_device_new_from_syspath(udev, sysfs);
70 *item = udev_list_entry_get_next(*item);
74 sn = udev_device_get_property_value(d, "ID_SEAT");
78 /* fixme, also check for tag 'seat' here */
79 if (!streq(seat, sn) || !device_has_tag(d, "seat")) {
81 *item = udev_list_entry_get_next(*item);
85 id = udev_device_get_property_value(d, "ID_FOR_SEAT");
86 name = udev_device_get_sysattr_value(d, "name");
88 name = udev_device_get_sysattr_value(d, "id");
89 subsystem = udev_device_get_subsystem(d);
90 sysname = udev_device_get_sysname(d);
92 /* Look if there's more coming after this */
93 lookahead = next = udev_list_entry_get_next(*item);
95 const char *lookahead_sysfs;
97 lookahead_sysfs = udev_list_entry_get_name(lookahead);
99 if (path_startswith(lookahead_sysfs, sub) &&
100 !path_startswith(lookahead_sysfs, sysfs)) {
101 struct udev_device *lookahead_d;
103 lookahead_d = udev_device_new_from_syspath(udev, lookahead_sysfs);
105 const char *lookahead_sn;
108 lookahead_sn = udev_device_get_property_value(d, "ID_SEAT");
109 if (isempty(lookahead_sn))
110 lookahead_sn = "seat0";
112 found = streq(seat, lookahead_sn) && device_has_tag(d, "seat");
113 udev_device_unref(lookahead_d);
120 lookahead = udev_list_entry_get_next(lookahead);
123 printf("%s%s %s (%s:%s)", prefix, lookahead ? "\342\224\234" : "\342\224\224", id ? id : sysfs, subsystem, sysname);
126 printf(" \"%s\"\n", name);
134 p = strappend(prefix, lookahead ? "\342\224\202 " : " ");
135 show_sysfs_one(udev, seat, item, sysfs, p, n_columns - 2);
139 udev_device_unref(d);
145 int show_sysfs(const char *seat, const char *prefix, unsigned n_columns) {
147 struct udev_list_entry *first = NULL;
148 struct udev_enumerate *e;
152 n_columns = columns();
164 e = udev_enumerate_new(udev);
170 if (!streq(seat, "seat0"))
171 r = udev_enumerate_add_match_tag(e, seat);
173 r = udev_enumerate_add_match_tag(e, "seat");
175 r = udev_enumerate_scan_devices(e);
179 first = udev_enumerate_get_list_entry(e);
181 show_sysfs_one(udev, seat, &first, "/", prefix, n_columns);
185 udev_enumerate_unref(e);