]> ruderich.org/simon Gitweb - coloredstderr/coloredstderr.git/blob - tests/example_stdio.c
update copyright year
[coloredstderr/coloredstderr.git] / tests / example_stdio.c
1 /*
2  * Test all hooked stdio.h functions.
3  *
4  * Copyright (C) 2013-2015  Simon Ruderich
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 #include <config.h>
21
22 /* For {fwrite,fputs,fputc}_unlocked(), if available. */
23 #define _GNU_SOURCE
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <stdarg.h>
28
29 #include "example.h"
30 #include "../src/compiler.h"
31
32
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);
37 }
38 #endif
39 #ifndef HAVE_FPUTS_UNLOCKED
40 static int fputs_unlocked(char const *s, FILE *stream) {
41     return fputs(s, stream);
42 }
43 #endif
44 #ifndef HAVE_FPUTC_UNLOCKED
45 static int fputc_unlocked(int c, FILE *stream) {
46     return fputc(c, stream);
47 }
48 #endif
49
50
51 static int out;
52
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;
56
57 static void test_vprintf(char const *format, ...) {
58     va_list ap;
59
60     va_start(ap, format);
61     vprintf(format, ap);
62     va_end(ap);
63 }
64 static void test_vfprintf(FILE *stream, char const *format, ...) {
65     va_list ap;
66
67     va_start(ap, format);
68     vfprintf(stream, format, ap);
69     va_end(ap);
70 }
71
72 static void NEWLINE(void) {
73     fflush(stdout);
74     fflush(stderr);
75     xwrite(out, "\n", 1);
76 }
77
78
79 int main(int argc, char **argv unused) {
80     /* stdout */
81
82     out = dup(STDOUT_FILENO);
83     if (out == -1) {
84         perror("dup");
85         return EXIT_FAILURE;
86     }
87
88     /* Redirect stdout to stderr so we can test all functions, including those
89      * not writing to stderr. */
90     xdup2(STDERR_FILENO, STDOUT_FILENO);
91
92     /* stdout redirected to stderr. */
93
94     xwrite(STDOUT_FILENO, "write()", 7); NEWLINE();
95     fwrite("fwrite()", 8, 1, stdout);   NEWLINE();
96
97     /* puts(3) */
98     fputs("fputs()", stdout); NEWLINE();
99     fputc('a', stdout);       NEWLINE();
100     putc('b', stdout);        NEWLINE();
101     putchar('c');             NEWLINE();
102     puts("puts()");           NEWLINE();
103
104     /* printf(3) */
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();
109
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();
117
118
119     /* stderr */
120
121     xwrite(STDERR_FILENO, "write()", 7); NEWLINE();
122     fwrite("fwrite()", 8, 1, stderr);   NEWLINE();
123
124     /* puts(3) */
125     fputs("fputs()", stderr); NEWLINE();
126     fputc('a', stderr);       NEWLINE();
127     putc('b', stderr);        NEWLINE();
128
129     /* printf(3) */
130     fprintf(stderr, "%s [%d]", "fprintf()", argc);        NEWLINE();
131     test_vfprintf(stderr, "%s [%d]", "vfprintf()", argc); NEWLINE();
132
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();
138
139     return EXIT_SUCCESS;
140 }