-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_wrappers.c
52 lines (45 loc) · 1.28 KB
/
std_wrappers.c
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
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include "std_wrappers.h"
void * wrapper_malloc(const size_t size, const char * file, const int line)
{
void * allocation = malloc(size);
if ( !allocation ) {
fprintf(stderr, "Error: %s (%d) at %s line %d\n",
strerror(errno), errno, file, line);
exit(EXIT_FAILURE);
}
return allocation;
}
void * wrapper_realloc(void * ptr, const size_t size,
const char * file, const int line)
{
void * allocation = realloc(ptr, size);
if ( !allocation ) {
fprintf(stderr, "Error: %s (%d) at %s line %d\n",
strerror(errno), errno, file, line);
exit(EXIT_FAILURE);
}
return allocation;
}
char * wrapper_strdup(const char * str, const char * file, const int line)
{
char * new_str = wrapper_malloc(strlen(str) + 1, file, line);
strcpy(new_str, str);
return new_str;
}
pid_t wrapper_fork(const char * file, const int line)
{
pid_t pid = fork();
if ( pid == -1 ) {
fprintf(stderr, "Error: %s (%d) at %s line %d\n",
strerror(errno), errno, file, line);
exit(EXIT_FAILURE);
}
return pid;
}