xref: /reactos/base/system/diskpart/diskpart.c (revision 2ba6b097)
1c2c66affSColin Finck /*
2c2c66affSColin Finck  * PROJECT:         ReactOS DiskPart
3c2c66affSColin Finck  * LICENSE:         GPL - See COPYING in the top level directory
4c2c66affSColin Finck  * FILE:            base/system/diskpart/diskpart.c
5c2c66affSColin Finck  * PURPOSE:         Manages all the partitions of the OS in an interactive way.
6c2c66affSColin Finck  * PROGRAMMERS:     Lee Schroeder
7c2c66affSColin Finck  */
8c2c66affSColin Finck 
9c2c66affSColin Finck /* INCLUDES ******************************************************************/
10c2c66affSColin Finck 
11c2c66affSColin Finck #include "diskpart.h"
12c2c66affSColin Finck 
13c2c66affSColin Finck /* FUNCTIONS ******************************************************************/
14c2c66affSColin Finck 
15c2c66affSColin Finck VOID
ShowHeader(VOID)16c2c66affSColin Finck ShowHeader(VOID)
17c2c66affSColin Finck {
18c2c66affSColin Finck     WCHAR szComputerName[MAX_STRING_SIZE];
19c2c66affSColin Finck     DWORD comp_size = MAX_STRING_SIZE;
20c2c66affSColin Finck 
21c2c66affSColin Finck     /* Get the name of the computer for us and change the value of comp_name */
22c2c66affSColin Finck     GetComputerNameW(szComputerName, &comp_size);
23c2c66affSColin Finck 
24c2c66affSColin Finck     /* TODO: Remove this section of code when program becomes stable enough for production use. */
25c2c66affSColin Finck     ConPuts(StdOut, L"\n*WARNING*: This program is incomplete and may not work properly.\n");
26c2c66affSColin Finck 
27c2c66affSColin Finck     /* Print the header information */
28c2c66affSColin Finck     ConPuts(StdOut, L"\n");
29c2c66affSColin Finck     ConResPuts(StdOut, IDS_APP_HEADER);
30c2c66affSColin Finck     ConPuts(StdOut, L"\n");
31c2c66affSColin Finck     ConResPuts(StdOut, IDS_APP_LICENSE);
32c2c66affSColin Finck     ConResPrintf(StdOut, IDS_APP_CURR_COMPUTER, szComputerName);
33c2c66affSColin Finck }
34c2c66affSColin Finck 
35c2c66affSColin Finck /*
36c2c66affSColin Finck  * RunScript(const char *filename):
37c2c66affSColin Finck  * opens the file, reads the contents, convert the text into readable
38c2c66affSColin Finck  * code for the computer, and then execute commands in order.
39c2c66affSColin Finck  */
40c2c66affSColin Finck BOOL
RunScript(LPCWSTR filename)41c2c66affSColin Finck RunScript(LPCWSTR filename)
42c2c66affSColin Finck {
43c2c66affSColin Finck     FILE *script;
44c2c66affSColin Finck     WCHAR tmp_string[MAX_STRING_SIZE];
45c2c66affSColin Finck 
46c2c66affSColin Finck     /* Open the file for processing */
47c2c66affSColin Finck     script = _wfopen(filename, L"r");
48c2c66affSColin Finck     if (script == NULL)
49c2c66affSColin Finck     {
50c2c66affSColin Finck         /* if there was problems opening the file */
51c2c66affSColin Finck         ConResPrintf(StdErr, IDS_ERROR_MSG_NO_SCRIPT, filename);
52c2c66affSColin Finck         return FALSE; /* if there is no script, exit the program */
53c2c66affSColin Finck     }
54c2c66affSColin Finck 
55c2c66affSColin Finck     /* Read and process the script */
56c2c66affSColin Finck     while (fgetws(tmp_string, MAX_STRING_SIZE, script) != NULL)
57c2c66affSColin Finck     {
58c2c66affSColin Finck         if (InterpretScript(tmp_string) == FALSE)
59c2c66affSColin Finck         {
60c2c66affSColin Finck             fclose(script);
61c2c66affSColin Finck             return FALSE;
62c2c66affSColin Finck         }
63c2c66affSColin Finck     }
64c2c66affSColin Finck 
65c2c66affSColin Finck     /* Close the file */
66c2c66affSColin Finck     fclose(script);
67c2c66affSColin Finck 
68c2c66affSColin Finck     return TRUE;
69c2c66affSColin Finck }
70c2c66affSColin Finck 
71c2c66affSColin Finck /*
72c2c66affSColin Finck  * wmain():
73c2c66affSColin Finck  * Main entry point of the application.
74c2c66affSColin Finck  */
wmain(int argc,const LPWSTR argv[])75c2c66affSColin Finck int wmain(int argc, const LPWSTR argv[])
76c2c66affSColin Finck {
77c2c66affSColin Finck     LPCWSTR script = NULL;
78c2c66affSColin Finck     LPCWSTR tmpBuffer = NULL;
79c2c66affSColin Finck     WCHAR appTitle[50];
80c2c66affSColin Finck     int index, timeout;
81c2c66affSColin Finck     int result = EXIT_SUCCESS;
82c2c66affSColin Finck 
83c2c66affSColin Finck     /* Initialize the Console Standard Streams */
84c2c66affSColin Finck     ConInitStdStreams();
85c2c66affSColin Finck 
86c2c66affSColin Finck     /* Sets the title of the program so the user will have an easier time
87c2c66affSColin Finck     determining the current program, especially if diskpart is running a
88c2c66affSColin Finck     script */
89c2c66affSColin Finck     K32LoadStringW(GetModuleHandle(NULL), IDS_APP_HEADER, appTitle, ARRAYSIZE(appTitle));
90c2c66affSColin Finck     SetConsoleTitleW(appTitle);
91c2c66affSColin Finck 
92c2c66affSColin Finck     /* Sets the timeout value to 0 just in case the user doesn't
93c2c66affSColin Finck     specify a value */
94c2c66affSColin Finck     timeout = 0;
95c2c66affSColin Finck 
96c2c66affSColin Finck     CreatePartitionList();
97*2ba6b097SEric Kohl     CreateVolumeList();
98c2c66affSColin Finck 
99c2c66affSColin Finck     /* If there are no command arguments, then go straight to the interpreter */
100c2c66affSColin Finck     if (argc < 2)
101c2c66affSColin Finck     {
102c2c66affSColin Finck         ShowHeader();
103c2c66affSColin Finck         InterpretMain();
104c2c66affSColin Finck     }
105c2c66affSColin Finck     /* If there are command arguments, then process them */
106c2c66affSColin Finck     else
107c2c66affSColin Finck     {
108c2c66affSColin Finck         for (index = 1; index < argc; index++)
109c2c66affSColin Finck         {
110c2c66affSColin Finck             /* checks for flags */
111c2c66affSColin Finck             if ((argv[index][0] == '/')||
112c2c66affSColin Finck                 (argv[index][0] == '-'))
113c2c66affSColin Finck             {
114c2c66affSColin Finck                 tmpBuffer = argv[index] + 1;
115c2c66affSColin Finck             }
116c2c66affSColin Finck             else
117c2c66affSColin Finck             {
118c2c66affSColin Finck                 /* If there is no flag, then return an error */
119c2c66affSColin Finck                 ConResPrintf(StdErr, IDS_ERROR_MSG_BAD_ARG, argv[index]);
120c2c66affSColin Finck                 result = EXIT_FAILURE;
121c2c66affSColin Finck                 goto done;
122c2c66affSColin Finck             }
123c2c66affSColin Finck 
124c2c66affSColin Finck             /* Checks for the /? flag first since the program
125c2c66affSColin Finck             exits as soon as the usage list is shown. */
126c2c66affSColin Finck             if (_wcsicmp(tmpBuffer, L"?") == 0)
127c2c66affSColin Finck             {
128c2c66affSColin Finck                 ConResPuts(StdOut, IDS_APP_USAGE);
129c2c66affSColin Finck                 result = EXIT_SUCCESS;
130c2c66affSColin Finck                 goto done;
131c2c66affSColin Finck             }
132c2c66affSColin Finck             /* Checks for the script flag */
133c2c66affSColin Finck             else if (_wcsicmp(tmpBuffer, L"s") == 0)
134c2c66affSColin Finck             {
135c2c66affSColin Finck                 if ((index + 1) < argc)
136c2c66affSColin Finck                 {
137c2c66affSColin Finck                     index++;
138c2c66affSColin Finck                     script = argv[index];
139c2c66affSColin Finck                 }
140c2c66affSColin Finck             }
141c2c66affSColin Finck             /* Checks for the timeout flag */
142c2c66affSColin Finck             else if (_wcsicmp(tmpBuffer, L"t") == 0)
143c2c66affSColin Finck             {
144c2c66affSColin Finck                 if ((index + 1) < argc)
145c2c66affSColin Finck                 {
146c2c66affSColin Finck                     index++;
147c2c66affSColin Finck                     timeout = _wtoi(argv[index]);
148c2c66affSColin Finck 
149c2c66affSColin Finck                     /* If the number is a negative number, then
150c2c66affSColin Finck                     change it so that the time is executed properly. */
151c2c66affSColin Finck                     if (timeout < 0)
152c2c66affSColin Finck                         timeout = 0;
153c2c66affSColin Finck                 }
154c2c66affSColin Finck             }
155c2c66affSColin Finck             else
156c2c66affSColin Finck             {
157c2c66affSColin Finck                 /* Assume that the flag doesn't exist. */
158c2c66affSColin Finck                 ConResPrintf(StdErr, IDS_ERROR_MSG_BAD_ARG, tmpBuffer);
159c2c66affSColin Finck                 result = EXIT_FAILURE;
160c2c66affSColin Finck                 goto done;
161c2c66affSColin Finck             }
162c2c66affSColin Finck         }
163c2c66affSColin Finck 
164c2c66affSColin Finck         /* Shows the program information */
165c2c66affSColin Finck         ShowHeader();
166c2c66affSColin Finck 
167c2c66affSColin Finck         /* Now we process the filename if it exists */
168c2c66affSColin Finck         if (script != NULL)
169c2c66affSColin Finck         {
170c2c66affSColin Finck             /* if the timeout is greater than 0, then assume
171c2c66affSColin Finck             that the user specified a specific time. */
172c2c66affSColin Finck             if (timeout > 0)
173c2c66affSColin Finck                 Sleep(timeout * 1000);
174c2c66affSColin Finck 
175c2c66affSColin Finck             if (RunScript(script) == FALSE)
176c2c66affSColin Finck             {
177c2c66affSColin Finck                 result = EXIT_FAILURE;
178c2c66affSColin Finck                 goto done;
179c2c66affSColin Finck             }
180c2c66affSColin Finck         }
181c2c66affSColin Finck         else
182c2c66affSColin Finck         {
183c2c66affSColin Finck             /* Exit failure since the user wanted to run a script */
184c2c66affSColin Finck             ConResPrintf(StdErr, IDS_ERROR_MSG_NO_SCRIPT, script);
185c2c66affSColin Finck             result = EXIT_FAILURE;
186c2c66affSColin Finck             goto done;
187c2c66affSColin Finck         }
188c2c66affSColin Finck     }
189c2c66affSColin Finck 
190c2c66affSColin Finck     /* Let the user know the program is exiting */
191c2c66affSColin Finck     ConResPuts(StdOut, IDS_APP_LEAVING);
192c2c66affSColin Finck 
193c2c66affSColin Finck done:
194*2ba6b097SEric Kohl     DestroyVolumeList();
195c2c66affSColin Finck     DestroyPartitionList();
196c2c66affSColin Finck 
197c2c66affSColin Finck     return result;
198c2c66affSColin Finck }
199