]> mj.ucw.cz Git - checkmail.git/blob - util.c
e211755461be58e75375b95739b9a1ce68c391c3
[checkmail.git] / util.c
1 /*
2  *      Utility Functions
3  *
4  *      (c) 2005 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <stdarg.h>
10 #include <string.h>
11
12 #include "util.h"
13
14 int debug_mode;
15
16 void NONRET
17 die(char *c, ...)
18 {
19   va_list args;
20   va_start(args, c);
21   fprintf(stderr, "cm: ");
22   vfprintf(stderr, c, args);
23   fputc('\n', stderr);
24   va_end(args);
25   exit(1);
26 }
27
28 void
29 debug(char *c, ...)
30 {
31   if (!debug_mode)
32     return;
33
34   va_list args;
35   va_start(args, c);
36   vfprintf(stderr, c, args);
37   fflush(stderr);
38   va_end(args);
39 }
40
41 void *
42 xmalloc(uns size)
43 {
44   void *buf = malloc(size);
45   if (!buf)
46     die("Unable to allocate %d bytes of memory", size);
47   return buf;
48 }
49
50 char *
51 xstrdup(char *s)
52 {
53   if (!s)
54     return s;
55   uns len = strlen(s) + 1;
56   char *new = xmalloc(len);
57   memcpy(new, s, len);
58   return new;
59 }