[LTP] [PATCH 1/4] lib/tst_tmpdir: Normalize user defined TMPDIR

Cyril Hrubis chrubis@suse.cz
Fri Feb 23 16:39:32 CET 2024


Hi!
> +	if (env_tmpdir[strlen(env_tmpdir)-1] == '/') {
> +		env_tmpdir[strlen(env_tmpdir)-1] = '\0';
> +		fixed = 1;
> +	}
> +
> +	while ((p = strstr(env_tmpdir, "//")) != NULL) {
> +		memmove(p, p + 1, strlen(env_tmpdir) - (p - env_tmpdir));
> +		fixed = 1;
> +	}
> +
> +	if (fixed) {
> +		tst_resm(TINFO, "WARNING: Remove double or trailing slashes from TMPDIR,"
> +			 " please fix your setup to: TMPDIR='%s'",
> +			 env_tmpdir);
> +	}
> +

This whole thing can be just a single loop (beware untested):

	size_t s = 0, d = 0;
	char prev_c = 0;

	for (;;) {
		switch (env_tmpdir[s]) {
		case '/':
			if (prev_c != '/')
				d++;
			s++;
		break;
		case '\0':
			if (d && prev_c == '/')
				env_tmpdir[d-1] = '\0';
			break;
		break;
		default:
			env_tmpdir[d++] = env_tmpdir[s++];
		}
	}

-- 
Cyril Hrubis
chrubis@suse.cz


More information about the ltp mailing list