From 2483f50686dc6f85a5d6c92b3faff0e9d5cb366e Mon Sep 17 00:00:00 2001 From: Christoph Mallon Date: Mon, 15 Dec 2008 21:07:33 +0000 Subject: [PATCH] When automatically generating a target filename, place it in the current directory instead of the directory of the source file. Also resolve the existing bug, that the file is placed in the wrong directory with the wrong name in case that the filename has no suffix, but a directory name contains a dot. PR: #0000024 [r24696] --- main.c | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/main.c b/main.c index e45a3de..d6cba01 100644 --- a/main.c +++ b/main.c @@ -181,30 +181,24 @@ static void initialize_firm(void) static void get_output_name(char *buf, size_t buflen, const char *inputname, const char *newext) { - size_t last_dot = 0xffffffff; - size_t i = 0; - - if (inputname == NULL) { - snprintf(buf, buflen, "a%s", newext); - return; - } - - for (const char *c = inputname; *c != 0; ++c) { - if (*c == '.') - last_dot = i; - ++i; - } - if (last_dot == 0xffffffff) - last_dot = i; - - if (last_dot >= buflen) - panic("filename too long"); - memcpy(buf, inputname, last_dot); - - size_t extlen = strlen(newext) + 1; - if (extlen + last_dot >= buflen) + if (inputname == NULL) + inputname = "a"; + + char const *const last_slash = strrchr(inputname, '/'); + char const *const filename = + last_slash != NULL ? last_slash + 1 : inputname; + char const *const last_dot = strrchr(filename, '.'); + char const *const name_end = + last_dot != NULL ? last_dot : strchr(filename, '\0'); + + int const len = snprintf(buf, buflen, "%.*s%s", + (int)(name_end - filename), filename, newext); +#ifdef _WIN32 + if (len < 0 || buflen <= (size_t)len) +#else + if (buflen <= (size_t)len) +#endif panic("filename too long"); - memcpy(buf+last_dot, newext, extlen); } #include "builtins.h" -- 2.20.1