blob: 56a2dd3b745f03157bb7aa3fc1a13c64298b485f [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>
srs5694e7b4ff92009-08-18 13:16:10 -040020#include "support.h"
21
22#include <sys/types.h>
23
srs56945d58fe02010-01-03 20:57:08 -050024// As of 1/2010, BLKPBSZGET is very new, so I'm explicitly defining it if
25// it's not already defined. This should become unnecessary in the future.
26// Note that this is a Linux-only ioctl....
27#ifndef BLKPBSZGET
28#define BLKPBSZGET _IO(0x12,123)
29#endif
30
srs5694e7b4ff92009-08-18 13:16:10 -040031using namespace std;
32
33// Get a numeric value from the user, between low and high (inclusive).
34// Keeps looping until the user enters a value within that range.
35// If user provides no input, def (default value) is returned.
36// (If def is outside of the low-high range, an explicit response
37// is required.)
srs5694fed16d02010-01-27 23:03:40 -050038int GetNumber(int low, int high, int def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040039 int response, num;
40 char line[255];
41
42 if (low != high) { // bother only if low and high differ...
43 response = low - 1; // force one loop by setting response outside range
44 while ((response < low) || (response > high)) {
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
54 } // while
55 } 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];
65 char response = '\0';
srs5694fed16d02010-01-27 23:03:40 -050066 char *junk;
srs5694e7b4ff92009-08-18 13:16:10 -040067
68 while ((response != 'Y') && (response != 'N')) {
srs5694fed16d02010-01-27 23:03:40 -050069 cout << "(Y/N): ";
srs56945d58fe02010-01-03 20:57:08 -050070 junk = fgets(line, 255, stdin);
srs5694e7b4ff92009-08-18 13:16:10 -040071 sscanf(line, "%c", &response);
72 if (response == 'y') response = 'Y';
73 if (response == 'n') response = 'N';
74 } // while
75 return response;
76} // GetYN(void)
77
srs5694e4ac11e2009-08-31 10:13:04 -040078// Obtains a sector number, between low and high, from the
srs5694e7b4ff92009-08-18 13:16:10 -040079// user, accepting values prefixed by "+" to add sectors to low,
80// or the same with "K", "M", "G", or "T" as suffixes to add
81// kilobytes, megabytes, gigabytes, or terabytes, respectively.
srs5694e4ac11e2009-08-31 10:13:04 -040082// If a "-" prefix is used, use the high value minus the user-
83// specified number of sectors (or KiB, MiB, etc.). Use the def
84 //value as the default if the user just hits Enter
srs5694fed16d02010-01-27 23:03:40 -050085uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt) {
srs5694e7b4ff92009-08-18 13:16:10 -040086 unsigned long long response;
srs5694ba00fed2010-01-12 18:18:36 -050087 int num, plusFlag = 0;
srs5694e7b4ff92009-08-18 13:16:10 -040088 uint64_t mult = 1;
89 char suffix;
90 char line[255];
91
92 response = low - 1; // Ensure one pass by setting a too-low initial value
93 while ((response < low) || (response > high)) {
srs5694fed16d02010-01-27 23:03:40 -050094 cout << prompt;
95 cin.getline(line, 255);
srs5694e7b4ff92009-08-18 13:16:10 -040096
97 // Remove leading spaces, if present
98 while (line[0] == ' ')
99 strcpy(line, &line[1]);
100
101 // If present, flag and remove leading plus sign
102 if (line[0] == '+') {
103 plusFlag = 1;
104 strcpy(line, &line[1]);
105 } // if
106
srs5694e4ac11e2009-08-31 10:13:04 -0400107 // If present, flag and remove leading minus sign
108 if (line[0] == '-') {
109 plusFlag = -1;
110 strcpy(line, &line[1]);
111 } // if
112
srs5694e7b4ff92009-08-18 13:16:10 -0400113 // Extract numeric response and, if present, suffix
114 num = sscanf(line, "%llu%c", &response, &suffix);
115
srs5694e4ac11e2009-08-31 10:13:04 -0400116 // If no response, use default (def)
srs5694e7b4ff92009-08-18 13:16:10 -0400117 if (num <= 0) {
srs5694e4ac11e2009-08-31 10:13:04 -0400118 response = (unsigned long long) def;
srs5694e7b4ff92009-08-18 13:16:10 -0400119 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
146 response *= (unsigned long long) mult;
147 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)
153 response = response + (unsigned long long) low - UINT64_C(1);
154 else
155 response = response + (unsigned long long) def - UINT64_C(1);
srs5694e4ac11e2009-08-31 10:13:04 -0400156 } // if
157 if (plusFlag == -1) {
158 response = (unsigned long long) high - response;
srs5694e19ba092009-08-24 14:10:35 -0400159 } // if
srs5694e7b4ff92009-08-18 13:16:10 -0400160 } // while
161 return ((uint64_t) 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;
169 char theValue[99];
srs5694e7b4ff92009-08-18 13:16:10 -0400170 float sizeInSI;
171
srs5694fed16d02010-01-27 23:03:40 -0500172 theValue[0] = '\0';
173 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
195 if (units == " bytes") { // in bytes, so no decimal point
196 sprintf(theValue, "%1.0f%s", sizeInSI, units.c_str());
197 } else {
198 sprintf(theValue, "%1.1f%s", sizeInSI, units.c_str());
199 } // if/else
srs5694e7b4ff92009-08-18 13:16:10 -0400200 return theValue;
201} // BlocksToSI()
202
srs56946699b012010-02-04 00:55:30 -0500203// Converts two consecutive characters in the input string into a
204// number, interpreting the string as a hexadecimal number, starting
205// at the specified position.
206unsigned char StrToHex(const string & input, unsigned int position) {
207 unsigned char retval = 0x00;
208 unsigned int temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400209
srs56946699b012010-02-04 00:55:30 -0500210 if (input.length() >= (position + 2)) {
211 sscanf(input.substr(position, 2).c_str(), "%x", &temp);
212 retval = (unsigned char) temp;
srs5694e7b4ff92009-08-18 13:16:10 -0400213 } // if
srs56946699b012010-02-04 00:55:30 -0500214 return retval;
215} // StrToHex()
srs5694e7b4ff92009-08-18 13:16:10 -0400216
srs56942a9f5da2009-08-26 00:48:01 -0400217// Return 1 if the CPU architecture is little endian, 0 if it's big endian....
218int IsLittleEndian(void) {
219 int littleE = 1; // assume little-endian (Intel-style)
220 union {
221 uint32_t num;
222 unsigned char uc[sizeof(uint32_t)];
223 } endian;
224
225 endian.num = 1;
226 if (endian.uc[0] != (unsigned char) 1) {
227 littleE = 0;
228 } // if
229 return (littleE);
230} // IsLittleEndian()
231
232// Reverse the byte order of theValue; numBytes is number of bytes
srs5694221e0872009-08-29 15:00:31 -0400233void ReverseBytes(void* theValue, int numBytes) {
srs5694fed16d02010-01-27 23:03:40 -0500234 char* tempValue = NULL;
srs56942a9f5da2009-08-26 00:48:01 -0400235 int i;
236
237 tempValue = (char*) malloc(numBytes);
srs5694fed16d02010-01-27 23:03:40 -0500238 if (tempValue != NULL) {
239 memcpy(tempValue, theValue, numBytes);
240 for (i = 0; i < numBytes; i++)
241 ((char*) theValue)[i] = tempValue[numBytes - i - 1];
242 free(tempValue);
243 } // if
srs56942a9f5da2009-08-26 00:48:01 -0400244} // ReverseBytes()
245
srs5694e7b4ff92009-08-18 13:16:10 -0400246// Compute (2 ^ value). Given the return type, value must be 63 or less.
247// Used in some bit-fiddling functions
248uint64_t PowerOf2(int value) {
249 uint64_t retval = 1;
250 int i;
251
252 if ((value < 64) && (value >= 0)) {
253 for (i = 0; i < value; i++) {
254 retval *= 2;
255 } // for
256 } else retval = 0;
257 return retval;
258} // PowerOf2()