Add missing -lpthread to some tests
[ust.git] / tests / tap.c
CommitLineData
48beefc9
NC
1/* Copyright (C) 2010 Nils Carlson
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17#include <pthread.h>
18#include <stdarg.h>
19#include <stdio.h>
20#include <unistd.h>
fbae86d6 21#include <string.h>
48beefc9
NC
22
23static int tap_planned = -1;
24static int tap_count = 1;
25static int tap_passed = 0;
26
27static pthread_t stdout_thread;
28static int pipefd[2];
29static FILE *pipe_r_file;
30static FILE *normal_stdout;
31
32static void *_tap_comment_stdout(void *_unused)
33{
34 char line[4096];
35
36 while (fgets(&line[0], 4096, pipe_r_file)) {
37 if (strncmp(line, "_TAP", 4)) {
38 fprintf(normal_stdout, "# %s", line);
39 } else {
e2b46575 40 fprintf(normal_stdout, "# %s", &line[4]);
48beefc9
NC
41 }
42 }
43 pthread_exit(0);
44}
45
46static void tap_comment_stdout(void)
47{
48 int stdout_fileno, new_stdout, result, fd;
49
50 if (pipe(pipefd) < 0) {
51 perror("# Failed to open pipe");
52 return;
53 }
54
55 pipe_r_file = fdopen(pipefd[0], "r");
56 if (!pipe_r_file) {
57 perror("# Couldn't create a FILE from the pipe");
58 goto close_pipe;
59 }
60
61 stdout_fileno = fileno(stdout);
62 if (stdout_fileno < 0) {
63 perror("# Couldn't get fileno for stdout!?");
64 goto close_pipe_r_file;
65 }
66
67 new_stdout = dup(stdout_fileno);
68 if (new_stdout < 0) {
69 perror("# Couldn't dup stdout");
70 goto close_pipe_r_file;
71 }
72
73 normal_stdout = fdopen(new_stdout, "w");
74 if (!normal_stdout) {
75 perror("# Could create a FILE from new_stdout");
76 goto close_dup_stdout;
77 }
78
79 result = pthread_create(&stdout_thread, NULL,
80 _tap_comment_stdout, NULL);
81 if (result < 0) {
82 perror("# Couldn't start stdout_thread");
83 goto close_normal_stdout;
84 }
85
86 fclose(stdout);
87 fclose(stderr);
88
89 fd = dup(pipefd[1]);
90 if (fd != STDOUT_FILENO) {
91 fprintf(stderr, "# Failed to open a new stdout!\n");
92 goto close_normal_stdout;
93 }
94
95 stdout = fdopen(fd, "w");
96 if (!stdout) {
97 perror("Couldn't open a new stdout");
98 goto close_fd;
99 }
100
101 fd = dup(pipefd[1]);
102 if (fd != STDERR_FILENO) {
103 fprintf(stderr, "# Failed to open a new stderr!\n");
104 goto close_fd;
105 }
106
107 stderr = fdopen(fd, "w");
108 if (!stderr) {
109 perror("Couldn't open a new stderr");
110 goto close_fd;
111 }
112
113 setlinebuf(stdout);
114 setlinebuf(stderr);
115 setlinebuf(pipe_r_file);
116
117 return;
118
119close_fd:
120 close(fd);
121
122close_normal_stdout:
123 fclose(normal_stdout);
124
125close_dup_stdout:
126 close(new_stdout);
127
128close_pipe_r_file:
129 fclose(pipe_r_file);
130
131close_pipe:
132 close(pipefd[0]);
133 close(pipefd[1]);
134
135 return;
136}
137
138void tap_plan(int count)
139{
140 printf("1..%d\n", count);
141
142 tap_count = 1;
143 tap_planned = count;
144
145 tap_comment_stdout();
146
147}
148
149int tap_status(void)
150{
151 if (tap_passed == tap_planned) {
152 return 0;
153 } else {
154 return 1;
155 }
156}
157
158void tap_ok(int bool, const char *format, ...)
159{
160 va_list args;
161 char *ok_string = "_TAPok";
162 char *not_ok_string = "_TAPnot ok";
163 char string[4000];
164
165 va_start(args, format);
166 vsprintf(string, format, args);
167 va_end(args);
168
169 printf("%s %d - %s\n", bool ? ok_string : not_ok_string,
170 tap_count++, string);
171
172 if (bool)
173 tap_passed++;
174}
This page took 0.02846 seconds and 4 git commands to generate.