-
Notifications
You must be signed in to change notification settings - Fork 2
/
io.c
274 lines (231 loc) · 4.78 KB
/
io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
// io.c - simple io and input parsing routines
//
// Written by Eryk Vershen ([email protected])
//
/*
* Copyright 1996,1997 by Apple Computer, Inc.
* All Rights Reserved
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appears in all copies and
* that both the copyright notice and this permission notice appear in
* supporting documentation.
*
* APPLE COMPUTER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL APPLE COMPUTER BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
* NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include "hfdisk.h"
#include "io.h"
#include "errors.h"
//
// Defines
//
#define BAD_DIGIT 17 /* must be greater than any base */
#define STRING_CHUNK 16
#define UNGET_MAX_COUNT 10
//
// Types
//
//
// Global Constants
//
const long kDefault = -1;
//
// Global Variables
//
//
// Forward declarations
//
//
// Routines
//
int
get_okay(char *prompt)
{
int result = 0;
char* string = NULL;
if (get_string_argument(prompt, &string, 1))
{
if (string[0] == 'Y' || string[0] == 'y')
{
result = 1;
}
}
free(string);
return result;
}
int
get_command(char *prompt, int promptBeforeGet, int *command)
{
int result = 0;
char* string = NULL;
if (get_string_argument(prompt, &string, 1))
{
*command = string[0];
result = 1;
}
free(string);
return result;
}
int
get_number_argument(char *prompt, long *number, long default_value)
{
int result = 0;
char* buf = NULL;
size_t buflen = 0;
char multiplier;
int matched;
while (result == 0) {
printf("%s", prompt);
if (getline(&buf, &buflen, stdin) == -1)
{
// EOF
break;
}
else if ((default_value > 0) && (strncmp(buf, "\n", 1) == 0))
{
*number = default_value;
result = 1;
break;
}
else if ((matched = sscanf(buf, "%ld%c", number, &multiplier)) >= 1)
{
result = 1;
if (matched == 2) {
if (multiplier == 'g' || multiplier == 'G') {
*number *= (1024*1024*1024 / PBLOCK_SIZE);
} else if (multiplier == 'm' || multiplier == 'M') {
*number *= (1024*1024 / PBLOCK_SIZE);
} else if (multiplier == 'k' || multiplier == 'K') {
*number *= (1024 / PBLOCK_SIZE);
} else if (multiplier != '\n') {
result = 0;
}
}
}
}
free(buf);
return result;
}
int
get_string_argument(char *prompt, char **string, int reprompt)
{
int result = 0;
size_t buflen = 0;
while (result == 0) {
printf("%s", prompt);
if (getline(string, &buflen, stdin) == -1)
{
// EOF
break;
}
else if ((strncmp(*string, "\n", 1) == 0) && !reprompt)
{
result = 0;
break;
}
else
{
size_t len = strnlen(*string, buflen);
if ((*string)[len - 1] == '\n') {
(*string)[len - 1] = '\0';
}
result = 1;
}
}
return result;
}
int
number_of_digits(unsigned long value)
{
int j;
j = 1;
while (value > 9) {
j++;
value = value / 10;
}
return j;
}
//
// Print a message on standard error & flush the input.
//
void
bad_input(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
int
read_block(int fd, unsigned long num, char *buf, int quiet)
{
off_t x;
long t;
{
x = ((long long) num * PBLOCK_SIZE); /* cast to ll to work around compiler bug */
if ((x = lseek(fd, x, 0)) < 0) {
if (quiet == 0) {
error(errno, "Can't seek on file");
}
return 0;
}
if ((t = read(fd, buf, PBLOCK_SIZE)) != PBLOCK_SIZE) {
if (quiet == 0) {
error((t<0?errno:0), "Can't read block %u from file", num);
}
return 0;
}
return 1;
}
}
int
write_block(int fd, unsigned long num, char *buf)
{
off_t x;
long t;
if (rflag) {
printf("Can't write block %lu to file", num);
return 0;
}
{
x = num * PBLOCK_SIZE;
if ((x = lseek(fd, x, 0)) < 0) {
error(errno, "Can't seek on file");
return 0;
}
if ((t = write(fd, buf, PBLOCK_SIZE)) != PBLOCK_SIZE) {
error((t<0?errno:0), "Can't write block %u to file", num);
return 0;
}
return 1;
}
}
int
close_device(int fildes)
{
return close(fildes);
}
int
open_device(const char *path, int oflag)
{
return open(path, oflag);
}