blob: ab8f7671e9f849cc91d3e11314a62ed38052c0d1 [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) {
srs569455d92612010-03-07 22:16:07 -050087 uint64_t response, mult = 1;
88 int plusFlag = 0;
89 char suffix, line[255];
srs5694e7b4ff92009-08-18 13:16:10 -040090
91 response = low - 1; // Ensure one pass by setting a too-low initial value
92 while ((response < low) || (response > high)) {
srs5694fed16d02010-01-27 23:03:40 -050093 cout << prompt;
94 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040095
96 // Remove leading spaces, if present
97 while (line[0] == ' ')
98 strcpy(line, &line[1]);
99
100 // If present, flag and remove leading plus sign
101 if (line[0] == '+') {
102 plusFlag = 1;
103 strcpy(line, &line[1]);
104 } // if
105
srs5694e4ac11e2009-08-31 10:13:04 -0400106 // If present, flag and remove leading minus sign
107 if (line[0] == '-') {
108 plusFlag = -1;
109 strcpy(line, &line[1]);
110 } // if
111
srs5694e7b4ff92009-08-18 13:16:10 -0400112 // Extract numeric response and, if present, suffix
srs569455d92612010-03-07 22:16:07 -0500113 istringstream inString(line);
114 inString >> response >> suffix;
srs5694e7b4ff92009-08-18 13:16:10 -0400115
srs5694e4ac11e2009-08-31 10:13:04 -0400116 // If no response, use default (def)
srs569455d92612010-03-07 22:16:07 -0500117 if (strlen(line) == 0) {
118 response = def;
119 suffix = ' ';
srs5694e19ba092009-08-24 14:10:35 -0400120 plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -0400121 } // if
122
123 // Set multiplier based on suffix
124 switch (suffix) {
125 case 'K':
126 case 'k':
127 mult = (uint64_t) 1024 / SECTOR_SIZE;
128 break;
129 case 'M':
130 case 'm':
131 mult = (uint64_t) 1048576 / SECTOR_SIZE;
132 break;
133 case 'G':
134 case 'g':
135 mult = (uint64_t) 1073741824 / SECTOR_SIZE;
136 break;
137 case 'T':
138 case 't':
139 mult = ((uint64_t) 1073741824 * (uint64_t) 1024) / (uint64_t) SECTOR_SIZE;
140 break;
141 default:
142 mult = 1;
143 } // switch
144
145 // Adjust response based on multiplier and plus flag, if present
srs569455d92612010-03-07 22:16:07 -0500146 response *= mult;
srs5694e7b4ff92009-08-18 13:16:10 -0400147 if (plusFlag == 1) {
srs5694ba00fed2010-01-12 18:18:36 -0500148 // Recompute response based on low part of range (if default = high
149 // value, which should be the case when prompting for the end of a
150 // range) or the defaut value (if default != high, which should be
151 // the case for the first sector of a partition).
152 if (def == high)
srs569455d92612010-03-07 22:16:07 -0500153 response = response + low - UINT64_C(1);
srs5694ba00fed2010-01-12 18:18:36 -0500154 else
srs569455d92612010-03-07 22:16:07 -0500155 response = response + def - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400156 } // if
157 if (plusFlag == -1) {
srs569455d92612010-03-07 22:16:07 -0500158 response = high - response;
srs5694e19ba092009-08-24 14:10:35 -0400159 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400160 } // while
srs569455d92612010-03-07 22:16:07 -0500161 return response;
srs5694e4ac11e2009-08-31 10:13:04 -0400162} // GetSectorNum()
srs5694e7b4ff92009-08-18 13:16:10 -0400163
srs5694e7b4ff92009-08-18 13:16:10 -0400164// Takes a size in bytes (in size) and converts this to a size in
165// SI units (KiB, MiB, GiB, TiB, or PiB), returned in C++ string
166// form
srs5694fed16d02010-01-27 23:03:40 -0500167string BytesToSI(uint64_t size) {
168 string units;
srs569408bb0da2010-02-19 17:19:55 -0500169 ostringstream theValue;
srs5694e7b4ff92009-08-18 13:16:10 -0400170 float sizeInSI;
171
srs5694fed16d02010-01-27 23:03:40 -0500172 sizeInSI = (float) size;
173 units = " bytes";
174 if (sizeInSI > 1024.0) {
175 sizeInSI /= 1024.0;
176 units = " KiB";
srs5694e7b4ff92009-08-18 13:16:10 -0400177 } // if
srs5694fed16d02010-01-27 23:03:40 -0500178 if (sizeInSI > 1024.0) {
179 sizeInSI /= 1024.0;
180 units = " MiB";
181 } // if
182 if (sizeInSI > 1024.0) {
183 sizeInSI /= 1024.0;
184 units = " GiB";
185 } // if
186 if (sizeInSI > 1024.0) {
187 sizeInSI /= 1024.0;
188 units = " TiB";
189 } // if
190 if (sizeInSI > 1024.0) {
191 sizeInSI /= 1024.0;
192 units = " PiB";
193 } // if
srs569408bb0da2010-02-19 17:19:55 -0500194 theValue.setf(ios::fixed);
srs5694fed16d02010-01-27 23:03:40 -0500195 if (units == " bytes") { // in bytes, so no decimal point
srs569408bb0da2010-02-19 17:19:55 -0500196 theValue.precision(0);
srs5694fed16d02010-01-27 23:03:40 -0500197 } else {
srs569408bb0da2010-02-19 17:19:55 -0500198 theValue.precision(1);
srs5694fed16d02010-01-27 23:03:40 -0500199 } // if/else
srs569408bb0da2010-02-19 17:19:55 -0500200 theValue << sizeInSI << units;
201 return theValue.str();
srs5694e7b4ff92009-08-18 13:16:10 -0400202} // BlocksToSI()
203
srs56946699b012010-02-04 00:55:30 -0500204// Converts two consecutive characters in the input string into a
205// number, interpreting the string as a hexadecimal number, starting
206// at the specified position.
207unsigned char StrToHex(const string & input, unsigned int position) {
208 unsigned char retval = 0x00;
209 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400210
srs56946699b012010-02-04 00:55:30 -0500211 if (input.length() >= (position + 2)) {
212 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
213 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400214 } // if
srs56946699b012010-02-04 00:55:30 -0500215 return retval;
216} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400217
srs56942a9f5da2009-08-26 00:48:01 -0400218// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
219int IsLittleEndian(void) {
220 int littleE = 1; // assume little-endian (Intel-style)
221 union {
222 uint32_t num;
223 unsigned char uc[sizeof(uint32_t)];
224 } endian;
225
226 endian.num = 1;
227 if (endian.uc[0] != (unsigned char) 1) {
228 littleE = 0;
229 } // if
230 return (littleE);
231} // IsLittleEndian()
232
233// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400234void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500235 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400236 int i;
237
srs5694cb76c672010-02-11 22:22:22 -0500238 tempValue = new char [numBytes];
srs5694fed16d02010-01-27 23:03:40 -0500239 if (tempValue != NULL) {
240 memcpy(tempValue, theValue, numBytes);
241 for (i = 0; i < numBytes; i++)
242 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
srs5694cb76c672010-02-11 22:22:22 -0500243 delete[] tempValue;
srs5694fed16d02010-01-27 23:03:40 -0500244 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400245} // ReverseBytes()
246
srs5694e7b4ff92009-08-18 13:16:10 -0400247// Compute (2 ^ value). Given the return type, value must be 63 or less.
248// Used in some bit-fiddling functions
249uint64_t PowerOf2(int value) {
250 uint64_t retval = 1;
251 int i;
252
253 if ((value < 64) && (value >= 0)) {
254 for (i = 0; i < value; i++) {
255 retval *= 2;
256 } // for
257 } else retval = 0;
258 return retval;
259} // PowerOf2()