blob: bd0b28b4c24cc75e5442cf6f9c69a5c1f73d7c70 [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;
srs5694e7b4ff92009-08-18 13:16:10 -040066
srs56940873e9d2010-10-07 13:00:45 -040067 do {
srs5694fed16d02010-01-27 23:03:40 -050068 cout << "(Y/N): ";
srs569464cbd172011-03-01 22:03:54 -050069 if (!fgets(line, 255, stdin)) {
70 cerr << "Critical error! Failed fgets() in GetYN()\n";
71 exit(1);
72 } // if
srs5694e7b4ff92009-08-18 13:16:10 -040073 sscanf(line, "%c", &response);
srs56940873e9d2010-10-07 13:00:45 -040074 if (response == 'y')
75 response = 'Y';
76 if (response == 'n')
77 response = 'N';
78 } while ((response != 'Y') && (response != 'N'));
srs5694e7b4ff92009-08-18 13:16:10 -040079 return response;
80} // GetYN(void)
81
srs5694e4ac11e2009-08-31 10:13:04 -040082// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040083// user, accepting values prefixed by "+" to add sectors to low,
srs56940873e9d2010-10-07 13:00:45 -040084// or the same with "K", "M", "G", "T", or "P" as suffixes to add
85// kilobytes, megabytes, gigabytes, terabytes, or petabytes,
86// respectively. If a "-" prefix is used, use the high value minus
87// the user-specified number of sectors (or KiB, MiB, etc.). Use the
88// def value as the default if the user just hits Enter. The sSize is
89// the sector size of the device.
srs5694df9d3632011-01-08 18:33:24 -050090uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, uint64_t sSize,
91 const string & prompt) {
92 uint64_t response;
93 char line[255];
srs56940873e9d2010-10-07 13:00:45 -040094
95 do {
srs5694fed16d02010-01-27 23:03:40 -050096 cout << prompt;
97 cin.getline(line, 255);
srs5694df9d3632011-01-08 18:33:24 -050098 response = SIToInt(line, sSize, low, high, def);
srs56940873e9d2010-10-07 13:00:45 -040099 } while ((response < low) || (response > high));
srs569455d92612010-03-07 22:16:07 -0500100 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400101} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400102
srs5694df9d3632011-01-08 18:33:24 -0500103// Convert an SI value (K, M, G, T, or P) to its equivalent in
104// number of sectors. If no units are appended, interprets as the number
105// of sectors; otherwise, interprets as number of specified units and
106// converts to sectors. For instance, with 512-byte sectors, "1K" converts
107// to 2. If value includes a "+", adds low and subtracts 1; if SIValue
108// inclues a "-", subtracts from high. If SIValue is empty, returns def.
109// Returns integral sector value.
110uint64_t SIToInt(string SIValue, uint64_t sSize, uint64_t low, uint64_t high, uint64_t def) {
111 int plusFlag = 0, badInput = 0;
112 uint64_t response = def, mult = 1, divide = 1;
113 char suffix;
114
115 if (sSize == 0) {
116 sSize = SECTOR_SIZE;
117 cerr << "Bug: Sector size invalid in SIToInt()!\n";
118 } // if
119
120 // Remove leading spaces, if present
121 while (SIValue[0] == ' ')
122 SIValue.erase(0, 1);
123
124 // If present, flag and remove leading plus sign
125 if (SIValue[0] == '+') {
126 plusFlag = 1;
127 SIValue.erase(0, 1);
128 } // if
129
130 // If present, flag and remove leading minus sign
131 if (SIValue[0] == '-') {
132 plusFlag = -1;
133 SIValue.erase(0, 1);
134 } // if
135
136 // Extract numeric response and, if present, suffix
137 istringstream inString(SIValue);
138 if (((inString.peek() < '0') || (inString.peek() > '9')) && (inString.peek() != -1))
139 badInput = 1;
140 inString >> response >> suffix;
141
142 // If no response, or if response == 0, use default (def)
143 if ((SIValue.length() == 0) || (response == 0)) {
144 response = def;
145 suffix = ' ';
146 plusFlag = 0;
147 } // if
148
149 // Set multiplier based on suffix
150 switch (suffix) {
151 case 'K':
152 case 'k':
153 mult = UINT64_C(1024) / sSize;
154 divide = sSize / UINT64_C(1024);
155 break;
156 case 'M':
157 case 'm':
158 mult = UINT64_C(1048576) / sSize;
159 divide = sSize / UINT64_C(1048576);
160 break;
161 case 'G':
162 case 'g':
163 mult = UINT64_C(1073741824) / sSize;
164 break;
165 case 'T':
166 case 't':
167 mult = UINT64_C(1099511627776) / sSize;
168 break;
169 case 'P':
170 case 'p':
171 mult = UINT64_C(1125899906842624) / sSize;
172 break;
173 default:
174 mult = 1;
175 } // switch
176
177 // Adjust response based on multiplier and plus flag, if present
178 if (mult > 1)
179 response *= mult;
180 else if (divide > 1)
181 response /= divide;
182 if (plusFlag == 1) {
183 // Recompute response based on low part of range (if default = high
184 // value, which should be the case when prompting for the end of a
185 // range) or the defaut value (if default != high, which should be
186 // the case for the first sector of a partition).
187 if (def == high)
188 response = response + low - UINT64_C(1);
189 else
190 response = response + def;
191 } // if
192 if (plusFlag == -1) {
193 response = high - response;
194 } // if
195
196 if (badInput)
197 response = high + UINT64_C(1);
198
199 return response;
200} // SIToInt()
201
srs56940873e9d2010-10-07 13:00:45 -0400202// Takes a size and converts this to a size in SI units (KiB, MiB, GiB,
203// TiB, or PiB), returned in C++ string form. The size is either in units
204// of the sector size or, if that parameter is omitted, in bytes.
205// (sectorSize defaults to 1).
206string BytesToSI(uint64_t size, uint32_t sectorSize) {
srs5694fed16d02010-01-27 23:03:40 -0500207 string units;
srs569408bb0da2010-02-19 17:19:55 -0500208 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400209 float sizeInSI;
210
srs56940873e9d2010-10-07 13:00:45 -0400211 sizeInSI = (float) size * (float) sectorSize;
srs5694fed16d02010-01-27 23:03:40 -0500212 units = " bytes";
213 if (sizeInSI > 1024.0) {
214 sizeInSI /= 1024.0;
215 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400216 } // if
srs5694fed16d02010-01-27 23:03:40 -0500217 if (sizeInSI > 1024.0) {
218 sizeInSI /= 1024.0;
219 units = " MiB";
220 } // if
221 if (sizeInSI > 1024.0) {
222 sizeInSI /= 1024.0;
223 units = " GiB";
224 } // if
225 if (sizeInSI > 1024.0) {
226 sizeInSI /= 1024.0;
227 units = " TiB";
228 } // if
229 if (sizeInSI > 1024.0) {
230 sizeInSI /= 1024.0;
231 units = " PiB";
232 } // if
srs569408bb0da2010-02-19 17:19:55 -0500233 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500234 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500235 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500236 } else {
srs569408bb0da2010-02-19 17:19:55 -0500237 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500238 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500239 theValue << sizeInSI << units;
240 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400241} // BlocksToSI()
242
srs56946699b012010-02-04 00:55:30 -0500243// Converts two consecutive characters in the input string into a
244// number, interpreting the string as a hexadecimal number, starting
245// at the specified position.
246unsigned char StrToHex(const string & input, unsigned int position) {
247 unsigned char retval = 0x00;
248 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400249
srs56946699b012010-02-04 00:55:30 -0500250 if (input.length() >= (position + 2)) {
251 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
252 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400253 } // if
srs56946699b012010-02-04 00:55:30 -0500254 return retval;
255} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400256
srs56940873e9d2010-10-07 13:00:45 -0400257// Returns 1 if input can be interpreted as a hexadecimal number --
258// all characters must be spaces, digits, or letters A-F (upper- or
259// lower-case), with at least one valid hexadecimal digit; otherwise
260// returns 0.
261int IsHex(const string & input) {
262 int isHex = 1, foundHex = 0, i;
263
264 for (i = 0; i < (int) input.length(); i++) {
265 if ((input[i] < '0') || (input[i] > '9')) {
266 if ((input[i] < 'A') || (input[i] > 'F')) {
267 if ((input[i] < 'a') || (input[i] > 'f')) {
268 if ((input[i] != ' ') && (input[i] != '\n')) {
269 isHex = 0;
270 }
271 } else foundHex = 1;
272 } else foundHex = 1;
273 } else foundHex = 1;
274 } // for
275 if (!foundHex)
276 isHex = 0;
277 return isHex;
278} // IsHex()
279
srs56942a9f5da2009-08-26 00:48:01 -0400280// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
281int IsLittleEndian(void) {
282 int littleE = 1; // assume little-endian (Intel-style)
283 union {
284 uint32_t num;
285 unsigned char uc[sizeof(uint32_t)];
286 } endian;
287
288 endian.num = 1;
289 if (endian.uc[0] != (unsigned char) 1) {
290 littleE = 0;
291 } // if
292 return (littleE);
293} // IsLittleEndian()
294
295// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400296void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500297 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400298 int i;
299
srs5694cb76c672010-02-11 22:22:22 -0500300 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500301 if (tempValue != NULL) {
302 memcpy(tempValue, theValue, numBytes);
303 for (i = 0; i < numBytes; i++)
304 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500305 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500306 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400307} // ReverseBytes()
308
srs56949ddc14b2010-08-22 22:44:42 -0400309// Extract integer data from argument string, which should be colon-delimited
310uint64_t GetInt(const string & argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500311 uint64_t retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400312
srs569464cbd172011-03-01 22:03:54 -0500313 istringstream inString(GetString(argument, itemNum));
srs56949ddc14b2010-08-22 22:44:42 -0400314 inString >> retval;
srs5694e7b4ff92009-08-18 13:16:10 -0400315 return retval;
srs56949ddc14b2010-08-22 22:44:42 -0400316} // GetInt()
317
318// Extract string data from argument string, which should be colon-delimited
319string GetString(const string & argument, int itemNum) {
srs569464cbd172011-03-01 22:03:54 -0500320 size_t startPos = -1, endPos = -1;
srs56949ddc14b2010-08-22 22:44:42 -0400321
322 while (itemNum-- > 0) {
323 startPos = endPos + 1;
srs569464cbd172011-03-01 22:03:54 -0500324 endPos = argument.find(':', startPos);
srs56949ddc14b2010-08-22 22:44:42 -0400325 }
srs569464cbd172011-03-01 22:03:54 -0500326 if (endPos == string::npos)
327 endPos = argument.length();
srs56949ddc14b2010-08-22 22:44:42 -0400328 endPos--;
329
330 return argument.substr(startPos, endPos - startPos + 1);
331} // GetString()