]>
git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/C/Split/Split.c
3 Split a file into two pieces at the request offset.
5 Copyright (c) 1999 - 2010, Intel Corporation. All rights reserved.<BR>
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
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.
16 // GC_TODO: fix comment to start with /*++
27 #include "CommonLib.h"
28 #include "EfiUtilityMsgs.h"
33 #define UTILITY_NAME "Split"
36 // Utility version information
38 #define UTILITY_MAJOR_VERSION 0
39 #define UTILITY_MINOR_VERSION 1
49 Displays the standard utility information to SDTOUT
61 printf ( "%s v%d.%d %s -Utility to break a file into two pieces at the request offset. \n " , UTILITY_NAME
, UTILITY_MAJOR_VERSION
, UTILITY_MINOR_VERSION
, __BUILD_VERSION
);
62 printf ( "Copyright (c) 1999-2010 Intel Corporation. All rights reserved. \n " );
73 GC_TODO: Add function description
80 GC_TODO: add return values
87 -f, --filename inputFile to split \n \
88 -s, --split VALUE the number of bytes in the first file \n \
89 [-p, --prefix OutputDir] \n \
90 [-o, --firstfile Filename1] \n \
91 [-t, --secondfile Filename2] \n \
94 [-q, --quiet disable all messages except fatal errors] \n \
101 IN CONST CHAR8
* SplitValueString
,
102 OUT UINT64
* ReturnValue
105 UINT64 len
= strlen ( SplitValueString
);
110 EFI_STATUS Status
= EFI_SUCCESS
;
116 Status
= AsciiStringToUint64 ( SplitValueString
, FALSE
, ReturnValue
);
117 if (! EFI_ERROR ( Status
)) {
121 if ( SplitValueString
[ 0 ] == '0' && ( SplitValueString
[ 1 ] == 'x' || SplitValueString
[ 1 ] == 'X' )) {
122 Status
= AsciiStringToUint64 ( SplitValueString
, TRUE
, ReturnValue
);
123 if (! EFI_ERROR ( Status
)) {
128 lastCHAR
= ( CHAR8
) toupper (( int ) SplitValueString
[ len
- 1 ]);
130 if ( lastCHAR
!= 'K' && lastCHAR
!= 'M' && lastCHAR
!= 'G' ) {
134 for (; index
< len
- 1 ; ++ index
) {
135 if (! isdigit (( int ) SplitValueString
[ index
])) {
140 number
= atol ( SplitValueString
);
143 else if ( lastCHAR
== 'M' )
146 base
= 1024 * 1024 * 1024 ;
148 * ReturnValue
= number
* base
;
155 IN CONST CHAR8
* VerboseLevelString
,
156 IN CONST UINT64 Length
,
157 OUT UINT64
* ReturnValue
161 for (; i
< Length
; ++ i
) {
162 if ( VerboseLevelString
[ i
] != 'v' && VerboseLevelString
[ i
] != 'V' ) {
173 IN OUT CHAR8
** FullFileName
176 CHAR8
* temp
= * FullFileName
;
180 for (; index
< strlen ( temp
); ++ index
) {
181 if ( temp
[ index
] == ' \\ ' || temp
[ index
] == '/' ) {
184 if ( mkdir ( start
, S_IRWXU
| S_IRWXG
| S_IRWXO
) != 0 ) {
189 start
= temp
+ index
+ 1 ;
206 GC_TODO: Add function description
210 argc - GC_TODO: add argument description
211 ] - GC_TODO: add argument description
215 GC_TODO: add return values
219 EFI_STATUS Status
= EFI_SUCCESS
;
221 CHAR8
* InputFileName
= NULL
;
222 CHAR8
* OutputDir
= NULL
;
223 CHAR8
* OutFileName1
= NULL
;
224 CHAR8
* OutFileName2
= NULL
;
225 UINT64 SplitValue
= ( UINT64
) - 1 ;
228 CHAR8
* OutName1
= NULL
;
229 CHAR8
* OutName2
= NULL
;
230 CHAR8
* CurrentDir
= NULL
;
233 BOOLEAN QuietFlag
= TRUE
;
234 UINT64 DebugLevel
= 0 ;
235 UINT64 VerboseLevel
= 0 ;
237 SetUtilityName ( UTILITY_NAME
);
246 if (( stricmp ( argv
[ 0 ], "-h" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--help" ) == 0 )) {
248 return STATUS_SUCCESS
;
251 if ( stricmp ( argv
[ 0 ], "--version" ) == 0 ) {
253 return STATUS_SUCCESS
;
257 if (( stricmp ( argv
[ 0 ], "-p" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--prefix" ) == 0 )) {
259 if ( OutputDir
== NULL
) {
260 Warning ( NULL
, 0 , 0 , "NO output directory specified." , NULL
);
268 if (( stricmp ( argv
[ 0 ], "-f" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--filename" ) == 0 )) {
269 InputFileName
= argv
[ 1 ];
270 if ( InputFileName
== NULL
) {
271 Error ( NULL
, 0 , 0x1001 , "NO Input file specified." , NULL
);
279 if (( stricmp ( argv
[ 0 ], "-s" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--split" ) == 0 )) {
280 Status
= GetSplitValue ( argv
[ 1 ], & SplitValue
);
281 if ( EFI_ERROR ( Status
)) {
282 Error ( NULL
, 0 , 0x1003 , "Input split value is not one valid integer." , NULL
);
290 if (( stricmp ( argv
[ 0 ], "-o" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--firstfile" ) == 0 )) {
291 OutFileName1
= argv
[ 1 ];
292 if ( OutFileName1
== NULL
) {
293 Warning ( NULL
, 0 , 0 , NULL
, "No output file1 specified." );
300 if (( stricmp ( argv
[ 0 ], "-t" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--secondfile" ) == 0 )) {
301 OutFileName2
= argv
[ 1 ];
302 if ( OutFileName2
== NULL
) {
303 Warning ( NULL
, 0 , 0 , NULL
, "No output file2 specified." );
310 if (( stricmp ( argv
[ 0 ], "-q" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--quiet" ) == 0 )) {
317 if (( strlen ( argv
[ 0 ]) >= 2 && argv
[ 0 ][ 0 ] == '-' && ( argv
[ 0 ][ 1 ] == 'v' || argv
[ 0 ][ 1 ] == 'V' )) || ( stricmp ( argv
[ 0 ], "--verbose" ) == 0 )) {
319 if ( strlen ( argv
[ 0 ]) > 2 ) {
320 Status
= CountVerboseLevel (& argv
[ 0 ][ 2 ], strlen ( argv
[ 0 ]) - 2 , & VerboseLevel
);
321 if ( EFI_ERROR ( Status
)) {
322 Error ( NULL
, 0 , 0x1003 , NULL
, "%s is invaild paramter!" , argv
[ 0 ]);
332 if (( stricmp ( argv
[ 0 ], "-d" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--debug" ) == 0 )) {
333 Status
= AsciiStringToUint64 ( argv
[ 1 ], FALSE
, & DebugLevel
);
334 if ( EFI_ERROR ( Status
)) {
335 Error ( NULL
, 0 , 0x1003 , "Input debug level is not one valid integrator." , NULL
);
343 // Don't recognize the paramter.
345 Error ( NULL
, 0 , 0x1003 , NULL
, "%s is invaild paramter!" , argv
[ 0 ]);
349 if ( InputFileName
== NULL
) {
350 Error ( NULL
, 0 , 0x1001 , "NO Input file specified." , NULL
);
354 In
= fopen ( InputFileName
, "rb" );
356 // ("Unable to open file \"%s\"\n", InputFileName);
357 Error ( InputFileName
, 0 , 1 , "File open failure" , NULL
);
361 if ( OutFileName1
== NULL
) {
362 OutName1
= ( CHAR8
*) malloc ( strlen ( InputFileName
) + 16 );
363 if ( OutName1
== NULL
) {
364 Warning ( NULL
, 0 , 0 , NULL
, "Memory Allocation Fail." );
367 strcpy ( OutName1
, InputFileName
);
368 strcat ( OutName1
, "1" );
369 OutFileName1
= OutName1
;
372 if ( OutFileName2
== NULL
) {
373 OutName2
= ( CHAR8
*) malloc ( strlen ( InputFileName
) + 16 );
374 if ( OutName2
== NULL
) {
375 Warning ( NULL
, 0 , 0 , NULL
, "Memory Allocation Fail." );
378 strcpy ( OutName2
, InputFileName
);
379 strcat ( OutName2
, "2" );
380 OutFileName2
= OutName2
;
384 if ( OutputDir
!= NULL
) {
385 //OutputDirSpecified = TRUE;
386 if ( chdir ( OutputDir
) != 0 ) {
387 Warning ( NULL
, 0 , 0 , NULL
, "Change dir to OutputDir Fail." );
392 CurrentDir
= ( CHAR8
*) getcwd (( CHAR8
*) 0 , 0 );
393 if ( EFI_ERROR ( CreateDir (& OutFileName1
))) {
394 Error ( OutFileName1
, 0 , 5 , "Create Dir for File1 Fail." , NULL
);
399 if ( EFI_ERROR ( CreateDir (& OutFileName2
))) {
400 Error ( OutFileName2
, 0 , 5 , "Create Dir for File2 Fail." , NULL
);
406 Out1
= fopen ( OutFileName1
, "wb" );
408 // ("Unable to open file \"%s\"\n", OutFileName1);
409 Error ( OutFileName1
, 0 , 1 , "File open failure" , NULL
);
413 Out2
= fopen ( OutFileName2
, "wb" );
415 // ("Unable to open file \"%s\"\n", OutFileName2);
416 Error ( OutFileName2
, 0 , 1 , "File open failure" , NULL
);
420 for ( Index
= 0 ; Index
< SplitValue
; Index
++) {
421 CharC
= ( CHAR8
) fgetc ( In
);
430 CharC
= ( CHAR8
) fgetc ( In
);
438 if ( OutName1
!= NULL
) {
441 if ( OutName2
!= NULL
) {
448 return STATUS_SUCCESS
;