~pixelherodev/c3lib

ref: tmp c3lib/util.c -rw-r--r-- 4.3 KiB
ddd78966Noam Preil tmp 3 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
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
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>

#include "util.h"

char*
avprintf(const char *fmt, va_list args)
{
	char *str;
	int len;
	va_list args2;
	va_copy(args2, args);
	len = vsnprintf(NULL, 0, fmt, args2);
	va_end(args2);
	if(len < 0)
		return NULL;
	len += 1;
	str = malloc(len);
	if(str == NULL)
		return NULL;
	vsnprintf(str, len, fmt, args);
	return str;
}

char*
aprintf(const char *fmt, ...)
{
	char *str;
	va_list args;
	va_start(args, fmt);
	str = avprintf(fmt, args);
	va_end(args);
	return str;
}

void
error(const char *fmt, ...)
{
	char *buf;
	va_list args;
	fflush(NULL);
	va_start(args, fmt);
	buf = avprintf(fmt, args);
	fprintf(stderr, "%s", buf);
	free(buf);
	va_end(args);
	fflush(NULL);
}

static void
hpr_append(char ***ctr, size_t *cnt, char *elem)
{
	if(elem == NULL)
		FATAL("Not appending NULL", 0);
	*cnt = *cnt + 1;
	char **ctr2 = realloc(*ctr, *cnt * sizeof(char*));
	if(ctr2 == NULL)
		FATAL("Out of memory", 0);
	*ctr = ctr2;
	ctr2[*cnt - 1] = elem;
}

static void
hpr_remove(char ***ctr, size_t *cnt, size_t index)
{
	size_t i;
	*cnt -= 1;
	char **ctr2 =  malloc(*cnt * sizeof(char*));
	if(ctr2 == NULL)
		FATAL("Out of memory", 0);
	free((*ctr)[index]);
	for(i = 0; i < index; i += 1)
		ctr2[i] = (*ctr)[i];
	for(i = index + 1; i <= *cnt; i += 1)
		ctr2[i - 1] = (*ctr)[i];
	free(*ctr);
	*ctr = ctr2;
}

char*
h_path_resolve(char *unknown)
{
	char *path;
	char **elements;
	char *buf;
	size_t elem_count, size;
	int i;
	elements = NULL;
	elem_count = 0;
	if(unknown[0] != '/'){
		path = malloc(512);
		if(path == NULL)
			FATAL("Out of memory", 0);
		if(getcwd(path, 512) == NULL)
			FATAL("getcwd failed",0);
		buf = strtok(path, "/");
		while(buf != NULL){
			hpr_append(&elements, &elem_count, strdup(buf));
			buf = strtok(NULL, "/");
		}
		free(path);
	}
	buf = strtok(unknown, "/");
	while(buf != NULL){
		hpr_append(&elements, &elem_count, strdup(buf));
		buf = strtok(NULL, "/");
	}
	for(i = 0; i < elem_count; i += 1){
		if(strcmp(elements[i], ".") == 0){
			hpr_remove(&elements, &elem_count, i);
			i -= 1;
			continue;
		}
		if(strcmp(elements[i], "..") == 0){
			if(i == 0)
				FATAL("Cannot go above root",0);
			hpr_remove(&elements, &elem_count, i-1);
			hpr_remove(&elements, &elem_count, i-1);
			i -= 2;
			continue;
		}
	}
	if(elem_count == 0)
		FATAL("Cannot compile the root folder", 0);
	size = 1;
	for(i = 0; i < elem_count; i += 1)
		size += strlen(elements[i]) + 1;
	path = malloc(size);
	path[0] = 0;
	if(path == NULL)
		FATAL("Out of memory", 0);
	for(i = 0; i < elem_count; i += 1){
		strcat(path, "/");
		strcat(path, elements[i]);
		free(elements[i]);
	}
	free(elements);
	return path;
}

char*
file_read(const char *const path, size_t *size)
{
	FILE *file;
	char *buf;
	size_t len;
	file = fopen(path, "r");
	if(file == NULL){
		ERROR("Failed to open file: '%s'", path);
		return NULL;
	}
	len = file_len(file, path);
	buf = malloc(len + 1);
	if(buf == NULL){
		ERROR("Failed to allocate buffer for '%s'", path);
		return NULL;
	}
	if(fread(buf, 1, len, file) != len)
		FATAL("Failed to read file '%s'", path);
	buf[len] = 0;
	fclose(file);
	if(size != NULL)
		*size = len;
	return buf;
}

size_t
file_len(FILE *file, const char *const path)
{
	long len, orig;
	orig = ftell(file);
	if(orig < 0)
		FATAL("Seek failed on '%s'", path);
	if(fseek(file, 0, SEEK_END) != 0)
		FATAL("Seek failed on '%s'", path);
	len = ftell(file);
	if(len < 0)
		FATAL("Seek failed on '%s'", path);
	if(fseek(file, orig, SEEK_SET) != 0)
		FATAL("Seek failed on '%s'", path);
	return (size_t)len;
}

// returns an allocated string of dirname(orig) ++ new
// out_len must be non-null
char*
file_rel_dir(char *orig, char *new, size_t new_len, size_t *out_len)
{
	unsigned i;
	unsigned last_slash_pos;
	char *out;
	last_slash_pos = 0;
	for(i = 0; orig[i] != 0; i += 1)
		if(orig[i] == '/')
			last_slash_pos = i + 1;
	if(last_slash_pos == 0)
		FATAL("The base path of orig must be an absolute path!", 0);
	out = malloc(last_slash_pos + new_len + 1);
	if(out == NULL)
		FATAL("Out of memory", 0);
	strncpy(out, orig, last_slash_pos);
	strncpy(out + last_slash_pos, new, new_len);
	out[new_len + last_slash_pos] = 0;
	if(out_len != NULL)
		*out_len = new_len + last_slash_pos;
	new = h_path_resolve(out);
	free(out);
	if(new == NULL)
    		FATAL("Out of memory", 0);
	return new;
}

void
c3oom(void)
{
	FATAL("Out of memory!", 0);
}