blob: 168262e84412e66c74a8605cf417f56de03e3532 [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...
44 response = low - 1; // force one loop by setting response outside range
45 while ((response < low) || (response > high)) {
srs5694fed16d02010-01-27 23:03:40 -050046 cout << prompt;
47 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040048 num = sscanf(line, "%d", &response);
49 if (num == 1) { // user provided a response
50 if ((response < low) || (response > high))
srs5694fed16d02010-01-27 23:03:40 -050051 cout << "Value out of range\n";
srs5694e7b4ff92009-08-18 13:16:10 -040052 } else { // user hit enter; return default
53 response = def;
54 } // if/else
55 } // while
56 } else { // low == high, so return this value
srs5694fed16d02010-01-27 23:03:40 -050057 cout << "Using " << low << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040058 response = low;
59 } // else
60 return (response);
61} // GetNumber()
62
63// Gets a Y/N response (and converts lowercase to uppercase)
64char GetYN(void) {
65 char line[255];
66 char response = '\0';
srs5694fed16d02010-01-27 23:03:40 -050067 char *junk;
srs5694e7b4ff92009-08-18 13:16:10 -040068
69 while ((response != 'Y') && (response != 'N')) {
srs5694fed16d02010-01-27 23:03:40 -050070 cout << "(Y/N): ";
srs56945d58fe02010-01-03 20:57:08 -050071 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040072 sscanf(line, "%c", &response);
73 if (response == 'y') response = 'Y';
74 if (response == 'n') response = 'N';
75 } // while
76 return response;
77} // GetYN(void)
78
srs5694e4ac11e2009-08-31 10:13:04 -040079// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040080// user, accepting values prefixed by "+" to add sectors to low,
81// or the same with "K", "M", "G", or "T" as suffixes to add
82// kilobytes, megabytes, gigabytes, or terabytes, respectively.
srs5694e4ac11e2009-08-31 10:13:04 -040083// If a "-" prefix is used, use the high value minus the user-
84// specified number of sectors (or KiB, MiB, etc.). Use the def
85 //value as the default if the user just hits Enter
srs5694fed16d02010-01-27 23:03:40 -050086uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040087 unsigned long long response;
srs5694ba00fed2010-01-12 18:18:36 -050088 int num, plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040089 uint64_t mult = 1;
90 char suffix;
91 char line[255];
92
93 response = low - 1; // Ensure one pass by setting a too-low initial value
94 while ((response < low) || (response > high)) {
srs5694fed16d02010-01-27 23:03:40 -050095 cout << prompt;
96 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040097
98 // Remove leading spaces, if present
99 while (line[0] == ' ')
100 strcpy(line, &line[1]);
101
102 // If present, flag and remove leading plus sign
103 if (line[0] == '+') {
104 plusFlag = 1;
105 strcpy(line, &line[1]);
106 } // if
107
srs5694e4ac11e2009-08-31 10:13:04 -0400108 // If present, flag and remove leading minus sign
109 if (line[0] == '-') {
110 plusFlag = -1;
111 strcpy(line, &line[1]);
112 } // if
113
srs5694e7b4ff92009-08-18 13:16:10 -0400114 // Extract numeric response and, if present, suffix
115 num = sscanf(line, "%llu%c", &response, &suffix);
116
srs5694e4ac11e2009-08-31 10:13:04 -0400117 // If no response, use default (def)
srs5694e7b4ff92009-08-18 13:16:10 -0400118 if (num <= 0) {
srs5694e4ac11e2009-08-31 10:13:04 -0400119 response = (unsigned long long) def;
srs5694e7b4ff92009-08-18 13:16:10 -0400120 suffix = ' ';
srs5694e19ba092009-08-24 14:10:35 -0400121 plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400122 } // if
123
124 // Set multiplier based on suffix
125 switch (suffix) {
126 case 'K':
127 case 'k':
128 mult = (uint64_t) 1024 / SECTOR_SIZE;
129 break;
130 case 'M':
131 case 'm':
132 mult = (uint64_t) 1048576 / SECTOR_SIZE;
133 break;
134 case 'G':
135 case 'g':
136 mult = (uint64_t) 1073741824 / SECTOR_SIZE;
137 break;
138 case 'T':
139 case 't':
140 mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE;
141 break;
142 default:
143 mult = 1;
144 } // switch
145
146 // Adjust response based on multiplier and plus flag, if present
147 response *= (unsigned long long) mult;
148 if (plusFlag == 1) {
srs5694ba00fed2010-01-12 18:18:36 -0500149 // Recompute response based on low part of range (if default = high
150 // value, which should be the case when prompting for the end of a
151 // range) or the defaut value (if default != high, which should be
152 // the case for the first sector of a partition).
153 if (def == high)
154 response = response + (unsigned long long) low - UINT64_C(1);
155 else
156 response = response + (unsigned long long) def - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400157 } // if
158 if (plusFlag == -1) {
159 response = (unsigned long long) high - response;
srs5694e19ba092009-08-24 14:10:35 -0400160 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400161 } // while
162 return ((uint64_t) response);
srs5694e4ac11e2009-08-31 10:13:04 -0400163} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400164
srs5694e7b4ff92009-08-18 13:16:10 -0400165// Takes a size in bytes (in size) and converts this to a size in
166// SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string
167// form
srs5694fed16d02010-01-27 23:03:40 -0500168string BytesToSI(uint64_t size) {
169 string units;
srs569408bb0da2010-02-19 17:19:55 -0500170 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400171 float sizeInSI;
172
srs5694fed16d02010-01-27 23:03:40 -0500173 sizeInSI = (float) size;
174 units = " bytes";
175 if (sizeInSI > 1024.0) {
176 sizeInSI /= 1024.0;
177 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400178 } // if
srs5694fed16d02010-01-27 23:03:40 -0500179 if (sizeInSI > 1024.0) {
180 sizeInSI /= 1024.0;
181 units = " MiB";
182 } // if
183 if (sizeInSI > 1024.0) {
184 sizeInSI /= 1024.0;
185 units = " GiB";
186 } // if
187 if (sizeInSI > 1024.0) {
188 sizeInSI /= 1024.0;
189 units = " TiB";
190 } // if
191 if (sizeInSI > 1024.0) {
192 sizeInSI /= 1024.0;
193 units = " PiB";
194 } // if
srs569408bb0da2010-02-19 17:19:55 -0500195 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500196 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500197 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500198 } else {
srs569408bb0da2010-02-19 17:19:55 -0500199 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500200 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500201 theValue << sizeInSI << units;
202 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400203} // BlocksToSI()
204
srs56946699b012010-02-04 00:55:30 -0500205// Converts two consecutive characters in the input string into a
206// number, interpreting the string as a hexadecimal number, starting
207// at the specified position.
208unsigned char StrToHex(const string & input, unsigned int position) {
209 unsigned char retval = 0x00;
210 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400211
srs56946699b012010-02-04 00:55:30 -0500212 if (input.length() >= (position + 2)) {
213 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
214 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400215 } // if
srs56946699b012010-02-04 00:55:30 -0500216 return retval;
217} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400218
srs56942a9f5da2009-08-26 00:48:01 -0400219// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
220int IsLittleEndian(void) {
221 int littleE = 1; // assume little-endian (Intel-style)
222 union {
223 uint32_t num;
224 unsigned char uc[sizeof(uint32_t)];
225 } endian;
226
227 endian.num = 1;
228 if (endian.uc[0] != (unsigned char) 1) {
229 littleE = 0;
230 } // if
231 return (littleE);
232} // IsLittleEndian()
233
234// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400235void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500236 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400237 int i;
238
srs5694cb76c672010-02-11 22:22:22 -0500239 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500240 if (tempValue != NULL) {
241 memcpy(tempValue, theValue, numBytes);
242 for (i = 0; i < numBytes; i++)
243 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500244 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500245 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400246} // ReverseBytes()
247
srs5694e7b4ff92009-08-18 13:16:10 -0400248// Compute (2 ^ value). Given the return type, value must be 63 or less.
249// Used in some bit-fiddling functions
250uint64_t PowerOf2(int value) {
251 uint64_t retval = 1;
252 int i;
253
254 if ((value < 64) && (value >= 0)) {
255 for (i = 0; i < value; i++) {
256 retval *= 2;
257 } // for
258 } else retval = 0;
259 return retval;
260} // PowerOf2()