Skip to content

Commit 67fe735

Browse files
kusmagitster
authored andcommitted
compat/terminal: separate input and output handles
On Windows, the terminal cannot be opened in read-write mode, so we need distinct pairs for reading and writing. Since this works fine on other platforms as well, always open them in pairs. Signed-off-by: Erik Faye-Lund <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9df92e6 commit 67fe735

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

compat/terminal.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,29 +50,36 @@ char *git_terminal_prompt(const char *prompt, int echo)
5050
{
5151
static struct strbuf buf = STRBUF_INIT;
5252
int r;
53-
FILE *fh;
53+
FILE *input_fh, *output_fh;
5454

55-
fh = fopen("/dev/tty", "w+");
56-
if (!fh)
55+
input_fh = fopen("/dev/tty", "r");
56+
if (!input_fh)
5757
return NULL;
5858

59+
output_fh = fopen("/dev/tty", "w");
60+
if (!output_fh) {
61+
fclose(input_fh);
62+
return NULL;
63+
}
64+
5965
if (!echo && disable_echo()) {
60-
fclose(fh);
66+
fclose(input_fh);
67+
fclose(output_fh);
6168
return NULL;
6269
}
6370

64-
fputs(prompt, fh);
65-
fflush(fh);
71+
fputs(prompt, output_fh);
72+
fflush(output_fh);
6673

67-
r = strbuf_getline(&buf, fh, '\n');
74+
r = strbuf_getline(&buf, input_fh, '\n');
6875
if (!echo) {
69-
fseek(fh, SEEK_CUR, 0);
70-
putc('\n', fh);
71-
fflush(fh);
76+
putc('\n', output_fh);
77+
fflush(output_fh);
7278
}
7379

7480
restore_term();
75-
fclose(fh);
81+
fclose(input_fh);
82+
fclose(output_fh);
7683

7784
if (r == EOF)
7885
return NULL;

0 commit comments

Comments
 (0)