From 625392b921bc28e78fe5b6deda0a39fcc16d8379 Mon Sep 17 00:00:00 2001 From: Artem Chudinov Date: Fri, 30 Oct 2015 17:11:18 +0500 Subject: b2sum: Support reading from stdin --- b2sum/b2sum.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/b2sum/b2sum.c b/b2sum/b2sum.c index 2d7df35..073b60f 100644 --- a/b2sum/b2sum.c +++ b/b2sum/b2sum.c @@ -232,6 +232,7 @@ static void usage( char **argv ) { fprintf( stderr, "Usage: %s [-a HASH] [FILE]...\n", argv[0] ); fprintf( stderr, "\tHASH in blake2b blake2s blake2bp blake2sp\n" ); + fprintf( stderr, "\tWith no FILE, or when FILE is -, read standard input.\n" ); exit( 111 ); } @@ -281,10 +282,16 @@ int main( int argc, char **argv ) } } + if( optind == argc ) + argv[argc++] = (char *) "-"; + for( int i = optind; i < argc; ++i ) { FILE *f = NULL; - f = fopen( argv[i], "rb" ); + if( argv[i][0] == '-' && argv[i][1] == '\0' ) + f = stdin; + else + f = fopen( argv[i], "rb" ); if( !f ) { @@ -303,7 +310,7 @@ int main( int argc, char **argv ) printf( " %s\n", argv[i] ); end1: - fclose( f ); + if( f != stdin ) fclose( f ); end0: ; } -- cgit v1.2.3 From 918e39086b0a686a6a0417fd8e898a830567bc28 Mon Sep 17 00:00:00 2001 From: Artem Chudinov Date: Fri, 30 Oct 2015 19:12:14 +0500 Subject: b2sum: Support --help --- b2sum/b2sum.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/b2sum/b2sum.c b/b2sum/b2sum.c index 073b60f..9d64900 100644 --- a/b2sum/b2sum.c +++ b/b2sum/b2sum.c @@ -228,12 +228,13 @@ cleanup_buffer: typedef int ( *blake2fn )( FILE *, void * ); -static void usage( char **argv ) +static void usage( char **argv, int errcode ) { - fprintf( stderr, "Usage: %s [-a HASH] [FILE]...\n", argv[0] ); - fprintf( stderr, "\tHASH in blake2b blake2s blake2bp blake2sp\n" ); - fprintf( stderr, "\tWith no FILE, or when FILE is -, read standard input.\n" ); - exit( 111 ); + FILE *out = errcode ? stderr : stdout; + fprintf( out, "Usage: %s [-a HASH] [FILE]...\n", argv[0] ); + fprintf( out, "\tHASH in blake2b blake2s blake2bp blake2sp\n" ); + fprintf( out, "\tWith no FILE, or when FILE is -, read standard input.\n" ); + exit( errcode ); } @@ -245,12 +246,22 @@ int main( int argc, char **argv ) int c; opterr = 1; - if ( argc == 1 ) usage( argv ); /* show usage upon no-argument */ - - while( ( c = getopt( argc, argv, "a:" ) ) != -1 ) + while( 1 ) { + int this_option_optind = optind ? optind : 1; + int option_index = 0; + static struct option long_options[] = { + { "help", no_argument, 0, 0 }, + { NULL, 0, NULL, 0 } + }; + c = getopt_long( argc, argv, "a:", long_options, &option_index ); + if( c == -1 ) break; switch( c ) { + case 0: + if( 0 == strcmp( "help", long_options[option_index].name ) ) + usage( argv, 0 ); + break; case 'a': if( 0 == strcmp( optarg, "blake2b" ) ) { @@ -275,10 +286,14 @@ int main( int argc, char **argv ) else { printf( "Invalid function name: `%s'\n", optarg ); - usage( argv ); + usage( argv, 111 ); } break; + + case '?': + usage( argv, 1 ); + break; } } -- cgit v1.2.3 From 70acf250b99973c7b44dd476065ea53fd14e874a Mon Sep 17 00:00:00 2001 From: Artem Chudinov Date: Fri, 30 Oct 2015 21:42:01 +0500 Subject: b2sum: Support a BSD-style checksum and improve the --help message --- b2sum/b2sum.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/b2sum/b2sum.c b/b2sum/b2sum.c index 9d64900..55c6136 100644 --- a/b2sum/b2sum.c +++ b/b2sum/b2sum.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "blake2.h" @@ -231,9 +232,14 @@ typedef int ( *blake2fn )( FILE *, void * ); static void usage( char **argv, int errcode ) { FILE *out = errcode ? stderr : stdout; - fprintf( out, "Usage: %s [-a HASH] [FILE]...\n", argv[0] ); - fprintf( out, "\tHASH in blake2b blake2s blake2bp blake2sp\n" ); - fprintf( out, "\tWith no FILE, or when FILE is -, read standard input.\n" ); + fprintf( out, "Usage: %s [OPTION]... [FILE]...\n", argv[0] ); + fprintf( out, "\n" ); + fprintf( out, "With no FILE, or when FILE is -, read standard input.\n" ); + fprintf( out, "\n" ); + fprintf( out, " -a hash algorithm (blake2b is default): \n" + " [blake2b|blake2s|blake2bp|blake2sp]\n" ); + fprintf( out, " --tag create a BSD-style checksum\n" ); + fprintf( out, " --help display this help and exit\n" ); exit( errcode ); } @@ -243,6 +249,7 @@ int main( int argc, char **argv ) blake2fn blake2_stream = blake2b_stream; size_t outlen = BLAKE2B_OUTBYTES; unsigned char hash[BLAKE2B_OUTBYTES] = {0}; + bool bsdstyle = false; int c; opterr = 1; @@ -252,16 +259,13 @@ int main( int argc, char **argv ) int option_index = 0; static struct option long_options[] = { { "help", no_argument, 0, 0 }, + { "tag", no_argument, 0, 0 }, { NULL, 0, NULL, 0 } }; c = getopt_long( argc, argv, "a:", long_options, &option_index ); if( c == -1 ) break; switch( c ) { - case 0: - if( 0 == strcmp( "help", long_options[option_index].name ) ) - usage( argv, 0 ); - break; case 'a': if( 0 == strcmp( optarg, "blake2b" ) ) { @@ -291,6 +295,13 @@ int main( int argc, char **argv ) break; + case 0: + if( 0 == strcmp( "help", long_options[option_index].name ) ) + usage( argv, 0 ); + else if( 0 == strcmp( "tag", long_options[option_index].name ) ) + bsdstyle = true; + break; + case '?': usage( argv, 1 ); break; @@ -320,10 +331,22 @@ int main( int argc, char **argv ) goto end1; } + if( bsdstyle ) + { + if( blake2_stream == blake2b_stream ) printf( "BLAKE2b" ); + else if( blake2_stream == blake2bp_stream ) printf( "BLAKE2bp" ); + else if( blake2_stream == blake2s_stream ) printf( "BLAKE2s" ); + else if( blake2_stream == blake2sp_stream ) printf( "BLAKE2sp" ); + printf( " (%s) = ", argv[i] ); + } + for( int j = 0; j < outlen; ++j ) printf( "%02x", hash[j] ); - printf( " %s\n", argv[i] ); + if( bsdstyle ) + printf( "\n" ); + else + printf( " %s\n", argv[i] ); end1: if( f != stdin ) fclose( f ); end0: ; -- cgit v1.2.3 From 7b8a7433407e411a5a838f3eb1f0bb6cb07b305e Mon Sep 17 00:00:00 2001 From: Artem Chudinov Date: Fri, 30 Oct 2015 21:51:33 +0500 Subject: b2sum: Replace "-a " with "-a " in the --help message --- b2sum/b2sum.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/b2sum/b2sum.c b/b2sum/b2sum.c index 55c6136..e9cd526 100644 --- a/b2sum/b2sum.c +++ b/b2sum/b2sum.c @@ -236,7 +236,7 @@ static void usage( char **argv, int errcode ) fprintf( out, "\n" ); fprintf( out, "With no FILE, or when FILE is -, read standard input.\n" ); fprintf( out, "\n" ); - fprintf( out, " -a hash algorithm (blake2b is default): \n" + fprintf( out, " -a hash algorithm (blake2b is default): \n" " [blake2b|blake2s|blake2bp|blake2sp]\n" ); fprintf( out, " --tag create a BSD-style checksum\n" ); fprintf( out, " --help display this help and exit\n" ); -- cgit v1.2.3 From 01f12dbc5ccf2fdb552339218ac92402a5f2ab22 Mon Sep 17 00:00:00 2001 From: Artem Chudinov Date: Fri, 30 Oct 2015 22:03:12 +0500 Subject: b2sum: Get rid of two `goto` --- b2sum/b2sum.c | 41 +++++++++++++++++++++-------------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/b2sum/b2sum.c b/b2sum/b2sum.c index e9cd526..ebf1757 100644 --- a/b2sum/b2sum.c +++ b/b2sum/b2sum.c @@ -258,10 +258,11 @@ int main( int argc, char **argv ) int this_option_optind = optind ? optind : 1; int option_index = 0; static struct option long_options[] = { - { "help", no_argument, 0, 0 }, - { "tag", no_argument, 0, 0 }, + { "help", no_argument, 0, 0 }, + { "tag", no_argument, 0, 0 }, { NULL, 0, NULL, 0 } }; + c = getopt_long( argc, argv, "a:", long_options, &option_index ); if( c == -1 ) break; switch( c ) @@ -322,34 +323,34 @@ int main( int argc, char **argv ) if( !f ) { fprintf( stderr, "Could not open `%s': %s\n", argv[i], strerror( errno ) ); - goto end0; + continue; } if( blake2_stream( f, hash ) < 0 ) { fprintf( stderr, "Failed to hash `%s'\n", argv[i] ); - goto end1; } - - if( bsdstyle ) + else { - if( blake2_stream == blake2b_stream ) printf( "BLAKE2b" ); - else if( blake2_stream == blake2bp_stream ) printf( "BLAKE2bp" ); - else if( blake2_stream == blake2s_stream ) printf( "BLAKE2s" ); - else if( blake2_stream == blake2sp_stream ) printf( "BLAKE2sp" ); - printf( " (%s) = ", argv[i] ); - } + if( bsdstyle ) + { + if( blake2_stream == blake2b_stream ) printf( "BLAKE2b" ); + else if( blake2_stream == blake2bp_stream ) printf( "BLAKE2bp" ); + else if( blake2_stream == blake2s_stream ) printf( "BLAKE2s" ); + else if( blake2_stream == blake2sp_stream ) printf( "BLAKE2sp" ); + printf( " (%s) = ", argv[i] ); + } - for( int j = 0; j < outlen; ++j ) - printf( "%02x", hash[j] ); + for( int j = 0; j < outlen; ++j ) + printf( "%02x", hash[j] ); + + if( bsdstyle ) + printf( "\n" ); + else + printf( " %s\n", argv[i] ); + } - if( bsdstyle ) - printf( "\n" ); - else - printf( " %s\n", argv[i] ); -end1: if( f != stdin ) fclose( f ); -end0: ; } return 0; -- cgit v1.2.3 From d97dff9d0250a6281edb548cd4ca66af36f159af Mon Sep 17 00:00:00 2001 From: Artem Chudinov Date: Fri, 30 Oct 2015 22:46:20 +0500 Subject: b2sum: Output 2 spaces instead of 1 To be similar to coreutils' sha256sum. --- b2sum/b2sum.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/b2sum/b2sum.c b/b2sum/b2sum.c index ebf1757..da12dd8 100644 --- a/b2sum/b2sum.c +++ b/b2sum/b2sum.c @@ -347,7 +347,7 @@ int main( int argc, char **argv ) if( bsdstyle ) printf( "\n" ); else - printf( " %s\n", argv[i] ); + printf( " %s\n", argv[i] ); } if( f != stdin ) fclose( f ); -- cgit v1.2.3