From f152bea8d8722220ec33bf70a0d0f20ac8262fc9 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Tue, 26 Feb 2019 16:04:17 +0800 Subject: [PATCH] Trim dev name --- native/jni/core/init.cpp | 2 +- native/jni/utils/include/utils.h | 1 + native/jni/utils/misc.cpp | 8 ++++++++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/native/jni/core/init.cpp b/native/jni/core/init.cpp index 5b958a549..926d59615 100644 --- a/native/jni/core/init.cpp +++ b/native/jni/core/init.cpp @@ -205,7 +205,7 @@ static bool read_fstab_dt(const struct cmdline *cmd, const char *mnt_point, char if ((fd = xopen(buf, O_RDONLY | O_CLOEXEC)) >= 0) { read(fd, buf, sizeof(buf)); close(fd); - char *name = strrchr(buf, '/') + 1; + char *name = rtrim(strrchr(buf, '/') + 1); sprintf(partname, "%s%s", name, strend(name, cmd->slot) ? cmd->slot : ""); sprintf(buf, "%s/fstab/%s/type", cmd->dt_dir, mnt_point); if ((fd = xopen(buf, O_RDONLY | O_CLOEXEC)) >= 0) { diff --git a/native/jni/utils/include/utils.h b/native/jni/utils/include/utils.h index 936b6118b..ddd0a8ad6 100644 --- a/native/jni/utils/include/utils.h +++ b/native/jni/utils/include/utils.h @@ -79,6 +79,7 @@ int fork_dont_care(); int fork_no_zombie(); void gen_rand_str(char *buf, int len); int strend(const char *s1, const char *s2); +char *rtrim(char *str); void init_argv0(int argc, char **argv); void set_nice_name(const char *name); diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index f39fcb0d5..3dbc2b8ca 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -222,3 +222,11 @@ bool ends_with(const std::string_view &s1, const std::string_view &s2) { unsigned l2 = s2.length(); return l1 < l2 ? false : s1.compare(l1 - l2, l2, s2) == 0; } + +char *rtrim(char *str) { + int len = strlen(str); + while (len > 0 && str[len - 1] == ' ') + --len; + str[len] = '\0'; + return str; +}