blob: 9bdd42869a8c3e64711d5403472483c617592cb0 [file] [log] [blame]
srs5694e7b4ff92009-08-18 13:16:10 -04001// 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
srs5694221e0872009-08-29 15:00:31 -04005/* 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
srs5694e7b4ff92009-08-18 13:16:10 -04008#define __STDC_LIMIT_MACROS
9#define __STDC_CONSTANT_MACROS
10
11#include <string.h>
12#include <stdint.h>
13#include <stdio.h>
srs5694fed16d02010-01-27 23:03:40 -050014#include <iostream>
srs5694e7b4ff92009-08-18 13:16:10 -040015#include "attributes.h"
16
17using namespace std;
18
19// Constructor. Its main task is to initialize the attribute name
20// data.
21Attributes::Attributes(void) {
22 int i;
23 char temp[ATR_NAME_SIZE];
24
25 // Most bits are undefined, so start by giving them an
26 // appropriate name
27 for (i = 1; i < NUM_ATR; i++) {
28 sprintf(temp, "Undefined bit #%d", i);
srs5694fed16d02010-01-27 23:03:40 -050029 atNames[i] = temp;
srs5694e7b4ff92009-08-18 13:16:10 -040030 } // for
31
32 // Now reset those names that are defined....
srs5694fed16d02010-01-27 23:03:40 -050033 atNames[0] = "system partition";
34 atNames[60] = "read-only";
35 atNames[62] = "hidden";
36 atNames[63] = "do not automount";
srs5694e7b4ff92009-08-18 13:16:10 -040037} // Attributes constructor
38
39// Destructor.
40Attributes::~Attributes(void) {
41} // Attributes destructor
42
43// Display current attributes to user
44void Attributes::DisplayAttributes(void) {
45 int i;
46
srs5694fed16d02010-01-27 23:03:40 -050047 cout << "Attribute value is ";
48 cout.setf(ios::uppercase);
49 cout.fill('0');
50 cout.width(16);
51 cout << hex << attributes << dec << ". Set fields are:\n";
srs5694e7b4ff92009-08-18 13:16:10 -040052 for (i = 0; i < NUM_ATR; i++) {
53 if (((attributes >> i) % 2) == 1) { // bit is set
srs5694fed16d02010-01-27 23:03:40 -050054 if (atNames[NUM_ATR - i - 1].substr(0, 9) != "Undefined")
55 cout << atNames[NUM_ATR - i - 1] << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040056 } // if
57 } // for
srs5694fed16d02010-01-27 23:03:40 -050058 cout.fill(' ');
srs5694e7b4ff92009-08-18 13:16:10 -040059} // Attributes::DisplayAttributes()
60
61// Prompt user for attribute changes
62void Attributes::ChangeAttributes(void) {
63 int response, i;
64 uint64_t bitValue;
65
srs5694fed16d02010-01-27 23:03:40 -050066 cout << "Known attributes are:\n";
srs5694e7b4ff92009-08-18 13:16:10 -040067 for (i = 0; i < NUM_ATR; i++) {
srs5694fed16d02010-01-27 23:03:40 -050068 if (atNames[i].substr(0, 9) != "Undefined")
69 cout << i << " - " << atNames[i] << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040070 } // for
71
72 do {
srs5694fed16d02010-01-27 23:03:40 -050073 response = GetNumber(0, 64, -1, (string) "Toggle which attribute field (0-63, 64 to exit): ");
srs5694e7b4ff92009-08-18 13:16:10 -040074 if (response != 64) {
75 bitValue = PowerOf2(NUM_ATR - response - 1); // Find the integer value of the bit
srs5694e7b4ff92009-08-18 13:16:10 -040076 if ((bitValue & attributes) == bitValue) { // bit is set
77 attributes -= bitValue; // so unset it
srs5694fed16d02010-01-27 23:03:40 -050078 cout << "Have disabled the '" << atNames[response] << "' attribute.\n";
srs5694e7b4ff92009-08-18 13:16:10 -040079 } else { // bit is not set
80 attributes += bitValue; // so set it
srs5694fed16d02010-01-27 23:03:40 -050081 cout << "Have enabled the '" << atNames[response] << "' attribute.\n";
srs5694e7b4ff92009-08-18 13:16:10 -040082 } // if/else
83 } // if
84 } while (response != 64);
85} // Attributes::ChangeAttributes()
86