blob: 9d111932233c8e1409aeb64e77aa1ca40935b4c0 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/* 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 Mower18794c82015-11-11 16:22:45 -06005 Copyright (C) 2008-2014 Daniel Baumann <mail@daniel-baumann.ch>
bigbiff bigbiff9c754052013-01-09 09:09:08 -05006
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 Mower18794c82015-11-11 16:22:45 -060020 The complete text of the GNU General Public License
bigbiff bigbiff9c754052013-01-09 09:09:08 -050021 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
35typedef struct _link {
36 void *data;
37 struct _link *next;
38} LINK;
39
Matt Mower18794c82015-11-11 16:22:45 -060040void die(const char *msg, ...)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050041{
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 Mower18794c82015-11-11 16:22:45 -060051void pdie(const char *msg, ...)
bigbiff bigbiff9c754052013-01-09 09:09:08 -050052{
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
62void *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
72void *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
82void 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
94int min(int a, int b)
95{
96 return a < b ? a : b;
97}
98
Matt Mower18794c82015-11-11 16:22:45 -060099char get_key(const char *valid, const char *prompt)
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500100{
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}