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