bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 1 | /* common.c - Common functions |
| 2 | |
| 3 | Copyright (C) 1993 Werner Almesberger <werner.almesberger@lrc.di.epfl.ch> |
| 4 | Copyright (C) 1998 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 5 | Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch> |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 6 | |
| 7 | This program is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 20 | The complete text of the GNU General Public License |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 21 | can be found in /usr/share/common-licenses/GPL-3 file. |
| 22 | */ |
| 23 | |
| 24 | /* FAT32, VFAT, Atari format support, and various fixes additions May 1998 |
| 25 | * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */ |
| 26 | |
| 27 | #include <stdlib.h> |
| 28 | #include <stdio.h> |
| 29 | #include <string.h> |
| 30 | #include <stdarg.h> |
| 31 | #include <errno.h> |
| 32 | |
| 33 | #include "common.h" |
| 34 | |
| 35 | typedef struct _link { |
| 36 | void *data; |
| 37 | struct _link *next; |
| 38 | } LINK; |
| 39 | |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 40 | void die(const char *msg, ...) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 41 | { |
| 42 | va_list args; |
| 43 | |
| 44 | va_start(args, msg); |
| 45 | vfprintf(stderr, msg, args); |
| 46 | va_end(args); |
| 47 | fprintf(stderr, "\n"); |
| 48 | exit(1); |
| 49 | } |
| 50 | |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 51 | void pdie(const char *msg, ...) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 52 | { |
| 53 | va_list args; |
| 54 | |
| 55 | va_start(args, msg); |
| 56 | vfprintf(stderr, msg, args); |
| 57 | va_end(args); |
| 58 | fprintf(stderr, ":%s\n", strerror(errno)); |
| 59 | exit(1); |
| 60 | } |
| 61 | |
| 62 | void *alloc(int size) |
| 63 | { |
| 64 | void *this; |
| 65 | |
| 66 | if ((this = malloc(size))) |
| 67 | return this; |
| 68 | pdie("malloc"); |
| 69 | return NULL; /* for GCC */ |
| 70 | } |
| 71 | |
| 72 | void *qalloc(void **root, int size) |
| 73 | { |
| 74 | LINK *link; |
| 75 | |
| 76 | link = alloc(sizeof(LINK)); |
| 77 | link->next = *root; |
| 78 | *root = link; |
| 79 | return link->data = alloc(size); |
| 80 | } |
| 81 | |
| 82 | void qfree(void **root) |
| 83 | { |
| 84 | LINK *this; |
| 85 | |
| 86 | while (*root) { |
| 87 | this = (LINK *) * root; |
| 88 | *root = this->next; |
| 89 | free(this->data); |
| 90 | free(this); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | int min(int a, int b) |
| 95 | { |
| 96 | return a < b ? a : b; |
| 97 | } |
| 98 | |
Matt Mower | 18794c8 | 2015-11-11 16:22:45 -0600 | [diff] [blame] | 99 | char get_key(const char *valid, const char *prompt) |
bigbiff bigbiff | 9c75405 | 2013-01-09 09:09:08 -0500 | [diff] [blame] | 100 | { |
| 101 | int ch, okay; |
| 102 | |
| 103 | while (1) { |
| 104 | if (prompt) |
| 105 | printf("%s ", prompt); |
| 106 | fflush(stdout); |
| 107 | while (ch = getchar(), ch == ' ' || ch == '\t') ; |
| 108 | if (ch == EOF) |
| 109 | exit(1); |
| 110 | if (!strchr(valid, okay = ch)) |
| 111 | okay = 0; |
| 112 | while (ch = getchar(), ch != '\n' && ch != EOF) ; |
| 113 | if (ch == EOF) |
| 114 | exit(1); |
| 115 | if (okay) |
| 116 | return okay; |
| 117 | printf("Invalid input.\n"); |
| 118 | } |
| 119 | } |