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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "alloc-util.h"
27 #include "locale-util.h"
28 #include "path-util.h"
29 #include "string-util.h"
30 #include "sysfs-show.h"
31 #include "terminal-util.h"
32 #include "udev-util.h"
35 static int show_sysfs_one(
38 struct udev_list_entry
**item
,
49 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
50 struct udev_list_entry
*next
, *lookahead
;
51 const char *sn
, *name
, *sysfs
, *subsystem
, *sysname
;
52 _cleanup_free_
char *k
= NULL
, *l
= NULL
;
55 sysfs
= udev_list_entry_get_name(*item
);
56 if (!path_startswith(sysfs
, sub
))
59 d
= udev_device_new_from_syspath(udev
, sysfs
);
61 *item
= udev_list_entry_get_next(*item
);
65 sn
= udev_device_get_property_value(d
, "ID_SEAT");
69 /* Explicitly also check for tag 'seat' here */
70 if (!streq(seat
, sn
) || !udev_device_has_tag(d
, "seat")) {
71 *item
= udev_list_entry_get_next(*item
);
75 is_master
= udev_device_has_tag(d
, "master-of-seat");
77 name
= udev_device_get_sysattr_value(d
, "name");
79 name
= udev_device_get_sysattr_value(d
, "id");
80 subsystem
= udev_device_get_subsystem(d
);
81 sysname
= udev_device_get_sysname(d
);
83 /* Look if there's more coming after this */
84 lookahead
= next
= udev_list_entry_get_next(*item
);
86 const char *lookahead_sysfs
;
88 lookahead_sysfs
= udev_list_entry_get_name(lookahead
);
90 if (path_startswith(lookahead_sysfs
, sub
) &&
91 !path_startswith(lookahead_sysfs
, sysfs
)) {
92 _cleanup_udev_device_unref_
struct udev_device
*lookahead_d
= NULL
;
94 lookahead_d
= udev_device_new_from_syspath(udev
, lookahead_sysfs
);
96 const char *lookahead_sn
;
98 lookahead_sn
= udev_device_get_property_value(d
, "ID_SEAT");
99 if (isempty(lookahead_sn
))
100 lookahead_sn
= "seat0";
102 if (streq(seat
, lookahead_sn
) && udev_device_has_tag(lookahead_d
, "seat"))
107 lookahead
= udev_list_entry_get_next(lookahead
);
110 k
= ellipsize(sysfs
, n_columns
, 20);
114 printf("%s%s%s\n", prefix
, draw_special_char(lookahead
? DRAW_TREE_BRANCH
: DRAW_TREE_RIGHT
), k
);
118 is_master
? "[MASTER] " : "",
120 name
? " \"" : "", strempty(name
), name
? "\"" : "") < 0)
124 k
= ellipsize(l
, n_columns
, 70);
128 printf("%s%s%s\n", prefix
, lookahead
? draw_special_char(DRAW_TREE_VERTICAL
) : " ", k
);
132 _cleanup_free_
char *p
= NULL
;
134 p
= strappend(prefix
, lookahead
? draw_special_char(DRAW_TREE_VERTICAL
) : " ");
138 show_sysfs_one(udev
, seat
, item
, sysfs
, p
, n_columns
- 2);
145 int show_sysfs(const char *seat
, const char *prefix
, unsigned n_columns
) {
146 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*e
= NULL
;
147 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
148 struct udev_list_entry
*first
= NULL
;
152 n_columns
= columns();
164 e
= udev_enumerate_new(udev
);
168 if (!streq(seat
, "seat0"))
169 r
= udev_enumerate_add_match_tag(e
, seat
);
171 r
= udev_enumerate_add_match_tag(e
, "seat");
175 r
= udev_enumerate_add_match_is_initialized(e
);
179 r
= udev_enumerate_scan_devices(e
);
183 first
= udev_enumerate_get_list_entry(e
);
185 show_sysfs_one(udev
, seat
, &first
, "/", prefix
, n_columns
);
187 printf("%s%s%s\n", prefix
, draw_special_char(DRAW_TREE_RIGHT
), "(none)");