util-lib: add a new skip_dev_prefix() helper

This new helper removes a leading /dev if there is one. We have code
doing this all over the place, let's unify this, and correct it while
we are at it, by using path_startswith() rather than startswith() to
drop the prefix.
This commit is contained in:
Lennart Poettering
2017-08-09 19:01:18 +02:00
parent b3f5897f6e
commit a119ec7c82
4 changed files with 31 additions and 13 deletions
+10
View File
@@ -143,3 +143,13 @@ bool is_deviceallow_pattern(const char *path);
int systemd_installation_has_version(const char *root, unsigned minimal_version);
bool dot_or_dot_dot(const char *path);
static inline const char *skip_dev_prefix(const char *p) {
const char *e;
/* Drop any /dev prefix if there is any */
e = path_startswith(p, "/dev/");
return e ?: p;
}
+4 -11
View File
@@ -649,10 +649,7 @@ bool tty_is_vc(const char *tty) {
bool tty_is_console(const char *tty) {
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
return streq(tty, "console");
return streq(skip_dev_prefix(tty), "console");
}
int vtnr_from_tty(const char *tty) {
@@ -660,8 +657,7 @@ int vtnr_from_tty(const char *tty) {
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
tty = skip_dev_prefix(tty);
if (!startswith(tty, "tty") )
return -EINVAL;
@@ -775,8 +771,7 @@ bool tty_is_vc_resolve(const char *tty) {
assert(tty);
if (startswith(tty, "/dev/"))
tty += 5;
tty = skip_dev_prefix(tty);
if (streq(tty, "console")) {
tty = resolve_dev_console(&active);
@@ -918,11 +913,9 @@ int getttyname_malloc(int fd, char **ret) {
r = ttyname_r(fd, path, sizeof(path));
if (r == 0) {
const char *p;
char *c;
p = startswith(path, "/dev/");
c = strdup(p ?: path);
c = strdup(skip_dev_prefix(path));
if (!c)
return -ENOMEM;