Modify to trim string in get_upgrade_state() 44/313244/1 accepted/tizen/8.0/unified/20240624.160042
authorSangYoun Kwak <sy.kwak@samsung.com>
Thu, 20 Jun 2024 08:56:02 +0000 (17:56 +0900)
committerSangYoun Kwak <sy.kwak@samsung.com>
Thu, 20 Jun 2024 08:56:02 +0000 (17:56 +0900)
To make upgrade state getter work even if there are whitespaces before
and after the upgrade state string.

Change-Id: I5aa08651110891a0dce3b5fbdaf72cfe427c7d3a
Signed-off-by: SangYoun Kwak <sy.kwak@samsung.com>
hw/board/board.c

index 91616cf6b73e148b5339e65973845d68302fe6c2..c396ff141e510779e43e03a9160f4814c78296de 100644 (file)
@@ -24,6 +24,7 @@
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
+#include <ctype.h>
 
 #include </hal/include/device/hal-backend-common.h>
 #include <libsyscommon/file.h>
@@ -291,6 +292,34 @@ static int set_upgrade_state(char *state)
        return sys_set_str(UPGRADE_STATE_PATH, state);
 }
 
+static void trim_str(char *str)
+{
+       if (str == NULL)
+               return;
+
+       size_t str_len = strlen(str);
+       if (str_len == 0)
+               return;
+
+       size_t start_index = 0;
+       size_t end_index = str_len;
+
+       while (start_index < end_index && isspace(str[start_index]))
+               ++start_index;
+       while (start_index < end_index && isspace(str[end_index - 1]))
+               --end_index;
+
+       if (start_index == 0) {
+               str[end_index] = '\0';
+               return;
+       }
+
+       for (int i = 0; (start_index + i) < end_index; ++i)
+               str[i] = str[start_index + i];
+
+       str[end_index - start_index] = '\0';
+}
+
 static int get_upgrade_state(char *buffer, const int max_len)
 {
        int ret = 0;
@@ -305,6 +334,8 @@ static int get_upgrade_state(char *buffer, const int max_len)
        if (ret < 0)
                return ret;
 
+       trim_str(buffer);
+
        return 0;
 }