//****************************\r
// Includes\r
//****************************\r
-#include <stdio.h> // printf, fopen, fread\r
+#include <stdio.h> // fprintf, fopen, fread\r
#include <stdlib.h> // malloc\r
+#include <string.h> // strcmp\r
#include "lz4.h"\r
\r
\r
//****************************\r
int usage()\r
{\r
- printf("Usage :\n");\r
- printf(" %s [arg] input output\n",BINARY_NAME);\r
- printf("Arguments :\n");\r
- printf(" -c : force compression (default)\n");\r
- printf(" -d : force decompression \n");\r
- printf(" -h : help (this text)\n"); \r
+ fprintf(stderr, "Usage :\n");\r
+ fprintf(stderr, " %s [arg] input output\n",BINARY_NAME);\r
+ fprintf(stderr, "Arguments :\n");\r
+ fprintf(stderr, " -c : force compression (default)\n");\r
+ fprintf(stderr, " -d : force decompression \n");\r
+ fprintf(stderr, " -h : help (this text)\n"); \r
+ fprintf(stderr, "input : can be 'stdin' (pipe) or a filename\n");\r
+ fprintf(stderr, "output : can be 'stdout' (pipe) or a filename\n");\r
return 0;\r
}\r
\r
\r
int badusage()\r
{\r
- printf("Wrong parameters\n");\r
+ fprintf(stderr, "Wrong parameters\n");\r
usage();\r
return 0;\r
}\r
U64 compressedfilesize = ARCHIVE_MAGICNUMBER_SIZE;\r
char* in_buff;\r
char* out_buff;\r
- FILE* finput = fopen( input_filename, "rb" ); \r
- FILE* foutput = fopen( output_filename, "wb" ); \r
+ FILE* finput;\r
+ FILE* foutput;\r
+ char stdinmark[] = "stdin";\r
+ char stdoutmark[] = "stdout";\r
+\r
+ if (!strcmp (input_filename, stdinmark)) {\r
+ fprintf(stderr, "Using stdin for input\n");\r
+ finput = stdin;\r
+ } else {\r
+ finput = fopen( input_filename, "rb" );\r
+ }\r
+\r
+ if (!strcmp (output_filename, stdoutmark)) {\r
+ fprintf(stderr, "Using stdout for output\n");\r
+ foutput = stdout;\r
+ } else {\r
+ foutput = fopen( output_filename, "wb" );\r
+ }\r
\r
- if ( finput==0 ) { printf("Pb opening %s\n", input_filename); return 2; }\r
- if ( foutput==0) { printf("Pb opening %s\n", output_filename); return 3; }\r
+ if ( finput==0 ) { fprintf(stderr, "Pb opening %s\n", input_filename); return 2; }\r
+ if ( foutput==0) { fprintf(stderr, "Pb opening %s\n", output_filename); return 3; }\r
\r
// Allocate Memory\r
in_buff = malloc(CHUNKSIZE);\r
}\r
\r
// Status\r
- printf("Compressed %llu bytes into %llu bytes ==> %.2f%%\n", \r
+ fprintf(stderr, "Compressed %llu bytes into %llu bytes ==> %.2f%%\n", \r
(unsigned long long) filesize, (unsigned long long) compressedfilesize, (double)compressedfilesize/filesize*100);\r
\r
fclose(finput);\r
U64 filesize = 0;\r
char* in_buff;\r
char* out_buff;\r
- FILE* finput = fopen( input_filename, "rb" ); \r
- FILE* foutput = fopen( output_filename, "wb" ); \r
size_t uselessRet;\r
int sinkint;\r
U32 nextSize;\r
- \r
- if (finput==0 ) { printf("Pb opening %s\n", input_filename); return 4; }\r
- if (foutput==0) { printf("Pb opening %s\n", output_filename); return 5; }\r
+ FILE* finput;\r
+ FILE* foutput;\r
+ char stdinmark[] = "stdin";\r
+ char stdoutmark[] = "stdout";\r
+\r
+ if (!strcmp (input_filename, stdinmark)) {\r
+ fprintf(stderr, "Using stdin for input\n");\r
+ finput = stdin;\r
+ } else {\r
+ finput = fopen( input_filename, "rb" );\r
+ }\r
+\r
+ if (!strcmp (output_filename, stdoutmark)) {\r
+ fprintf(stderr, "Using stdout for output\n");\r
+ foutput = stdout;\r
+ } else {\r
+ foutput = fopen( output_filename, "wb" );\r
+ }\r
\r
// Allocate Memory\r
in_buff = malloc(OUT_CHUNKSIZE);\r
\r
// Check Archive Header\r
uselessRet = fread(out_buff, 1, ARCHIVE_MAGICNUMBER_SIZE, finput);\r
- if (*(U32*)out_buff != ARCHIVE_MAGICNUMBER) { printf("Wrong file : cannot be decoded\n"); return 6; }\r
+ if (*(U32*)out_buff != ARCHIVE_MAGICNUMBER) { fprintf(stderr,"Wrong file : cannot be decoded\n"); return 6; }\r
uselessRet = fread(in_buff, 1, 4, finput);\r
nextSize = *(U32*)in_buff;\r
\r
fwrite(out_buff, 1, sinkint, foutput);\r
\r
// Status\r
- printf("Successfully decoded %llu bytes \n", (unsigned long long)filesize);\r
+ fprintf(stderr, "Successfully decoded %llu bytes \n", (unsigned long long)filesize);\r
\r
fclose(finput);\r
fclose(foutput);\r
*output_filename=0;\r
\r
// Welcome message\r
- printf(WELCOME_MESSAGE);\r
+ fprintf(stderr, WELCOME_MESSAGE);\r
\r
if (argc<2) { badusage(); return 1; }\r
\r