1 /* $OpenBSD: misc.c,v 1.18 2006/11/08 23:22:26 deraadt 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 do { 100 again: 101 num = dflt; 102 if (flags == ASK_HEX) 103 printf("%s [%X - %X]: [%X] ", str, low, high, num); 104 else 105 printf("%s [%d - %d]: [%d] ", str, low, high, num); 106 if (help) 107 printf("(? for help) "); 108 109 if (fgets(lbuf, sizeof lbuf, stdin) == NULL) 110 errx(1, "eof"); 111 lbuflen = strlen(lbuf); 112 if (lbuflen > 0 && lbuf[lbuflen - 1] == '\n') 113 lbuf[lbuflen - 1] = '\0'; 114 115 if (help && lbuf[0] == '?') { 116 (*help)(); 117 goto again; 118 } 119 120 /* Convert */ 121 cp = lbuf; 122 num = strtol(lbuf, &cp, ((flags==ASK_HEX)?16:10)); 123 124 /* Make sure only number present */ 125 if (cp == lbuf) 126 num = dflt; 127 if (*cp != '\0') { 128 printf("'%s' is not a valid number.\n", lbuf); 129 num = low - 1; 130 } else if (num < low || num > high) { 131 printf("'%d' is out of range.\n", num); 132 } 133 } while (num < low || num > high); 134 135 return (num); 136 } 137 138 int 139 ask_yn(const char *str) 140 { 141 int ch, first; 142 extern int y_flag; 143 144 if (y_flag) 145 return (1); 146 147 printf("%s [n] ", str); 148 fflush(stdout); 149 150 first = ch = getchar(); 151 while (ch != '\n' && ch != EOF) 152 ch = getchar(); 153 154 if (ch == EOF || first == EOF) 155 errx(1, "eof"); 156 157 return (first == 'y' || first == 'Y'); 158 } 159 160 u_int16_t 161 getshort(void *p) 162 { 163 unsigned char *cp = p; 164 165 return (cp[0] | (cp[1] << 8)); 166 } 167 168 void 169 putshort(void *p, u_int16_t l) 170 { 171 unsigned char *cp = p; 172 173 *cp++ = l; 174 *cp++ = l >> 8; 175 } 176 177 u_int32_t 178 getlong(void *p) 179 { 180 unsigned char *cp = p; 181 182 return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24)); 183 } 184 185 void 186 putlong(void *p, u_int32_t l) 187 { 188 unsigned char *cp = p; 189 190 *cp++ = l; 191 *cp++ = l >> 8; 192 *cp++ = l >> 16; 193 *cp++ = l >> 24; 194 } 195 196 /* 197 * adapted from sbin/disklabel/editor.c 198 * Returns UINT_MAX on error 199 */ 200 u_int32_t 201 getuint(disk_t *disk, char *prompt, char *helpstring, u_int32_t oval, 202 u_int32_t maxval, u_int32_t offset, int flags) 203 { 204 char buf[BUFSIZ], *endptr, *p, operator = '\0'; 205 u_int32_t rval = oval; 206 size_t n; 207 int mult = 1, secsize = unit_types[SECTORS].conversion; 208 double d; 209 int secpercyl; 210 211 secpercyl = disk->real->sectors * disk->real->heads; 212 213 /* We only care about the remainder */ 214 offset = offset % secpercyl; 215 216 buf[0] = '\0'; 217 do { 218 printf("%s: [%u] ", prompt, oval); 219 if (fgets(buf, sizeof(buf), stdin) == NULL) { 220 buf[0] = '\0'; 221 if (feof(stdin)) { 222 clearerr(stdin); 223 putchar('\n'); 224 return(UINT_MAX - 1); 225 } 226 } 227 n = strlen(buf); 228 if (n > 0 && buf[n-1] == '\n') 229 buf[--n] = '\0'; 230 if (buf[0] == '?') 231 puts(helpstring); 232 } while (buf[0] == '?'); 233 234 if (buf[0] == '*' && buf[1] == '\0') { 235 rval = maxval; 236 } else { 237 /* deal with units */ 238 if (buf[0] != '\0' && n > 0) { 239 if ((flags & DO_CONVERSIONS)) { 240 switch (tolower(buf[n-1])) { 241 242 case 'c': 243 mult = secpercyl; 244 buf[--n] = '\0'; 245 break; 246 case 'b': 247 mult = -secsize; 248 buf[--n] = '\0'; 249 break; 250 case 's': 251 buf[--n] = '\0'; 252 break; 253 case 'k': 254 if (secsize > 1024) 255 mult = -secsize / 1024; 256 else 257 mult = 1024 / secsize; 258 buf[--n] = '\0'; 259 break; 260 case 'm': 261 mult = 1048576 / secsize; 262 buf[--n] = '\0'; 263 break; 264 case 'g': 265 mult = 1073741824 / secsize; 266 buf[--n] = '\0'; 267 break; 268 } 269 } 270 271 /* Did they give us an operator? */ 272 p = &buf[0]; 273 if (*p == '+' || *p == '-') 274 operator = *p++; 275 276 endptr = p; 277 errno = 0; 278 d = strtod(p, &endptr); 279 if (errno == ERANGE) 280 rval = UINT_MAX; /* too big/small */ 281 else if (*endptr != '\0') { 282 errno = EINVAL; /* non-numbers in str */ 283 rval = UINT_MAX; 284 } else { 285 /* XXX - should check for overflow */ 286 if (mult > 0) 287 rval = d * mult; 288 else 289 /* Negative mult means divide (fancy) */ 290 rval = d / (-mult); 291 292 /* Apply the operator */ 293 if (operator == '+') 294 rval += oval; 295 else if (operator == '-') 296 rval = oval - rval; 297 } 298 } 299 } 300 if ((flags & DO_ROUNDING) && rval < UINT_MAX) { 301 #ifndef CYLCHECK 302 /* Round to nearest cylinder unless given in sectors */ 303 if (mult != 1) 304 #endif 305 { 306 u_int32_t cyls; 307 308 /* If we round up past the end, round down instead */ 309 cyls = (u_int32_t)((rval / (double)secpercyl) 310 + 0.5); 311 if (cyls != 0 && secpercyl != 0) { 312 if ((cyls * secpercyl) - offset > maxval) 313 cyls--; 314 315 if (rval != (cyls * secpercyl) - offset) { 316 rval = (cyls * secpercyl) - offset; 317 printf("Rounding to nearest cylinder: %u\n", 318 rval); 319 } 320 } 321 } 322 } 323 324 return(rval); 325 } 326