xref: /reactos/base/system/diskpart/select.c (revision 2752c42f)
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     ULONG ulValue;
25 
26     DPRINT("Select Disk()\n");
27 
28     if (argc > 3)
29     {
30         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
31         return;
32     }
33 
34     if (argc == 2)
35     {
36         if (CurrentDisk == NULL)
37             ConResPuts(StdOut, IDS_SELECT_NO_DISK);
38         else
39             ConResPrintf(StdOut, IDS_SELECT_DISK, CurrentDisk->DiskNumber);
40         return;
41     }
42 
43     if (!IsDecString(argv[2]))
44     {
45         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
46         return;
47     }
48 
49     ulValue = wcstoul(argv[2], NULL, 10);
50     if ((ulValue == 0) && (errno == ERANGE))
51     {
52         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
53         return;
54     }
55 
56     CurrentDisk = NULL;
57 
58     Entry = DiskListHead.Flink;
59     while (Entry != &DiskListHead)
60     {
61         DiskEntry = CONTAINING_RECORD(Entry, DISKENTRY, ListEntry);
62 
63         if (DiskEntry->DiskNumber == ulValue)
64         {
65             CurrentDisk = DiskEntry;
66             CurrentPartition = NULL;
67             ConResPrintf(StdOut, IDS_SELECT_DISK, CurrentDisk->DiskNumber);
68             return;
69         }
70 
71         Entry = Entry->Flink;
72     }
73 
74     ConResPuts(StdErr, IDS_SELECT_DISK_INVALID);
75 }
76 
77 
78 static
79 VOID
80 SelectPartition(
81     INT argc,
82     LPWSTR *argv)
83 {
84     PLIST_ENTRY Entry;
85     PPARTENTRY PartEntry;
86     ULONG ulValue;
87     ULONG PartNumber = 1;
88 
89     DPRINT("Select Partition()\n");
90 
91     if (argc > 3)
92     {
93         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
94         return;
95     }
96 
97     if (CurrentDisk == NULL)
98     {
99         ConResPuts(StdOut, IDS_SELECT_PARTITION_NO_DISK);
100         return;
101     }
102 
103     if (argc == 2)
104     {
105         if (CurrentPartition == NULL)
106             ConResPuts(StdOut, IDS_SELECT_NO_PARTITION);
107         else
108             ConResPrintf(StdOut, IDS_SELECT_PARTITION, CurrentPartition);
109         return;
110     }
111 
112     if (!IsDecString(argv[2]))
113     {
114         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
115         return;
116     }
117 
118     ulValue = wcstoul(argv[2], NULL, 10);
119     if ((ulValue == 0) && (errno == ERANGE))
120     {
121         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
122         return;
123     }
124 
125     Entry = CurrentDisk->PrimaryPartListHead.Flink;
126     while (Entry != &CurrentDisk->PrimaryPartListHead)
127     {
128         PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
129 
130         if (PartEntry->PartitionType != 0)
131         {
132             if (PartNumber == ulValue)
133             {
134                 CurrentPartition = PartEntry;
135                 ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
136                 return;
137             }
138 
139             PartNumber++;
140         }
141 
142         Entry = Entry->Flink;
143     }
144 
145     Entry = CurrentDisk->LogicalPartListHead.Flink;
146     while (Entry != &CurrentDisk->LogicalPartListHead)
147     {
148         PartEntry = CONTAINING_RECORD(Entry, PARTENTRY, ListEntry);
149 
150         if (PartEntry->PartitionType != 0)
151         {
152             if (PartNumber == ulValue)
153             {
154                 CurrentPartition = PartEntry;
155                 ConResPrintf(StdOut, IDS_SELECT_PARTITION, PartNumber);
156                 return;
157             }
158 
159             PartNumber++;
160         }
161         Entry = Entry->Flink;
162     }
163 
164     ConResPuts(StdErr, IDS_SELECT_PARTITION_INVALID);
165 }
166 
167 
168 static
169 VOID
170 SelectVolume(
171     INT argc,
172     LPWSTR *argv)
173 {
174     PLIST_ENTRY Entry;
175     PVOLENTRY VolumeEntry;
176     ULONG ulValue;
177 
178     DPRINT("SelectVolume()\n");
179 
180     if (argc > 3)
181     {
182         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
183         return;
184     }
185 
186     if (argc == 2)
187     {
188         if (CurrentDisk == NULL)
189             ConResPuts(StdOut, IDS_SELECT_NO_VOLUME);
190         else
191             ConResPrintf(StdOut, IDS_SELECT_VOLUME, CurrentVolume->VolumeNumber);
192         return;
193     }
194 
195     if (!IsDecString(argv[2]))
196     {
197         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
198         return;
199     }
200 
201     ulValue = wcstoul(argv[2], NULL, 10);
202     if ((ulValue == 0) && (errno == ERANGE))
203     {
204         ConResPuts(StdErr, IDS_ERROR_INVALID_ARGS);
205         return;
206     }
207 
208     CurrentVolume = NULL;
209 
210     Entry = VolumeListHead.Flink;
211     while (Entry != &VolumeListHead)
212     {
213         VolumeEntry = CONTAINING_RECORD(Entry, VOLENTRY, ListEntry);
214 
215         if (VolumeEntry->VolumeNumber == ulValue)
216         {
217             CurrentVolume = VolumeEntry;
218             ConResPrintf(StdOut, IDS_SELECT_VOLUME, CurrentVolume->VolumeNumber);
219             return;
220         }
221 
222         Entry = Entry->Flink;
223     }
224 
225     ConResPuts(StdErr, IDS_SELECT_VOLUME_INVALID);
226 }
227 
228 
229 BOOL
230 select_main(
231     INT argc,
232     LPWSTR *argv)
233 {
234     /* gets the first word from the string */
235     if (argc == 1)
236     {
237         ConResPuts(StdOut, IDS_HELP_CMD_SELECT);
238         return TRUE;
239     }
240 
241     /* determines which to list (disk, partition, etc.) */
242     if (!wcsicmp(argv[1], L"disk"))
243         SelectDisk(argc, argv);
244     else if (!wcsicmp(argv[1], L"partition"))
245         SelectPartition(argc, argv);
246     else if (!wcsicmp(argv[1], L"volume"))
247         SelectVolume(argc, argv);
248     else
249         ConResPuts(StdOut, IDS_HELP_CMD_SELECT);
250 
251     return TRUE;
252 }
253