xref: /openbsd/sbin/fdisk/misc.c (revision cca36db2)
1 /*	$OpenBSD: misc.c,v 1.23 2010/07/04 22:15:31 halex Exp $	*/
2 
3 /*
4  * Copyright (c) 1997 Tobias Weingartner
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <err.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/disklabel.h>
35 #include <limits.h>
36 #include "misc.h"
37 
38 struct unit_type unit_types[] = {
39 	{"b", 1			, "Bytes"},
40 	{" ", DEV_BSIZE		, "Sectors"},
41 	{"K", 1024		, "Kilobytes"},
42 	{"M", 1024 * 1024	, "Megabytes"},
43 	{"G", 1024 * 1024 *1024	, "Gigabytes"},
44 	{NULL, 0		, NULL },
45 };
46 
47 int
48 unit_lookup(char *units)
49 {
50 	int i = 0;
51 	if (units == NULL)
52 		return (UNIT_TYPE_DEFAULT);
53 
54 	while (unit_types[i].abbr != NULL) {
55 		if (strncasecmp(unit_types[i].abbr, units, 1) == 0)
56 			break;
57 		i++;
58 	}
59 	/* default */
60 	if (unit_types[i].abbr == NULL)
61 		return (UNIT_TYPE_DEFAULT);
62 
63 	return (i);
64 }
65 
66 int
67 ask_cmd(cmd_t *cmd)
68 {
69 	char lbuf[100], *cp, *buf;
70 	size_t lbuflen;
71 
72 	/* Get input */
73 	if (fgets(lbuf, sizeof lbuf, stdin) == NULL)
74 		errx(1, "eof");
75 	lbuflen = strlen(lbuf);
76 	if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n')
77 		lbuf[lbuflen - 1] = '\0';
78 
79 	/* Parse input */
80 	buf = lbuf;
81 	buf = &buf[strspn(buf, " \t")];
82 	cp = &buf[strcspn(buf, " \t")];
83 	*cp++ = '\0';
84 	strncpy(cmd->cmd, buf, sizeof(cmd->cmd));
85 	buf = &cp[strspn(cp, " \t")];
86 	strncpy(cmd->args, buf, sizeof(cmd->args));
87 
88 	return (0);
89 }
90 
91 int
92 ask_num(const char *str, int flags, int dflt, int low, int high,
93     void (*help)(void))
94 {
95 	char lbuf[100], *cp;
96 	size_t lbuflen;
97 	int num;
98 
99 	if (dflt < low)
100 		dflt = low;
101 	else if (dflt > high)
102 		dflt = high;
103 
104 	do {
105 again:
106 		if (flags == ASK_HEX)
107 			printf("%s [%X - %X]: [%X] ", str, low, high, dflt);
108 		else
109 			printf("%s [%d - %d]: [%d] ", str, low, high, dflt);
110 		if (help)
111 			printf("(? for help) ");
112 
113 		if (fgets(lbuf, sizeof lbuf, stdin) == NULL)
114 			errx(1, "eof");
115 		lbuflen = strlen(lbuf);
116 		if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n')
117 			lbuf[lbuflen - 1] = '\0';
118 
119 		if (help && lbuf[0] == '?') {
120 			(*help)();
121 			goto again;
122 		}
123 
124 		/* Convert */
125 		cp = lbuf;
126 		num = strtol(lbuf, &cp, ((flags==ASK_HEX)?16:10));
127 
128 		/* Make sure only number present */
129 		if (cp == lbuf)
130 			num = dflt;
131 		if (*cp != '\0') {
132 			printf("'%s' is not a valid number.\n", lbuf);
133 			num = low - 1;
134 		} else if (num < low || num > high) {
135 			printf("'%d' is out of range.\n", num);
136 		}
137 	} while (num < low || num > high);
138 
139 	return (num);
140 }
141 
142 int
143 ask_yn(const char *str)
144 {
145 	int ch, first;
146 	extern int y_flag;
147 
148 	if (y_flag)
149 		return (1);
150 
151 	printf("%s [n] ", str);
152 	fflush(stdout);
153 
154 	first = ch = getchar();
155 	while (ch != '\n' && ch != EOF)
156 		ch = getchar();
157 
158 	if (ch == EOF || first == EOF)
159 		errx(1, "eof");
160 
161 	return (first == 'y' || first == 'Y');
162 }
163 
164 u_int16_t
165 getshort(void *p)
166 {
167 	unsigned char *cp = p;
168 
169 	return (cp[0] | (cp[1] << 8));
170 }
171 
172 void
173 putshort(void *p, u_int16_t l)
174 {
175 	unsigned char *cp = p;
176 
177 	*cp++ = l;
178 	*cp++ = l >> 8;
179 }
180 
181 u_int32_t
182 getlong(void *p)
183 {
184 	unsigned char *cp = p;
185 
186 	return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
187 }
188 
189 void
190 putlong(void *p, u_int32_t l)
191 {
192 	unsigned char *cp = p;
193 
194 	*cp++ = l;
195 	*cp++ = l >> 8;
196 	*cp++ = l >> 16;
197 	*cp++ = l >> 24;
198 }
199 
200 /*
201  * adapted from sbin/disklabel/editor.c
202  * Returns UINT_MAX on error
203  */
204 u_int32_t
205 getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval,
206     u_int32_t maxval, u_int32_t offset,	int flags)
207 {
208 	char buf[BUFSIZ], *endptr, *p, operator = '\0';
209 	u_int32_t rval = oval;
210 	size_t n;
211 	int mult = 1, secsize = unit_types[SECTORS].conversion;
212 	double d;
213 	int secpercyl;
214 
215 	secpercyl = disk->real->sectors * disk->real->heads;
216 
217 	/* We only care about the remainder */
218 	offset = offset % secpercyl;
219 
220 	buf[0] = '\0';
221 	do {
222 		printf("%s: [%u] ", prompt, oval);
223 		if (fgets(buf, sizeof(buf), stdin) == NULL)
224 			errx(1, "eof");
225 		n = strlen(buf);
226 		if (n > 0 && buf[n-1] == '\n')
227 			buf[--n] = '\0';
228 		if (buf[0] == '?')
229 			puts(helpstring);
230 	} while (buf[0] == '?');
231 
232 	if (buf[0] == '*' && buf[1] == '\0') {
233 		rval = maxval;
234 	} else {
235 		/* deal with units */
236 		if (buf[0] != '\0' && n > 0) {
237 			if ((flags & DO_CONVERSIONS)) {
238 				switch (tolower(buf[n-1])) {
239 
240 				case 'c':
241 					mult = secpercyl;
242 					buf[--n] = '\0';
243 					break;
244 				case 'b':
245 					mult = -secsize;
246 					buf[--n] = '\0';
247 					break;
248 				case 's':
249 					buf[--n] = '\0';
250 					break;
251 				case 'k':
252 					if (secsize > 1024)
253 						mult = -secsize / 1024;
254 					else
255 						mult = 1024 / secsize;
256 					buf[--n] = '\0';
257 					break;
258 				case 'm':
259 					mult = 1048576 / secsize;
260 					buf[--n] = '\0';
261 					break;
262 				case 'g':
263 					mult = 1073741824 / secsize;
264 					buf[--n] = '\0';
265 					break;
266 				}
267 			}
268 
269 			/* Did they give us an operator? */
270 			p = &buf[0];
271 			if (*p == '+' || *p == '-')
272 				operator = *p++;
273 
274 			endptr = p;
275 			errno = 0;
276 			d = strtod(p, &endptr);
277 			if (errno == ERANGE)
278 				rval = UINT_MAX;	/* too big/small */
279 			else if (*endptr != '\0') {
280 				errno = EINVAL;		/* non-numbers in str */
281 				rval = UINT_MAX;
282 			} else {
283 				/* XXX - should check for overflow */
284 				if (mult > 0)
285 					rval = d * mult;
286 				else
287 					/* Negative mult means divide (fancy) */
288 					rval = d / (-mult);
289 
290 				/* Apply the operator */
291 				if (operator == '+')
292 					rval += oval;
293 				else if (operator == '-')
294 					rval = oval - rval;
295 			}
296 		}
297 	}
298 	if ((flags & DO_ROUNDING) && rval < UINT_MAX) {
299 #ifndef CYLCHECK
300 		/* Round to nearest cylinder unless given in sectors */
301 		if (mult != 1)
302 #endif
303 		{
304 			u_int32_t cyls;
305 
306 			/* If we round up past the end, round down instead */
307 			cyls = (u_int32_t)((rval / (double)secpercyl)
308 			    + 0.5);
309 			if (cyls != 0 && secpercyl != 0) {
310 				if ((cyls * secpercyl) - offset > maxval)
311 					cyls--;
312 
313 				if (rval != (cyls * secpercyl) - offset) {
314 					rval = (cyls * secpercyl) - offset;
315 					printf("Rounding to nearest cylinder: %u\n",
316 					    rval);
317 				}
318 			}
319 		}
320 	}
321 
322 	return(rval);
323 }
324