[LTP] [PATCH 1/1] tst_tmpdir: Remove possible double/trailing slashes from TMPDIR

Petr Vorel pvorel@suse.cz
Thu Apr 13 13:14:34 CEST 2023


Similarly to previous commit, which normalized TMPDIR for shell API,
do the same for C API.

Unlike for shell API, here we modify $TMPDIR directly, because
tst_get_tmpdir_root() is used o more places.

Signed-off-by: Petr Vorel <pvorel@suse.cz>
---
Follow up of the same change in shell API:
https://lore.kernel.org/ltp/20230412073953.1983857-1-pvorel@suse.cz/

Kind regards,
Petr

 lib/tst_tmpdir.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/tst_tmpdir.c b/lib/tst_tmpdir.c
index b73b5c66f..8db5c47e8 100644
--- a/lib/tst_tmpdir.c
+++ b/lib/tst_tmpdir.c
@@ -124,16 +124,28 @@ char *tst_get_tmpdir(void)
 
 const char *tst_get_tmpdir_root(void)
 {
-	const char *env_tmpdir = getenv("TMPDIR");
+	char *env_tmpdir = getenv("TMPDIR");
 
-	if (!env_tmpdir)
+	if (env_tmpdir) {
+		/* remove duplicate slashes */
+		for (char *p = env_tmpdir, *q = env_tmpdir; *q;) {
+			if (*++q != '/' || *p != '/')
+				*++p = *q;
+		}
+		/* Remove slash on the last place  */
+		size_t last = strlen(env_tmpdir)-1;
+		if (env_tmpdir[last] == '/')
+			env_tmpdir[last] = '\0';
+	} else {
 		env_tmpdir = TEMPDIR;
+	}
 
 	if (env_tmpdir[0] != '/') {
 		tst_brkm(TBROK, NULL, "You must specify an absolute "
 				"pathname for environment variable TMPDIR");
 		return NULL;
 	}
+
 	return env_tmpdir;
 }
 
-- 
2.40.0



More information about the ltp mailing list