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
|
/* $OpenBSD: misc.c,v 1.79 2021/08/07 13:33:12 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 "part.h"
#include "disk.h"
#include "misc.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(const char *units)
{
int i = 0;
if (units == NULL)
return SECTORS;
while (unit_types[i].ut_abbr != NULL) {
if (strncasecmp(unit_types[i].ut_abbr, units, 1) == 0)
break;
i++;
}
if (unit_types[i].ut_abbr == NULL)
return SECTORS;
return i;
}
int
string_from_line(char *buf, const size_t buflen)
{
static char *line;
static size_t sz;
ssize_t len;
len = getline(&line, &sz, stdin);
if (len == -1)
return -1;
if (line[len - 1] == '\n')
line[len - 1] = '\0';
strlcpy(buf, line, buflen);
return 0;
}
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
*/
uint64_t
getuint64(const char *prompt, uint64_t oval, const uint64_t minval,
const uint64_t maxval)
{
char buf[BUFSIZ], *endptr, *p, operator = '\0';
const int secsize = unit_types[SECTORS].ut_conversion;
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.dk_sectors * disk.dk_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 < 0 || 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 (uint64_t)d;
}
char *
utf16le_to_string(const uint16_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;
}
uint16_t *
string_to_utf16le(const char *ch)
{
static uint16_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;
}
|