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