tomwalters@0: /* tomwalters@0: swab.c swap bytes tomwalters@0: ------ tomwalters@0: tomwalters@0: Read and write binary shorts (2-bytes). tomwalters@0: With no filename arguments, data is expected on the stdin, and the tomwalters@0: result is written on the stdout. Otherwise each given filename is tomwalters@0: processed in turn. When the `output' option is "off" output overwrites the tomwalters@0: respective input file. (The respective output for the stdin is the stdout). tomwalters@0: Otherwise all results are written to the one given output file tomwalters@0: (which is the stdout by default). tomwalters@0: tomwalters@0: */ tomwalters@0: tomwalters@0: #include tomwalters@0: #include tomwalters@0: #include "options.h" tomwalters@0: tomwalters@0: tomwalters@0: char applic[] = "Swap byte order of binary 2-byte data." ; tomwalters@0: tomwalters@0: static char *helpstr, *outstr, *sizestr ; tomwalters@0: tomwalters@0: static Options option[] = { tomwalters@0: { "help" , "off" , &helpstr , "help" , DEBUG }, tomwalters@0: { "output" , "stdout" , &outstr , "output filename (off = overwrite input)" , VAL }, tomwalters@0: { "SIZE" , "262144" , &sizestr , "buffer size in bytes" , SVAL }, tomwalters@0: ( char * ) 0 } ; tomwalters@0: tomwalters@0: tomwalters@0: int SIZE ; /* size of temp buffer for stdin data */ tomwalters@0: tomwalters@0: main(argc, argv) tomwalters@0: int argc ; tomwalters@0: char *argv[] ; tomwalters@0: { tomwalters@0: FILE *ifp, *ofp ; tomwalters@0: int i, j, n, doOpen, doCreate ; tomwalters@0: char *data, *Y ; tomwalters@0: tomwalters@0: i = getopts( option, argc, argv ) ; tomwalters@0: if ( !isoff( helpstr ) ) tomwalters@0: helpopts( helpstr, argv[0], applic, option ) ; tomwalters@0: tomwalters@0: SIZE = atoi( sizestr ) ; tomwalters@0: tomwalters@0: doOpen = doCreate = 0 ; tomwalters@0: tomwalters@0: if ( i == 0 ) ifp = stdin ; tomwalters@0: else doOpen++ ; tomwalters@0: tomwalters@0: if ( isoff( outstr ) ) { tomwalters@0: if ( i == 0 ) ofp = stdout ; /* o/p = i/p which is stdin */ tomwalters@0: else doCreate++ ; /* o/p = i/p for current file */ tomwalters@0: } tomwalters@0: else if ( isstr( outstr, "stdout") ) ofp = stdout ; tomwalters@0: else if ( ( ofp = fopen( outstr, "w" ) ) == (FILE *)0 ) { tomwalters@0: fprintf( stderr,"swab: can't create %s\n", outstr ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: Y = (char *)malloc( sizeof( short ) ) ; tomwalters@0: if ( ( data = (char *)malloc( SIZE ) ) == NULL ) { tomwalters@0: fprintf( stderr, "malloc out of space\n" ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: do { tomwalters@0: tomwalters@0: if ( doOpen && ( ifp = fopen( argv[argc-i], "r" ) ) == (FILE *)0 ) { tomwalters@0: fprintf( stderr,"swab: can't open %s\n", argv[argc-i] ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: for ( n = 0 ; n < SIZE && fread( &data[n], sizeof(short), 1, ifp ) ; n += 2 ) tomwalters@0: ; tomwalters@0: if ( n == SIZE ) fprintf( stderr, "warning: buffer out of space, increase SIZE\n" ) ; tomwalters@0: tomwalters@0: if ( doOpen ) fclose( ifp ) ; tomwalters@0: tomwalters@0: if ( doCreate && ( ofp = fopen( argv[argc-i], "w" ) ) == (FILE *)0 ) { tomwalters@0: fprintf( stderr,"swab: can't create %s\n", argv[argc-i] ) ; tomwalters@0: exit( 1 ) ; tomwalters@0: } tomwalters@0: tomwalters@0: for ( j = 0 ; j < n ; j += 2 ) { tomwalters@0: *Y = data[j+1] ; tomwalters@0: *(Y+1) = data[j] ; tomwalters@0: fwrite( Y, sizeof(short), 1, ofp ); tomwalters@0: } tomwalters@0: tomwalters@0: if ( doCreate ) fclose( ofp ) ; tomwalters@0: tomwalters@0: } while ( --i > 0 ) ; tomwalters@0: tomwalters@0: } tomwalters@0: