xref: /openbsd/sbin/fdisk/misc.c (revision 78b63d65)
1 /*	$OpenBSD: misc.c,v 1.6 1997/10/19 23:30:48 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  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *    This product includes software developed by Tobias Weingartner.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <err.h>
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/disklabel.h>
39 #include "misc.h"
40 
41 
42 int
43 ask_cmd(cmd)
44 	cmd_t *cmd;
45 {
46 	char lbuf[100], *cp, *buf;
47 
48 	/* Get input */
49 	if (fgets(lbuf, sizeof lbuf, stdin) == NULL)
50 		errx(1, "eof");
51 	lbuf[strlen(lbuf)-1] = '\0';
52 
53 	/* Parse input */
54 	buf = lbuf;
55 	buf = &buf[strspn(buf, " \t")];
56 	cp = &buf[strcspn(buf, " \t")];
57 	*cp++ = '\0';
58 	strncpy(cmd->cmd, buf, 10);
59 	buf = &cp[strspn(cp, " \t")];
60 	strncpy(cmd->args, buf, 100);
61 
62 	return (0);
63 }
64 
65 int
66 ask_num(str, flags, dflt, low, high, help)
67 	const char *str;
68 	int flags;
69 	int dflt;
70 	int low;
71 	int high;
72 	void (*help) __P((void));
73 {
74 	char lbuf[100], *cp;
75 	int num;
76 
77 	do {
78 again:
79 		num = dflt;
80 		if (flags == ASK_HEX)
81 			printf("%s [%X - %X]: [%X] ", str, low, high, num);
82 		else
83 			printf("%s [%d - %d]: [%d] ", str, low, high, num);
84 		if (help)
85 			printf("(? for help) ");
86 
87 		if (fgets(lbuf, sizeof lbuf, stdin) == NULL)
88 			errx(1, "eof");
89 		lbuf[strlen(lbuf)-1] = '\0';
90 
91 		if (help && lbuf[0] == '?') {
92 			(*help)();
93 			goto again;
94 		}
95 
96 		/* Convert */
97 		cp = lbuf;
98 		num = strtol(lbuf, &cp, ((flags==ASK_HEX)?16:10));
99 
100 		/* Make sure only number present */
101 		if (cp == lbuf)
102 			num = dflt;
103 		if (*cp != '\0') {
104 			printf("'%s' is not a valid number.\n", lbuf);
105 			num = low - 1;
106 		} else if (num < low || num > high) {
107 			printf("'%d' is out of range.\n", num);
108 		}
109 	} while (num < low || num > high);
110 
111 	return (num);
112 }
113 
114 int
115 ask_yn(str)
116 	const char *str;
117 {
118 	int ch, first;
119 
120 	printf("%s [n] ", str);
121 	fflush(stdout);
122 
123 	first = ch = getchar();
124 	while (ch != '\n' && ch != EOF)
125 		ch = getchar();
126 
127 	if (ch == EOF || first == EOF)
128 		errx(1, "eof");
129 
130 	return (first == 'y' || first == 'Y');
131 }
132 
133 u_int16_t
134 getshort(p)
135 	void *p;
136 {
137 	unsigned char *cp = p;
138 
139 	return (cp[0] | (cp[1] << 8));
140 }
141 
142 void
143 putshort(p, l)
144 	void *p;
145 	u_int16_t l;
146 {
147 	unsigned char *cp = p;
148 
149 	*cp++ = l;
150 	*cp++ = l >> 8;
151 }
152 
153 u_int32_t
154 getlong(p)
155 	void *p;
156 {
157 	unsigned char *cp = p;
158 
159 	return (cp[0] | (cp[1] << 8) | (cp[2] << 16) | (cp[3] << 24));
160 }
161 
162 void
163 putlong(p, l)
164 	void *p;
165 	u_int32_t l;
166 {
167 	unsigned char *cp = p;
168 
169 	*cp++ = l;
170 	*cp++ = l >> 8;
171 	*cp++ = l >> 16;
172 	*cp++ = l >> 24;
173 }
174