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