summaryrefslogtreecommitdiff
path: root/dmake/dbug/malloc/mallopt.c
blob: d70daf88647ae9bdee9503a0994b92fa4fdcfd61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * (c) Copyright 1990 Conor P. Cahill (uunet!virtech!cpcahil).
 * You may copy, distribute, and use this software as long as this
 * copyright statement is not removed.
 */
#include <stdio.h>
#include <fcntl.h>
#include "malloc.h"

/*
 * Function:    mallopt()
 *
 * Purpose: to set options for the malloc debugging library
 *
 * Arguments:   none
 *
 * Returns: nothing of any value
 *
 * Narrative:
 *
 */

#ifndef lint
static
char rcs_hdr[] = "$Id: mallopt.c,v 1.2 2006-07-25 10:09:05 rt Exp $";
#endif

int
mallopt(cmd,value)
    int           cmd;
    union malloptarg      value;
{
    int           i;
    extern int        malloc_checking;
    extern char     * malloc_data_start;
    extern int        malloc_errfd;
    extern int        malloc_fatal_level;
    void              malloc_init();
    extern int        malloc_warn_level;
    register char       * s;

    /*
      * If not initialized...
     */
    if( malloc_data_start == (char *) 0)
    {
        malloc_init();
    }


    switch(cmd)
    {
        case MALLOC_WARN:
            malloc_warn_level = value.i;
            break;

        case MALLOC_FATAL:
            malloc_fatal_level = value.i;
            break;

        case MALLOC_CKCHAIN:
            malloc_checking = value.i;
            break;

        case MALLOC_ERRFILE:

            i = open(value.str,O_CREAT|O_APPEND|O_WRONLY,0666);
            if( i == -1 )
            {
                (void) write(2,
                      "Unable to open malloc error file: ",
                      (unsigned) 34);
                for(s=value.str; *s; s++)
                {
                    /* do nothing */;
                }
                (void) write(2,value.str,
                         (unsigned)(s-value.str));
                (void) write(2,"\n",(unsigned)1);
            }
            else
            {
                if( malloc_errfd != 2 )
                {
                    (void) close(malloc_errfd);
                }
                malloc_errfd = i;
            }

            break;

        default:
            return(1);
    }

    return(0);
}