]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
fix cmd exec a file without ending newline by adding one after read
authorNaitLee <naitli@foxmail.com>
Tue, 11 Jul 2023 01:17:57 +0000 (09:17 +0800)
committerNaitLee <naitli@foxmail.com>
Tue, 11 Jul 2023 01:17:57 +0000 (09:17 +0800)
Signed-off-by: NaitLee <naitli@foxmail.com>
cmd.c

diff --git a/cmd.c b/cmd.c
index c9b2cd5d8b0fc8d172d69685c59880122518d1d7..684be183a5a77ad4c01d489920fdb68f71ad043e 100644 (file)
--- a/cmd.c
+++ b/cmd.c
@@ -605,6 +605,7 @@ static void Cmd_StuffCmds_f (cmd_state_t *cmd)
 static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
 {
        char *f;
+       long int fsize;
        size_t filenameLen = strlen(filename);
        qbool isdefaultcfg =
                !strcmp(filename, "default.cfg") ||
@@ -617,7 +618,7 @@ static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
                        return; // don't execute config.cfg
        }
 
-       f = (char *)FS_LoadFile (filename, tempmempool, false, NULL);
+       f = (char *)FS_LoadFile (filename, tempmempool, false, &fsize);
        if (!f)
        {
                Con_Printf("couldn't exec %s\n",filename);
@@ -625,6 +626,14 @@ static void Cmd_Exec(cmd_state_t *cmd, const char *filename)
        }
        Con_Printf("execing %s\n",filename);
 
+       if (f[fsize - 1] != '\n')
+       {
+               f = (char *)Mem_Realloc(cbuf_mempool, f, fsize + 2);
+               f[fsize] = '\n';
+               f[fsize + 1] = '\0';
+               ++fsize;
+       }
+
        // if executing default.cfg for the first time, lock the cvar defaults
        // it may seem backwards to insert this text BEFORE the default.cfg
        // but Cbuf_InsertText inserts before, so this actually ends up after it.