Mercurial > projects > rantaiwarna
comparison util.c @ 0:a9a7ad180c3b version-1
Initial revision
author | Guido Berhoerster <guido+rantaiwarna@berhoerster.name> |
---|---|
date | Sat, 15 Mar 2014 18:41:03 +0100 |
parents | |
children | 4f6bf50dbc4a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:a9a7ad180c3b |
---|---|
1 /* | |
2 * Copyright (C) 2014 Guido Berhoerster <guido+rantaiwarna@berhoerster.name> | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining | |
5 * a copy of this software and associated documentation files (the | |
6 * "Software"), to deal in the Software without restriction, including | |
7 * without limitation the rights to use, copy, modify, merge, publish, | |
8 * distribute, sublicense, and/or sell copies of the Software, and to | |
9 * permit persons to whom the Software is furnished to do so, subject to | |
10 * the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included | |
13 * in all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #define _XOPEN_SOURCE 600 | |
25 | |
26 #include <stdlib.h> | |
27 #include <string.h> | |
28 #include <stdio.h> | |
29 #include <errno.h> | |
30 #include <curses.h> | |
31 #include <term.h> | |
32 #include <stdarg.h> | |
33 | |
34 #include "util.h" | |
35 #include "compat.h" | |
36 #include "rantaiwarna.h" | |
37 | |
38 void | |
39 restore_term(void) { | |
40 if (curses_init) { | |
41 endwin(); | |
42 curses_init = FALSE; | |
43 curs_set(1); | |
44 } | |
45 } | |
46 | |
47 void | |
48 rantaiwarna_err(int eval, const char *fmt, ...) | |
49 { | |
50 int saved_errno = errno; | |
51 va_list args; | |
52 | |
53 restore_term(); | |
54 | |
55 fprintf(stderr, "%s: ", progname); | |
56 va_start(args, fmt); | |
57 if (fmt != NULL) { | |
58 vfprintf(stderr, fmt, args); | |
59 fprintf(stderr, ": "); | |
60 } | |
61 va_end(args); | |
62 fprintf(stderr, "%s\n", strerror(saved_errno)); | |
63 | |
64 exit(eval); | |
65 } | |
66 | |
67 void | |
68 rantaiwarna_errx(int eval, const char *fmt, ...) | |
69 { | |
70 va_list args; | |
71 | |
72 restore_term(); | |
73 | |
74 fprintf(stderr, "%s: ", progname); | |
75 va_start(args, fmt); | |
76 if (fmt != NULL) { | |
77 vfprintf(stderr, fmt, args); | |
78 } | |
79 va_end(args); | |
80 fputc('\n', stderr); | |
81 | |
82 exit(eval); | |
83 } | |
84 | |
85 void * | |
86 rantaiwarna_malloc(size_t size) | |
87 { | |
88 void *ptr; | |
89 | |
90 ptr = malloc(size); | |
91 if (ptr == NULL) { | |
92 rantaiwarna_err(1, NULL); | |
93 } | |
94 return (ptr); | |
95 } | |
96 | |
97 void * | |
98 rantaiwarna_calloc(size_t nelem, size_t elsize) | |
99 { | |
100 void *ptr; | |
101 | |
102 ptr = calloc(nelem, elsize); | |
103 if (ptr == NULL) { | |
104 rantaiwarna_err(1, NULL); | |
105 } | |
106 return (ptr); | |
107 } | |
108 | |
109 uint32_t | |
110 rantaiwarna_rand(uint32_t *seed) | |
111 { | |
112 uint32_t x = *seed; | |
113 | |
114 x ^= x << 13; | |
115 x ^= x >> 17; | |
116 x ^= x << 5; | |
117 *seed = x; | |
118 | |
119 return (x); | |
120 } |