added port of dosfsck. Does not work yet as I cannot figure how to open the device...
[reactos.git] / rosapps / sysutils / dosfsck / common.c
1 /* common.c - Common functions */
2
3 /* Written 1993 by Werner Almesberger */
4
5 /* FAT32, VFAT, Atari format support, and various fixes additions May 1998
6 * by Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> */
7
8
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdarg.h>
13 #include <errno.h>
14
15 #include "common.h"
16
17
18 typedef struct _link {
19 void *data;
20 struct _link *next;
21 } LINK;
22
23
24 void die(char *msg,...)
25 {
26 va_list args;
27
28 va_start(args,msg);
29 vfprintf(stderr,msg,args);
30 va_end(args);
31 fprintf(stderr,"\n");
32 exit(1);
33 }
34
35
36 void pdie(char *msg,...)
37 {
38 va_list args;
39
40 va_start(args,msg);
41 vfprintf(stderr,msg,args);
42 va_end(args);
43 fprintf(stderr,":%s\n",strerror(errno));
44 exit(1);
45 }
46
47
48 void *alloc(int size)
49 {
50 void *this;
51
52 if ((this = malloc(size))) return this;
53 pdie("malloc");
54 return NULL; /* for GCC */
55 }
56
57
58 void *qalloc(void **root,int size)
59 {
60 LINK *link;
61
62 link = alloc(sizeof(LINK));
63 link->next = *root;
64 *root = link;
65 return link->data = alloc(size);
66 }
67
68
69 void qfree(void **root)
70 {
71 LINK *this;
72
73 while (*root) {
74 this = (LINK *) *root;
75 *root = this->next;
76 free(this->data);
77 free(this);
78 }
79 }
80
81
82 int min(int a,int b)
83 {
84 return a < b ? a : b;
85 }
86
87
88 char get_key(char *valid,char *prompt)
89 {
90 int ch,okay;
91
92 while (1) {
93 if (prompt) printf("%s ",prompt);
94 fflush(stdout);
95 while (ch = getchar(), ch == ' ' || ch == '\t');
96 if (ch == EOF) exit(1);
97 if (!strchr(valid,okay = ch)) okay = 0;
98 while (ch = getchar(), ch != '\n' && ch != EOF);
99 if (ch == EOF) exit(1);
100 if (okay) return okay;
101 printf("Invalid input.\n");
102 }
103 }
104
105 /* Local Variables: */
106 /* tab-width: 8 */
107 /* End: */