-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.c
101 lines (88 loc) · 1.97 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright: 2013 Xilinx Inc
* Written by Edgar E. Iglesias <[email protected]>
*
* 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; version 2.
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
bool fd_is_socket(int fd)
{
struct stat statbuf;
fstat(fd, &statbuf);
return S_ISSOCK(statbuf.st_mode);
}
bool filename_is_likely_header(const char *s)
{
unsigned int len;
if (!s)
return false;
len = strlen(s);
if (len < 3)
return false;
if (s[len - 2] == '.'
&& s[len - 1] == 'h')
return true;
return false;
}
void *safe_malloc(size_t size)
{
void *p = malloc(size);
if (!p) {
fprintf(stderr, "malloc(%zd) Out of memory!\n", size);
exit(1);
}
return p;
}
void *safe_mallocz(size_t size)
{
void *p = calloc(1, size);
if (!p) {
fprintf(stderr, "mallocz(%zd) Out of memory!\n", size);
exit(1);
}
return p;
}
void *safe_realloc(void *ptr, size_t size)
{
void *new;
new = realloc(ptr, size);
if (new == NULL && size > 0) {
fprintf(stderr, "realloc(%zd) Out of memory!\n", size);
exit(1);
}
return new;
}
size_t get_filesize(int fd)
{
struct stat sbuf;
int err;
err = fstat(fd, &sbuf);
if (err < 0) {
perror("fstat");
exit(1);
}
return sbuf.st_size;
}