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