srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 1 | // attributes.cc |
| 2 | // Class to manage partition attribute codes. These are binary bit fields, |
| 3 | // of which only three are currently (2/2009) documented on Wikipedia. |
| 4 | |
srs5694 | 221e087 | 2009-08-29 15:00:31 -0400 | [diff] [blame] | 5 | /* This program is copyright (c) 2009 by Roderick W. Smith. It is distributed |
| 6 | under the terms of the GNU GPL version 2, as detailed in the COPYING file. */ |
| 7 | |
srs5694 | e7b4ff9 | 2009-08-18 13:16:10 -0400 | [diff] [blame] | 8 | #define __STDC_LIMIT_MACROS |
| 9 | #define __STDC_CONSTANT_MACROS |
| 10 | |
| 11 | #include <string.h> |
| 12 | #include <stdint.h> |
| 13 | #include <stdio.h> |
| 14 | #include "attributes.h" |
| 15 | |
| 16 | using namespace std; |
| 17 | |
| 18 | // Constructor. Its main task is to initialize the attribute name |
| 19 | // data. |
| 20 | Attributes::Attributes(void) { |
| 21 | int i; |
| 22 | char temp[ATR_NAME_SIZE]; |
| 23 | |
| 24 | // Most bits are undefined, so start by giving them an |
| 25 | // appropriate name |
| 26 | for (i = 1; i < NUM_ATR; i++) { |
| 27 | sprintf(temp, "Undefined bit #%d", i); |
| 28 | strcpy(atNames[i], temp); |
| 29 | } // for |
| 30 | |
| 31 | // Now reset those names that are defined.... |
| 32 | strcpy(atNames[0], "system partition"); |
| 33 | strcpy(atNames[60], "read-only"); |
| 34 | strcpy(atNames[62], "hidden"); |
| 35 | strcpy(atNames[63], "do not automount"); |
| 36 | } // Attributes constructor |
| 37 | |
| 38 | // Destructor. |
| 39 | Attributes::~Attributes(void) { |
| 40 | } // Attributes destructor |
| 41 | |
| 42 | // Display current attributes to user |
| 43 | void Attributes::DisplayAttributes(void) { |
| 44 | int i; |
| 45 | |
| 46 | printf("Attribute value is %llX. Set fields are:\n", |
| 47 | (unsigned long long) attributes); |
| 48 | for (i = 0; i < NUM_ATR; i++) { |
| 49 | if (((attributes >> i) % 2) == 1) { // bit is set |
| 50 | /* if (strncmp("Undefined", atNames[i], 9) != 0) |
| 51 | printf("%s\n", atNames[i]); */ |
| 52 | if (strncmp("Undefined", atNames[NUM_ATR - i - 1], 9) != 0) |
| 53 | printf("%s\n", atNames[NUM_ATR - i - 1]); |
| 54 | } // if |
| 55 | } // for |
| 56 | } // Attributes::DisplayAttributes() |
| 57 | |
| 58 | // Prompt user for attribute changes |
| 59 | void Attributes::ChangeAttributes(void) { |
| 60 | int response, i; |
| 61 | uint64_t bitValue; |
| 62 | |
| 63 | printf("Known attributes are:\n"); |
| 64 | for (i = 0; i < NUM_ATR; i++) { |
| 65 | if (strncmp("Undefined", atNames[i], 9) != 0) |
| 66 | printf("%d - %s\n", i, atNames[i]); |
| 67 | } // for |
| 68 | |
| 69 | do { |
| 70 | response = GetNumber(0, 64, -1, "Toggle which attribute field (0-63, 64 to exit): "); |
| 71 | if (response != 64) { |
| 72 | bitValue = PowerOf2(NUM_ATR - response - 1); // Find the integer value of the bit |
| 73 | // bitValue = PowerOf2(response); // Find the integer value of the bit |
| 74 | if ((bitValue & attributes) == bitValue) { // bit is set |
| 75 | attributes -= bitValue; // so unset it |
| 76 | printf("Have disabled the '%s' attribute.\n", atNames[response]); |
| 77 | } else { // bit is not set |
| 78 | attributes += bitValue; // so set it |
| 79 | printf("Have enabled the '%s' attribute.\n", atNames[response]); |
| 80 | } // if/else |
| 81 | } // if |
| 82 | } while (response != 64); |
| 83 | } // Attributes::ChangeAttributes() |
| 84 | |