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 <kusmabite@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Erik Faye-Lund committed Dec 4, 2012 at 09:10 UTC 67fe7356538c714da9da6061abe99209452260d7
1 file changed +18 -11
compat/terminal.c
+18 -11
@@ -50,29 +50,36 @@ char *git_terminal_prompt(const char *prompt, int echo)
50 {
51 static struct strbuf buf = STRBUF_INIT;
52 int r;
53 - FILE *fh;
53 + FILE *input_fh, *output_fh;
54
55 - fh = fopen("/dev/tty", "w+");
56 - if (!fh)
55 + input_fh = fopen("/dev/tty", "r");
56 + if (!input_fh)
57 return NULL;
58
59 + output_fh = fopen("/dev/tty", "w");
60 + if (!output_fh) {
61 + fclose(input_fh);
62 + return NULL;
63 + }
64 +
65 if (!echo && disable_echo()) {
60 - fclose(fh);
66 + fclose(input_fh);
67 + fclose(output_fh);
68 return NULL;
69 }
70
64 - fputs(prompt, fh);
65 - fflush(fh);
71 + fputs(prompt, output_fh);
72 + fflush(output_fh);
73
67 - r = strbuf_getline(&buf, fh, '\n');
74 + r = strbuf_getline(&buf, input_fh, '\n');
75 if (!echo) {
69 - fseek(fh, SEEK_CUR, 0);
70 - putc('\n', fh);
71 - fflush(fh);
76 + putc('\n', output_fh);
77 + fflush(output_fh);
78 }
79
80 restore_term();
75 - fclose(fh);
81 + fclose(input_fh);
82 + fclose(output_fh);
83
84 if (r == EOF)
85 return NULL;