mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-28 22:44:24 +01:00
pty: polish args join code
This commit is contained in:
46
src/pty.c
46
src/pty.c
@@ -187,35 +187,6 @@ bool conpty_init() {
|
||||
return true;
|
||||
}
|
||||
|
||||
// convert argv to cmdline for CreateProcessW
|
||||
static WCHAR *join_args(char **argv) {
|
||||
char *args = NULL;
|
||||
char **ptr = argv;
|
||||
for (; *ptr; ptr++) {
|
||||
char *quoted = (char *) quote_arg(*ptr);
|
||||
size_t arg_len = args == NULL ? 1 : strlen(args) + 1;
|
||||
size_t quoted_len = strlen(quoted);
|
||||
args = xrealloc(args, arg_len + quoted_len);
|
||||
if (arg_len == 1) memset(args, 0, 2);
|
||||
if (arg_len != 1) strcat(args, " ");
|
||||
strncat(args, quoted, quoted_len);
|
||||
if (quoted != *ptr) free(quoted);
|
||||
}
|
||||
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, args, -1, NULL, 0);
|
||||
if (len <= 0) goto failed;
|
||||
WCHAR *ws = (WCHAR *) xmalloc(len * sizeof(WCHAR));
|
||||
if (len != MultiByteToWideChar(CP_UTF8, 0, args, -1, ws, len)) {
|
||||
free(ws);
|
||||
goto failed;
|
||||
}
|
||||
return ws;
|
||||
|
||||
failed:
|
||||
if (args != NULL) free(args);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static WCHAR *to_utf16(char *str) {
|
||||
int len = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
|
||||
if (len <= 0) return NULL;
|
||||
@@ -228,6 +199,23 @@ static WCHAR *to_utf16(char *str) {
|
||||
return wstr;
|
||||
}
|
||||
|
||||
// convert argv to cmdline for CreateProcessW
|
||||
static WCHAR *join_args(char **argv) {
|
||||
char args[256] = {0};
|
||||
char **ptr = argv;
|
||||
for (; *ptr; ptr++) {
|
||||
char *quoted = (char *) quote_arg(*ptr);
|
||||
size_t arg_len = strlen(args) + 1;
|
||||
size_t quoted_len = strlen(quoted);
|
||||
if (arg_len == 1) memset(args, 0, 2);
|
||||
if (arg_len != 1) strcat(args, " ");
|
||||
strncat(args, quoted, quoted_len);
|
||||
if (quoted != *ptr) free(quoted);
|
||||
}
|
||||
if (args[255] != '\0') args[255] = '\0'; // truncate
|
||||
return to_utf16(args);
|
||||
}
|
||||
|
||||
static bool conpty_setup(HPCON *hnd, COORD size, STARTUPINFOEXW *si_ex, char **in_name, char **out_name) {
|
||||
static int count = 0;
|
||||
char buf[256];
|
||||
|
||||
Reference in New Issue
Block a user