1 /* Test that assigning to stdin redirects input (bug 24153).
2 Copyright (C) 2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* Prevent getchar -> getc inline expansion. */
21 #pragma GCC optimize ("O0")
27 #include <support/check.h>
28 #include <support/temp_file.h>
29 #include <support/xstdio.h>
30 #include <support/xunistd.h>
34 call_vscanf (const char *format
, ...)
37 va_start (ap
, format
);
38 int ret
= vscanf (format
, ap
);
44 call_vwscanf (const wchar_t *format
, ...)
47 va_start (ap
, format
);
48 int ret
= vwscanf (format
, ap
);
54 narrow (const char *path
)
56 FILE *old_stdin
= stdin
;
57 stdin
= xfopen (path
, "r");
59 TEST_COMPARE (getchar (), 'a');
60 TEST_COMPARE (getchar_unlocked (), 'b');
62 TEST_COMPARE (scanf ("%c", &ch
), 1);
63 TEST_COMPARE (ch
, 'c');
64 TEST_COMPARE (call_vscanf ("%c", &ch
), 1);
65 TEST_COMPARE (ch
, 'd');
67 memset (buf
, 'X', sizeof (buf
));
69 /* Legacy interface. */
70 extern char *gets (char *);
71 TEST_VERIFY (gets (buf
) == buf
);
72 TEST_COMPARE_BLOB (buf
, sizeof (buf
), "ef\0XXXXX", sizeof (buf
));
79 wide (const char *path
)
81 FILE *old_stdin
= stdin
;
82 stdin
= xfopen (path
, "r");
84 TEST_COMPARE (getwchar (), L
'a');
85 TEST_COMPARE (getwchar_unlocked (), L
'b');
87 TEST_COMPARE (wscanf (L
"%lc", &ch
), 1);
88 TEST_COMPARE (ch
, L
'c');
89 TEST_COMPARE (call_vwscanf (L
"%lc", &ch
), 1);
90 TEST_COMPARE (ch
, L
'd');
101 int fd
= create_temp_file ("tst-bz24153-", &path
);
102 TEST_VERIFY_EXIT (fd
>= 0);
103 xwrite (fd
, "abcdef", strlen ("abcdef"));
114 #include <support/test-driver.c>