2 * Test all hooked stdio.h functions.
4 * Copyright (C) 2013 Simon Ruderich
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* For {fwrite,fputs,fputc}_unlocked(), if available. */
30 #include "../src/compiler.h"
33 /* These are not in POSIX. */
34 #ifndef HAVE_FWRITE_UNLOCKED
35 static size_t fwrite_unlocked(void const *ptr, size_t size, size_t n, FILE *stream) {
36 return fwrite(ptr, size, n, stream);
39 #ifndef HAVE_FPUTS_UNLOCKED
40 static int fputs_unlocked(char const *s, FILE *stream) {
41 return fputs(s, stream);
44 #ifndef HAVE_FPUTC_UNLOCKED
45 static int fputc_unlocked(int c, FILE *stream) {
46 return fputc(c, stream);
53 static void test_vprintf(char const *format, ...) noinline;
54 static void test_vfprintf(FILE *stream, char const *format, ...) noinline;
55 static void NEWLINE(void) noinline;
57 static void test_vprintf(char const *format, ...) {
64 static void test_vfprintf(FILE *stream, char const *format, ...) {
68 vfprintf(stream, format, ap);
72 static void NEWLINE(void) {
79 int main(int argc, char **argv unused) {
82 out = dup(STDOUT_FILENO);
88 /* Redirect stdout to stderr so we can test all functions, including those
89 * not writing to stderr. */
90 xdup2(STDERR_FILENO, STDOUT_FILENO);
92 /* stdout redirected to stderr. */
94 xwrite(STDOUT_FILENO, "write()", 7); NEWLINE();
95 fwrite("fwrite()", 8, 1, stdout); NEWLINE();
98 fputs("fputs()", stdout); NEWLINE();
99 fputc('a', stdout); NEWLINE();
100 putc('b', stdout); NEWLINE();
101 putchar('c'); NEWLINE();
102 puts("puts()"); NEWLINE();
105 printf("%s [%d]", "printf()", argc); NEWLINE();
106 fprintf(stdout, "%s [%d]", "fprintf()", argc); NEWLINE();
107 test_vprintf("%s [%d]", "vprintf()", argc); NEWLINE();
108 test_vfprintf(stdout, "%s [%d]", "vfprintf()", argc); NEWLINE();
110 /* unlocked_stdio(3) */
111 fwrite_unlocked("fwrite_unlocked()", 17, 1, stdout); NEWLINE();
112 fputs_unlocked("fputs_unlocked()", stdout); NEWLINE();
113 /* FIXME: 'x', 'y' and 'z' are not colored */
114 fputc_unlocked('x', stdout); NEWLINE();
115 putc_unlocked('y', stdout); NEWLINE();
116 putchar_unlocked('z'); NEWLINE();
121 xwrite(STDERR_FILENO, "write()", 7); NEWLINE();
122 fwrite("fwrite()", 8, 1, stderr); NEWLINE();
125 fputs("fputs()", stderr); NEWLINE();
126 fputc('a', stderr); NEWLINE();
127 putc('b', stderr); NEWLINE();
130 fprintf(stderr, "%s [%d]", "fprintf()", argc); NEWLINE();
131 test_vfprintf(stderr, "%s [%d]", "vfprintf()", argc); NEWLINE();
133 /* unlocked_stdio(3) */
134 fwrite_unlocked("fwrite_unlocked()", 17, 1, stderr); NEWLINE();
135 fputs_unlocked("fputs_unlocked()", stderr); NEWLINE();
136 fputc_unlocked('x', stderr); NEWLINE();
137 putc_unlocked('y', stderr); NEWLINE();