Blame src/check_error.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "../lib/libcompat.h"
Packit 0b5880
Packit 0b5880
#include <stdarg.h>
Packit 0b5880
#include <stdlib.h>
Packit 0b5880
#include <string.h>
Packit 0b5880
#include <stdio.h>
Packit 0b5880
#include <errno.h>
Packit 0b5880
#include <setjmp.h>
Packit 0b5880
Packit 0b5880
#include "check_error.h"
Packit 0b5880
Packit 0b5880
/**
Packit 0b5880
 * Storage for setjmp/longjmp context information used in NOFORK mode
Packit 0b5880
 */
Packit 0b5880
jmp_buf error_jmp_buffer;
Packit 0b5880
Packit 0b5880
Packit 0b5880
/* FIXME: including a colon at the end is a bad way to indicate an error */
Packit 0b5880
void eprintf(const char *fmt, const char *file, int line, ...)
Packit 0b5880
{
Packit 0b5880
    va_list args;
Packit 0b5880
Packit 0b5880
    fflush(stderr);
Packit 0b5880
Packit 0b5880
    fprintf(stderr, "%s:%d: ", file, line);
Packit 0b5880
    va_start(args, line);
Packit 0b5880
    vfprintf(stderr, fmt, args);
Packit 0b5880
    va_end(args);
Packit 0b5880
Packit 0b5880
    /*include system error information if format ends in colon */
Packit 0b5880
    if(fmt[0] != '\0' && fmt[strlen(fmt) - 1] == ':')
Packit 0b5880
        fprintf(stderr, " %s", strerror(errno));
Packit 0b5880
    fprintf(stderr, "\n");
Packit 0b5880
Packit 0b5880
    exit(2);
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void *emalloc(size_t n)
Packit 0b5880
{
Packit 0b5880
    void *p;
Packit 0b5880
Packit 0b5880
    p = malloc(n);
Packit 0b5880
    if(p == NULL)
Packit 0b5880
        eprintf("malloc of %u bytes failed:", __FILE__, __LINE__ - 2, n);
Packit 0b5880
    return p;
Packit 0b5880
}
Packit 0b5880
Packit 0b5880
void *erealloc(void *ptr, size_t n)
Packit 0b5880
{
Packit 0b5880
    void *p;
Packit 0b5880
Packit 0b5880
    p = realloc(ptr, n);
Packit 0b5880
    if(p == NULL)
Packit 0b5880
        eprintf("realloc of %u bytes failed:", __FILE__, __LINE__ - 2, n);
Packit 0b5880
    return p;
Packit 0b5880
}