forked from gfto/videohubctrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.c
82 lines (72 loc) · 1.57 KB
/
util.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
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
/*
* === Utility functions ===
*
* Blackmagic Design Videohub control application
* Copyright (C) 2014 Unix Solutions Ltd.
* Written by Georgi Chorbadzhiyski
*
* Released under MIT license.
* See LICENSE-MIT.txt for license terms.
*
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <unistd.h>
#include "util.h"
void die(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
fprintf(stderr, "ERROR: ");
vfprintf(stderr, fmt, args);
if (fmt[strlen(fmt) - 1] != '\n')
fprintf(stderr, "\n");
va_end(args);
exit(EXIT_FAILURE);
}
void *xmalloc(size_t size) {
void *ret = malloc(size);
if (!ret)
die("Can't alloc %ld bytes\n", (unsigned long)size);
return ret;
}
void *xzalloc(size_t size) {
void *ret = xmalloc(size);
memset(ret, 0, size);
return ret;
}
void *xcalloc(size_t nmemb, size_t size) {
return xzalloc(nmemb * size);
}
void *xrealloc(void *ptr, size_t size) {
void *ret = realloc(ptr, size);
if (!ret)
die("Can't realloc %ld bytes\n", (unsigned long)size);
return ret;
}
char *xstrdup(const char *s) {
char *ret;
if (!s)
return NULL;
ret = strdup(s);
if (!ret)
die("Can't strdup %lu bytes\n", (unsigned long)strlen(s) + 1);
return ret;
}
char *xstrndup(const char *s, size_t n) {
char *ret;
if (!s)
return NULL;
ret = strndup(s, n);
if (!ret)
die("Can't strndup %lu bytes\n", (unsigned long)n + 1);
return ret;
}
bool streq(const char *s1, const char *s2) {
if(!s1 && s2) { return 0; }
if(s1 && !s2) { return 0; }
if(!s1 && !s2) { return 1; }
return strcmp(s1, s2) == 0;
}