• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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 
36 typedef struct _link {
37     void *data;
38     struct _link *next;
39 } LINK;
40 
41 
die(char * msg,...)42 void die(char *msg,...)
43 {
44     va_list args;
45 
46     va_start(args,msg);
47     vfprintf(stderr,msg,args);
48     va_end(args);
49     fprintf(stderr,"\n");
50     exit(1);
51 }
52 
53 
pdie(char * msg,...)54 void pdie(char *msg,...)
55 {
56     va_list args;
57 
58     va_start(args,msg);
59     vfprintf(stderr,msg,args);
60     va_end(args);
61     fprintf(stderr,":%s\n",strerror(errno));
62     exit(1);
63 }
64 
65 
alloc(int size)66 void *alloc(int size)
67 {
68     void *this;
69 
70     if ((this = malloc(size))) return this;
71     pdie("malloc");
72     return NULL; /* for GCC */
73 }
74 
75 
qalloc(void ** root,int size)76 void *qalloc(void **root,int size)
77 {
78     LINK *link;
79 
80     link = alloc(sizeof(LINK));
81     link->next = *root;
82     *root = link;
83     return link->data = alloc(size);
84 }
85 
86 
qfree(void ** root)87 void qfree(void **root)
88 {
89     LINK *this;
90 
91     while (*root) {
92 	this = (LINK *) *root;
93 	*root = this->next;
94 	free(this->data);
95 	free(this);
96     }
97 }
98 
99 
min(int a,int b)100 int min(int a,int b)
101 {
102     return a < b ? a : b;
103 }
104 
105 
get_key(char * valid,char * prompt)106 char get_key(char *valid,char *prompt)
107 {
108     int ch,okay;
109 
110     while (1) {
111 	if (prompt) printf("%s ",prompt);
112 	fflush(stdout);
113 	while (ch = getchar(), ch == ' ' || ch == '\t');
114 	if (ch == EOF) exit(1);
115 	if (!strchr(valid,okay = ch)) okay = 0;
116 	while (ch = getchar(), ch != '\n' && ch != EOF);
117 	if (ch == EOF) exit(1);
118 	if (okay) return okay;
119 	printf("Invalid input.\n");
120     }
121 }
122 
123 /* Local Variables: */
124 /* tab-width: 8     */
125 /* End:             */
126