Back to index

glibc  2.9
test-assert-perr.c
Go to the documentation of this file.
00001 /* Test assert_perror().
00002  *
00003  * This is hairier than you'd think, involving games with
00004  * stdio and signals.
00005  *
00006  */
00007 
00008 #include <signal.h>
00009 #include <stdlib.h>
00010 #include <stdio.h>
00011 #include <string.h>
00012 #include <setjmp.h>
00013 
00014 jmp_buf rec;
00015 char buf[160];
00016 
00017 static void
00018 sigabrt (int unused)
00019 {
00020   longjmp (rec, 1);  /* recover control */
00021 }
00022 
00023 #undef NDEBUG
00024 #include <assert.h>
00025 static void
00026 assert1 (void)
00027 {
00028   assert_perror (1);
00029 }
00030 
00031 static void
00032 assert2 (void)
00033 {
00034   assert_perror (0);
00035 }
00036 
00037 #define NDEBUG
00038 #include <assert.h>
00039 static void
00040 assert3 (void)
00041 {
00042   assert_perror (2);
00043 }
00044 
00045 int
00046 main(void)
00047 {
00048   volatile int failed = 1;  /* safety in presence of longjmp() */
00049 
00050   fclose (stderr);
00051   stderr = tmpfile ();
00052   if (!stderr)
00053     abort ();
00054 
00055   signal (SIGABRT, sigabrt);
00056 
00057   if (!setjmp (rec))
00058     assert1 ();
00059   else
00060     failed = 0;  /* should happen */
00061 
00062   if (!setjmp (rec))
00063     assert2 ();
00064   else
00065     failed = 1; /* should not happen */
00066 
00067   if (!setjmp (rec))
00068     assert3 ();
00069   else
00070     failed = 1; /* should not happen */
00071 
00072   rewind (stderr);
00073   fgets (buf, 160, stderr);
00074   if (!strstr(buf, strerror (1)))
00075     failed = 1;
00076 
00077   fgets (buf, 160, stderr);
00078   if (strstr (buf, strerror (0)))
00079     failed = 1;
00080 
00081   fgets (buf, 160, stderr);
00082   if (strstr (buf, strerror (2)))
00083     failed = 1;
00084 
00085   return failed;
00086 }