]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
updated WinNtSerialIoDxe driver not to create new child handle if RemainingDeviepath...
[mirror_edk2.git] / BaseTools / Source / C / Split / Split.c
1 /** @file
2
3 Split a file into two pieces at the request offset.
4
5 Copyright (c) 1999-2008 Intel Corporation. All rights reserved
6 This program and the accompanying materials are licensed and made available
7 under the terms and conditions of the BSD License which accompanies this
8 distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 // GC_TODO: fix comment to start with /*++
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #ifdef __GNUC__
21 #include <unistd.h>
22 #endif
23 #include <ctype.h>
24 #include "ParseInf.h"
25 #include "CommonLib.h"
26 #include "EfiUtilityMsgs.h"
27 //
28 // Utility Name
29 //
30 #define UTILITY_NAME "Split"
31
32 //
33 // Utility version information
34 //
35 #define UTILITY_MAJOR_VERSION 0
36 #define UTILITY_MINOR_VERSION 1
37
38 void
39 Version (
40 void
41 )
42 /*++
43
44 Routine Description:
45
46 Displays the standard utility information to SDTOUT
47
48 Arguments:
49
50 None
51
52 Returns:
53
54 None
55
56 --*/
57 {
58 printf ("%s v%d.%d -Utility to break a file into two pieces at the request offset.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
59 printf ("Copyright (c) 1999-2007 Intel Corporation. All rights reserved.\n");
60 }
61
62 void
63 Usage (
64 void
65 )
66 /*++
67
68 Routine Description:
69
70 GC_TODO: Add function description
71
72 Arguments:
73
74
75 Returns:
76
77 GC_TODO: add return values
78
79 --*/
80 {
81 Version();
82 printf ("\nUsage: \n\
83 Split\n\
84 -f, --filename inputFile to split\n\
85 -s, --split VALUE the number of bytes in the first file\n\
86 [-p, --prefix OutputDir]\n\
87 [-o, --firstfile Filename1]\n\
88 [-t, --secondfile Filename2]\n\
89 [-v, --verbose]\n\
90 [--version]\n\
91 [-q, --quiet disable all messages except fatal errors]\n\
92 [-d, --debug[#]\n\
93 [-h, --help]\n");
94 }
95
96 EFI_STATUS
97 GetSplitValue (
98 IN CONST CHAR8* SplitValueString,
99 OUT UINT64 *ReturnValue
100 )
101 {
102 UINT64 len = strlen(SplitValueString);
103 UINT64 base = 1;
104 UINT64 index = 0;
105 UINT64 number = 0;
106 CHAR8 lastCHAR = 0;
107 EFI_STATUS Status = EFI_SUCCESS;
108
109 if (len == 0) {
110 return EFI_ABORTED;
111 }
112
113 Status = AsciiStringToUint64 (SplitValueString, FALSE, ReturnValue);
114 if (!EFI_ERROR (Status)) {
115 return Status;
116 }
117
118 if (SplitValueString[0] == '0' && (SplitValueString[1] == 'x' || SplitValueString[1] == 'X')) {
119 Status = AsciiStringToUint64 (SplitValueString, TRUE, ReturnValue);
120 if (!EFI_ERROR (Status)) {
121 return Status;
122 }
123 }
124
125 lastCHAR = (CHAR8)toupper(SplitValueString[len - 1]);
126
127 if (lastCHAR != 'K' && lastCHAR != 'M' && lastCHAR != 'G') {
128 return STATUS_ERROR;
129 }
130
131 for (;index < len - 1; ++index) {
132 if (!isdigit(SplitValueString[index])) {
133 return EFI_ABORTED;
134 }
135 }
136
137 number = atol (SplitValueString);
138 if (lastCHAR == 'K')
139 base = 1024;
140 else if (lastCHAR == 'M')
141 base = 1024*1024;
142 else
143 base = 1024*1024*1024;
144
145 *ReturnValue = number*base;
146
147 return EFI_SUCCESS;
148 }
149
150 EFI_STATUS
151 CountVerboseLevel (
152 IN CONST CHAR8* VerboseLevelString,
153 IN CONST UINT64 Length,
154 OUT UINT64 *ReturnValue
155 )
156 {
157 UINT64 i = 0;
158 for (;i < Length; ++i) {
159 if (VerboseLevelString[i] != 'v' && VerboseLevelString[i] != 'V') {
160 return EFI_ABORTED;
161 }
162 ++(*ReturnValue);
163 }
164
165 return EFI_SUCCESS;
166 }
167
168 EFI_STATUS
169 CreateDir (
170 IN OUT CHAR8** FullFileName
171 )
172 {
173 CHAR8* temp = *FullFileName;
174 CHAR8* start = temp;
175 UINT64 index = 0;
176
177 for (;index < strlen(temp); ++index) {
178 if (temp[index] == '\\' || temp[index] == '/') {
179 temp[index] = 0;
180 if (chdir(start)) {
181 if (mkdir(start, S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
182 return EFI_ABORTED;
183 }
184 chdir(start);
185 }
186 start = temp + index + 1;
187 temp[index] = '/';
188 }
189 }
190
191 return EFI_SUCCESS;
192 }
193
194 int
195 main (
196 int argc,
197 char*argv[]
198 )
199 /*++
200
201 Routine Description:
202
203 GC_TODO: Add function description
204
205 Arguments:
206
207 argc - GC_TODO: add argument description
208 ] - GC_TODO: add argument description
209
210 Returns:
211
212 GC_TODO: add return values
213
214 --*/
215 {
216 EFI_STATUS Status = EFI_SUCCESS;
217 FILE *In;
218 CHAR8 *InputFileName = NULL;
219 CHAR8 *OutputDir = NULL;
220 CHAR8 *OutFileName1 = NULL;
221 CHAR8 *OutFileName2 = NULL;
222 UINT64 SplitValue = -1;
223 FILE *Out1;
224 FILE *Out2;
225 CHAR8 *OutName1 = NULL;
226 CHAR8 *OutName2 = NULL;
227 CHAR8 *CurrentDir = NULL;
228 UINT64 Index;
229 CHAR8 CharC;
230 BOOLEAN QuietFlag = TRUE;
231 UINT64 DebugLevel = 0;
232 UINT64 VerboseLevel = 0;
233
234 SetUtilityName(UTILITY_NAME);
235 if (argc == 1) {
236 Usage();
237 return STATUS_ERROR;
238 }
239
240 argc --;
241 argv ++;
242
243 if ((stricmp (argv[0], "-h") == 0) || (stricmp (argv[0], "--help") == 0)) {
244 Usage();
245 return STATUS_SUCCESS;
246 }
247
248 if (stricmp (argv[0], "--version") == 0) {
249 Version();
250 return STATUS_SUCCESS;
251 }
252
253 while (argc > 0) {
254 if ((stricmp (argv[0], "-p") == 0) || (stricmp (argv[0], "--prefix") == 0)) {
255 OutputDir = argv[1];
256 if (OutputDir == NULL) {
257 Warning (NULL, 0, 0, "NO output directory specified.", NULL);
258 return STATUS_ERROR;
259 }
260 argc -= 2;
261 argv += 2;
262 continue;
263 }
264
265 if ((stricmp (argv[0], "-f") == 0) || (stricmp (argv[0], "--filename") == 0)) {
266 InputFileName = argv[1];
267 if (InputFileName == NULL) {
268 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
269 return STATUS_ERROR;
270 }
271 argc -= 2;
272 argv += 2;
273 continue;
274 }
275
276 if ((stricmp (argv[0], "-s") == 0) || (stricmp (argv[0], "--split") == 0)) {
277 Status = GetSplitValue(argv[1], &SplitValue);
278 if (EFI_ERROR (Status)) {
279 Error (NULL, 0, 0x1003, "Input split value is not one valid integer.", NULL);
280 return STATUS_ERROR;
281 }
282 argc -= 2;
283 argv += 2;
284 continue;
285 }
286
287 if ((stricmp (argv[0], "-o") == 0) || (stricmp (argv[0], "--firstfile") == 0)) {
288 OutFileName1 = argv[1];
289 if (OutFileName1 == NULL) {
290 Warning (NULL, 0, 0, NULL, "No output file1 specified.");
291 }
292 argc -= 2;
293 argv += 2;
294 continue;
295 }
296
297 if ((stricmp (argv[0], "-t") == 0) || (stricmp (argv[0], "--secondfile") == 0)) {
298 OutFileName2 = argv[1];
299 if (OutFileName2 == NULL) {
300 Warning (NULL, 0, 0, NULL, "No output file2 specified.");
301 }
302 argc -= 2;
303 argv += 2;
304 continue;
305 }
306
307 if ((stricmp (argv[0], "-q") == 0) || (stricmp (argv[0], "--quiet") == 0)) {
308 QuietFlag = TRUE;
309 argc --;
310 argv ++;
311 continue;
312 }
313
314 if ((strlen(argv[0]) >= 2 && argv[0][0] == '-' && (argv[0][1] == 'v' || argv[0][1] == 'V')) || (stricmp (argv[0], "--verbose") == 0)) {
315 VerboseLevel = 1;
316 if (strlen(argv[0]) > 2) {
317 Status = CountVerboseLevel (&argv[0][2], strlen(argv[0]) - 2, &VerboseLevel);
318 if (EFI_ERROR (Status)) {
319 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
320 return STATUS_ERROR;
321 }
322 }
323
324 argc --;
325 argv ++;
326 continue;
327 }
328
329 if ((stricmp (argv[0], "-d") == 0) || (stricmp (argv[0], "--debug") == 0)) {
330 Status = AsciiStringToUint64 (argv[1], FALSE, &DebugLevel);
331 if (EFI_ERROR (Status)) {
332 Error (NULL, 0, 0x1003, "Input debug level is not one valid integrator.", NULL);
333 return STATUS_ERROR;
334 }
335 argc -= 2;
336 argv += 2;
337 continue;
338 }
339 //
340 // Don't recognize the paramter.
341 //
342 Error (NULL, 0, 0x1003, NULL, "%s is invaild paramter!", argv[0]);
343 return STATUS_ERROR;
344 }
345
346 if (InputFileName == NULL) {
347 Error (NULL, 0, 0x1001, "NO Input file specified.", NULL);
348 return STATUS_ERROR;
349 }
350
351 In = fopen (InputFileName, "rb");
352 if (In == NULL) {
353 // ("Unable to open file \"%s\"\n", InputFileName);
354 Error (InputFileName, 0, 1, "File open failure", NULL);
355 return STATUS_ERROR;
356 }
357
358 if (OutFileName1 == NULL) {
359 OutName1 = (CHAR8*)malloc(strlen(InputFileName) + 16);
360 if (OutName1 == NULL) {
361 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
362 return STATUS_ERROR;
363 }
364 strcpy (OutName1, InputFileName);
365 strcat (OutName1, "1");
366 OutFileName1 = OutName1;
367
368 }
369 if (OutFileName2 == NULL) {
370 OutName2 = (CHAR8*)malloc(strlen(InputFileName) + 16);
371 if (OutName2 == NULL) {
372 Warning (NULL, 0, 0, NULL, "Memory Allocation Fail.");
373 return STATUS_ERROR;
374 }
375 strcpy (OutName2, InputFileName);
376 strcat (OutName2, "2");
377 OutFileName2 = OutName2;
378
379 }
380
381 if (OutputDir != NULL) {
382 //OutputDirSpecified = TRUE;
383 if (chdir(OutputDir) != 0) {
384 Warning (NULL, 0, 0, NULL, "Change dir to OutputDir Fail.");
385 return STATUS_ERROR;
386 }
387 }
388
389 CurrentDir = (CHAR8*)getcwd((CHAR8*)0, 0);
390 if (EFI_ERROR(CreateDir(&OutFileName1))) {
391 Error (OutFileName1, 0, 5, "Create Dir for File1 Fail.", NULL);
392 return STATUS_ERROR;
393 }
394 chdir(CurrentDir);
395
396 if (EFI_ERROR(CreateDir(&OutFileName2))) {
397 Error (OutFileName2, 0, 5, "Create Dir for File2 Fail.", NULL);
398 return STATUS_ERROR;
399 }
400 chdir(CurrentDir);
401 free(CurrentDir);
402
403 Out1 = fopen (OutFileName1, "wb");
404 if (Out1 == NULL) {
405 // ("Unable to open file \"%s\"\n", OutFileName1);
406 Error (OutFileName1, 0, 1, "File open failure", NULL);
407 return STATUS_ERROR;
408 }
409
410 Out2 = fopen (OutFileName2, "wb");
411 if (Out2 == NULL) {
412 // ("Unable to open file \"%s\"\n", OutFileName2);
413 Error (OutFileName2, 0, 1, "File open failure", NULL);
414 return STATUS_ERROR;
415 }
416
417 for (Index = 0; Index < SplitValue; Index++) {
418 CharC = (CHAR8) fgetc (In);
419 if (feof (In)) {
420 break;
421 }
422
423 fputc (CharC, Out1);
424 }
425
426 for (;;) {
427 CharC = (CHAR8) fgetc (In);
428 if (feof (In)) {
429 break;
430 }
431
432 fputc (CharC, Out2);
433 }
434
435 if (OutName1 != NULL) {
436 free(OutName1);
437 }
438 if (OutName2 != NULL) {
439 free(OutName2);
440 }
441 fclose (In);
442 fclose (Out1);
443 fclose (Out2);
444
445 return STATUS_SUCCESS;
446 }