forked from BreadOnPenguins/dwm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.c
More file actions
58 lines (50 loc) · 1.58 KB
/
util.c
File metadata and controls
58 lines (50 loc) · 1.58 KB
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
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "util.h"
/**
* @brief Print error message and exit program
* @param fmt Format string for printf-style formatting
* @param ... Variable arguments matching the format string
* @note If format string ends with ':', appends strerror(errno)
* @warning This function terminates the program with exit code 1
* @bug No validation of fmt parameter - could cause segmentation fault if NULL
* @bug Always exits with code 1, doesn't allow for different error severity levels
* @return void (never returns due to exit)
*/
void
die(const char *fmt, ...)
{
va_list ap;
int saved_errno;
saved_errno = errno;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
if (fmt[0] && fmt[strlen(fmt)-1] == ':')
fprintf(stderr, " %s", strerror(saved_errno));
fputc('\n', stderr);
exit(1);
}
/**
* @brief Allocate zero-initialized memory with error handling
* @param nmemb Number of elements to allocate
* @param size Size of each element in bytes
* @return Pointer to allocated zero-initialized memory
* @note Calls die() on allocation failure, never returns NULL
* @warning Potential integer overflow if nmemb * size exceeds SIZE_MAX
* @warning No bounds checking on parameters - could allocate excessive memory
* @bug Does not check for multiplication overflow in calloc
* @see die() for error handling behavior
*/
void *
ecalloc(size_t nmemb, size_t size)
{
void *p;
if (!(p = calloc(nmemb, size)))
die("calloc:");
return p;
}