blob: 6211fdb7638d9bc99e5befe8a3875e0194f8cf71 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001// support.cc
2// Non-class support functions for gdisk program.
3// Primarily by Rod Smith, February 2009, but with a few functions
4// copied from other sources (see attributions below).
5
srs5694221e0872009-08-29 15:00:31 -04006/* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed
7 under the terms of the GNU GPL version 2, as detailed in the COPYING file. */
8
srs5694e7b4ff92009-08-18 13:16:10 -04009#define __STDC_LIMIT_MACROS
10#define __STDC_CONSTANT_MACROS
11
srs5694e7b4ff92009-08-18 13:16:10 -040012#include <stdio.h>
srs5694e7b4ff92009-08-18 13:16:10 -040013#include <stdint.h>
14#include <errno.h>
srs5694e4ac11e2009-08-31 10:13:04 -040015#include <fcntl.h>
srs5694fed16d02010-01-27 23:03:40 -050016#include <string.h>
srs5694e35eb1b2009-09-14 00:29:34 -040017#include <sys/stat.h>
srs5694fed16d02010-01-27 23:03:40 -050018#include <string>
19#include <iostream>
srs569408bb0da2010-02-19 17:19:55 -050020#include <sstream>
srs5694e7b4ff92009-08-18 13:16:10 -040021#include "support.h"
22
23#include <sys/types.h>
24
srs56945d58fe02010-01-03 20:57:08 -050025// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
26// it's not already defined. This should become unnecessary in the future.
27// Note that this is a Linux-only ioctl....
28#ifndef BLKPBSZGET
29#define BLKPBSZGET _IO(0x12,123)
30#endif
31
srs5694e7b4ff92009-08-18 13:16:10 -040032using namespace std;
33
34// Get a numeric value from the user, between low and high (inclusive).
35// Keeps looping until the user enters a value within that range.
36// If user provides no input, def (default value) is returned.
37// (If def is outside of the low-high range, an explicit response
38// is required.)
srs5694fed16d02010-01-27 23:03:40 -050039int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040040 int response, num;
41 char line[255];
42
43 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040044 do {
srs5694fed16d02010-01-27 23:03:40 -050045 cout << prompt;
46 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040047 num = sscanf(line, "%d", &response);
48 if (num == 1) { // user provided a response
49 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050050 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040051 } else { // user hit enter; return default
52 response = def;
53 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040054 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040055 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050056 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040057 response = low;
58 } // else
59 return (response);
60} // GetNumber()
61
62// Gets a Y/N response (and converts lowercase to uppercase)
63char GetYN(void) {
64 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040065 char response;
srs5694fed16d02010-01-27 23:03:40 -050066 char *junk;
srs5694e7b4ff92009-08-18 13:16:10 -040067
srs56940873e9d2010-10-07 13:00:45 -040068 do {
srs5694fed16d02010-01-27 23:03:40 -050069 cout << "(Y/N): ";
srs56945d58fe02010-01-03 20:57:08 -050070 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040071 sscanf(line, "%c", &response);
srs56940873e9d2010-10-07 13:00:45 -040072 if (response == 'y')
73 response = 'Y';
74 if (response == 'n')
75 response = 'N';
76 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040077 return response;
78} // GetYN(void)
79
srs5694e4ac11e2009-08-31 10:13:04 -040080// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040081// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040082// or the same with "K", "M", "G", "T", or "P" as suffixes to add
83// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
84// respectively. If a "-" prefix is used, use the high value minus
85// the user-specified number of sectors (or KiB, MiB, etc.). Use the
86// def value as the default if the user just hits Enter. The sSize is
87// the sector size of the device.
88uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize, const string & prompt) {
89 uint64_t response, mult = 1, divide = 1;
srs569455d92612010-03-07 22:16:07 -050090 int plusFlag = 0;
91 char suffix, line[255];
srs5694e7b4ff92009-08-18 13:16:10 -040092
srs56940873e9d2010-10-07 13:00:45 -040093 if (sSize == 0) {
94 sSize = SECTOR_SIZE;
95 cerr << "Bug: Sector size invalid in GetSectorNum()!\n";
96 } // if
97
98 do {
srs5694fed16d02010-01-27 23:03:40 -050099 cout << prompt;
100 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -0400101
102 // Remove leading spaces, if present
103 while (line[0] == ' ')
104 strcpy(line, &line[1]);
105
106 // If present, flag and remove leading plus sign
107 if (line[0] == '+') {
108 plusFlag = 1;
109 strcpy(line, &line[1]);
110 } // if
111
srs5694e4ac11e2009-08-31 10:13:04 -0400112 // If present, flag and remove leading minus sign
113 if (line[0] == '-') {
114 plusFlag = -1;
115 strcpy(line, &line[1]);
116 } // if
117
srs5694e7b4ff92009-08-18 13:16:10 -0400118 // Extract numeric response and, if present, suffix
srs569455d92612010-03-07 22:16:07 -0500119 istringstream inString(line);
120 inString >> response >> suffix;
srs5694e7b4ff92009-08-18 13:16:10 -0400121
srs5694e4ac11e2009-08-31 10:13:04 -0400122 // If no response, use default (def)
srs569455d92612010-03-07 22:16:07 -0500123 if (strlen(line) == 0) {
124 response = def;
125 suffix = ' ';
srs5694e19ba092009-08-24 14:10:35 -0400126 plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400127 } // if
128
129 // Set multiplier based on suffix
130 switch (suffix) {
131 case 'K':
132 case 'k':
srs56940873e9d2010-10-07 13:00:45 -0400133 mult = UINT64_C(1024) / sSize;
134 divide = sSize / UINT64_C(1024);
135 break;
srs5694e7b4ff92009-08-18 13:16:10 -0400136 break;
137 case 'M':
srs56940873e9d2010-10-07 13:00:45 -0400138 case 'm':
139 mult = UINT64_C(1048576) / sSize;
140 divide = sSize / UINT64_C(1048576);
srs5694e7b4ff92009-08-18 13:16:10 -0400141 break;
142 case 'G':
143 case 'g':
srs56940873e9d2010-10-07 13:00:45 -0400144 mult = UINT64_C(1073741824) / sSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400145 break;
146 case 'T':
srs56940873e9d2010-10-07 13:00:45 -0400147 case 't':
148 mult = UINT64_C(1099511627776) / sSize;
149 break;
150 case 'P':
151 case 'p':
152 mult = UINT64_C(1125899906842624) / sSize;
srs5694e7b4ff92009-08-18 13:16:10 -0400153 break;
154 default:
155 mult = 1;
156 } // switch
157
158 // Adjust response based on multiplier and plus flag, if present
srs56940873e9d2010-10-07 13:00:45 -0400159 if (mult > 1)
160 response *= mult;
161 else if (divide > 1)
162 response /= divide;
srs5694e7b4ff92009-08-18 13:16:10 -0400163 if (plusFlag == 1) {
srs5694ba00fed2010-01-12 18:18:36 -0500164 // Recompute response based on low part of range (if default = high
165 // value, which should be the case when prompting for the end of a
166 // range) or the defaut value (if default != high, which should be
167 // the case for the first sector of a partition).
168 if (def == high)
srs569455d92612010-03-07 22:16:07 -0500169 response = response + low - UINT64_C(1);
srs5694ba00fed2010-01-12 18:18:36 -0500170 else
srs569455d92612010-03-07 22:16:07 -0500171 response = response + def - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400172 } // if
173 if (plusFlag == -1) {
srs569455d92612010-03-07 22:16:07 -0500174 response = high - response;
srs5694e19ba092009-08-24 14:10:35 -0400175 } // if
srs56940873e9d2010-10-07 13:00:45 -0400176 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500177 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400178} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400179
srs56940873e9d2010-10-07 13:00:45 -0400180// Takes a size and converts this to a size in SI units (KiB, MiB, GiB,
181// TiB, or PiB), returned in C++ string form. The size is either in units
182// of the sector size or, if that parameter is omitted, in bytes.
183// (sectorSize defaults to 1).
184string BytesToSI(uint64_t size, uint32_t sectorSize) {
srs5694fed16d02010-01-27 23:03:40 -0500185 string units;
srs569408bb0da2010-02-19 17:19:55 -0500186 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400187 float sizeInSI;
188
srs56940873e9d2010-10-07 13:00:45 -0400189 sizeInSI = (float) size * (float) sectorSize;
srs5694fed16d02010-01-27 23:03:40 -0500190 units = " bytes";
191 if (sizeInSI > 1024.0) {
192 sizeInSI /= 1024.0;
193 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400194 } // if
srs5694fed16d02010-01-27 23:03:40 -0500195 if (sizeInSI > 1024.0) {
196 sizeInSI /= 1024.0;
197 units = " MiB";
198 } // if
199 if (sizeInSI > 1024.0) {
200 sizeInSI /= 1024.0;
201 units = " GiB";
202 } // if
203 if (sizeInSI > 1024.0) {
204 sizeInSI /= 1024.0;
205 units = " TiB";
206 } // if
207 if (sizeInSI > 1024.0) {
208 sizeInSI /= 1024.0;
209 units = " PiB";
210 } // if
srs569408bb0da2010-02-19 17:19:55 -0500211 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500212 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500213 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500214 } else {
srs569408bb0da2010-02-19 17:19:55 -0500215 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500216 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500217 theValue << sizeInSI << units;
218 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400219} // BlocksToSI()
220
srs56946699b012010-02-04 00:55:30 -0500221// Converts two consecutive characters in the input string into a
222// number, interpreting the string as a hexadecimal number, starting
223// at the specified position.
224unsigned char StrToHex(const string & input, unsigned int position) {
225 unsigned char retval = 0x00;
226 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400227
srs56946699b012010-02-04 00:55:30 -0500228 if (input.length() >= (position + 2)) {
229 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
230 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400231 } // if
srs56946699b012010-02-04 00:55:30 -0500232 return retval;
233} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400234
srs56940873e9d2010-10-07 13:00:45 -0400235// Returns 1 if input can be interpreted as a hexadecimal number --
236// all characters must be spaces, digits, or letters A-F (upper- or
237// lower-case), with at least one valid hexadecimal digit; otherwise
238// returns 0.
239int IsHex(const string & input) {
240 int isHex = 1, foundHex = 0, i;
241
242 for (i = 0; i < (int) input.length(); i++) {
243 if ((input[i] < '0') || (input[i] > '9')) {
244 if ((input[i] < 'A') || (input[i] > 'F')) {
245 if ((input[i] < 'a') || (input[i] > 'f')) {
246 if ((input[i] != ' ') && (input[i] != '\n')) {
247 isHex = 0;
248 }
249 } else foundHex = 1;
250 } else foundHex = 1;
251 } else foundHex = 1;
252 } // for
253 if (!foundHex)
254 isHex = 0;
255 return isHex;
256} // IsHex()
257
srs56942a9f5da2009-08-26 00:48:01 -0400258// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
259int IsLittleEndian(void) {
260 int littleE = 1; // assume little-endian (Intel-style)
261 union {
262 uint32_t num;
263 unsigned char uc[sizeof(uint32_t)];
264 } endian;
265
266 endian.num = 1;
267 if (endian.uc[0] != (unsigned char) 1) {
268 littleE = 0;
269 } // if
270 return (littleE);
271} // IsLittleEndian()
272
273// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400274void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500275 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400276 int i;
277
srs5694cb76c672010-02-11 22:22:22 -0500278 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500279 if (tempValue != NULL) {
280 memcpy(tempValue, theValue, numBytes);
281 for (i = 0; i < numBytes; i++)
282 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500283 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500284 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400285} // ReverseBytes()
286
srs56949ddc14b2010-08-22 22:44:42 -0400287// Extract integer data from argument string, which should be colon-delimited
288uint64_t GetInt(const string & argument, int itemNum) {
289 int startPos = -1, endPos = -1;
290 uint64_t retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400291
srs56949ddc14b2010-08-22 22:44:42 -0400292 while (itemNum-- > 0) {
293 startPos = endPos + 1;
srs5694ab4b0432010-09-25 20:39:52 -0400294 endPos = (int) argument.find(':', startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400295 }
296 if (endPos == (int) string::npos)
srs5694ab4b0432010-09-25 20:39:52 -0400297 endPos = (int) argument.length();
srs56949ddc14b2010-08-22 22:44:42 -0400298 endPos--;
299
300 istringstream inString(argument.substr(startPos, endPos - startPos + 1));
301 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400302 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400303} // GetInt()
304
305// Extract string data from argument string, which should be colon-delimited
306string GetString(const string & argument, int itemNum) {
307 int startPos = -1, endPos = -1;
308
309 while (itemNum-- > 0) {
310 startPos = endPos + 1;
srs5694ab4b0432010-09-25 20:39:52 -0400311 endPos = (int) argument.find(':', startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400312 }
313 if (endPos == (int) string::npos)
srs5694ab4b0432010-09-25 20:39:52 -0400314 endPos = (int) argument.length();
srs56949ddc14b2010-08-22 22:44:42 -0400315 endPos--;
316
317 return argument.substr(startPos, endPos - startPos + 1);
318} // GetString()