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