xref: /aosp_15_r20/external/gptfdisk/gptcl.cc (revision 57696d54d05c64fd1b1787f8371dbcf104911cfb)
1 /*
2     Implementation of GPTData class derivative with popt-based command
3     line processing
4     Copyright (C) 2010-2022 Roderick W. Smith
5 
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10 
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20 
21 #include <string.h>
22 #include <string>
23 #include <iostream>
24 #include <sstream>
25 #include <errno.h>
26 #include "gptcl.h"
27 
28 using namespace std;
29 
GPTDataCL(void)30 GPTDataCL::GPTDataCL(void) {
31    attributeOperation = backupFile = partName = hybrids = newPartInfo = NULL;
32    mbrParts = twoParts = outDevice = typeCode = partGUID = diskGUID = NULL;
33    alignment = DEFAULT_ALIGNMENT;
34    alignEnd = false;
35    deletePartNum = infoPartNum = largestPartNum = bsdPartNum = 0;
36    tableSize = GPT_SIZE;
37 } // GPTDataCL constructor
38 
GPTDataCL(string filename)39 GPTDataCL::GPTDataCL(string filename) {
40 } // GPTDataCL constructor with filename
41 
~GPTDataCL(void)42 GPTDataCL::~GPTDataCL(void) {
43 } // GPTDataCL destructor
44 
LoadBackupFile(string backupFile,int & saveData,int & neverSaveData)45 void GPTDataCL::LoadBackupFile(string backupFile, int &saveData, int &neverSaveData) {
46    if (LoadGPTBackup(backupFile) == 1) {
47       JustLooking(0);
48       saveData = 1;
49    } else {
50       saveData = 0;
51       neverSaveData = 1;
52       cerr << "Error loading backup file!\n";
53    } // else
54 } // GPTDataCL::LoadBackupFile()
55 
56 // Perform the actions specified on the command line. This is necessarily one
57 // monster of a function!
58 // Returns values:
59 // 0 = success
60 // 1 = too few arguments
61 // 2 = error when reading partition table
62 // 3 = non-GPT disk and no -g option
63 // 4 = unable to save changes
64 // 8 = disk replication operation (-R) failed
DoOptions(int argc,char * argv[])65 int GPTDataCL::DoOptions(int argc, char* argv[]) {
66    GPTData secondDevice;
67    int opt, numOptions = 0, saveData = 0, neverSaveData = 0;
68    int partNum = 0, newPartNum = -1, saveNonGPT = 1, retval = 0, pretend = 0;
69    int byteSwapPartNum = 0;
70    uint64_t low, high, startSector, endSector, sSize, mainTableLBA;
71    uint64_t temp; // temporary variable; free to use in any case
72    char *device;
73    string cmd, typeGUID, name;
74    PartType typeHelper;
75 
76    struct poptOption theOptions[] =
77    {
78       {"attributes", 'A', POPT_ARG_STRING, &attributeOperation, 'A', "operate on partition attributes",
79           "list|[partnum:show|or|nand|xor|=|set|clear|toggle|get[:bitnum|hexbitmask]]"},
80       {"set-alignment", 'a', POPT_ARG_INT, &alignment, 'a', "set sector alignment", "value"},
81       {"backup", 'b', POPT_ARG_STRING, &backupFile, 'b', "backup GPT to file", "file"},
82       {"byte-swap-name", 'B',  POPT_ARG_INT, &byteSwapPartNum, 'B', "byte-swap partition's name", "partnum"},
83       {"change-name", 'c', POPT_ARG_STRING, &partName, 'c', "change partition's name", "partnum:name"},
84       {"recompute-chs", 'C', POPT_ARG_NONE, NULL, 'C', "recompute CHS values in protective/hybrid MBR", ""},
85       {"delete", 'd', POPT_ARG_INT, &deletePartNum, 'd', "delete a partition", "partnum"},
86       {"display-alignment", 'D', POPT_ARG_NONE, NULL, 'D', "show number of sectors per allocation block", ""},
87       {"move-second-header", 'e', POPT_ARG_NONE, NULL, 'e', "move second header to end of disk", ""},
88       {"end-of-largest", 'E', POPT_ARG_NONE, NULL, 'E', "show end of largest free block", ""},
89       {"first-in-largest", 'f', POPT_ARG_NONE, NULL, 'f', "show start of the largest free block", ""},
90       {"first-aligned-in-largest", 'F', POPT_ARG_NONE, NULL, 'F', "show start of the largest free block, aligned", ""},
91       {"mbrtogpt", 'g', POPT_ARG_NONE, NULL, 'g', "convert MBR to GPT", ""},
92       {"randomize-guids", 'G', POPT_ARG_NONE, NULL, 'G', "randomize disk and partition GUIDs", ""},
93       {"hybrid", 'h', POPT_ARG_STRING, &hybrids, 'h', "create hybrid MBR", "partnum[:partnum...][:EE]"},
94       {"info", 'i', POPT_ARG_INT, &infoPartNum, 'i', "show detailed information on partition", "partnum"},
95       {"align-end", 'I', POPT_ARG_NONE, NULL, 'I', "align partition end points", ""},
96       {"move-main-table", 'j', POPT_ARG_INT, &mainTableLBA, 'j', "adjust the location of the main partition table", "sector"},
97       {"load-backup", 'l', POPT_ARG_STRING, &backupFile, 'l', "load GPT backup from file", "file"},
98       {"list-types", 'L', POPT_ARG_NONE, NULL, 'L', "list known partition types", ""},
99       {"gpttombr", 'm', POPT_ARG_STRING, &mbrParts, 'm', "convert GPT to MBR", "partnum[:partnum...]"},
100       {"new", 'n', POPT_ARG_STRING, &newPartInfo, 'n', "create new partition", "partnum:start:end"},
101       {"largest-new", 'N', POPT_ARG_INT, &largestPartNum, 'N', "create largest possible new partition", "partnum"},
102       {"clear", 'o', POPT_ARG_NONE, NULL, 'o', "clear partition table", ""},
103       {"print-mbr", 'O', POPT_ARG_NONE, NULL, 'O', "print MBR partition table", ""},
104       {"print", 'p', POPT_ARG_NONE, NULL, 'p', "print partition table", ""},
105       {"pretend", 'P', POPT_ARG_NONE, NULL, 'P', "make changes in memory, but don't write them", ""},
106       {"transpose", 'r', POPT_ARG_STRING, &twoParts, 'r', "transpose two partitions", "partnum:partnum"},
107       {"replicate", 'R', POPT_ARG_STRING, &outDevice, 'R', "replicate partition table", "device_filename"},
108       {"sort", 's', POPT_ARG_NONE, NULL, 's', "sort partition table entries", ""},
109       {"resize-table", 'S', POPT_ARG_INT, &tableSize, 'S', "resize partition table", "numparts"},
110       {"typecode", 't', POPT_ARG_STRING, &typeCode, 't', "change partition type code", "partnum:{hexcode|GUID}"},
111       {"transform-bsd", 'T', POPT_ARG_INT, &bsdPartNum, 'T', "transform BSD disklabel partition to GPT", "partnum"},
112       {"partition-guid", 'u', POPT_ARG_STRING, &partGUID, 'u', "set partition GUID", "partnum:guid"},
113       {"disk-guid", 'U', POPT_ARG_STRING, &diskGUID, 'U', "set disk GUID", "guid"},
114       {"verify", 'v', POPT_ARG_NONE, NULL, 'v', "check partition table integrity", ""},
115       {"version", 'V', POPT_ARG_NONE, NULL, 'V', "display version information", ""},
116       {"zap", 'z', POPT_ARG_NONE, NULL, 'z', "zap (destroy) GPT (but not MBR) data structures", ""},
117       {"zap-all", 'Z', POPT_ARG_NONE, NULL, 'Z', "zap (destroy) GPT and MBR data structures", ""},
118       POPT_AUTOHELP { NULL, 0, 0, NULL, 0, NULL, NULL }
119    };
120 
121    // Create popt context...
122    poptCon = poptGetContext(NULL, argc, (const char**) argv, theOptions, 0);
123 
124    poptSetOtherOptionHelp(poptCon, " [OPTION...] <device>");
125 
126    if (argc < 2) {
127       poptPrintUsage(poptCon, stderr, 0);
128       return 1;
129    }
130 
131    // Do one loop through the options to find the device filename and deal
132    // with options that don't require a device filename, to flag destructive
133    // (o, z, or Z) options, and to flag presence of a --pretend/-P option
134    while ((opt = poptGetNextOpt(poptCon)) > 0) {
135       switch (opt) {
136          case 'A':
137             cmd = GetString(attributeOperation, 1);
138             if (cmd == "list")
139                Attributes::ListAttributes();
140             break;
141          case 'L':
142             typeHelper.ShowAllTypes(0);
143             break;
144          case 'P':
145             pretend = 1;
146             break;
147          case 'V':
148             cout << "GPT fdisk (sgdisk) version " << GPTFDISK_VERSION << "\n\n";
149             break;
150          default:
151             break;
152       } // switch
153       numOptions++;
154    } // while
155 
156    // Assume first non-option argument is the device filename....
157    device = (char*) poptGetArg(poptCon);
158 
159    if (device != NULL) {
160       device = strdup(device);
161       poptResetContext(poptCon);
162       JustLooking(); // reset as necessary
163       BeQuiet(); // Tell called functions to be less verbose & interactive
164       if (LoadPartitions((string) device)) {
165          if ((WhichWasUsed() == use_mbr) || (WhichWasUsed() == use_bsd))
166             saveNonGPT = 0; // flag so we don't overwrite unless directed to do so
167          sSize = GetBlockSize();
168          while ((opt = poptGetNextOpt(poptCon)) > 0) {
169             switch (opt) {
170                case 'A': {
171                   if (cmd != "list") {
172                      partNum = (int) GetInt(attributeOperation, 1) - 1;
173                      if (partNum < 0)
174                         partNum = newPartNum;
175                      if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
176                         switch (ManageAttributes(partNum, GetString(attributeOperation, 2),
177                            GetString(attributeOperation, 3))) {
178                            case -1:
179                               saveData = 0;
180                               neverSaveData = 1;
181                               break;
182                            case 1:
183                               JustLooking(0);
184                               saveData = 1;
185                               break;
186                            default:
187                               break;
188                         } // switch
189                      } else {
190                         cerr << "Error: Invalid partition number " << partNum + 1 << "\n";
191                         saveData = 0;
192                         neverSaveData = 1;
193                      } // if/else reasonable partition #
194                   } // if (cmd != "list")
195                   break;
196                } // case 'A':
197                case 'a':
198                   SetAlignment(alignment);
199                   break;
200                case 'B':
201                   if (IsUsedPartNum(byteSwapPartNum - 1)) {
202                      partitions[byteSwapPartNum - 1].ReverseNameBytes();
203                      cout << "Changed partition " << byteSwapPartNum << "'s name to "
204                           << partitions[byteSwapPartNum - 1].GetDescription() << "\n";
205                      JustLooking(0);
206                      saveData = 1;
207                   }
208                   break;
209                case 'b':
210                   SaveGPTBackup(backupFile);
211                   free(backupFile);
212                   break;
213                case 'c':
214                   JustLooking(0);
215                   partNum = (int) GetInt(partName, 1) - 1;
216                   if (partNum < 0)
217                      partNum = newPartNum;
218                   if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
219                      name = GetString(partName, 2);
220                      if (SetName(partNum, (UnicodeString) name.c_str())) {
221                         saveData = 1;
222                      } else {
223                         cerr << "Unable to set partition " << partNum + 1
224                              << "'s name to '" << GetString(partName, 2) << "'!\n";
225                         neverSaveData = 1;
226                      } // if/else
227                      free(partName);
228                   }
229                   break;
230                case 'C':
231                   JustLooking(0);
232                   RecomputeCHS();
233                   saveData = 1;
234                   break;
235                case 'd':
236                   JustLooking(0);
237                   if (DeletePartition(deletePartNum - 1) == 0) {
238                      cerr << "Error " << errno << " deleting partition!\n";
239                      neverSaveData = 1;
240                   } else saveData = 1;
241                                                       break;
242                case 'D':
243                   cout << GetAlignment() << "\n";
244                   break;
245                case 'e':
246                   JustLooking(0);
247                   MoveSecondHeaderToEnd();
248                   saveData = 1;
249                   break;
250                case 'E':
251                   cout << FindLastInFree(FindFirstInLargest()) << "\n";
252                   break;
253                case 'f':
254                   cout << FindFirstInLargest() << "\n";
255                   break;
256                case 'F':
257                   temp = FindFirstInLargest();
258                   Align(&temp);
259                   cout << temp << "\n";
260                   break;
261                case 'g':
262                   JustLooking(0);
263                   saveData = 1;
264                   saveNonGPT = 1;
265                   break;
266                case 'G':
267                   JustLooking(0);
268                   saveData = 1;
269                   RandomizeGUIDs();
270                   break;
271                case 'h':
272                   JustLooking(0);
273                   if (BuildMBR(hybrids, 1) == 1)
274                      saveData = 1;
275                   break;
276                case 'i':
277                   ShowPartDetails(infoPartNum - 1);
278                   break;
279                case 'I':
280                   alignEnd = true;
281                   break;
282                case 'j':
283                    if (MoveMainTable(mainTableLBA)) {
284                        JustLooking(0);
285                        saveData = 1;
286                    } else {
287                        neverSaveData = 1;
288                    } // if/else
289                    break;
290                case 'l':
291                   LoadBackupFile(backupFile, saveData, neverSaveData);
292                   free(backupFile);
293                   break;
294                case 'L':
295                   break;
296                case 'm':
297                   JustLooking(0);
298                   if (BuildMBR(mbrParts, 0) == 1) {
299                      if (!pretend) {
300                         if (SaveMBR()) {
301                            DestroyGPT();
302                         } else
303                            cerr << "Problem saving MBR!\n";
304                      } // if
305                      saveNonGPT = 0;
306                      pretend = 1; // Not really, but works around problem if -g is used with this...
307                      saveData = 0;
308                   } // if
309                   break;
310                case 'n':
311                   JustLooking(0);
312                   newPartNum = (int) GetInt(newPartInfo, 1) - 1;
313                   if (newPartNum < 0)
314                      newPartNum = FindFirstFreePart();
315                   low = FindFirstInLargest();
316                   Align(&low);
317                   high = FindLastInFree(low, alignEnd);
318                   startSector = IeeeToInt(GetString(newPartInfo, 2), sSize, low, high, sectorAlignment, low);
319                   endSector = IeeeToInt(GetString(newPartInfo, 3), sSize, startSector, high, sectorAlignment, high);
320                   if (CreatePartition(newPartNum, startSector, endSector)) {
321                      saveData = 1;
322                   } else {
323                      cerr << "Could not create partition " << newPartNum + 1 << " from "
324                           << startSector << " to " << endSector << "\n";
325                      neverSaveData = 1;
326                   } // if/else
327                   free(newPartInfo);
328                   break;
329                case 'N':
330                   JustLooking(0);
331                   startSector = FindFirstInLargest();
332                   Align(&startSector);
333                   endSector = FindLastInFree(startSector, alignEnd);
334                   if (largestPartNum <= 0) {
335                      largestPartNum = FindFirstFreePart() + 1;
336                      newPartNum = largestPartNum - 1;
337                   }
338                   if (CreatePartition(largestPartNum - 1, startSector, endSector)) {
339                      saveData = 1;
340                   } else {
341                      cerr << "Could not create partition " << largestPartNum << " from "
342                      << startSector << " to " << endSector << "\n";
343                      neverSaveData = 1;
344                   } // if/else
345                   break;
346                case 'o':
347                   JustLooking(0);
348                   ClearGPTData();
349                   saveData = 1;
350                   break;
351                case 'O':
352                    DisplayMBRData();
353                    break;
354                case 'p':
355                   DisplayGPTData();
356                   break;
357                case 'P':
358                   pretend = 1;
359                   break;
360                case 'r':
361                   JustLooking(0);
362                   uint64_t p1, p2;
363                   p1 = GetInt(twoParts, 1) - 1;
364                   p2 = GetInt(twoParts, 2) - 1;
365                   if (SwapPartitions((uint32_t) p1, (uint32_t) p2) == 0) {
366                      neverSaveData = 1;
367                      cerr << "Cannot swap partitions " << p1 + 1 << " and " << p2 + 1 << "\n";
368                   } else saveData = 1;
369                                                       break;
370                case 'R':
371                   secondDevice = *this;
372                   secondDevice.SetDisk(outDevice);
373                   secondDevice.JustLooking(0);
374                   if (!secondDevice.SaveGPTData(1))
375                      retval = 8;
376                   break;
377                case 's':
378                   JustLooking(0);
379                   SortGPT();
380                   saveData = 1;
381                   break;
382                case 'S':
383                   JustLooking(0);
384                   if (SetGPTSize(tableSize) == 0)
385                      neverSaveData = 1;
386                   else
387                      saveData = 1;
388                   break;
389                case 't':
390                   JustLooking(0);
391                   partNum = (int) GetInt(typeCode, 1) - 1;
392                   if (partNum < 0)
393                      partNum = newPartNum;
394                   if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
395                      // Remember the original hex value requested
396                      string raw = GetString(typeCode, 2);
397                      if (raw.size() == 4) {
398                         typeRaw[partNum] = StrToHex(raw, 0);
399                      }
400                      typeHelper = GetString(typeCode, 2);
401                      if ((typeHelper != PartType::unusedPartType) &&
402                          (ChangePartType(partNum, typeHelper))) {
403                         saveData = 1;
404                         } else {
405                            cerr << "Could not change partition " << partNum + 1
406                            << "'s type code to " << GetString(typeCode, 2) << "!\n";
407                            neverSaveData = 1;
408                         } // if/else
409                      free(typeCode);
410                   }
411                   break;
412                case 'T':
413                   JustLooking(0);
414                   XFormDisklabel(bsdPartNum - 1);
415                   saveData = 1;
416                   break;
417                case 'u':
418                   JustLooking(0);
419                   saveData = 1;
420                   partNum = (int) GetInt(partGUID, 1) - 1;
421                   if (partNum < 0)
422                      partNum = newPartNum;
423                   if ((partNum >= 0) && (partNum < (int) GetNumParts())) {
424                      SetPartitionGUID(partNum, GetString(partGUID, 2).c_str());
425                   }
426                   break;
427                case 'U':
428                   JustLooking(0);
429                   saveData = 1;
430                   SetDiskGUID(diskGUID);
431                   break;
432                case 'v':
433                   Verify();
434                   break;
435                case 'z':
436                   if (!pretend) {
437                      DestroyGPT();
438                   } // if
439                   saveNonGPT = 1;
440                   saveData = 0;
441                   break;
442                case 'Z':
443                   if (!pretend) {
444                      DestroyGPT();
445                      DestroyMBR();
446                   } // if
447                   saveNonGPT = 1;
448                   saveData = 0;
449                   break;
450                default:
451                   cerr << "Unknown option (-" << opt << ")!\n";
452                   break;
453                } // switch
454          } // while
455       } else { // if loaded OK
456          poptResetContext(poptCon);
457          // Do a few types of operations even if there are problems....
458          while ((opt = poptGetNextOpt(poptCon)) > 0) {
459             switch (opt) {
460                case 'l':
461                   LoadBackupFile(backupFile, saveData, neverSaveData);
462                   cout << "Information: Loading backup partition table; will override earlier problems!\n";
463                   free(backupFile);
464                   retval = 0;
465                   break;
466                case 'o':
467                   JustLooking(0);
468                   ClearGPTData();
469                   saveData = 1;
470                   cout << "Information: Creating fresh partition table; will override earlier problems!\n";
471                   retval = 0;
472                   break;
473                case 'v':
474                   cout << "Verification may miss some problems or report too many!\n";
475                   Verify();
476                   break;
477                case 'z':
478                   if (!pretend) {
479                      DestroyGPT();
480                   } // if
481                   saveNonGPT = 1;
482                   saveData = 0;
483                   break;
484                case 'Z':
485                   if (!pretend) {
486                      DestroyGPT();
487                      DestroyMBR();
488                   } // if
489                   saveNonGPT = 1;
490                   saveData = 0;
491                   break;
492             } // switch
493          } // while
494          retval = 2;
495       } // if/else loaded OK
496       if ((saveData) && (!neverSaveData) && (saveNonGPT) && (!pretend)) {
497          if (!SaveGPTData(1))
498             retval = 4;
499       }
500       if (saveData && (!saveNonGPT)) {
501          cout << "Non-GPT disk; not saving changes. Use -g to override.\n";
502          retval = 3;
503       } // if
504       if (neverSaveData) {
505          cerr << "Error encountered; not saving changes.\n";
506          retval = 4;
507       } // if
508       free(device);
509    } // if (device != NULL)
510    poptFreeContext(poptCon);
511    return retval;
512 } // GPTDataCL::DoOptions()
513 
514 // Create a hybrid or regular MBR from GPT data structures
BuildMBR(char * argument,int isHybrid)515 int GPTDataCL::BuildMBR(char* argument, int isHybrid) {
516    int numParts, allOK = 1, i, origPartNum;
517    int eeLast = 0, mbrNum = 0;
518    MBRPart newPart;
519    BasicMBRData newMBR;
520 
521    if (argument != NULL) {
522       numParts = CountColons(argument) + 1;
523       if (isHybrid) {
524          eeLast = GetString(argument, numParts) == "EE";
525          if (eeLast) {
526             numParts--;
527          }
528       }
529 
530       if (numParts <= (4 - isHybrid)) {
531          newMBR.SetDisk(GetDisk());
532          for (i = 0; i < numParts; i++) {
533             origPartNum = GetInt(argument, i + 1) - 1;
534             if (IsUsedPartNum(origPartNum) && (partitions[origPartNum].IsSizedForMBR() == MBR_SIZED_GOOD)) {
535                mbrNum = i + (isHybrid && ! eeLast);
536                newPart.SetInclusion(PRIMARY);
537                newPart.SetLocation(operator[](origPartNum).GetFirstLBA(),
538                                    operator[](origPartNum).GetLengthLBA());
539                newPart.SetStatus(0);
540                newPart.SetType((uint8_t)(operator[](origPartNum).GetHexType() / 0x0100));
541                // If we were created with a specific hex type, use that instead
542                // of risking fidelity loss by doing a GUID-based lookup
543                if (typeRaw.count(origPartNum) == 1) {
544                   newPart.SetType(typeRaw[origPartNum]);
545                }
546                newMBR.AddPart(mbrNum, newPart);
547             } else {
548                cerr << "Original partition " << origPartNum + 1 << " does not exist or is too big! Aborting operation!\n";
549                allOK = 0;
550             } // if/else
551          } // for
552          if (isHybrid) {
553             if (eeLast) {
554                mbrNum = i;
555             } else {
556                mbrNum = 0;
557             }
558             newPart.SetInclusion(PRIMARY);
559             newPart.SetLocation(1, newMBR.FindLastInFree(1));
560             newPart.SetStatus(0);
561             newPart.SetType(0xEE);
562             newMBR.AddPart(mbrNum, newPart);
563          } // if
564          if (allOK)
565             SetProtectiveMBR(newMBR);
566       } else allOK = 0;
567    } else allOK = 0;
568    if (!allOK)
569       cerr << "Problem creating MBR!\n";
570    return allOK;
571 } // GPTDataCL::BuildMBR()
572 
573 // Returns the number of colons in argument string, ignoring the
574 // first character (thus, a leading colon is ignored, as GetString()
575 // does).
CountColons(char * argument)576 int CountColons(char* argument) {
577    int num = 0;
578 
579    while ((argument[0] != '\0') && (argument = strchr(&argument[1], ':')))
580       num++;
581 
582    return num;
583 } // GPTDataCL::CountColons()
584 
585 // Extract integer data from argument string, which should be colon-delimited
GetInt(const string & argument,int itemNum)586 uint64_t GetInt(const string & argument, int itemNum) {
587    uint64_t retval;
588 
589    istringstream inString(GetString(argument, itemNum));
590    inString >> retval;
591    return retval;
592 } // GPTDataCL::GetInt()
593 
594 // Extract string data from argument string, which should be colon-delimited
595 // If string begins with a colon, that colon is skipped in the counting. If an
596 // invalid itemNum is specified, returns an empty string.
GetString(string argument,int itemNum)597 string GetString(string argument, int itemNum) {
598    size_t startPos = 0, endPos = 0;
599    string retVal = "";
600    int foundLast = 0;
601    int numFound = 0;
602 
603    if (argument[0] == ':')
604       argument.erase(0, 1);
605    while ((numFound < itemNum) && (!foundLast)) {
606       endPos = argument.find(':', startPos);
607       numFound++;
608       if (endPos == string::npos) {
609          foundLast = 1;
610          endPos = argument.length();
611       } else if (numFound < itemNum) {
612          startPos = endPos + 1;
613       } // if/elseif
614    } // while
615    if ((numFound == itemNum) && (numFound > 0))
616       retVal = argument.substr(startPos, endPos - startPos);
617 
618    return retVal;
619 } // GetString()
620