Skip to contentSkip to conversation

tst_kvcmp: Strip double quotes when parsing /etc/os-release

PATCH Superseded Petr Vorel 2 participants 5 messages Aug 20, 2020 Library code

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Petr Vorel

author
#0 lore raw

PATCH v2 1/1 tst_kvcmp: Strip double quotes when parsing /etc/os-release

ID is normally without double quotes, e.g.: ID=debian

But at least SLES and openSUSE contain double quotes, e.g.:
ID="opensuse-tumbleweed"

thus optionally strip the double quotes after scanning them.

Fixes e2e60a39b ("lib/tst_kvercmp: Add support /etc/os-release")
---
 lib/tst_kvercmp.c | 6 ++++++
 1 file changed, 6 insertions(+)
lib/tst_kvercmp.c +6 -0
index 185a5c39c..dfd81ac83 100644
@@ -148,6 +148,12 @@ const char *tst_kvcmp_distname(const char *kver)
 	if (access(OSRELEASE_PATH, F_OK) != -1) {
 		SAFE_FILE_LINES_SCANF(NULL, OSRELEASE_PATH, "ID=%s", distname);
 
+		if (p[0] == '"')
+			memmove(p, p + 1, strlen(p));
+
+		if (p[strlen(p) - 1] == '"')
+			p[strlen(p) - 1] = '\0';
+
 		while (*p) {
 			*p = toupper((unsigned char)*p);
 			p++;
Signature · 1 line
2.28.0
#1 lore raw

PATCH v2 1/1 tst_kvcmp: Strip double quotes when parsing /etc/os-release

Hi!

lib/tst_kvercmp.c const char *tst_kvcmp_distname(const char *kver) · line 152

Quoted 10 lines
lib/tst_kvercmp.c +2 -0
index 185a5c39c..dfd81ac83 100644
@@ -148,6 +148,12 @@ const char *tst_kvcmp_distname(const char *kver)
 	if (access(OSRELEASE_PATH, F_OK) != -1) {
 		SAFE_FILE_LINES_SCANF(NULL, OSRELEASE_PATH, "ID=%s", distname);
 
+		if (p[0] == '"')
+			memmove(p, p + 1, strlen(p));

Why can't we just do distname++ and move the p = distname after this condition?

lib/tst_kvercmp.c const char *tst_kvcmp_distname(const char *kver) · line 155

Quoted 2 lines
lib/tst_kvercmp.c +2 -0
+		if (p[strlen(p) - 1] == '"')
+			p[strlen(p) - 1] = '\0';

I guess that we can move this to the while loop with:

	while (*p) {
		if (*p == '"') {
			*p = 0;
			break;
		}

		*p = ....
		p++;
	}
Quoted 6 lines
lib/tst_kvercmp.c +0 -0
 		while (*p) {
 			*p = toupper((unsigned char)*p);
 			p++;
Signature · 1 line
2.28.0
Signature · 2 lines
Cyril Hrubis
[email protected]
author
#2 lore raw

PATCH v2 1/1 tst_kvcmp: Strip double quotes when parsing /etc/os-release

Hi Cyril,

Quoted 8 lines
Quoted 7 lines
lib/tst_kvercmp.c +0 -0
index 185a5c39c..dfd81ac83 100644
@@ -148,6 +148,12 @@ const char *tst_kvcmp_distname(const char *kver)
 	if (access(OSRELEASE_PATH, F_OK) != -1) {
 		SAFE_FILE_LINES_SCANF(NULL, OSRELEASE_PATH, "ID=%s", distname);
Quoted 5 lines

lib/tst_kvercmp.c const char *tst_kvcmp_distname(const char *kver) · line 152

Quoted 2 lines
lib/tst_kvercmp.c +2 -0
+		if (p[0] == '"')
+			memmove(p, p + 1, strlen(p));

Why can't we just do distname++ and move the p = distname after this condition?

Sure.

Quoted 5 lines

lib/tst_kvercmp.c const char *tst_kvcmp_distname(const char *kver) · line 155

Quoted 2 lines
lib/tst_kvercmp.c +2 -0
+		if (p[strlen(p) - 1] == '"')
+			p[strlen(p) - 1] = '\0';

I guess that we can move this to the while loop with:

Quoted 9 lines
	while (*p) {
		if (*p == '"') {
			*p = 0;
			break;
		}

		*p = ....
		p++;
	}

OK, " shouldn't be in the middle of the character, thus safe

Thanks for the suggestions.

Kind regards,
Petr

author
#3 lore raw

PATCH v2 1/1 tst_kvcmp: Strip double quotes when parsing /etc/os-release

In reply to Cyril Hrubis

Hi Cyril,

Quoted 8 lines
Quoted 7 lines
lib/tst_kvercmp.c +0 -0
index 185a5c39c..dfd81ac83 100644
@@ -148,6 +148,12 @@ const char *tst_kvcmp_distname(const char *kver)
 	if (access(OSRELEASE_PATH, F_OK) != -1) {
 		SAFE_FILE_LINES_SCANF(NULL, OSRELEASE_PATH, "ID=%s", distname);
Quoted 5 lines

lib/tst_kvercmp.c const char *tst_kvcmp_distname(const char *kver) · line 152

Quoted 2 lines
lib/tst_kvercmp.c +2 -0
+		if (p[0] == '"')
+			memmove(p, p + 1, strlen(p));

Why can't we just do distname++ and move the p = distname after this condition?

OK, distname is char[], not a pointer, I guess I have to use memmove().

Kind regards,
Petr

#4 lore raw

PATCH v2 1/1 tst_kvcmp: Strip double quotes when parsing /etc/os-release

Hi!

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
lib/tst_kvercmp.c +0 -0
index 185a5c39c..dfd81ac83 100644
@@ -148,6 +148,12 @@ const char *tst_kvcmp_distname(const char *kver)
 	if (access(OSRELEASE_PATH, F_OK) != -1) {
 		SAFE_FILE_LINES_SCANF(NULL, OSRELEASE_PATH, "ID=%s", distname);
Quoted 6 lines
Quoted 5 lines

lib/tst_kvercmp.c const char *tst_kvcmp_distname(const char *kver) · line 152

Quoted 2 lines
lib/tst_kvercmp.c +2 -0
+		if (p[0] == '"')
+			memmove(p, p + 1, strlen(p));

Why can't we just do distname++ and move the p = distname after this condition?

OK, distname is char[], not a pointer, I guess I have to use memmove().

In that case you just need to do char *ret = distname + 1; p = ret; .... return ret;

Signature · 2 lines
Cyril Hrubis
[email protected]