blob: f88f02108bf91a9a230e52b345b7f398664c2ecf [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
srs5694bf8950c2011-03-12 01:23:12 -050034char* ReadCString(char *inStr, int numchars) {
35 if (!fgets(inStr, 255, stdin)) {
36 cerr << "Critical error! Failed fgets() in ReadCString()\n";
37 exit(1);
38 } // if
39 return inStr;
40} // ReadCString()
41
srs5694e7b4ff92009-08-18 13:16:10 -040042// Get a numeric value from the user, between low and high (inclusive).
43// Keeps looping until the user enters a value within that range.
44// If user provides no input, def (default value) is returned.
45// (If def is outside of the low-high range, an explicit response
46// is required.)
srs5694fed16d02010-01-27 23:03:40 -050047int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040048 int response, num;
49 char line[255];
50
51 if (low != high) { // bother only if low and high differ...
srs56940873e9d2010-10-07 13:00:45 -040052 do {
srs5694fed16d02010-01-27 23:03:40 -050053 cout << prompt;
54 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040055 num = sscanf(line, "%d", &response);
56 if (num == 1) { // user provided a response
57 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050058 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040059 } else { // user hit enter; return default
60 response = def;
61 } // if/else
srs56940873e9d2010-10-07 13:00:45 -040062 } while ((response < low) || (response > high));
srs5694e7b4ff92009-08-18 13:16:10 -040063 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050064 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040065 response = low;
66 } // else
67 return (response);
68} // GetNumber()
69
70// Gets a Y/N response (and converts lowercase to uppercase)
71char GetYN(void) {
72 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040073 char response;
srs5694e7b4ff92009-08-18 13:16:10 -040074
srs56940873e9d2010-10-07 13:00:45 -040075 do {
srs5694fed16d02010-01-27 23:03:40 -050076 cout << "(Y/N): ";
srs5694bf8950c2011-03-12 01:23:12 -050077 ReadCString(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040078 sscanf(line, "%c", &response);
srs56940873e9d2010-10-07 13:00:45 -040079 if (response == 'y')
80 response = 'Y';
81 if (response == 'n')
82 response = 'N';
83 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040084 return response;
85} // GetYN(void)
86
srs5694e4ac11e2009-08-31 10:13:04 -040087// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040088// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040089// or the same with "K", "M", "G", "T", or "P" as suffixes to add
90// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
91// respectively. If a "-" prefix is used, use the high value minus
92// the user-specified number of sectors (or KiB, MiB, etc.). Use the
93// def value as the default if the user just hits Enter. The sSize is
94// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050095uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
96 const string & prompt) {
97 uint64_t response;
98 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040099
100 do {
srs5694fed16d02010-01-27 23:03:40 -0500101 cout << prompt;
102 cin.getline(line, 255);
srs5694df9d3632011-01-08 18:33:24 -0500103 response = SIToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -0400104 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500105 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400106} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400107
srs5694df9d3632011-01-08 18:33:24 -0500108// Convert an SI value (K, M, G, T, or P) to its equivalent in
109// number of sectors. If no units are appended, interprets as the number
110// of sectors; otherwise, interprets as number of specified units and
111// converts to sectors. For instance, with 512-byte sectors, "1K" converts
112// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
113// inclues a "-", subtracts from high. If SIValue is empty, returns def.
114// Returns integral sector value.
115uint64_t SIToInt(string SIValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
116 int plusFlag = 0, badInput = 0;
117 uint64_t response = def, mult = 1, divide = 1;
118 char suffix;
119
120 if (sSize == 0) {
121 sSize = SECTOR_SIZE;
122 cerr << "Bug: Sector size invalid in SIToInt()!\n";
123 } // if
124
125 // Remove leading spaces, if present
126 while (SIValue[0] == ' ')
127 SIValue.erase(0, 1);
128
129 // If present, flag and remove leading plus sign
130 if (SIValue[0] == '+') {
131 plusFlag = 1;
132 SIValue.erase(0, 1);
133 } // if
134
135 // If present, flag and remove leading minus sign
136 if (SIValue[0] == '-') {
137 plusFlag = -1;
138 SIValue.erase(0, 1);
139 } // if
140
141 // Extract numeric response and, if present, suffix
142 istringstream inString(SIValue);
143 if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
144 badInput = 1;
145 inString >> response >> suffix;
146
147 // If no response, or if response == 0, use default (def)
148 if ((SIValue.length() == 0) || (response == 0)) {
149 response = def;
150 suffix = ' ';
151 plusFlag = 0;
152 } // if
153
154 // Set multiplier based on suffix
155 switch (suffix) {
156 case 'K':
157 case 'k':
158 mult = UINT64_C(1024) / sSize;
159 divide = sSize / UINT64_C(1024);
160 break;
161 case 'M':
162 case 'm':
163 mult = UINT64_C(1048576) / sSize;
164 divide = sSize / UINT64_C(1048576);
165 break;
166 case 'G':
167 case 'g':
168 mult = UINT64_C(1073741824) / sSize;
169 break;
170 case 'T':
171 case 't':
172 mult = UINT64_C(1099511627776) / sSize;
173 break;
174 case 'P':
175 case 'p':
176 mult = UINT64_C(1125899906842624) / sSize;
177 break;
178 default:
179 mult = 1;
180 } // switch
181
182 // Adjust response based on multiplier and plus flag, if present
183 if (mult > 1)
184 response *= mult;
185 else if (divide > 1)
186 response /= divide;
187 if (plusFlag == 1) {
188 // Recompute response based on low part of range (if default = high
189 // value, which should be the case when prompting for the end of a
190 // range) or the defaut value (if default != high, which should be
191 // the case for the first sector of a partition).
192 if (def == high)
193 response = response + low - UINT64_C(1);
194 else
195 response = response + def;
196 } // if
197 if (plusFlag == -1) {
198 response = high - response;
199 } // if
200
201 if (badInput)
202 response = high + UINT64_C(1);
203
204 return response;
205} // SIToInt()
206
srs56940873e9d2010-10-07 13:00:45 -0400207// Takes a size and converts this to a size in SI units (KiB, MiB, GiB,
208// TiB, or PiB), returned in C++ string form. The size is either in units
209// of the sector size or, if that parameter is omitted, in bytes.
210// (sectorSize defaults to 1).
211string BytesToSI(uint64_t size, uint32_t sectorSize) {
srs5694fed16d02010-01-27 23:03:40 -0500212 string units;
srs569408bb0da2010-02-19 17:19:55 -0500213 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400214 float sizeInSI;
215
srs56940873e9d2010-10-07 13:00:45 -0400216 sizeInSI = (float) size * (float) sectorSize;
srs5694fed16d02010-01-27 23:03:40 -0500217 units = " bytes";
218 if (sizeInSI > 1024.0) {
219 sizeInSI /= 1024.0;
220 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400221 } // if
srs5694fed16d02010-01-27 23:03:40 -0500222 if (sizeInSI > 1024.0) {
223 sizeInSI /= 1024.0;
224 units = " MiB";
225 } // if
226 if (sizeInSI > 1024.0) {
227 sizeInSI /= 1024.0;
228 units = " GiB";
229 } // if
230 if (sizeInSI > 1024.0) {
231 sizeInSI /= 1024.0;
232 units = " TiB";
233 } // if
234 if (sizeInSI > 1024.0) {
235 sizeInSI /= 1024.0;
236 units = " PiB";
237 } // if
srs569408bb0da2010-02-19 17:19:55 -0500238 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500239 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500240 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500241 } else {
srs569408bb0da2010-02-19 17:19:55 -0500242 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500243 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500244 theValue << sizeInSI << units;
245 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400246} // BlocksToSI()
247
srs56946699b012010-02-04 00:55:30 -0500248// Converts two consecutive characters in the input string into a
249// number, interpreting the string as a hexadecimal number, starting
250// at the specified position.
251unsigned char StrToHex(const string & input, unsigned int position) {
252 unsigned char retval = 0x00;
253 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400254
srs56946699b012010-02-04 00:55:30 -0500255 if (input.length() >= (position + 2)) {
256 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
257 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400258 } // if
srs56946699b012010-02-04 00:55:30 -0500259 return retval;
260} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400261
srs56940873e9d2010-10-07 13:00:45 -0400262// Returns 1 if input can be interpreted as a hexadecimal number --
263// all characters must be spaces, digits, or letters A-F (upper- or
264// lower-case), with at least one valid hexadecimal digit; otherwise
265// returns 0.
266int IsHex(const string & input) {
267 int isHex = 1, foundHex = 0, i;
268
269 for (i = 0; i < (int) input.length(); i++) {
270 if ((input[i] < '0') || (input[i] > '9')) {
271 if ((input[i] < 'A') || (input[i] > 'F')) {
272 if ((input[i] < 'a') || (input[i] > 'f')) {
273 if ((input[i] != ' ') && (input[i] != '\n')) {
274 isHex = 0;
275 }
276 } else foundHex = 1;
277 } else foundHex = 1;
278 } else foundHex = 1;
279 } // for
280 if (!foundHex)
281 isHex = 0;
282 return isHex;
283} // IsHex()
284
srs56942a9f5da2009-08-26 00:48:01 -0400285// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
286int IsLittleEndian(void) {
287 int littleE = 1; // assume little-endian (Intel-style)
288 union {
289 uint32_t num;
290 unsigned char uc[sizeof(uint32_t)];
291 } endian;
292
293 endian.num = 1;
294 if (endian.uc[0] != (unsigned char) 1) {
295 littleE = 0;
296 } // if
297 return (littleE);
298} // IsLittleEndian()
299
300// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400301void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500302 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400303 int i;
304
srs5694cb76c672010-02-11 22:22:22 -0500305 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500306 if (tempValue != NULL) {
307 memcpy(tempValue, theValue, numBytes);
308 for (i = 0; i < numBytes; i++)
309 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500310 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500311 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400312} // ReverseBytes()
313
srs56949ddc14b2010-08-22 22:44:42 -0400314// Extract integer data from argument string, which should be colon-delimited
315uint64_t GetInt(const string & argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500316 uint64_t retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400317
srs569464cbd172011-03-01 22:03:54 -0500318 istringstream inString(GetString(argument, itemNum));
srs56949ddc14b2010-08-22 22:44:42 -0400319 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400320 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400321} // GetInt()
322
323// Extract string data from argument string, which should be colon-delimited
srs5694bf8950c2011-03-12 01:23:12 -0500324string GetString(string argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500325 size_t startPos = -1, endPos = -1;
srs56949ddc14b2010-08-22 22:44:42 -0400326
327 while (itemNum-- > 0) {
328 startPos = endPos + 1;
srs569464cbd172011-03-01 22:03:54 -0500329 endPos = argument.find(':', startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400330 }
srs569464cbd172011-03-01 22:03:54 -0500331 if (endPos == string::npos)
332 endPos = argument.length();
srs56949ddc14b2010-08-22 22:44:42 -0400333 endPos--;
334
335 return argument.substr(startPos, endPos - startPos + 1);
336} // GetString()