Mercurial > projects > pwm
annotate util.c @ 21:ee4d36c85287
Make EOF in interactive mode equivalent to the q command
author | Guido Berhoerster <guido+pwm@berhoerster.name> |
---|---|
date | Wed, 06 Sep 2017 16:41:58 +0200 |
parents | a07665727c19 |
children |
rev | line source |
---|---|
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
1 /* |
16
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
2 * Copyright (C) 2017 Guido Berhoerster <guido+pwm@berhoerster.name> |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
3 * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
4 * Permission is hereby granted, free of charge, to any person obtaining |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
5 * a copy of this software and associated documentation files (the |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
6 * "Software"), to deal in the Software without restriction, including |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
7 * without limitation the rights to use, copy, modify, merge, publish, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
8 * distribute, sublicense, and/or sell copies of the Software, and to |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
9 * permit persons to whom the Software is furnished to do so, subject to |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
10 * the following conditions: |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
11 * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
12 * The above copyright notice and this permission notice shall be included |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
13 * in all copies or substantial portions of the Software. |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
14 * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
22 */ |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
23 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
24 #include "compat.h" |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
25 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
26 #ifdef HAVE_ERR_H |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
27 #include <err.h> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
28 #endif /* HAVE_ERR_H */ |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
29 #include <stdio.h> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
30 #include <string.h> |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
31 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
32 #include "util.h" |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
33 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
34 void * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
35 xmalloc(size_t size) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
36 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
37 void *ptr; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
38 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
39 ptr = malloc(size); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
40 if (ptr == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
41 err(1, "malloc"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
42 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
43 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
44 return (ptr); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
45 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
46 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
47 void * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
48 xrealloc(void *ptr, size_t size) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
49 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
50 ptr = realloc(ptr, size); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
51 if (ptr == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
52 err(1, "realloc"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
53 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
54 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
55 return (ptr); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
56 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
57 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
58 char * |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
59 xstrdup(const char *str) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
60 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
61 char *new; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
62 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
63 new = strdup(str); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
64 if (new == NULL) { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
65 err(1, "strdup"); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
66 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
67 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
68 return (new); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
69 } |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
70 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
71 char * |
16
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
72 xvasprintf(char **strp, const char *fmt, va_list args) |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
73 { |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
74 if (vasprintf(strp, fmt, args) < 0) { |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
75 err(1, "vasprintf"); |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
76 } |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
77 |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
78 return (*strp); |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
79 } |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
80 |
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
81 char * |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
82 xasprintf(char **strp, const char *fmt, ...) |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
83 { |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
84 va_list args; |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
85 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
86 va_start(args, fmt); |
16
a07665727c19
Add status command
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
0
diff
changeset
|
87 xvasprintf(strp, fmt, args); |
0
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
88 va_end(args); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
89 |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
90 return (*strp); |
a7e41e1a79c8
Initial revision
Guido Berhoerster <guido+pwm@berhoerster.name>
parents:
diff
changeset
|
91 } |