1/* Test assert_perror().
2 *
3 * This is hairier than you'd think, involving games with
4 * stdio and signals.
5 *
6 */
7
8#include <signal.h>
9#include <stdlib.h>
10#include <stdio.h>
11#include <string.h>
12#include <setjmp.h>
13
14#include <support/xstdio.h>
15
16jmp_buf rec;
17char buf[160];
18
19static void
20sigabrt (int unused)
21{
22 longjmp (rec, 1); /* recover control */
23}
24
25#undef NDEBUG
26#include <assert.h>
27static void
28assert1 (void)
29{
30 assert_perror (1);
31}
32
33static void
34assert2 (void)
35{
36 assert_perror (0);
37}
38
39#define NDEBUG
40#include <assert.h>
41static void
42assert3 (void)
43{
44 assert_perror (2);
45}
46
47int
48main(void)
49{
50 volatile int failed = 1; /* safety in presence of longjmp() */
51
52 fclose (stderr);
53 stderr = tmpfile ();
54 if (!stderr)
55 abort ();
56
57 signal (SIGABRT, handler: sigabrt);
58
59 if (!setjmp (rec))
60 assert1 ();
61 else
62 failed = 0; /* should happen */
63
64 if (!setjmp (rec))
65 assert2 ();
66 else
67 failed = 1; /* should not happen */
68
69 if (!setjmp (rec))
70 assert3 ();
71 else
72 failed = 1; /* should not happen */
73
74 rewind (stderr);
75 xfgets (s: buf, size: 160, stderr);
76 if (!strstr(buf, strerror (errnum: 1)))
77 failed = 1;
78
79 xfgets (s: buf, size: 160, stderr);
80 if (strstr (buf, strerror (errnum: 0)))
81 failed = 1;
82
83 xfgets (s: buf, size: 160, stderr);
84 if (strstr (buf, strerror (errnum: 2)))
85 failed = 1;
86
87 return failed;
88}
89

source code of glibc/assert/test-assert-perr.c