https://git.reactos.org/?p=reactos.git;a=commitdiff;h=aae161d06184383e3ed06…
commit aae161d06184383e3ed06a85918c02ef47f77526
Author: Jérôme Gardou <jerome.gardou(a)reactos.org>
AuthorDate: Wed Jun 23 16:26:50 2021 +0200
Commit: Jérôme Gardou <zefklop(a)users.noreply.github.com>
CommitDate: Thu Jun 24 15:03:36 2021 +0200
[CMD] Do not use an intermediate buffer when reading lines from batch files
This is easier on the heap and improves cmd:batch winetest nicely
---
base/shell/cmd/batch.c | 41 ++++++++++++-----------------------------
1 file changed, 12 insertions(+), 29 deletions(-)
diff --git a/base/shell/cmd/batch.c b/base/shell/cmd/batch.c
index 8fc6bf8466e..f20028af23f 100644
--- a/base/shell/cmd/batch.c
+++ b/base/shell/cmd/batch.c
@@ -75,7 +75,6 @@ BOOL bEcho = TRUE; /* The echo flag */
/* Buffer for reading Batch file lines */
TCHAR textline[BATCH_BUFFSIZE];
-
/*
* Returns a pointer to the n'th parameter of the current batch file.
* If no such parameter exists returns pointer to empty string.
@@ -527,47 +526,31 @@ VOID AddBatchRedirection(REDIRECTION **RedirList)
*/
BOOL BatchGetString(LPTSTR lpBuffer, INT nBufferLength)
{
- LPSTR lpString;
INT len = 0;
-#ifdef _UNICODE
- lpString = cmd_alloc(nBufferLength);
- if (!lpString)
- {
- WARN("Cannot allocate memory for lpString\n");
- error_out_of_memory();
- return FALSE;
- }
-#else
- lpString = lpBuffer;
-#endif
+
/* read all chars from memory until a '\n' is encountered */
if (bc->mem)
{
- for (; (bc->mempos < bc->memsize && len <
(nBufferLength-1)); len++)
- {
- lpString[len] = bc->mem[bc->mempos++];
- if (lpString[len] == '\n' )
+ for (; ((bc->mempos + len) < bc->memsize && len <
(nBufferLength-1)); len++)
+ {
+#ifndef _UNICODE
+ lpBuffer[len] = bc->mem[bc->mempos + len];
+#endif
+ if (bc->mem[bc->mempos + len] == '\n')
{
len++;
break;
}
}
- }
-
- if (!len)
- {
#ifdef _UNICODE
- cmd_free(lpString);
+ nBufferLength = MultiByteToWideChar(OutputCodePage, 0,
&bc->mem[bc->mempos], len, lpBuffer, nBufferLength);
+ lpBuffer[nBufferLength] = L'\0';
+ lpBuffer[len] = '\0';
#endif
- return FALSE;
+ bc->mempos += len;
}
- lpString[len++] = '\0';
-#ifdef _UNICODE
- MultiByteToWideChar(OutputCodePage, 0, lpString, -1, lpBuffer, len);
- cmd_free(lpString);
-#endif
- return TRUE;
+ return len != 0;
}
/*