]>
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 - 2011, 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"
32 #define UTILITY_NAME "Split"
35 // Utility version information
37 #define UTILITY_MAJOR_VERSION 0
38 #define UTILITY_MINOR_VERSION 1
48 Displays the standard utility information to SDTOUT
60 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
);
61 printf ( "Copyright (c) 1999-2010 Intel Corporation. All rights reserved. \n " );
72 GC_TODO: Add function description
79 GC_TODO: add return values
86 -f, --filename inputFile to split \n \
87 -s, --split VALUE the number of bytes in the first file \n \
88 [-p, --prefix OutputDir] \n \
89 [-o, --firstfile Filename1] \n \
90 [-t, --secondfile Filename2] \n \
93 [-q, --quiet disable all messages except fatal errors] \n \
100 IN CONST CHAR8
* SplitValueString
,
101 OUT UINT64
* ReturnValue
104 UINT64 len
= strlen ( SplitValueString
);
109 EFI_STATUS Status
= EFI_SUCCESS
;
115 Status
= AsciiStringToUint64 ( SplitValueString
, FALSE
, ReturnValue
);
116 if (! EFI_ERROR ( Status
)) {
120 if ( SplitValueString
[ 0 ] == '0' && ( SplitValueString
[ 1 ] == 'x' || SplitValueString
[ 1 ] == 'X' )) {
121 Status
= AsciiStringToUint64 ( SplitValueString
, TRUE
, ReturnValue
);
122 if (! EFI_ERROR ( Status
)) {
127 lastCHAR
= ( CHAR8
) toupper (( int ) SplitValueString
[ len
- 1 ]);
129 if ( lastCHAR
!= 'K' && lastCHAR
!= 'M' && lastCHAR
!= 'G' ) {
133 for (; index
< len
- 1 ; ++ index
) {
134 if (! isdigit (( int ) SplitValueString
[ index
])) {
139 number
= atol ( SplitValueString
);
142 else if ( lastCHAR
== 'M' )
145 base
= 1024 * 1024 * 1024 ;
147 * ReturnValue
= number
* base
;
154 IN CONST CHAR8
* VerboseLevelString
,
155 IN CONST UINT64 Length
,
156 OUT UINT64
* ReturnValue
160 for (; i
< Length
; ++ i
) {
161 if ( VerboseLevelString
[ i
] != 'v' && VerboseLevelString
[ i
] != 'V' ) {
172 IN OUT CHAR8
** FullFileName
175 CHAR8
* temp
= * FullFileName
;
179 for (; index
< strlen ( temp
); ++ index
) {
180 if ( temp
[ index
] == ' \\ ' || temp
[ index
] == '/' ) {
183 if ( mkdir ( start
, S_IRWXU
| S_IRWXG
| S_IRWXO
) != 0 ) {
188 start
= temp
+ index
+ 1 ;
205 GC_TODO: Add function description
209 argc - GC_TODO: add argument description
210 ] - GC_TODO: add argument description
214 GC_TODO: add return values
218 EFI_STATUS Status
= EFI_SUCCESS
;
220 CHAR8
* InputFileName
= NULL
;
221 CHAR8
* OutputDir
= NULL
;
222 CHAR8
* OutFileName1
= NULL
;
223 CHAR8
* OutFileName2
= NULL
;
224 UINT64 SplitValue
= ( UINT64
) - 1 ;
227 CHAR8
* OutName1
= NULL
;
228 CHAR8
* OutName2
= NULL
;
229 CHAR8
* CurrentDir
= NULL
;
232 UINT64 DebugLevel
= 0 ;
233 UINT64 VerboseLevel
= 0 ;
235 SetUtilityName ( UTILITY_NAME
);
244 if (( stricmp ( argv
[ 0 ], "-h" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--help" ) == 0 )) {
246 return STATUS_SUCCESS
;
249 if ( stricmp ( argv
[ 0 ], "--version" ) == 0 ) {
251 return STATUS_SUCCESS
;
255 if (( stricmp ( argv
[ 0 ], "-p" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--prefix" ) == 0 )) {
257 if ( OutputDir
== NULL
) {
258 Warning ( NULL
, 0 , 0 , "NO output directory specified." , NULL
);
266 if (( stricmp ( argv
[ 0 ], "-f" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--filename" ) == 0 )) {
267 InputFileName
= argv
[ 1 ];
268 if ( InputFileName
== NULL
) {
269 Error ( NULL
, 0 , 0x1001 , "NO Input file specified." , NULL
);
277 if (( stricmp ( argv
[ 0 ], "-s" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--split" ) == 0 )) {
278 Status
= GetSplitValue ( argv
[ 1 ], & SplitValue
);
279 if ( EFI_ERROR ( Status
)) {
280 Error ( NULL
, 0 , 0x1003 , "Input split value is not one valid integer." , NULL
);
288 if (( stricmp ( argv
[ 0 ], "-o" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--firstfile" ) == 0 )) {
289 OutFileName1
= argv
[ 1 ];
290 if ( OutFileName1
== NULL
) {
291 Warning ( NULL
, 0 , 0 , NULL
, "No output file1 specified." );
298 if (( stricmp ( argv
[ 0 ], "-t" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--secondfile" ) == 0 )) {
299 OutFileName2
= argv
[ 1 ];
300 if ( OutFileName2
== NULL
) {
301 Warning ( NULL
, 0 , 0 , NULL
, "No output file2 specified." );
308 if (( stricmp ( argv
[ 0 ], "-q" ) == 0 ) || ( stricmp ( argv
[ 0 ], "--quiet" ) == 0 )) {
314 if (( strlen ( argv
[ 0 ]) >= 2 && argv
[ 0 ][ 0 ] == '-' && ( argv
[ 0 ][ 1 ] == 'v' || argv
[ 0 ][ 1 ] == 'V' )) || ( stricmp ( argv
[ 0 ], "--verbose" ) == 0 )) {
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 ]);
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
);
340 // Don't recognize the paramter.
342 Error ( NULL
, 0 , 0x1003 , NULL
, "%s is invaild paramter!" , argv
[ 0 ]);
346 if ( InputFileName
== NULL
) {
347 Error ( NULL
, 0 , 0x1001 , "NO Input file specified." , NULL
);
351 In
= fopen ( InputFileName
, "rb" );
353 // ("Unable to open file \"%s\"\n", InputFileName);
354 Error ( InputFileName
, 0 , 1 , "File open failure" , NULL
);
358 if ( OutFileName1
== NULL
) {
359 OutName1
= ( CHAR8
*) malloc ( strlen ( InputFileName
) + 16 );
360 if ( OutName1
== NULL
) {
361 Warning ( NULL
, 0 , 0 , NULL
, "Memory Allocation Fail." );
364 strcpy ( OutName1
, InputFileName
);
365 strcat ( OutName1
, "1" );
366 OutFileName1
= OutName1
;
369 if ( OutFileName2
== NULL
) {
370 OutName2
= ( CHAR8
*) malloc ( strlen ( InputFileName
) + 16 );
371 if ( OutName2
== NULL
) {
372 Warning ( NULL
, 0 , 0 , NULL
, "Memory Allocation Fail." );
375 strcpy ( OutName2
, InputFileName
);
376 strcat ( OutName2
, "2" );
377 OutFileName2
= OutName2
;
381 if ( OutputDir
!= NULL
) {
382 //OutputDirSpecified = TRUE;
383 if ( chdir ( OutputDir
) != 0 ) {
384 Warning ( NULL
, 0 , 0 , NULL
, "Change dir to OutputDir Fail." );
389 CurrentDir
= ( CHAR8
*) getcwd (( CHAR8
*) 0 , 0 );
390 if ( EFI_ERROR ( CreateDir (& OutFileName1
))) {
391 Error ( OutFileName1
, 0 , 5 , "Create Dir for File1 Fail." , NULL
);
396 if ( EFI_ERROR ( CreateDir (& OutFileName2
))) {
397 Error ( OutFileName2
, 0 , 5 , "Create Dir for File2 Fail." , NULL
);
403 Out1
= fopen ( OutFileName1
, "wb" );
405 // ("Unable to open file \"%s\"\n", OutFileName1);
406 Error ( OutFileName1
, 0 , 1 , "File open failure" , NULL
);
410 Out2
= fopen ( OutFileName2
, "wb" );
412 // ("Unable to open file \"%s\"\n", OutFileName2);
413 Error ( OutFileName2
, 0 , 1 , "File open failure" , NULL
);
417 for ( Index
= 0 ; Index
< SplitValue
; Index
++) {
418 CharC
= ( CHAR8
) fgetc ( In
);
427 CharC
= ( CHAR8
) fgetc ( In
);
435 if ( OutName1
!= NULL
) {
438 if ( OutName2
!= NULL
) {
445 return STATUS_SUCCESS
;