xkb: don't require a trailing slash for the XKM output dir

Rework the function to use a single snprintf call instead of a mix of
strcpy/strcats. This now also appends a trailing slash where needed so we
don't rely on the build system to set this for us.

Also, since /tmp/ is the fallback and we never check if everything succeeded,
assert if we can't use /tmp/. This will never be triggered anyway, the only
caller to OutputDirectory() uses sizeof(PATH_MAX-sized array).

Follow-up from 6c51818a0f

Signed-off-by: Peter Hutterer <peter.hutterer@who-t.net>
This commit is contained in:
Peter Hutterer 2021-03-18 09:44:53 +10:00 committed by Povilas Kanapickas
parent eceafd4a2d
commit f6b8f8c071
1 changed files with 16 additions and 11 deletions

View File

@ -62,22 +62,27 @@ LoadXKM(unsigned want, unsigned need, const char *keymap, XkbDescPtr *xkbRtrn);
static void
OutputDirectory(char *outdir, size_t size)
{
const char *directory = NULL;
const char *pathsep = "";
int r = -1;
#ifndef WIN32
/* Can we write an xkm and then open it too? */
if (access(XKM_OUTPUT_DIR, W_OK | X_OK) == 0 &&
(strlen(XKM_OUTPUT_DIR) < size)) {
(void) strcpy(outdir, XKM_OUTPUT_DIR);
if (access(XKM_OUTPUT_DIR, W_OK | X_OK) == 0) {
directory = XKM_OUTPUT_DIR;
if (XKM_OUTPUT_DIR[strlen(XKM_OUTPUT_DIR) - 1] != '/')
pathsep = "/";
}
else
#else
if (strlen(Win32TempDir()) + 1 < size) {
(void) strcpy(outdir, Win32TempDir());
(void) strcat(outdir, "\\");
}
else
directory = Win32TempDir();
pathsep = "\\";
#endif
if (strlen("/tmp/") < size) {
(void) strcpy(outdir, "/tmp/");
if (directory)
r = snprintf(outdir, size, "%s%s", directory, pathsep);
if (r < 0 || r >= size) {
assert(strlen("/tmp/") < size);
strcpy(outdir, "/tmp/");
}
}