Merge branch 'master' of ssh://gapil.gnulinux.it/srv/git/gapil
[gapil.git] / sources / chechmcheck.c
1 /* checkmcheck.c
2  * 
3  * Copyright (C) 2009 Simone Piccardi
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 /****************************************************************
20  *
21  * Program checkmcheck: 
22  * Some sample tests on mcheck
23  *
24  * Author: Simone Piccardi
25  * Jun. 2009
26  *
27  * Usage: errcode -h give all info's
28  *
29  ****************************************************************/
30 /* 
31  * Include needed headers
32  */
33 #include <errno.h>       /* error definitions and routines */ 
34 #include <stdlib.h>      /* C standard library */
35 #include <unistd.h>      /* unix standard library */
36 #include <stdio.h>       /* standard I/O library */
37 #include <string.h>      /* C strings library */
38 #include <mcheck.h>      /* Malloc checking library */
39
40
41 void myabort(enum mcheck_status status);
42 void printcode(enum mcheck_status status);
43
44 int main(int argc, char *argv[])
45 {
46 /* 
47  * Variables definition  
48  */
49     int ret;
50     void * ptr;
51     /*
52      */
53     ret = mcheck(myabort); 
54     ptr = malloc(1000);
55     printcode(mprobe(ptr));
56     free(ptr);
57     printcode(mprobe(ptr));
58     free(ptr);
59     printcode(mprobe(ptr));
60         
61     /* normal exit */
62     return 0;
63 }
64
65 void printcode(status)
66 {
67     switch (status) {
68     case MCHECK_DISABLED:
69         printf("Status code = MCHECK_DISABLED\n");
70         return;
71     case MCHECK_OK:
72         printf("Status code = MCHECK_OK\n");
73         return;
74     case MCHECK_HEAD:
75         printf("Status code = MCHECK_HEAD\n");
76         return;
77     case MCHECK_TAIL:
78         printf("Status code = MCHECK_TAIL\n");
79         return;
80     case MCHECK_FREE:
81         printf("Status code = MCHECK_FREE\n");
82         return;
83     default:
84         return;
85     }
86 }
87 /*
88  * routine to print code and abort
89  */
90 void myabort(enum mcheck_status status)
91 {
92     printcode(status);
93     abort();
94 }