File: xmalloc.c

package info (click to toggle)
fetchmail 6.5.6-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 7,596 kB
  • sloc: ansic: 19,190; sh: 7,108; python: 2,395; perl: 564; yacc: 447; lex: 286; makefile: 260; awk: 124; lisp: 84; exp: 43; sed: 17
file content (65 lines) | stat: -rw-r--r-- 1,063 bytes parent folder | download
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
/*
 * xmalloc.c -- allocate space or die 
 *
 * Copyright 1998 by Eric S. Raymond.
 * For license terms, see the file COPYING in this directory.
 */

#include "config.h"
#include "fetchmail.h"

#include "xmalloc.h"
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include  <stdlib.h>
#include "i18n.h"

void *xmalloc (size_t n)
{
    void *p;

    p = (void *) malloc(n);
    if (p == (void *) 0)
    {
	report(stderr, GT_("malloc failed\n"));
	abort();
    }
    return(p);
}

void *xrealloc (void *p, size_t n)
{
    if (p == 0)
	return xmalloc (n);
    p = (void *) realloc(p, n);
    if (p == (void *) 0)
    {
	report(stderr, GT_("realloc failed\n"));
	abort();
    }
    return p;
}

char *xstrdup(const char *s)
{
    char *p;
    p = (char *) xmalloc(strlen(s)+1);
    strcpy(p,s);
    return p;
}

char *xstrndup(const char *s, size_t len)
{
    char *p;
    size_t l = strlen(s);

    if (len < l) l = len;
    p = (char *)xmalloc(l + 1);
    memcpy(p, s, l);
    p[l] = '\0';
    return p;
}

/* xmalloc.c ends here */