spmenu/libs/main.c

58 lines
1 KiB
C
Raw Permalink Normal View History

2023-01-20 23:17:30 +01:00
/* See LICENSE file for copyright and license details. */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
void * ecalloc(size_t nmemb, size_t size) {
2023-05-08 23:00:45 +02:00
void *p;
2023-01-20 23:17:30 +01:00
2023-05-08 23:00:45 +02:00
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
2023-01-20 23:17:30 +01:00
}
2023-06-12 03:39:29 +02:00
size_t sp_strncpy(char *restrict dst, const char *restrict src, size_t size) {
int offset;
offset = 0;
if (size > 0) {
while (*(src+offset) != '\0' ) {
if (offset == size) {
offset--;
break;
}
*(dst+offset) = *(src+offset);
offset++;
}
}
*(dst+offset) = '\0';
while (*(src+offset) != '\0')
offset++;
return offset;
}
void die(const char *fmt, ...) {
2023-05-08 23:00:45 +02:00
va_list ap;
2023-01-20 23:17:30 +01:00
2023-05-08 23:00:45 +02:00
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
2023-01-20 23:17:30 +01:00
2023-05-08 23:00:45 +02:00
if (fmt[0] && fmt[strlen(fmt)-1] == ':') {
fputc(' ', stderr);
perror(NULL);
} else {
fputc('\n', stderr);
}
2023-01-20 23:17:30 +01:00
2023-05-08 23:00:45 +02:00
exit(1);
2023-01-20 23:17:30 +01:00
}