mirror of
https://github.com/ioacademy-jikim/debugging
synced 2026-08-11 16:32:58 +00:00
first commit
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <setjmp.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define FILE_ERROR 1
|
||||
#define MEM_ERROR 2
|
||||
|
||||
jmp_buf jmpbuf;
|
||||
|
||||
|
||||
FILE * find_file(const char *fname)
|
||||
{
|
||||
FILE *f;
|
||||
|
||||
if (access(fname, F_OK))
|
||||
longjmp(jmpbuf, FILE_ERROR);
|
||||
|
||||
f = fopen(fname, "r");
|
||||
if (f == NULL)
|
||||
longjmp(jmpbuf, FILE_ERROR);
|
||||
|
||||
return f;
|
||||
}
|
||||
|
||||
void read_file(FILE *f, int size)
|
||||
{
|
||||
char *buf;
|
||||
|
||||
if (size < 0)
|
||||
longjmp(jmpbuf, MEM_ERROR);
|
||||
|
||||
buf = malloc(size);
|
||||
if (buf == NULL)
|
||||
longjmp(jmpbuf, MEM_ERROR);
|
||||
|
||||
fread(buf, size, 1, f);
|
||||
|
||||
/* do something */
|
||||
|
||||
free(buf);
|
||||
}
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int res;
|
||||
int step;
|
||||
FILE *fp;
|
||||
|
||||
/* Usage: ./a.out <filename> <bufsize> */
|
||||
|
||||
step = -1;
|
||||
if ((res = setjmp(jmpbuf)) == 0) {
|
||||
step = 0;
|
||||
fp = find_file(argv[1]);
|
||||
step = 1;
|
||||
read_file(fp, strtol(argv[2], NULL, 10));
|
||||
step = 2;
|
||||
fclose(fp);
|
||||
}
|
||||
else if (res == FILE_ERROR) {
|
||||
printf("file exception: %s\n", argv[1]);
|
||||
}
|
||||
else if (res == MEM_ERROR) {
|
||||
printf("memory exception: %s\n", argv[2]);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
printf("last step = %d\n", step);
|
||||
return res;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
|
||||
#define CATCH } else {
|
||||
#define ETRY } }while(0)
|
||||
#define THROW longjmp(ex_buf__, 1)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW;
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH
|
||||
{
|
||||
printf("Got Exception!\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0:
|
||||
#define CATCH(x) break; case x:
|
||||
#define ETRY } }while(0)
|
||||
#define THROW(x) longjmp(ex_buf__, x)
|
||||
|
||||
#define FOO_EXCEPTION (1)
|
||||
#define BAR_EXCEPTION (2)
|
||||
#define BAZ_EXCEPTION (3)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW( BAR_EXCEPTION );
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH( FOO_EXCEPTION )
|
||||
{
|
||||
printf("Got Foo!\n");
|
||||
}
|
||||
CATCH( BAR_EXCEPTION )
|
||||
{
|
||||
printf("Got Bar!\n");
|
||||
}
|
||||
CATCH( BAZ_EXCEPTION )
|
||||
{
|
||||
printf("Got Baz!\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
|
||||
#define CATCH(x) break; case x:
|
||||
#define FINALLY break; } default:
|
||||
#define ETRY } }while(0)
|
||||
#define THROW(x) longjmp(ex_buf__, x)
|
||||
|
||||
#define FOO_EXCEPTION (1)
|
||||
#define BAR_EXCEPTION (2)
|
||||
#define BAZ_EXCEPTION (3)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW( BAR_EXCEPTION );
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH( FOO_EXCEPTION )
|
||||
{
|
||||
printf("Got Foo!\n");
|
||||
}
|
||||
CATCH( BAR_EXCEPTION )
|
||||
{
|
||||
printf("Got Bar!\n");
|
||||
}
|
||||
CATCH( BAZ_EXCEPTION )
|
||||
{
|
||||
printf("Got Baz!\n");
|
||||
}
|
||||
FINALLY
|
||||
{
|
||||
printf("...et in arcadia Ego\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
|
||||
#define CATCH(x) break; case x:
|
||||
#define FINALLY break; } default:
|
||||
#define ETRY } }while(0)
|
||||
#define THROW(x) longjmp(ex_buf__, x)
|
||||
|
||||
#define FOO_EXCEPTION (1)
|
||||
#define BAR_EXCEPTION (2)
|
||||
#define BAZ_EXCEPTION (3)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW( BAR_EXCEPTION );
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH( FOO_EXCEPTION )
|
||||
{
|
||||
printf("Got Foo!\n");
|
||||
}
|
||||
CATCH( BAR_EXCEPTION )
|
||||
{
|
||||
printf("Got Bar!\n");
|
||||
}
|
||||
CATCH( BAZ_EXCEPTION )
|
||||
{
|
||||
printf("Got Baz!\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
|
||||
#define CATCH(x) break; case x:
|
||||
#define FINALLY break; } default: {
|
||||
#define ETRY } } }while(0)
|
||||
#define THROW(x) longjmp(ex_buf__, x)
|
||||
|
||||
#define FOO_EXCEPTION (1)
|
||||
#define BAR_EXCEPTION (2)
|
||||
#define BAZ_EXCEPTION (3)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW( BAR_EXCEPTION );
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH( FOO_EXCEPTION )
|
||||
{
|
||||
printf("Got Foo!\n");
|
||||
}
|
||||
CATCH( BAR_EXCEPTION )
|
||||
{
|
||||
printf("Got Bar!\n");
|
||||
}
|
||||
CATCH( BAZ_EXCEPTION )
|
||||
{
|
||||
printf("Got Baz!\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
|
||||
#define CATCH(x) break; case x:
|
||||
#define FINALLY break; } default: {
|
||||
#define ETRY } } }while(0)
|
||||
#define THROW(x) longjmp(ex_buf__, x)
|
||||
|
||||
#define FOO_EXCEPTION (1)
|
||||
#define BAR_EXCEPTION (2)
|
||||
#define BAZ_EXCEPTION (3)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW( BAZ_EXCEPTION );
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH( FOO_EXCEPTION )
|
||||
{
|
||||
printf("Got Foo!\n");
|
||||
}
|
||||
CATCH( BAR_EXCEPTION )
|
||||
{
|
||||
printf("Got Bar!\n");
|
||||
}
|
||||
CATCH( BAZ_EXCEPTION )
|
||||
{
|
||||
printf("Got Baz!\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <stdio.h>
|
||||
#include <setjmp.h>
|
||||
|
||||
#define TRY do{ jmp_buf ex_buf__; switch( setjmp(ex_buf__) ){ case 0: while(1){
|
||||
#define CATCH(x) break; case x:
|
||||
#define FINALLY break; } default: {
|
||||
#define ETRY break; } } }while(0)
|
||||
#define THROW(x) longjmp(ex_buf__, x)
|
||||
|
||||
#define FOO_EXCEPTION (1)
|
||||
#define BAR_EXCEPTION (2)
|
||||
#define BAZ_EXCEPTION (3)
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
TRY
|
||||
{
|
||||
printf("In Try Statement\n");
|
||||
THROW( BAZ_EXCEPTION );
|
||||
printf("I do not appear\n");
|
||||
}
|
||||
CATCH( FOO_EXCEPTION )
|
||||
{
|
||||
printf("Got Foo!\n");
|
||||
}
|
||||
CATCH( BAR_EXCEPTION )
|
||||
{
|
||||
printf("Got Bar!\n");
|
||||
}
|
||||
CATCH( BAZ_EXCEPTION )
|
||||
{
|
||||
printf("Got Baz!\n");
|
||||
}
|
||||
ETRY;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user