summaryrefslogtreecommitdiff
path: root/src/shared.c
blob: d67e3621e8852e353da0d9d4cb5114adab5889ac (plain)
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
250
251
252
253
254
255
256
257
#include "shared.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <error.h>
#include <stdio.h>

void* xcalloc(size_t nmemb, size_t size) {
    void *mem = calloc(nmemb, size);

    if(!mem) {
        #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
            #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                error(1, errno, "<xcalloc> Could not allocate memory");
            #else
                exit(EXIT_FAILURE);
            #endif
        #endif

        abort();
    }
    

    return mem;
}

void* xreallocarray(void *ptr, size_t nmemb, size_t size) {
    void *mem = reallocarray(ptr, nmemb, size);
    if(mem == NULL) {
        #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
            #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                error(1, errno, "<xreallocarray> Could not allocate memory");
            #else
                exit(EXIT_FAILURE);
            #endif
        #endif

        abort();
    }

    return mem;
}

int readwholebuffer(char **str, unsigned long int initsize, int fd) {
	// Try to read bytes from fd into str
		// Bytes read == 0, return 0
		// Bytes read < 0, free string, return -1;
	// When string hits capacity, double the capacity, and reallocate the string

	char *lstr = NULL, *tmp = NULL;
	ssize_t bytesread = -1;
	int csize = initsize, ccap = initsize;

	lstr = xcalloc(initsize, sizeof(char));
	while((bytesread = read(fd, lstr + (csize - ccap), ccap)) > 0) {
		ccap -= bytesread;
		if(ccap <= 0) {
			csize *= 2;
			ccap = csize / 2;

			tmp = realloc(lstr, csize * sizeof(char));
			if(!tmp) {
                #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
				    error(0, errno, "Could not reallocate enough space for lstr");
                #endif
				free(lstr);
                lstr = NULL; // Need to set this because of the break
				bytesread = -100;
				break;
			}
			lstr = tmp;
		}
	}
	if(bytesread < 0 && bytesread != -100) {
        #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
		    error(0, errno, "Ran into a read() error");
        #endif
		free(lstr);
		lstr = NULL;
	}

	if(lstr) {
		tmp = realloc(lstr, csize - ccap + 1);
		if(!tmp) {
            #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
			    error(0, errno, "Could not shrink lstr after reading buffer");
            #endif
			free(lstr);
			bytesread = -100;
		}
		lstr = tmp;
	}

    if(lstr && fd == STDIN_FILENO) {
        lstr[csize - ccap - 1] = '\0'; // Don't include the newline
    }

	*str = lstr;
	return ((bytesread == 0) ? (csize - ccap) : -1);
}


int writewholebuffer(int fd, const unsigned char *buf, int len) {
    int total = 0;
    int left = len;
    int n;

    while(total < len) {
        if((n = write(fd, buf + total, left)) < 0) 
            break;

        total += n;
        left -= n;
    }

    return (n < 0) ? -1 : 0;
}
// Adapted from Beej's `sendall()` function
// https://beej.us/guide/bgnet/html/split/slightly-advanced-techniques.html#sendall
    // Thanks Beej!

// dirname but less retarded hopefully
char *xdirname(const char * const path) {
	char *tmp = NULL;
	if(!path) { // Path being null is a special case which should return super early, before anything else
		tmp = strdup(".");
		if(!tmp) {
            #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
                #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                    error(1, errno, "<xdirname> could not strdup \".\" for set path result \"NULL\"");
                #else
                    exit(EXIT_FAILURE);
                #endif
            #endif
			abort();
        }

		return tmp;
	}

	unsigned char flag = 0;
	if(strcmp(path, ".") 	== 0) 			{tmp = strdup("."); flag++;}
	if(strcmp(path, "/") 	== 0 && !flag) 	{tmp = strdup("/"); flag++;}
	if(strcmp(path, "..") 	== 0 && !flag) 	{tmp = strdup("."); flag++;}

	if(flag) {
		if(!tmp) {
		    #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
                #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                    error(1, errno, "<xdirname> could not strdup a set path result");
                #else
                    exit(EXIT_FAILURE);
                #endif
            #endif
        	abort();
        }
		return tmp;
	}


	/* From the manpages: (man 3 dirname)
	//		+=======================================+
	//		|	path		dirname		basename	|
	//		+=======================================+
	//		|	/usr/lib   	/usr      	lib			|
	//		|	/usr/      	/         	usr			|
	//		|	usr        	.         	usr			|
	//		+=======================================+
	*/

	// Get a temp copy of the path for manipulation purposes
	tmp = strdup(path);
	if(!tmp) {
	    #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
            #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                error(1, errno, "<xdirname> could not strdup the given path \"%s\" for internal manipulation", path);
            #else
                exit(EXIT_FAILURE);
            #endif
        #endif
    	abort();
    }

	// If there's a trailing '/', delete it
	size_t pathlen = strlen(path);
	if(tmp[pathlen - 1] == '/') {
		tmp[pathlen - 1] = '\0';
		pathlen--;
	}

	// Ok, I think the easiest way to do this (if maybe a bit slow) is to count the number of '/'s in the string
		// If there's only one, return '/'
		// If there are 2 or more, find the last one in the list and set it to '\0'

	size_t count = 0;
	for(size_t i = 0; i < pathlen; i++) {
		if(tmp[i] == '/')
			count++;
	}

	if(count == 0) {
		free(tmp);
		tmp = strdup(".");
		if(!tmp) {
            #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
                #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                    error(1, errno, "<xdirname> could not strdup \".\" for set path result");
                #else
                    exit(EXIT_FAILURE);
                #endif
            #endif
        	abort();
        }
		return tmp;
	}
	if(count == 1) {
		free(tmp);
		tmp = strdup("/");
		if(!tmp) {
            #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
                #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                    error(1, errno, "<xdirname> could not strdup \"/\" for set path result");
                #else
                    exit(EXIT_FAILURE);
                #endif
            #endif
        	abort();
        }

		return tmp;
	}

	for(size_t i = 0, c2 = 0; i < pathlen; i++) {
		if(tmp[i] == '/')
			c2++;
		if(c2 == count)
			tmp[i] = '\0';
	}

	char * const actual = strdup(tmp);
	if(!actual) {
	    #if defined ___VXGG___XALLOC_EXIT_ON_ERROR___ && ___VXGG___XALLOC_EXIT_ON_ERROR___ > 0
            #if defined ___VXGG___VERBOSE_ERRORS___ && ___VXGG___VERBOSE_ERRORS___ > 0
                error(1, errno, "<xdirname> could not strdup tmp string to make a shorter end string");
            #else
                exit(EXIT_FAILURE);
            #endif
        #endif
    	abort();
    }
	free(tmp);

	return actual;
}