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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
/* $OpenBSD: misc.c,v 1.62 2017/11/16 11:08:28 krw Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/disklabel.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <uuid.h>
#include "disk.h"
#include "misc.h"
#include "part.h"
struct unit_type unit_types[] = {
{ "b" , 1LL , "Bytes" },
{ " " , 0LL , "Sectors" },
{ "K" , 1024LL , "Kilobytes" },
{ "M" , 1024LL * 1024 , "Megabytes" },
{ "G" , 1024LL * 1024 *1024 , "Gigabytes" },
{ "T" , 1024LL * 1024 * 1024 * 1024 , "Terabytes" },
{ NULL , 0 , NULL },
};
int
unit_lookup(char *units)
{
int i = 0;
if (units == NULL)
return (SECTORS);
while (unit_types[i].abbr != NULL) {
if (strncasecmp(unit_types[i].abbr, units, 1) == 0)
break;
i++;
}
/* default */
if (unit_types[i].abbr == NULL)
return (SECTORS);
return (i);
}
int
string_from_line(char *buf, size_t buflen)
{
char *line;
size_t sz;
line = fgetln(stdin, &sz);
if (line == NULL)
return (1);
if (line[sz - 1] == '\n')
sz--;
if (sz >= buflen)
sz = buflen - 1;
memcpy(buf, line, sz);
buf[sz] = '\0';
return (0);
}
void
ask_cmd(char **cmd, char **arg)
{
static char lbuf[100];
size_t cmdstart, cmdend, argstart;
/* Get NUL terminated string from stdin. */
if (string_from_line(lbuf, sizeof(lbuf)))
errx(1, "eof");
cmdstart = strspn(lbuf, " \t");
cmdend = cmdstart + strcspn(&lbuf[cmdstart], " \t");
argstart = cmdend + strspn(&lbuf[cmdend], " \t");
/* *cmd and *arg may be set to point at final NUL! */
*cmd = &lbuf[cmdstart];
lbuf[cmdend] = '\0';
*arg = &lbuf[argstart];
}
int
ask_num(const char *str, int dflt, int low, int high)
{
char lbuf[100];
const char *errstr;
int num;
if (dflt < low)
dflt = low;
else if (dflt > high)
dflt = high;
do {
printf("%s [%d - %d]: [%d] ", str, low, high, dflt);
if (string_from_line(lbuf, sizeof(lbuf)))
errx(1, "eof");
if (lbuf[0] == '\0') {
num = dflt;
errstr = NULL;
} else {
num = (int)strtonum(lbuf, low, high, &errstr);
if (errstr)
printf("%s is %s: %s.\n", str, errstr, lbuf);
}
} while (errstr);
return (num);
}
int
ask_pid(int dflt, struct uuid *guid)
{
char lbuf[100], *cp;
int num = -1, status;
do {
printf("Partition id ('0' to disable) [01 - FF]: [%X] ", dflt);
printf("(? for help) ");
if (string_from_line(lbuf, sizeof(lbuf)))
errx(1, "eof");
if (lbuf[0] == '?') {
PRT_printall();
continue;
}
if (guid && strlen(lbuf) == UUID_STR_LEN) {
uuid_from_string(lbuf, guid, &status);
if (status == uuid_s_ok)
return (0x100);
}
/* Convert */
cp = lbuf;
num = strtol(lbuf, &cp, 16);
/* Make sure only number present */
if (cp == lbuf)
num = dflt;
if (*cp != '\0') {
printf("'%s' is not a valid number.\n", lbuf);
num = -1;
} else if (num == 0) {
break;
} else if (num < 0 || num > 0xff) {
printf("'%x' is out of range.\n", num);
}
} while (num < 0 || num > 0xff);
return (num);
}
int
ask_yn(const char *str)
{
int ch, first;
extern int y_flag;
if (y_flag)
return (1);
printf("%s [n] ", str);
fflush(stdout);
first = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
if (ch == EOF || first == EOF)
errx(1, "eof");
return (first == 'y' || first == 'Y');
}
/*
* adapted from sbin/disklabel/editor.c
*/
u_int64_t
getuint64(char *prompt, u_int64_t oval, u_int64_t minval, u_int64_t maxval)
{
const int secsize = unit_types[SECTORS].conversion;
char buf[BUFSIZ], *endptr, *p, operator = '\0';
size_t n;
int64_t mult = 1;
double d, d2;
int rslt, secpercyl, saveerr;
char unit;
if (oval > maxval)
oval = maxval;
if (oval < minval)
oval = minval;
secpercyl = disk.sectors * disk.heads;
do {
printf("%s [%llu - %llu]: [%llu] ", prompt, minval, maxval,
oval);
if (string_from_line(buf, sizeof(buf)))
errx(1, "eof");
if (buf[0] == '\0') {
rslt = snprintf(buf, sizeof(buf), "%llu", oval);
if (rslt == -1 || rslt >= sizeof(buf))
errx(1, "default value too long");
} else if (buf[0] == '*' && buf[1] == '\0') {
return (maxval);
}
/* deal with units */
n = strlen(buf);
switch (tolower((unsigned char)buf[n-1])) {
case 'c':
unit = 'c';
mult = secpercyl;
buf[--n] = '\0';
break;
case 'b':
unit = 'b';
mult = -(int64_t)secsize;
buf[--n] = '\0';
break;
case 's':
unit = 's';
mult = 1;
buf[--n] = '\0';
break;
case 'k':
unit = 'k';
if (secsize > 1024)
mult = -(int64_t)secsize / 1024LL;
else
mult = 1024LL / secsize;
buf[--n] = '\0';
break;
case 'm':
unit = 'm';
mult = (1024LL * 1024) / secsize;
buf[--n] = '\0';
break;
case 'g':
unit = 'g';
mult = (1024LL * 1024 * 1024) / secsize;
buf[--n] = '\0';
break;
case 't':
unit = 't';
mult = (1024LL * 1024 * 1024 * 1024) / secsize;
buf[--n] = '\0';
break;
default:
unit = ' ';
mult = 1;
break;
}
/* deal with the operator */
p = &buf[0];
if (*p == '+' || *p == '-')
operator = *p++;
else
operator = ' ';
endptr = p;
errno = 0;
d = strtod(p, &endptr);
saveerr = errno;
d2 = d;
if (mult > 0)
d *= mult;
else {
d /= (-mult);
d2 = d;
}
/* Apply the operator */
if (operator == '+')
d = oval + d;
else if (operator == '-') {
d = oval - d;
d2 = d;
}
if (saveerr == ERANGE || d > maxval || d < minval || d < d2) {
printf("%s is out of range: %c%s%c\n", prompt, operator,
p, unit);
} else if (*endptr != '\0') {
printf("%s is invalid: %c%s%c\n", prompt, operator,
p, unit);
} else {
break;
}
} while (1);
return((u_int64_t)d);
}
char *
ask_string(const char *prompt, const char *oval)
{
static char buf[37];
buf[0] = '\0';
printf("%s: [%s] ", prompt, oval ? oval : "");
if (string_from_line(buf, sizeof(buf)))
errx(1, "eof");
if (buf[0] == '\0' && oval)
strlcpy(buf, oval, sizeof(buf));
return(buf);
}
/*
* Adapted from Hacker's Delight crc32b().
*
* To quote http://www.hackersdelight.org/permissions.htm :
*
* "You are free to use, copy, and distribute any of the code on
* this web site, whether modified by you or not. You need not give
* attribution. This includes the algorithms (some of which appear
* in Hacker's Delight), the Hacker's Assistant, and any code submitted
* by readers. Submitters implicitly agree to this."
*/
u_int32_t
crc32(const u_char *buf, const u_int32_t size)
{
int j;
u_int32_t i, byte, crc, mask;
crc = 0xFFFFFFFF;
for (i = 0; i < size; i++) {
byte = buf[i]; /* Get next byte. */
crc = crc ^ byte;
for (j = 7; j >= 0; j--) { /* Do eight times. */
mask = -(crc & 1);
crc = (crc >> 1) ^ (0xEDB88320 & mask);
}
}
return ~crc;
}
char *
utf16le_to_string(u_int16_t *utf)
{
static char name[GPTPARTNAMESIZE];
int i;
for (i = 0; i < GPTPARTNAMESIZE; i++) {
name[i] = letoh16(utf[i]) & 0x7F;
if (name[i] == '\0')
break;
}
if (i == GPTPARTNAMESIZE)
name[i - 1] = '\0';
return (name);
}
u_int16_t *
string_to_utf16le(char *ch)
{
static u_int16_t utf[GPTPARTNAMESIZE];
int i;
for (i = 0; i < GPTPARTNAMESIZE; i++) {
utf[i] = htole16((unsigned int)ch[i]);
if (utf[i] == 0)
break;
}
if (i == GPTPARTNAMESIZE)
utf[i - 1] = 0;
return (utf);
}
|