Skip to content

Commit 2ae6533

Browse files
committed
s_mp_get_token stops at the right time now
1 parent 6426c46 commit 2ae6533

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

demo/mtest_opponent.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ static char *s_mp_get_token(char *s, int size, FILE *stream)
2020
{
2121
char *s_bar = s;
2222
int c;
23+
bool eol_hit = false;
2324

2425
/* "fgets [...] reads in at most one less than size characters from stream" */
2526
while (--size) {
@@ -32,9 +33,16 @@ static char *s_mp_get_token(char *s, int size, FILE *stream)
3233
break;
3334
}
3435
/* Ignore line-breaks but keep reading to get them out of the stream-buffer */
35-
if ((c == '\n') || (c == '\r')) {
36+
if ((c == '\n') || (c == '\r')) {
37+
eol_hit = true;
3638
continue;
3739
}
40+
/* Stop reading after linebreak */
41+
if (eol_hit) {
42+
/* We already read the character after the linebreak, put it back */
43+
ungetc(c, stream);
44+
break;
45+
}
3846
*s_bar++ = c;
3947
}
4048
/* "A terminating null byte ('\0') is stored after the last character in the buffer" */
@@ -108,7 +116,6 @@ static int mtest_opponent(void)
108116
add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n,
109117
expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n);
110118
GET_TOKEN(cmd, 4095, stdin);
111-
cmd[strlen(cmd) - 1u] = '\0';
112119
printf("%-6s ]\r", cmd);
113120
fflush(stdout);
114121
if (strcmp(cmd, "mul2d") == 0) {

0 commit comments

Comments
 (0)