summaryrefslogtreecommitdiff
path: root/alloc.c
blob: 78f5c5759c2989d47c52faea5150b3bc13ab1f6e (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 *  CU sudo version 1.5.8
 *  Copyright (c) 1999 Todd C. Miller <Todd.Miller@courtesan.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please send bugs, changes, problems to sudo-bugs@courtesan.com
 *
 *******************************************************************
 *
 *  This module contains memory allocation routines used by sudo.
 *
 *  Todd C. Miller <Todd.Miller@courtesan.com> Fri Jun  3 18:32:19 MDT 1994
 */

#include "config.h"

#include <stdio.h>
#ifdef STDC_HEADERS
#include <stdlib.h>
#endif /* STDC_HEADERS */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
#include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */

#include "compat.h"

#ifndef STDC_HEADERS
#ifndef __GNUC__		/ *gcc has its own malloc */
extern VOID *malloc	__P((size_t));
#endif /* __GNUC__ */
extern char *strdup	__P((const char *));
#endif /* !STDC_HEADERS */

extern char **Argv;		/* from sudo.c */

#ifndef lint
static const char rcsid[] = "$Sudo$";
#endif /* lint */


/**********************************************************************
 *
 * emalloc()
 *
 *  emalloc() calls the system malloc(3) and exits with an error if
 *  malloc(3) fails.
 */

VOID *emalloc(size)
    size_t size;
{
    VOID *ret;

    if ((ret = malloc(size)) == NULL) {
	(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
	exit(1);
    }
    return(ret);
}

/**********************************************************************
 *
 * erealloc()
 *
 *  erealloc() calls the system realloc(3) and exits with an error if
 *  realloc(3) fails.
 */

VOID *erealloc(ptr, size)
    VOID *ptr;
    size_t size;
{

    if ((ptr = ptr ? realloc(ptr, size) : malloc(size)) == NULL) {
	(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
	exit(1);
    }
    return(ptr);
}

/**********************************************************************
 *
 * estrdup()
 *
 *  estrdup() calls the system strdup(3) and exits with an error if
 *  strdup(3) fails.  NOTE: unlike strdup(3), estrdup(NULL) is legal.
 */

char *estrdup(str)
    char *str;
{

    if (str != NULL && (str = (char *)strdup(str)) == NULL) {
	(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
	exit(1);
    }
    return(str);
}