From a84363b9b1a4c0b309c8cc09a9b812fdd7784fa4 Mon Sep 17 00:00:00 2001 From: Gilles Moris Date: Wed, 15 May 2019 06:42:41 +0200 Subject: [PATCH 1/6] gdisk: show also previous type as cgdisk does when changing partition type --- gptpart.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gptpart.cc b/gptpart.cc index 666263b..0684fea 100644 --- a/gptpart.cc +++ b/gptpart.cc @@ -442,7 +442,7 @@ void GPTPart::ChangeType(void) { changeName = (GetDescription() == GetTypeName()); #endif - cout << "Current type is '" << GetTypeName() << "'\n"; + cout << "Current type is " << hex << GetHexType() << dec << " (" << GetTypeName() << ")\n"; do { cout << "Hex code or GUID (L to show codes, Enter = " << hex << DEFAULT_GPT_TYPE << dec << "): "; line = ReadString(); From faaaa2618f965d12fc47837aa026bd123f680098 Mon Sep 17 00:00:00 2001 From: Gilles Moris Date: Wed, 15 May 2019 06:46:32 +0200 Subject: [PATCH 2/6] gdisk: use previous code when changing partition type to be idempotent like cgdisk --- gptpart.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gptpart.cc b/gptpart.cc index 0684fea..56415e1 100644 --- a/gptpart.cc +++ b/gptpart.cc @@ -444,13 +444,13 @@ void GPTPart::ChangeType(void) { cout << "Current type is " << hex << GetHexType() << dec << " (" << GetTypeName() << ")\n"; do { - cout << "Hex code or GUID (L to show codes, Enter = " << hex << DEFAULT_GPT_TYPE << dec << "): "; + cout << "Hex code or GUID (L to show codes, Enter = " << hex << GetHexType() << dec << "): "; line = ReadString(); if ((line[0] == 'L') || (line[0] == 'l')) { partitionType.ShowAllTypes(); } else { if (line.length() == 0) - tempType = DEFAULT_GPT_TYPE; + tempType = GetHexType(); else tempType = line; } // if/else From 94b1490b7b906b90e743512edd305302ed96ce0f Mon Sep 17 00:00:00 2001 From: Gilles Moris Date: Wed, 15 May 2019 07:07:21 +0200 Subject: [PATCH 3/6] Create a constant unusedPartType object instead of recreating it each time --- gptcl.cc | 2 +- gptpart.cc | 4 ++-- parttypes.cc | 1 + parttypes.h | 3 +++ 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/gptcl.cc b/gptcl.cc index 6c36738..bd80984 100644 --- a/gptcl.cc +++ b/gptcl.cc @@ -375,7 +375,7 @@ int GPTDataCL::DoOptions(int argc, char* argv[]) { partNum = newPartNum; if ((partNum >= 0) && (partNum < (int) GetNumParts())) { typeHelper = GetString(typeCode, 2); - if ((typeHelper != (GUIDData) "00000000-0000-0000-0000-000000000000") && + if ((typeHelper != PartType::unusedPartType) && (ChangePartType(partNum, typeHelper))) { saveData = 1; } else { diff --git a/gptpart.cc b/gptpart.cc index 56415e1..b4f2698 100644 --- a/gptpart.cc +++ b/gptpart.cc @@ -434,7 +434,7 @@ void GPTPart::ReversePartBytes(void) { void GPTPart::ChangeType(void) { string line; int changeName; - PartType tempType = (GUIDData) "00000000-0000-0000-0000-000000000000"; + PartType tempType = PartType::unusedPartType; #ifdef USE_UTF16 changeName = (GetDescription() == GetUTypeName()); @@ -454,7 +454,7 @@ void GPTPart::ChangeType(void) { else tempType = line; } // if/else - } while (tempType == (GUIDData) "00000000-0000-0000-0000-000000000000"); + } while (tempType == PartType::unusedPartType); partitionType = tempType; cout << "Changed type of partition to '" << partitionType.TypeName() << "'\n"; if (changeName) { diff --git a/parttypes.cc b/parttypes.cc index cd225d1..1b32569 100644 --- a/parttypes.cc +++ b/parttypes.cc @@ -19,6 +19,7 @@ using namespace std; int PartType::numInstances = 0; AType* PartType::allTypes = NULL; AType* PartType::lastType = NULL; +const PartType PartType::unusedPartType = (GUIDData) "00000000-0000-0000-0000-000000000000"; // Constructor. Its main task is to initialize the data list, but only // if this is the first instance, since it's a static linked list. diff --git a/parttypes.h b/parttypes.h index c108a17..2b80026 100644 --- a/parttypes.h +++ b/parttypes.h @@ -36,6 +36,9 @@ protected: static AType* lastType; // Pointer to last entry in the list void AddAllTypes(void); public: + // PartType with GUID "00000000-0000-0000-0000-000000000000" + static const PartType unusedPartType; + PartType(void); PartType(const PartType & orig); PartType(const GUIDData & orig); From 522273e3db5823aedf6af8786cb01a316367ed32 Mon Sep 17 00:00:00 2001 From: Gilles Moris Date: Wed, 15 May 2019 07:17:40 +0200 Subject: [PATCH 4/6] Allow to quit early the display of partition types using 'q' --- parttypes.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/parttypes.cc b/parttypes.cc index 1b32569..38b9193 100644 --- a/parttypes.cc +++ b/parttypes.cc @@ -525,8 +525,10 @@ void PartType::ShowAllTypes(int maxLines) const { if (thisType->next) { cout << "\n"; if ((maxLines > 0) && (lineCount++ % maxLines) == 0) { - cout << "Press the key to see more codes: "; + cout << "Press the key to see more codes, q to quit: "; getline(cin, line); + if ((line[0] =='q') || (line[0] =='Q')) + break; } // if reached screen line limit } // if there's another entry following this one } else { From f9d08a6b6bf0bae229f45eeebeeacc64d4369151 Mon Sep 17 00:00:00 2001 From: Gilles Moris Date: Wed, 15 May 2019 07:33:38 +0200 Subject: [PATCH 5/6] Perform case insensitive search of partition types --- cgdisk.8 | 4 ++-- gdisk.8 | 4 ++-- parttypes.cc | 4 ++-- support.cc | 10 ++++++++++ support.h | 1 + 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/cgdisk.8 b/cgdisk.8 index ef982b4..72dcb2a 100644 --- a/cgdisk.8 +++ b/cgdisk.8 @@ -263,9 +263,9 @@ two\-byte hexadecimal number. You may also enter a GUID directly, if you have one and \fBcgdisk\fR doesn't know it. If you don't know the type code for your partition, you can type \fBL\fR to see a list of known type codes. The type code list may optionally be filtered by a search string; for -instance, entering \fI\fBLinux\fR\fR shows only partition type codes with +instance, entering \fI\fBlinux\fR\fR shows only partition type codes with descriptions that include the string \fILinux\fR. This search is performed -case\-sensitively. +case\-insensitively. .TP .B Verify diff --git a/gdisk.8 b/gdisk.8 index fcd07e2..9a968ec 100644 --- a/gdisk.8 +++ b/gdisk.8 @@ -191,9 +191,9 @@ more codes in GPT. For these, \fBgdisk\fR adds code numbers sequentially, such as 0xa500 for a FreeBSD disklabel, 0xa501 for FreeBSD boot, 0xa502 for FreeBSD swap, and so on. Note that these two\-byte codes are unique to \fBgdisk\fR. The type code list may optionally be filtered by a search -string; for instance, entering \fI\fBLinux\fR\fR shows only partition type +string; for instance, entering \fI\fBlinux\fR\fR shows only partition type codes with descriptions that include the string \fILinux\fR. This search is -performed case\-sensitively. +performed case\-insensitively. .TP .B n diff --git a/parttypes.cc b/parttypes.cc index 38b9193..9508933 100644 --- a/parttypes.cc +++ b/parttypes.cc @@ -510,10 +510,10 @@ void PartType::ShowAllTypes(int maxLines) const { cout.unsetf(ios::uppercase); if (maxLines > 0) { cout << "Type search string, or to show all codes: "; - matchString = ReadString(); + matchString = ToLower(ReadString()); } // if while (thisType != NULL) { - found = thisType->name.find(matchString); + found = ToLower(thisType->name).find(matchString); if ((thisType->display == 1) && (found != string::npos)) { // show it cout.fill('0'); cout.width(4); diff --git a/support.cc b/support.cc index d47965a..c80cb2d 100644 --- a/support.cc +++ b/support.cc @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include #include #include "support.h" @@ -357,3 +359,11 @@ void WinWarning(void) { exit(0); #endif } // WinWarning() + +// Returns the input string in lower case +string ToLower(const string& input) { + string lower = input; // allocate correct size through copy + + transform(input.begin(), input.end(), lower.begin(), ::tolower); + return lower; +} // ToLower() diff --git a/support.h b/support.h index 8f976da..f71c7cb 100644 --- a/support.h +++ b/support.h @@ -82,5 +82,6 @@ int IsHex(string input); // Returns 1 if input can be hexadecimal number.... int IsLittleEndian(void); // Returns 1 if CPU is little-endian, 0 if it's big-endian void ReverseBytes(void* theValue, int numBytes); // Reverses byte-order of theValue void WinWarning(void); +string ToLower(const string& input); #endif From 6a2b5acdcf9511bf3b50508910b718e369a028f1 Mon Sep 17 00:00:00 2001 From: Gilles Moris Date: Sun, 19 May 2019 16:10:16 +0200 Subject: [PATCH 6/6] Use PRIu64 macro to have a portable way to scan in uint64_t --- support.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/support.cc b/support.cc index c80cb2d..03bd1bb 100644 --- a/support.cc +++ b/support.cc @@ -8,7 +8,9 @@ #define __STDC_LIMIT_MACROS #define __STDC_CONSTANT_MACROS +#define __STDC_FORMAT_MACROS +#include #include #include #include @@ -79,7 +81,7 @@ uint64_t GetNumber(uint64_t low, uint64_t high, uint64_t def, const string & pro cin.getline(line, 255); if (!cin.good()) exit(5); - num = sscanf(line, "%lld", &response); + num = sscanf(line, "%" PRIu64, &response); if (num == 1) { // user provided a response if ((response < low) || (response > high)) cout << "Value out of range\n";