feat(build): use meson so I don't end up ripping my hair out
This commit is contained in:
57
src/utils.c
Normal file
57
src/utils.c
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "log.h"
|
||||
|
||||
char
|
||||
*get_substring(char *s, int pos, int l)
|
||||
{
|
||||
int i = 0;
|
||||
char *out;
|
||||
|
||||
if (l <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
out = malloc((l + 1) * sizeof(char));
|
||||
if (out == NULL) {
|
||||
log_fatal("failed to get substring malloc: %s", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Copy substring into ss */
|
||||
while (i < l) {
|
||||
out[i] = s[pos + i];
|
||||
i++;
|
||||
}
|
||||
|
||||
/* Null terminate the substring */
|
||||
out[i] = '\0';
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
char
|
||||
*concat(const char *s1, const char *s2)
|
||||
{
|
||||
int len1, len2;
|
||||
char *result;
|
||||
|
||||
len1 = strlen(s1);
|
||||
len2 = strlen(s2);
|
||||
|
||||
/* attempt to allocate size for the new string */
|
||||
result = malloc((len1 + len2 + 1) * sizeof(char));
|
||||
if (result == NULL) {
|
||||
log_fatal("malloc: %s", strerror(errno));
|
||||
exit(1);
|
||||
}
|
||||
|
||||
memcpy(result, s1, len1);
|
||||
memcpy(result + len1, s2, len2 + 1);
|
||||
// + 1 copies the null terminator
|
||||
|
||||
return result;
|
||||
}
|
Reference in New Issue
Block a user