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
|
/* util.c -- Simple utility functions that everyone uses. */
/* Copyright (C) 1988, 1990, 1992 Free Software Foundation, Inc.
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 2, 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., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA. */
#include "util.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
memory_error_and_abort (const char *what, int nbytes)
{
fprintf (stderr, "Fatal error: can't %s %d bytes.\n", what, nbytes);
abort ();
}
void *
xmalloc (int nbytes)
{
char *temp = (char *) malloc ((size_t)nbytes);
if (!temp)
memory_error_and_abort ("alloc", (int)nbytes);
return ((void *) temp);
}
void *
xrealloc (void *pointer, int nbytes)
{
char *temp;
if (!pointer)
temp = (char *) xmalloc (nbytes);
else
temp = (char *) realloc (pointer, (size_t)nbytes);
if (!temp)
memory_error_and_abort (pointer ? "realloc" : "alloc", nbytes);
return ((void *) temp);
}
/* Return freshly allocated copy of S. */
char *
xstrdup (const char *s)
{
char *tmp = xmalloc (strlen (s) + 1);
strcpy (tmp, s);
return tmp;
}
/* create a full path from a base path and a file name on a fresh
place */
char *
xmakepath (const char *s, const char *q)
{
char *tmp = xmalloc (strlen (s) + strlen (q) + 2);
sprintf (tmp, "%s/%s", s, q);
return tmp;
}
|