xref: /reactos/base/system/diskpart/select.c (revision 40462c92)
1 /*
2  * PROJECT:         ReactOS DiskPart
3  * LICENSE:         GPL - See COPYING in the top level directory
4  * FILE:            base/system/diskpart/select.c
5  * PURPOSE:         Manages all the partitions of the OS in an interactive way.
6  * PROGRAMMERS:     Lee Schroeder
7  */
8 
9 #include "diskpart.h"
10 
11 #define NDEBUG
12 #include <debug.h>
13 
14 /* FUNCTIONS ******************************************************************/
15 
16 static
17 VOID
18 SelectDisk(
19     INT argc,
20     LPWSTR *argv)
21 {
22     PLIST_ENTRY Entry;
23     PDISKENTRY DiskEntry;
24     LONG lValue;
25     LPWSTR endptr = NULL;
26 
27     DPRINT("Select Disk()\n");
28 
29     if (argc > 3)
30     {
31         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
32         return;
33     }
34 
35     if (argc == 2)
36     {
37         if (CurrentDisk == NULL)
38             ConResPuts(StdOut, IDS_SELECT_NO_DISK);
39         else
40             ConResPrintf(StdOut, IDS_SELECT_DISK, CurrentDisk->DiskNumber);
41         return;
42     }
43 
44     lValue = wcstol(argv[2], &endptr, 10);
45     if (((lValue == 0) && (endptr == argv[2])) ||
46         (lValue < 0))
47     {
48         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
49         return;
50     }
51 
52     CurrentDisk = NULL;
53 
54     Entry = DiskListHead.Flink;
55     while (Entry != &DiskListHead)
56     {
57         DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
58 
59         if (DiskEntry->DiskNumber == (ULONG)lValue)
60         {
61             CurrentDisk = DiskEntry;
62             CurrentPartition = NULL;
63             ConResPrintf(StdOut, IDS_SELECT_DISK, CurrentDisk->DiskNumber);
64             return;
65         }
66 
67         Entry = Entry->Flink;
68     }
69 
70     ConResPuts(StdErr, IDS_SELECT_DISK_INVALID);
71 }
72 
73 
74 static
75 VOID
76 SelectPartition(
77     INT argc,
78     LPWSTR *argv)
79 {
80     PLIST_ENTRY Entry;
81     PPARTENTRY PartEntry;
82     LONG lValue;
83     LPWSTR endptr = NULL;
84     ULONG PartNumber = 1;
85 
86     DPRINT("Select Partition()\n");
87 
88     if (argc > 3)
89     {
90         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
91         return;
92     }
93 
94     if (CurrentDisk == NULL)
95     {
96         ConResPuts(StdOut, IDS_SELECT_PARTITION_NO_DISK);
97         return;
98     }
99 
100     if (argc == 2)
101     {
102         if (CurrentPartition == NULL)
103             ConResPuts(StdOut, IDS_SELECT_NO_PARTITION);
104         else
105             ConResPrintf(StdOut, IDS_SELECT_PARTITION, CurrentPartition);
106         return;
107     }
108 
109     lValue = wcstol(argv[2], &endptr, 10);
110     if (((lValue == 0) && (endptr == argv[2])) ||
111         (lValue < 0))
112     {
113         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
114         return;
115     }
116 
117     Entry = CurrentDisk->PrimaryPartListHead.Flink;
118     while (Entry != &CurrentDisk->PrimaryPartListHead)
119     {
120         PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
121 
122         if (PartEntry->PartitionType != 0)
123         {
124             if (PartNumber == (ULONG)lValue)
125             {
126                 CurrentPartition = PartEntry;
127                 ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
128                 return;
129             }
130 
131             PartNumber++;
132         }
133 
134         Entry = Entry->Flink;
135     }
136 
137     Entry = CurrentDisk->LogicalPartListHead.Flink;
138     while (Entry != &CurrentDisk->LogicalPartListHead)
139     {
140         PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
141 
142         if (PartEntry->PartitionType != 0)
143         {
144             if (PartNumber == (ULONG)lValue)
145             {
146                 CurrentPartition = PartEntry;
147                 ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
148                 return;
149             }
150 
151             PartNumber++;
152         }
153         Entry = Entry->Flink;
154     }
155 
156     ConResPuts(StdErr, IDS_SELECT_PARTITION_INVALID);
157 }
158 
159 
160 BOOL
161 select_main(
162     INT argc,
163     LPWSTR *argv)
164 {
165     /* gets the first word from the string */
166     if (argc == 1)
167     {
168         ConResPuts(StdOut, IDS_HELP_CMD_SELECT);
169         return TRUE;
170     }
171 
172     /* determines which to list (disk, partition, etc.) */
173     if (!wcsicmp(argv[1], L"disk"))
174         SelectDisk(argc, argv);
175     else if (!wcsicmp(argv[1], L"partition"))
176         SelectPartition(argc, argv);
177     else
178         ConResPuts(StdOut, IDS_HELP_CMD_SELECT);
179 
180     return TRUE;
181 }
182