241cade38939338b7a9cd541cc1e52add7130f24
[oftp] / src / util.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "util.h"
5 #include "tui.h"
6
7 void *malloc_nf_impl(size_t sz, const char *file, int line)
8 {
9         void *p;
10         if(!(p = malloc(sz))) {
11                 fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)sz);
12                 abort();
13         }
14         return p;
15 }
16
17 void *calloc_nf_impl(size_t num, size_t sz, const char *file, int line)
18 {
19         void *p;
20         if(!(p = calloc(num, sz))) {
21                 fprintf(stderr, "%s:%d failed to allocate %lu bytes\n", file, line, (unsigned long)(sz * num));
22                 abort();
23         }
24         return p;
25 }
26
27 void *realloc_nf_impl(void *p, size_t sz, const char *file, int line)
28 {
29         if(!(p = realloc(p, sz))) {
30                 fprintf(stderr, "%s:%d failed to realloc %lu bytes\n", file, line, (unsigned long)sz);
31                 abort();
32         }
33         return p;
34 }
35
36 char *strdup_nf_impl(const char *s, const char *file, int line)
37 {
38         char *res;
39         if(!(res = strdup(s))) {
40                 fprintf(stderr, "%s:%d failed to duplicate string\n", file, line);
41                 abort();
42         }
43         return res;
44 }
45
46
47 int match_prefix(const char *str, const char *prefix)
48 {
49         while(*str && *prefix) {
50                 if(*str++ != *prefix++) {
51                         return 0;
52                 }
53         }
54         return *prefix ? 0 : 1;
55 }
56
57 void errmsg(const char *fmt, ...)
58 {
59         va_list ap;
60
61         va_start(ap, fmt);
62         tui_vmsgbox(TUI_ERROR, "error", fmt, ap);
63         va_end(ap);
64 }
65
66 void warnmsg(const char *fmt, ...)
67 {
68         va_list ap;
69
70         va_start(ap, fmt);
71         tui_status(TUI_WARN, fmt, ap);
72         va_end(ap);
73 }
74
75 void infomsg(const char *fmt, ...)
76 {
77         va_list ap;
78
79         va_start(ap, fmt);
80         tui_status(TUI_INFO, fmt, ap);
81         va_end(ap);
82 }