1 /* $OpenBSD: misc.c,v 1.22 2009/07/24 23:28:00 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 buf[0] = '\0'; 225 if (feof(stdin)) { 226 clearerr(stdin); 227 putchar('\n'); 228 return(UINT_MAX - 1); 229 } 230 } 231 n = strlen(buf); 232 if (n > 0 && buf[n-1] == '\n') 233 buf[--n] = '\0'; 234 if (buf[0] == '?') 235 puts(helpstring); 236 } while (buf[0] == '?'); 237 238 if (buf[0] == '*' && buf[1] == '\0') { 239 rval = maxval; 240 } else { 241 /* deal with units */ 242 if (buf[0] != '\0' && n > 0) { 243 if ((flags & DO_CONVERSIONS)) { 244 switch (tolower(buf[n-1])) { 245 246 case 'c': 247 mult = secpercyl; 248 buf[--n] = '\0'; 249 break; 250 case 'b': 251 mult = -secsize; 252 buf[--n] = '\0'; 253 break; 254 case 's': 255 buf[--n] = '\0'; 256 break; 257 case 'k': 258 if (secsize > 1024) 259 mult = -secsize / 1024; 260 else 261 mult = 1024 / secsize; 262 buf[--n] = '\0'; 263 break; 264 case 'm': 265 mult = 1048576 / secsize; 266 buf[--n] = '\0'; 267 break; 268 case 'g': 269 mult = 1073741824 / secsize; 270 buf[--n] = '\0'; 271 break; 272 } 273 } 274 275 /* Did they give us an operator? */ 276 p = &buf[0]; 277 if (*p == '+' || *p == '-') 278 operator = *p++; 279 280 endptr = p; 281 errno = 0; 282 d = strtod(p, &endptr); 283 if (errno == ERANGE) 284 rval = UINT_MAX; /* too big/small */ 285 else if (*endptr != '\0') { 286 errno = EINVAL; /* non-numbers in str */ 287 rval = UINT_MAX; 288 } else { 289 /* XXX - should check for overflow */ 290 if (mult > 0) 291 rval = d * mult; 292 else 293 /* Negative mult means divide (fancy) */ 294 rval = d / (-mult); 295 296 /* Apply the operator */ 297 if (operator == '+') 298 rval += oval; 299 else if (operator == '-') 300 rval = oval - rval; 301 } 302 } 303 } 304 if ((flags & DO_ROUNDING) && rval < UINT_MAX) { 305 #ifndef CYLCHECK 306 /* Round to nearest cylinder unless given in sectors */ 307 if (mult != 1) 308 #endif 309 { 310 u_int32_t cyls; 311 312 /* If we round up past the end, round down instead */ 313 cyls = (u_int32_t)((rval / (double)secpercyl) 314 + 0.5); 315 if (cyls != 0 && secpercyl != 0) { 316 if ((cyls * secpercyl) - offset > maxval) 317 cyls--; 318 319 if (rval != (cyls * secpercyl) - offset) { 320 rval = (cyls * secpercyl) - offset; 321 printf("Rounding to nearest cylinder: %u\n", 322 rval); 323 } 324 } 325 } 326 } 327 328 return(rval); 329 } 330