~kris/hacks

sframe

ref: 155c468d6c81a5f1a7d352f27ffd8f354dcad6c2 sframe/src/util.c -rw-r--r-- 1.5 KiB
155c468d — Kris Yotam Initial commit: sframe - unique frame extractor 6 months ago
                                                                                
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* See LICENSE file for copyright and license details. */

#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>

#include "util.h"

void
die(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	fprintf(stderr, "sframe: ");
	vfprintf(stderr, fmt, ap);
	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
		fprintf(stderr, " %s", strerror(errno));
	}
	fprintf(stderr, "\n");
	va_end(ap);
	exit(1);
}

void
warn(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	fprintf(stderr, "sframe: ");
	vfprintf(stderr, fmt, ap);
	if (fmt[0] && fmt[strlen(fmt) - 1] == ':') {
		fprintf(stderr, " %s", strerror(errno));
	}
	fprintf(stderr, "\n");
	va_end(ap);
}

void *
ecalloc(size_t nmemb, size_t size)
{
	void *p;

	p = calloc(nmemb, size);
	if (!p)
		die("calloc:");
	return p;
}

void *
emalloc(size_t size)
{
	void *p;

	p = malloc(size);
	if (!p)
		die("malloc:");
	return p;
}

char *
estrdup(const char *s)
{
	char *p;

	p = strdup(s);
	if (!p)
		die("strdup:");
	return p;
}

/* mkdir -p: create all components of path */
int
mkdirp(const char *path)
{
	char buf[4096];
	char *p;
	size_t len;

	len = strlen(path);
	if (len == 0 || len >= sizeof(buf))
		return -1;

	memcpy(buf, path, len + 1);

	for (p = buf + 1; *p; p++) {
		if (*p == '/') {
			*p = '\0';
			if (mkdir(buf, 0755) < 0 && errno != EEXIST)
				return -1;
			*p = '/';
		}
	}
	if (mkdir(buf, 0755) < 0 && errno != EEXIST)
		return -1;

	return 0;
}