blob: 200cbca0af9e36970141947cf0ccf571cf59048f [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
srs56949a46b042011-03-15 00:34:10 -040034void ReadCString(char *inStr, int numchars) {
35 if (!fgets(inStr, numchars, stdin)) {
srs5694bf8950c2011-03-12 01:23:12 -050036 cerr << "Critical error! Failed fgets() in ReadCString()\n";
37 exit(1);
38 } // if
srs5694bf8950c2011-03-12 01:23:12 -050039} // ReadCString()
40
srs5694e7b4ff92009-08-18 13:16:10 -040041// Get a numeric value from the user, between low and high (inclusive).
42// Keeps looping until the user enters a value within that range.
43// If user provides no input, def (default value) is returned.
44// (If def is outside of the low-high range, an explicit response
45// is required.)
srs5694fed16d02010-01-27 23:03:40 -050046int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040047 int response, num;
48 char line[255];
49
50 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040051 do {
srs5694fed16d02010-01-27 23:03:40 -050052 cout << prompt;
53 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040054 num = sscanf(line, "%d", &response);
55 if (num == 1) { // user provided a response
56 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050057 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040058 } else { // user hit enter; return default
59 response = def;
60 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040061 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040062 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050063 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040064 response = low;
65 } // else
66 return (response);
67} // GetNumber()
68
69// Gets a Y/N response (and converts lowercase to uppercase)
70char GetYN(void) {
71 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040072 char response;
srs5694e7b4ff92009-08-18 13:16:10 -040073
srs56940873e9d2010-10-07 13:00:45 -040074 do {
srs5694fed16d02010-01-27 23:03:40 -050075 cout << "(Y/N): ";
srs56949a46b042011-03-15 00:34:10 -040076 ReadCString(line, sizeof(line));
srs5694e7b4ff92009-08-18 13:16:10 -040077 sscanf(line, "%c", &response);
srs56940873e9d2010-10-07 13:00:45 -040078 if (response == 'y')
79 response = 'Y';
80 if (response == 'n')
81 response = 'N';
82 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040083 return response;
84} // GetYN(void)
85
srs5694e4ac11e2009-08-31 10:13:04 -040086// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040087// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040088// or the same with "K", "M", "G", "T", or "P" as suffixes to add
89// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
90// respectively. If a "-" prefix is used, use the high value minus
91// the user-specified number of sectors (or KiB, MiB, etc.). Use the
92// def value as the default if the user just hits Enter. The sSize is
93// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050094uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
95 const string & prompt) {
96 uint64_t response;
97 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040098
99 do {
srs5694fed16d02010-01-27 23:03:40 -0500100 cout << prompt;
101 cin.getline(line, 255);
srs5694df9d3632011-01-08 18:33:24 -0500102 response = SIToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -0400103 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500104 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400105} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400106
srs5694df9d3632011-01-08 18:33:24 -0500107// Convert an SI value (K, M, G, T, or P) to its equivalent in
108// number of sectors. If no units are appended, interprets as the number
109// of sectors; otherwise, interprets as number of specified units and
110// converts to sectors. For instance, with 512-byte sectors, "1K" converts
111// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
112// inclues a "-", subtracts from high. If SIValue is empty, returns def.
113// Returns integral sector value.
114uint64_t SIToInt(string SIValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
115 int plusFlag = 0, badInput = 0;
116 uint64_t response = def, mult = 1, divide = 1;
117 char suffix;
118
119 if (sSize == 0) {
120 sSize = SECTOR_SIZE;
121 cerr << "Bug: Sector size invalid in SIToInt()!\n";
122 } // if
123
124 // Remove leading spaces, if present
125 while (SIValue[0] == ' ')
126 SIValue.erase(0, 1);
127
128 // If present, flag and remove leading plus sign
129 if (SIValue[0] == '+') {
130 plusFlag = 1;
131 SIValue.erase(0, 1);
132 } // if
133
134 // If present, flag and remove leading minus sign
135 if (SIValue[0] == '-') {
136 plusFlag = -1;
137 SIValue.erase(0, 1);
138 } // if
139
140 // Extract numeric response and, if present, suffix
141 istringstream inString(SIValue);
142 if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
143 badInput = 1;
144 inString >> response >> suffix;
145
146 // If no response, or if response == 0, use default (def)
147 if ((SIValue.length() == 0) || (response == 0)) {
148 response = def;
149 suffix = ' ';
150 plusFlag = 0;
151 } // if
152
153 // Set multiplier based on suffix
154 switch (suffix) {
155 case 'K':
156 case 'k':
157 mult = UINT64_C(1024) / sSize;
158 divide = sSize / UINT64_C(1024);
159 break;
160 case 'M':
161 case 'm':
162 mult = UINT64_C(1048576) / sSize;
163 divide = sSize / UINT64_C(1048576);
164 break;
165 case 'G':
166 case 'g':
167 mult = UINT64_C(1073741824) / sSize;
168 break;
169 case 'T':
170 case 't':
171 mult = UINT64_C(1099511627776) / sSize;
172 break;
173 case 'P':
174 case 'p':
175 mult = UINT64_C(1125899906842624) / sSize;
176 break;
177 default:
178 mult = 1;
179 } // switch
180
181 // Adjust response based on multiplier and plus flag, if present
182 if (mult > 1)
183 response *= mult;
184 else if (divide > 1)
185 response /= divide;
186 if (plusFlag == 1) {
187 // Recompute response based on low part of range (if default = high
188 // value, which should be the case when prompting for the end of a
189 // range) or the defaut value (if default != high, which should be
190 // the case for the first sector of a partition).
191 if (def == high)
192 response = response + low - UINT64_C(1);
193 else
194 response = response + def;
195 } // if
196 if (plusFlag == -1) {
197 response = high - response;
198 } // if
199
200 if (badInput)
201 response = high + UINT64_C(1);
202
203 return response;
204} // SIToInt()
205
srs56940873e9d2010-10-07 13:00:45 -0400206// Takes a size and converts this to a size in SI units (KiB, MiB, GiB,
207// TiB, or PiB), returned in C++ string form. The size is either in units
208// of the sector size or, if that parameter is omitted, in bytes.
209// (sectorSize defaults to 1).
210string BytesToSI(uint64_t size, uint32_t sectorSize) {
srs5694fed16d02010-01-27 23:03:40 -0500211 string units;
srs569408bb0da2010-02-19 17:19:55 -0500212 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400213 float sizeInSI;
214
srs56940873e9d2010-10-07 13:00:45 -0400215 sizeInSI = (float) size * (float) sectorSize;
srs5694fed16d02010-01-27 23:03:40 -0500216 units = " bytes";
217 if (sizeInSI > 1024.0) {
218 sizeInSI /= 1024.0;
219 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400220 } // if
srs5694fed16d02010-01-27 23:03:40 -0500221 if (sizeInSI > 1024.0) {
222 sizeInSI /= 1024.0;
223 units = " MiB";
224 } // if
225 if (sizeInSI > 1024.0) {
226 sizeInSI /= 1024.0;
227 units = " GiB";
228 } // if
229 if (sizeInSI > 1024.0) {
230 sizeInSI /= 1024.0;
231 units = " TiB";
232 } // if
233 if (sizeInSI > 1024.0) {
234 sizeInSI /= 1024.0;
235 units = " PiB";
236 } // if
srs569408bb0da2010-02-19 17:19:55 -0500237 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500238 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500239 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500240 } else {
srs569408bb0da2010-02-19 17:19:55 -0500241 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500242 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500243 theValue << sizeInSI << units;
244 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400245} // BlocksToSI()
246
srs56946699b012010-02-04 00:55:30 -0500247// Converts two consecutive characters in the input string into a
248// number, interpreting the string as a hexadecimal number, starting
249// at the specified position.
250unsigned char StrToHex(const string & input, unsigned int position) {
251 unsigned char retval = 0x00;
252 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400253
srs56946699b012010-02-04 00:55:30 -0500254 if (input.length() >= (position + 2)) {
255 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
256 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400257 } // if
srs56946699b012010-02-04 00:55:30 -0500258 return retval;
259} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400260
srs56940873e9d2010-10-07 13:00:45 -0400261// Returns 1 if input can be interpreted as a hexadecimal number --
262// all characters must be spaces, digits, or letters A-F (upper- or
263// lower-case), with at least one valid hexadecimal digit; otherwise
264// returns 0.
265int IsHex(const string & input) {
266 int isHex = 1, foundHex = 0, i;
267
268 for (i = 0; i < (int) input.length(); i++) {
269 if ((input[i] < '0') || (input[i] > '9')) {
270 if ((input[i] < 'A') || (input[i] > 'F')) {
271 if ((input[i] < 'a') || (input[i] > 'f')) {
272 if ((input[i] != ' ') && (input[i] != '\n')) {
273 isHex = 0;
274 }
275 } else foundHex = 1;
276 } else foundHex = 1;
277 } else foundHex = 1;
278 } // for
279 if (!foundHex)
280 isHex = 0;
281 return isHex;
282} // IsHex()
283
srs56942a9f5da2009-08-26 00:48:01 -0400284// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
285int IsLittleEndian(void) {
286 int littleE = 1; // assume little-endian (Intel-style)
287 union {
288 uint32_t num;
289 unsigned char uc[sizeof(uint32_t)];
290 } endian;
291
292 endian.num = 1;
293 if (endian.uc[0] != (unsigned char) 1) {
294 littleE = 0;
295 } // if
296 return (littleE);
297} // IsLittleEndian()
298
299// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400300void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500301 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400302 int i;
303
srs5694cb76c672010-02-11 22:22:22 -0500304 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500305 if (tempValue != NULL) {
306 memcpy(tempValue, theValue, numBytes);
307 for (i = 0; i < numBytes; i++)
308 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500309 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500310 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400311} // ReverseBytes()
312
srs56949ddc14b2010-08-22 22:44:42 -0400313// Extract integer data from argument string, which should be colon-delimited
314uint64_t GetInt(const string & argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500315 uint64_t retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400316
srs569464cbd172011-03-01 22:03:54 -0500317 istringstream inString(GetString(argument, itemNum));
srs56949ddc14b2010-08-22 22:44:42 -0400318 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400319 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400320} // GetInt()
321
322// Extract string data from argument string, which should be colon-delimited
srs56949a46b042011-03-15 00:34:10 -0400323// If string begins with a colon, that colon is skipped in the counting. If an
324// invalid itemNum is specified, returns an empty string.
srs5694bf8950c2011-03-12 01:23:12 -0500325string GetString(string argument, int itemNum) {
srs56949a46b042011-03-15 00:34:10 -0400326 size_t startPos = 0, endPos = 0;
327 string retVal = "";
328 int foundLast = 0;
329 int numFound = 0;
srs56949ddc14b2010-08-22 22:44:42 -0400330
srs56949a46b042011-03-15 00:34:10 -0400331 if (argument[0] == ':')
332 argument.erase(0, 1);
333 while ((numFound < itemNum) && (!foundLast)) {
srs569464cbd172011-03-01 22:03:54 -0500334 endPos = argument.find(':', startPos);
srs56949a46b042011-03-15 00:34:10 -0400335 numFound++;
336 if (endPos == string::npos) {
337 foundLast = 1;
338 endPos = argument.length();
339 } else if (numFound < itemNum) {
340 startPos = endPos + 1;
341 } // if/elseif
342 } // while
343 if ((numFound == itemNum) && (numFound > 0))
344 retVal = argument.substr(startPos, endPos - startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400345
srs56949a46b042011-03-15 00:34:10 -0400346 return retVal;
srs56949ddc14b2010-08-22 22:44:42 -0400347} // GetString()