view src/util.c @ 10:d6f8f73a0c50 v2.1

util: make strnchr an actual strnchr
author Paper <paper@paper.us.eu.org>
date Fri, 22 Mar 2024 20:51:40 -0400
parents 0c98b46eaf73
children 2d7c810a1ac2
line wrap: on
line source

#include "util.h"

#include <stdlib.h>
#include <string.h>

/* reimplementations of non-portable stdlib functions */

size_t EDL_internal_strnlen(const char* s, size_t maxlen) {
	size_t len;

	for (len = 0; len < maxlen; len++, s++) {
		if (!*s)
			break;
	}

	return len;
}

char* EDL_internal_strnchr(const char* haystack, char needle, size_t length) {
	for (; length && *haystack && *haystack != needle; haystack++, length--);

	return length ? haystack : NULL;
}

char* EDL_internal_strdup(const char* s) {
	const size_t len = strlen(s);

	char* d = malloc((len + 1) * sizeof(char));
	memcpy(d, s, len * sizeof(char));
	d[len] = '\0';

	return d;
}