Back to index

glibc  2.9
tst-setjmp.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991, 1992, 1997, 1998, 2000 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <stdio.h>
00020 #include <setjmp.h>
00021 #include <stdlib.h>
00022 
00023 static jmp_buf env;
00024 static int last_value = -1, lose = 0;
00025 
00026 static void
00027 jump (int val)
00028 {
00029   longjmp (env, val);
00030 }
00031 
00032 int
00033 main (void)
00034 {
00035   int value;
00036 
00037   value = setjmp (env);
00038   if (value != last_value + 1)
00039     {
00040       fputs("Shouldn't have ", stdout);
00041       lose = 1;
00042     }
00043   last_value = value;
00044   switch (value)
00045     {
00046     case 0:
00047       puts("Saved environment.");
00048       jump (0);
00049     default:
00050       printf ("Jumped to %d.\n", value);
00051       if (value < 10)
00052        jump (value + 1);
00053     }
00054 
00055   if (!lose && value == 10)
00056     {
00057       /* Do a second test, this time without `setjmp' being a macro.
00058          This is not required by ISO C but we have this for compatibility.  */
00059 #undef setjmp
00060       extern int setjmp (jmp_buf);
00061 
00062       last_value = -1;
00063       lose = 0;
00064 
00065       value = setjmp (env);
00066       if (value != last_value + 1)
00067        {
00068          fputs("Shouldn't have ", stdout);
00069          lose = 1;
00070        }
00071       last_value = value;
00072       switch (value)
00073        {
00074        case 0:
00075          puts("Saved environment.");
00076          jump (0);
00077        default:
00078          printf ("Jumped to %d.\n", value);
00079          if (value < 10)
00080            jump (value + 1);
00081        }
00082     }
00083 
00084   if (!lose && value == 10)
00085     {
00086       /* And again for the `_setjmp' function.  */
00087 #ifndef _setjmp
00088       extern int _setjmp (jmp_buf);
00089 #endif
00090       last_value = -1;
00091       lose = 0;
00092 
00093       value = _setjmp (env);
00094       if (value != last_value + 1)
00095        {
00096          fputs("Shouldn't have ", stdout);
00097          lose = 1;
00098        }
00099       last_value = value;
00100       switch (value)
00101        {
00102        case 0:
00103          puts("Saved environment.");
00104          jump (0);
00105        default:
00106          printf ("Jumped to %d.\n", value);
00107          if (value < 10)
00108            jump (value + 1);
00109        }
00110     }
00111 
00112   if (lose || value != 10)
00113     puts ("Test FAILED!");
00114   else
00115     puts ("Test succeeded!");
00116 
00117   return lose ? EXIT_FAILURE : EXIT_SUCCESS;
00118 }