blob: ac9f1fc14b785c9697b99b9f4642e53253619eb7 [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>
srs569408bb0da2010-02-19 17:19:55 -050015#include <sstream>
srs5694e7b4ff92009-08-18 13:16:10 -040016#include "attributes.h"
17
18using namespace std;
19
20// Constructor. Its main task is to initialize the attribute name
21// data.
22Attributes::Attributes(void) {
23 int i;
srs569408bb0da2010-02-19 17:19:55 -050024 ostringstream temp;
srs5694e7b4ff92009-08-18 13:16:10 -040025
26 // Most bits are undefined, so start by giving them an
27 // appropriate name
28 for (i = 1; i < NUM_ATR; i++) {
srs569408bb0da2010-02-19 17:19:55 -050029 temp << "Undefined bit #" << i;
30 atNames[i] = temp.str();
srs5694e7b4ff92009-08-18 13:16:10 -040031 } // for
32
33 // Now reset those names that are defined....
srs56946699b012010-02-04 00:55:30 -050034 atNames[0] = "system partition"; // required for computer to operate
srs5694fed16d02010-01-27 23:03:40 -050035 atNames[60] = "read-only";
36 atNames[62] = "hidden";
37 atNames[63] = "do not automount";
srs5694e7b4ff92009-08-18 13:16:10 -040038} // Attributes constructor
39
40// Destructor.
41Attributes::~Attributes(void) {
42} // Attributes destructor
43
44// Display current attributes to user
45void Attributes::DisplayAttributes(void) {
46 int i;
47
srs5694fed16d02010-01-27 23:03:40 -050048 cout << "Attribute value is ";
49 cout.setf(ios::uppercase);
50 cout.fill('0');
51 cout.width(16);
52 cout << hex << attributes << dec << ". Set fields are:\n";
srs5694e7b4ff92009-08-18 13:16:10 -040053 for (i = 0; i < NUM_ATR; i++) {
54 if (((attributes >> i) % 2) == 1) { // bit is set
srs5694fed16d02010-01-27 23:03:40 -050055 if (atNames[NUM_ATR - i - 1].substr(0, 9) != "Undefined")
56 cout << atNames[NUM_ATR - i - 1] << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040057 } // if
58 } // for
srs5694fed16d02010-01-27 23:03:40 -050059 cout.fill(' ');
srs5694e7b4ff92009-08-18 13:16:10 -040060} // Attributes::DisplayAttributes()
61
62// Prompt user for attribute changes
63void Attributes::ChangeAttributes(void) {
64 int response, i;
65 uint64_t bitValue;
66
srs5694fed16d02010-01-27 23:03:40 -050067 cout << "Known attributes are:\n";
srs5694e7b4ff92009-08-18 13:16:10 -040068 for (i = 0; i < NUM_ATR; i++) {
srs5694fed16d02010-01-27 23:03:40 -050069 if (atNames[i].substr(0, 9) != "Undefined")
70 cout << i << " - " << atNames[i] << "\n";
srs5694e7b4ff92009-08-18 13:16:10 -040071 } // for
72
73 do {
srs5694fed16d02010-01-27 23:03:40 -050074 response = GetNumber(0, 64, -1, (string) "Toggle which attribute field (0-63, 64 to exit): ");
srs5694e7b4ff92009-08-18 13:16:10 -040075 if (response != 64) {
76 bitValue = PowerOf2(NUM_ATR - response - 1); // Find the integer value of the bit
srs5694e7b4ff92009-08-18 13:16:10 -040077 if ((bitValue & attributes) == bitValue) { // bit is set
78 attributes -= bitValue; // so unset it
srs5694fed16d02010-01-27 23:03:40 -050079 cout << "Have disabled the '" << atNames[response] << "' attribute.\n";
srs5694e7b4ff92009-08-18 13:16:10 -040080 } else { // bit is not set
81 attributes += bitValue; // so set it
srs5694fed16d02010-01-27 23:03:40 -050082 cout << "Have enabled the '" << atNames[response] << "' attribute.\n";
srs5694e7b4ff92009-08-18 13:16:10 -040083 } // if/else
84 } // if
85 } while (response != 64);
86} // Attributes::ChangeAttributes()
87