srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // 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 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 6 | /* 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 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 9 | #define __STDC_LIMIT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 12 | #include <stdio.h> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 13 | #include <stdint.h> |
| 14 | #include <errno.h> |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 15 | #include <fcntl.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 16 | #include <string.h> |
srs5694 | e35eb1b | 2009-09-14 00:29:34 -0400 | [diff] [blame] | 17 | #include <sys/stat.h> |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <iostream> |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 20 | #include "support.h" |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 24 | // 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 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 31 | using 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.) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 38 | int GetNumber(int low, int high, int def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 39 | 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)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 45 | cout << prompt; |
| 46 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 47 | num = sscanf(line, "%d", &response); |
| 48 | if (num == 1) { // user provided a response |
| 49 | if ((response < low) || (response > high)) |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 50 | cout << "Value out of range\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 51 | } else { // user hit enter; return default |
| 52 | response = def; |
| 53 | } // if/else |
| 54 | } // while |
| 55 | } else { // low == high, so return this value |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 56 | cout << "Using " << low << "\n"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 57 | response = low; |
| 58 | } // else |
| 59 | return (response); |
| 60 | } // GetNumber() |
| 61 | |
| 62 | // Gets a Y/N response (and converts lowercase to uppercase) |
| 63 | char GetYN(void) { |
| 64 | char line[255]; |
| 65 | char response = '\0'; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 66 | char *junk; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 67 | |
| 68 | while ((response != 'Y') && (response != 'N')) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 69 | cout << "(Y/N): "; |
srs5694 | 5d58fe0 | 2010-01-03 20:57:08 -0500 | [diff] [blame] | 70 | junk = fgets(line, 255, stdin); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 71 | 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 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 78 | // Obtains a sector number, between low and high, from the |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 79 | // 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. |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 82 | // 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 |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 85 | uint64_t GetSectorNum(uint64_t low, uint64_t high, uint64_t def, const string & prompt) { |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 86 | unsigned long long response; |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 87 | int num, plusFlag = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 88 | 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)) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 94 | cout << prompt; |
| 95 | cin.getline(line, 255); |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 96 | |
| 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 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 107 | // If present, flag and remove leading minus sign |
| 108 | if (line[0] == '-') { |
| 109 | plusFlag = -1; |
| 110 | strcpy(line, &line[1]); |
| 111 | } // if |
| 112 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 113 | // Extract numeric response and, if present, suffix |
| 114 | num = sscanf(line, "%llu%c", &response, &suffix); |
| 115 | |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 116 | // If no response, use default (def) |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 117 | if (num <= 0) { |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 118 | response = (unsigned long long) def; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 119 | suffix = ' '; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 120 | plusFlag = 0; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 121 | } // 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) { |
srs5694 | ba00fed | 2010-01-12 18:18:36 -0500 | [diff] [blame] | 148 | // 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); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 156 | } // if |
| 157 | if (plusFlag == -1) { |
| 158 | response = (unsigned long long) high - response; |
srs5694 | e19ba09 | 2009-08-24 14:10:35 -0400 | [diff] [blame] | 159 | } // if |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 160 | } // while |
| 161 | return ((uint64_t) response); |
srs5694 | e4ac11e | 2009-08-31 10:13:04 -0400 | [diff] [blame] | 162 | } // GetSectorNum() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 163 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 164 | // 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 |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 167 | string BytesToSI(uint64_t size) { |
| 168 | string units; |
| 169 | char theValue[99]; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 170 | float sizeInSI; |
| 171 | |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 172 | theValue[0] = '\0'; |
| 173 | sizeInSI = (float) size; |
| 174 | units = " bytes"; |
| 175 | if (sizeInSI > 1024.0) { |
| 176 | sizeInSI /= 1024.0; |
| 177 | units = " KiB"; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 178 | } // if |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 179 | 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 |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 200 | return theValue; |
| 201 | } // BlocksToSI() |
| 202 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 203 | // 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. |
| 206 | unsigned char StrToHex(const string & input, unsigned int position) { |
| 207 | unsigned char retval = 0x00; |
| 208 | unsigned int temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 209 | |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 210 | if (input.length() >= (position + 2)) { |
| 211 | sscanf(input.substr(position, 2).c_str(), "%x", &temp); |
| 212 | retval = (unsigned char) temp; |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 213 | } // if |
srs5694 | 6699b01 | 2010-02-04 00:55:30 -0500 | [diff] [blame] | 214 | return retval; |
| 215 | } // StrToHex() |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 216 | |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 217 | // Return 1 if the CPU architecture is little endian, 0 if it's big endian.... |
| 218 | int 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 |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 233 | void ReverseBytes(void* theValue, int numBytes) { |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 234 | char* tempValue = NULL; |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 235 | int i; |
| 236 | |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 237 | tempValue = new char [numBytes]; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 238 | if (tempValue != NULL) { |
| 239 | memcpy(tempValue, theValue, numBytes); |
| 240 | for (i = 0; i < numBytes; i++) |
| 241 | ((char*) theValue)[i] = tempValue[numBytes - i - 1]; |
srs5694 | cb76c67 | 2010-02-11 22:22:22 -0500 | [diff] [blame] | 242 | delete[] tempValue; |
srs5694 | fed16d0 | 2010-01-27 23:03:40 -0500 | [diff] [blame] | 243 | } // if |
srs5694 | 2a9f5da | 2009-08-26 00:48:01 -0400 | [diff] [blame] | 244 | } // ReverseBytes() |
| 245 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 246 | // Compute (2 ^ value). Given the return type, value must be 63 or less. |
| 247 | // Used in some bit-fiddling functions |
| 248 | uint64_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() |