blob: ee445661bea66ebca11b6d16d84e6207776dda2a [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.
srs5694df9d3632011-01-08 18:33:24 -050088uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
89 const string & prompt) {
90 uint64_t response;
91 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040092
93 do {
srs5694fed16d02010-01-27 23:03:40 -050094 cout << prompt;
95 cin.getline(line, 255);
srs5694df9d3632011-01-08 18:33:24 -050096 response = SIToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -040097 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -050098 return response;
srs5694e4ac11e2009-08-31 10:13:04 -040099} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400100
srs5694df9d3632011-01-08 18:33:24 -0500101// Convert an SI value (K, M, G, T, or P) to its equivalent in
102// number of sectors. If no units are appended, interprets as the number
103// of sectors; otherwise, interprets as number of specified units and
104// converts to sectors. For instance, with 512-byte sectors, "1K" converts
105// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
106// inclues a "-", subtracts from high. If SIValue is empty, returns def.
107// Returns integral sector value.
108uint64_t SIToInt(string SIValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
109 int plusFlag = 0, badInput = 0;
110 uint64_t response = def, mult = 1, divide = 1;
111 char suffix;
112
113 if (sSize == 0) {
114 sSize = SECTOR_SIZE;
115 cerr << "Bug: Sector size invalid in SIToInt()!\n";
116 } // if
117
118 // Remove leading spaces, if present
119 while (SIValue[0] == ' ')
120 SIValue.erase(0, 1);
121
122 // If present, flag and remove leading plus sign
123 if (SIValue[0] == '+') {
124 plusFlag = 1;
125 SIValue.erase(0, 1);
126 } // if
127
128 // If present, flag and remove leading minus sign
129 if (SIValue[0] == '-') {
130 plusFlag = -1;
131 SIValue.erase(0, 1);
132 } // if
133
134 // Extract numeric response and, if present, suffix
135 istringstream inString(SIValue);
136 if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
137 badInput = 1;
138 inString >> response >> suffix;
139
140 // If no response, or if response == 0, use default (def)
141 if ((SIValue.length() == 0) || (response == 0)) {
142 response = def;
143 suffix = ' ';
144 plusFlag = 0;
145 } // if
146
147 // Set multiplier based on suffix
148 switch (suffix) {
149 case 'K':
150 case 'k':
151 mult = UINT64_C(1024) / sSize;
152 divide = sSize / UINT64_C(1024);
153 break;
154 case 'M':
155 case 'm':
156 mult = UINT64_C(1048576) / sSize;
157 divide = sSize / UINT64_C(1048576);
158 break;
159 case 'G':
160 case 'g':
161 mult = UINT64_C(1073741824) / sSize;
162 break;
163 case 'T':
164 case 't':
165 mult = UINT64_C(1099511627776) / sSize;
166 break;
167 case 'P':
168 case 'p':
169 mult = UINT64_C(1125899906842624) / sSize;
170 break;
171 default:
172 mult = 1;
173 } // switch
174
175 // Adjust response based on multiplier and plus flag, if present
176 if (mult > 1)
177 response *= mult;
178 else if (divide > 1)
179 response /= divide;
180 if (plusFlag == 1) {
181 // Recompute response based on low part of range (if default = high
182 // value, which should be the case when prompting for the end of a
183 // range) or the defaut value (if default != high, which should be
184 // the case for the first sector of a partition).
185 if (def == high)
186 response = response + low - UINT64_C(1);
187 else
188 response = response + def;
189 } // if
190 if (plusFlag == -1) {
191 response = high - response;
192 } // if
193
194 if (badInput)
195 response = high + UINT64_C(1);
196
197 return response;
198} // SIToInt()
199
srs56940873e9d2010-10-07 13:00:45 -0400200// Takes a size and converts this to a size in SI units (KiB, MiB, GiB,
201// TiB, or PiB), returned in C++ string form. The size is either in units
202// of the sector size or, if that parameter is omitted, in bytes.
203// (sectorSize defaults to 1).
204string BytesToSI(uint64_t size, uint32_t sectorSize) {
srs5694fed16d02010-01-27 23:03:40 -0500205 string units;
srs569408bb0da2010-02-19 17:19:55 -0500206 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400207 float sizeInSI;
208
srs56940873e9d2010-10-07 13:00:45 -0400209 sizeInSI = (float) size * (float) sectorSize;
srs5694fed16d02010-01-27 23:03:40 -0500210 units = " bytes";
211 if (sizeInSI > 1024.0) {
212 sizeInSI /= 1024.0;
213 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400214 } // if
srs5694fed16d02010-01-27 23:03:40 -0500215 if (sizeInSI > 1024.0) {
216 sizeInSI /= 1024.0;
217 units = " MiB";
218 } // if
219 if (sizeInSI > 1024.0) {
220 sizeInSI /= 1024.0;
221 units = " GiB";
222 } // if
223 if (sizeInSI > 1024.0) {
224 sizeInSI /= 1024.0;
225 units = " TiB";
226 } // if
227 if (sizeInSI > 1024.0) {
228 sizeInSI /= 1024.0;
229 units = " PiB";
230 } // if
srs569408bb0da2010-02-19 17:19:55 -0500231 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500232 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500233 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500234 } else {
srs569408bb0da2010-02-19 17:19:55 -0500235 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500236 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500237 theValue << sizeInSI << units;
238 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400239} // BlocksToSI()
240
srs56946699b012010-02-04 00:55:30 -0500241// Converts two consecutive characters in the input string into a
242// number, interpreting the string as a hexadecimal number, starting
243// at the specified position.
244unsigned char StrToHex(const string & input, unsigned int position) {
245 unsigned char retval = 0x00;
246 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400247
srs56946699b012010-02-04 00:55:30 -0500248 if (input.length() >= (position + 2)) {
249 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
250 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400251 } // if
srs56946699b012010-02-04 00:55:30 -0500252 return retval;
253} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400254
srs56940873e9d2010-10-07 13:00:45 -0400255// Returns 1 if input can be interpreted as a hexadecimal number --
256// all characters must be spaces, digits, or letters A-F (upper- or
257// lower-case), with at least one valid hexadecimal digit; otherwise
258// returns 0.
259int IsHex(const string & input) {
260 int isHex = 1, foundHex = 0, i;
261
262 for (i = 0; i < (int) input.length(); i++) {
263 if ((input[i] < '0') || (input[i] > '9')) {
264 if ((input[i] < 'A') || (input[i] > 'F')) {
265 if ((input[i] < 'a') || (input[i] > 'f')) {
266 if ((input[i] != ' ') && (input[i] != '\n')) {
267 isHex = 0;
268 }
269 } else foundHex = 1;
270 } else foundHex = 1;
271 } else foundHex = 1;
272 } // for
273 if (!foundHex)
274 isHex = 0;
275 return isHex;
276} // IsHex()
277
srs56942a9f5da2009-08-26 00:48:01 -0400278// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
279int IsLittleEndian(void) {
280 int littleE = 1; // assume little-endian (Intel-style)
281 union {
282 uint32_t num;
283 unsigned char uc[sizeof(uint32_t)];
284 } endian;
285
286 endian.num = 1;
287 if (endian.uc[0] != (unsigned char) 1) {
288 littleE = 0;
289 } // if
290 return (littleE);
291} // IsLittleEndian()
292
293// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400294void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500295 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400296 int i;
297
srs5694cb76c672010-02-11 22:22:22 -0500298 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500299 if (tempValue != NULL) {
300 memcpy(tempValue, theValue, numBytes);
301 for (i = 0; i < numBytes; i++)
302 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500303 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500304 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400305} // ReverseBytes()
306
srs56949ddc14b2010-08-22 22:44:42 -0400307// Extract integer data from argument string, which should be colon-delimited
308uint64_t GetInt(const string & argument, int itemNum) {
309 int startPos = -1, endPos = -1;
310 uint64_t retval = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400311
srs56949ddc14b2010-08-22 22:44:42 -0400312 while (itemNum-- > 0) {
313 startPos = endPos + 1;
srs5694ab4b0432010-09-25 20:39:52 -0400314 endPos = (int) argument.find(':', startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400315 }
316 if (endPos == (int) string::npos)
srs5694ab4b0432010-09-25 20:39:52 -0400317 endPos = (int) argument.length();
srs56949ddc14b2010-08-22 22:44:42 -0400318 endPos--;
319
320 istringstream inString(argument.substr(startPos, endPos - startPos + 1));
321 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400322 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400323} // GetInt()
324
325// Extract string data from argument string, which should be colon-delimited
326string GetString(const string & argument, int itemNum) {
327 int startPos = -1, endPos = -1;
328
329 while (itemNum-- > 0) {
330 startPos = endPos + 1;
srs5694ab4b0432010-09-25 20:39:52 -0400331 endPos = (int) argument.find(':', startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400332 }
333 if (endPos == (int) string::npos)
srs5694ab4b0432010-09-25 20:39:52 -0400334 endPos = (int) argument.length();
srs56949ddc14b2010-08-22 22:44:42 -0400335 endPos--;
336
337 return argument.substr(startPos, endPos - startPos + 1);
338} // GetString()