2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
26 #include "locale-util.h"
27 #include "path-util.h"
28 #include "string-util.h"
29 #include "sysfs-show.h"
30 #include "terminal-util.h"
31 #include "udev-util.h"
34 static int show_sysfs_one(
37 struct udev_list_entry
**item
,
48 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
49 struct udev_list_entry
*next
, *lookahead
;
50 const char *sn
, *name
, *sysfs
, *subsystem
, *sysname
;
51 _cleanup_free_
char *k
= NULL
, *l
= NULL
;
54 sysfs
= udev_list_entry_get_name(*item
);
55 if (!path_startswith(sysfs
, sub
))
58 d
= udev_device_new_from_syspath(udev
, sysfs
);
60 *item
= udev_list_entry_get_next(*item
);
64 sn
= udev_device_get_property_value(d
, "ID_SEAT");
68 /* Explicitly also check for tag 'seat' here */
69 if (!streq(seat
, sn
) || !udev_device_has_tag(d
, "seat")) {
70 *item
= udev_list_entry_get_next(*item
);
74 is_master
= udev_device_has_tag(d
, "master-of-seat");
76 name
= udev_device_get_sysattr_value(d
, "name");
78 name
= udev_device_get_sysattr_value(d
, "id");
79 subsystem
= udev_device_get_subsystem(d
);
80 sysname
= udev_device_get_sysname(d
);
82 /* Look if there's more coming after this */
83 lookahead
= next
= udev_list_entry_get_next(*item
);
85 const char *lookahead_sysfs
;
87 lookahead_sysfs
= udev_list_entry_get_name(lookahead
);
89 if (path_startswith(lookahead_sysfs
, sub
) &&
90 !path_startswith(lookahead_sysfs
, sysfs
)) {
91 _cleanup_udev_device_unref_
struct udev_device
*lookahead_d
= NULL
;
93 lookahead_d
= udev_device_new_from_syspath(udev
, lookahead_sysfs
);
95 const char *lookahead_sn
;
97 lookahead_sn
= udev_device_get_property_value(d
, "ID_SEAT");
98 if (isempty(lookahead_sn
))
99 lookahead_sn
= "seat0";
101 if (streq(seat
, lookahead_sn
) && udev_device_has_tag(lookahead_d
, "seat"))
106 lookahead
= udev_list_entry_get_next(lookahead
);
109 k
= ellipsize(sysfs
, n_columns
, 20);
113 printf("%s%s%s\n", prefix
, special_glyph(lookahead
? TREE_BRANCH
: TREE_RIGHT
), k
);
117 is_master
? "[MASTER] " : "",
119 name
? " \"" : "", strempty(name
), name
? "\"" : "") < 0)
123 k
= ellipsize(l
, n_columns
, 70);
127 printf("%s%s%s\n", prefix
, lookahead
? special_glyph(TREE_VERTICAL
) : " ", k
);
131 _cleanup_free_
char *p
= NULL
;
133 p
= strappend(prefix
, lookahead
? special_glyph(TREE_VERTICAL
) : " ");
137 show_sysfs_one(udev
, seat
, item
, sysfs
, p
, n_columns
- 2);
144 int show_sysfs(const char *seat
, const char *prefix
, unsigned n_columns
) {
145 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
146 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
147 struct udev_list_entry
*first
= NULL
;
151 n_columns
= columns();
163 e
= udev_enumerate_new(udev
);
167 if (!streq(seat
, "seat0"))
168 r
= udev_enumerate_add_match_tag(e
, seat
);
170 r
= udev_enumerate_add_match_tag(e
, "seat");
174 r
= udev_enumerate_add_match_is_initialized(e
);
178 r
= udev_enumerate_scan_devices(e
);
182 first
= udev_enumerate_get_list_entry(e
);
184 show_sysfs_one(udev
, seat
, &first
, "/", prefix
, n_columns
);
186 printf("%s%s%s\n", prefix
, special_glyph(TREE_RIGHT
), "(none)");