-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathind.c
249 lines (204 loc) · 4.62 KB
/
ind.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* Copy me if you can.
* by 20h, documentation by lazr
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include "ind.h"
/// Exit with a status message.
//
// Prints out a string to stderr like fprintf(), but also appends ": " and
// the string returned from perror.
//
// Does not return.
void
edie(char *fmt, ...)
{
va_list fmtargs;
va_start(fmtargs, fmt);
vfprintf(stderr, fmt, fmtargs);
va_end(fmtargs);
fprintf(stderr, ": ");
perror(NULL);
exit(1);
}
/*
* Exit without a status message.
*
* Prints out a string to stdout, and exits with an error.
*/
void
die(char *fmt, ...)
{
va_list fmtargs;
va_start(fmtargs, fmt);
vfprintf(stderr, fmt, fmtargs);
va_end(fmtargs);
exit(1);
}
/* You can use these constants if you want to be clearer. Up to you. */
const int ALLOCZ_CLEAR = 1;
const int ALLOCZ_DIRTY = 0;
/// Reallocate and zero memory.
//
// p: a pointer to memory to reallocate. Give NULL here to allocate new memory.
// l: an integer to the size of the memory to allocate, in bytes.
// z: a flag where, if truthy, the memory is set to all 0 bytes.
//
// returns the pointer to the memory in question, or NULL if there was an issue.
//
void *
reallocz(void *p, const size_t l, const int z)
{
p = realloc(p, l);
if(p == NULL)
edie("realloc");
if(z)
memset(p, 0, l);
return p;
}
// Allocate and zero new memory.
void *
mallocz(const size_t l, int z)
{
return reallocz(NULL, l, z);
}
void *
memdup(void *p, const size_t l)
{
char *ret;
ret = reallocz(NULL, l, 2); // 2 looks like a Z and compares true.
memmove(ret, p, l);
return (void *)ret;
}
// Duplicate memory.
//
void *
memdupz(const void *p, const size_t l)
{
char *ret;
ret = reallocz(NULL, l+1, 2);
memmove(ret, p, l);
return (void *)ret;
}
// Create a duplicate of the given memory and concatenate another chunk of memory to it.
void *
memdupcat(void *p, const size_t lp, void *c, const size_t lc)
{
p = reallocz(p, lp+lc, 0);
memset(&((char *)p)[lp], 0, lc);
memmove(&((char *)p)[lp], c, lc);
return p;
}
// Using a variable argument list, create a new allocated string in memory.
char *
vsmprintf(char *fmt, va_list fmtargs, const size_t size)
{
char *ret;
ret = reallocz(NULL, (size+1), ALLOCZ_CLEAR);
vsnprintf(ret, size+1, fmt, fmtargs);
return ret;
}
// Create a new allocated string in memory.
//
// Don't forget to free()!
char *
smprintf(char *fmt, ...)
{
va_list fmtargs;
char *ret;
int len;
va_start(fmtargs, fmt);
len = vsnprintf(NULL, 0, fmt, fmtargs);
va_end(fmtargs);
va_start(fmtargs, fmt);
ret = vsmprintf(fmt, fmtargs, len);
va_end(fmtargs);
return ret;
}
// Read in an entire file into a string
char *
readtoeoffd(int fd, int *len)
{
char *ret, buf[4096];
int olen, nlen, rl;
for (nlen = 0, olen = 0, ret = NULL;
(rl = read(fd, buf, sizeof(buf))); olen = nlen) {
if (rl > 0) {
nlen += rl;
ret = reallocz(ret, nlen+1, 0);
ret[nlen] = '\0';
memmove(&ret[olen], buf, rl);
} else if (rl == 0) {
break;
} else {
edie("readtoeoffd");
}
}
*len = nlen;
return ret;
}
char *
freadall(FILE* f, int *len)
{
*len = fseek(f, 0, SEEK_END);
rewind(f);
char *s = malloc(*len);
fread(s, sizeof(char), *len, f);
if (ferror(f)) {
return NULL;
}
return s;
}
/// Get the next line from a string.
//
// Returns NULL if p is NULL.
// When a line is found, p points to the rest of the string.
// If the whole string is moved to s, p will be NULL.
//
// s : The line from the string.
// size : Maximum size of the string to be extracted.
// p : The buffer to extract the string from.
//
// Returns: the extracted string.
char *
sgets(char *s, const size_t size, char **p)
{
char *newline;
int cur_len;
if (*p == NULL)
return NULL;
newline = strchr(*p, '\n');
if (newline == NULL) {
cur_len = strlen(*p);
if (cur_len < 1) {
*p = NULL;
return NULL;
}
} else {
cur_len = newline - *p;
}
if (cur_len >= size)
cur_len = size - 1;
memmove(s, *p, cur_len);
s[cur_len] = '\0';
if (newline == NULL) {
*p = NULL;
} else {
*p = newline+1;
}
return s;
}
int
ssplit(const char *s, const char *delimiters, char **parts) {
}