Doxygen Source Code Documentation
Main Page Alphabetical List Data Structures File List Data Fields Globals Search
4swap.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <unistd.h>
00009 #include <stdlib.h>
00010
00011 typedef struct { unsigned char a,b,c,d ; } fourbytes ;
00012
00013 #define TEMP_FILE "Mozart.Lives"
00014 #define BUFSIZE 32000
00015
00016 static int buf[BUFSIZE] ;
00017
00018 int main( argc , argv )
00019 int argc ;
00020 char * argv[] ;
00021 {
00022 FILE * infil , * outfil ;
00023 int narg , nbyte , nint , quiet = 0 , ndone ;
00024
00025 if( argc < 2 || strncmp(argv[1],"-help",2) == 0 ){
00026 printf("Usage: 4swap [-q] file ...\n") ;
00027 printf("-- Swaps byte quadruples on the files listed.\n") ;
00028 printf(" The -q option means to work quietly.\n") ;
00029 exit(0) ;
00030 }
00031
00032 narg = 1 ;
00033 if( strncmp(argv[1],"-q",2) == 0 ){ quiet = 1 ; narg++ ; }
00034
00035 for( ; narg < argc ; narg++ ){
00036 infil = fopen( argv[narg] , "r" ) ;
00037 if( infil == NULL ){
00038 printf("** file %s not found!\n",argv[narg]) ; fflush(stdout) ;
00039 continue ;
00040 }
00041
00042 outfil = fopen( TEMP_FILE , "w" ) ;
00043 if( outfil == NULL ){
00044 printf("** Cannot open temporary file!\n") ;
00045 exit(1) ;
00046 }
00047
00048 if( !quiet){ printf("-- opened %s",argv[narg]) ; fflush(stdout) ; }
00049
00050 ndone = 0 ;
00051 do {
00052 nint = fread( buf , sizeof(int) , BUFSIZE , infil ) ;
00053 if( nint <= 0 ) break ;
00054 swap4( nint , buf ) ;
00055 fwrite( buf , sizeof(int) , nint , outfil ) ;
00056 ndone += nint ;
00057 if( ndone > 1000000 && ! quiet ){
00058 ndone -= 1000000 ; printf(".") ; fflush(stdout) ;
00059 }
00060 } while( nint == BUFSIZE ) ;
00061
00062 fsync(fileno(outfil)) ; fclose( infil ) ; fclose( outfil ) ;
00063
00064 unlink( argv[narg] ) ;
00065 rename( TEMP_FILE , argv[narg] ) ;
00066
00067 if( !quiet ){ printf(".\n") ; fflush(stdout) ; }
00068 }
00069 exit(0) ;
00070 }
00071
00072 int swap4( n , ar )
00073 int n ;
00074 int * ar ;
00075 {
00076 register int ii ;
00077 register fourbytes * tb = (fourbytes *) ar ;
00078 register unsigned char tt ;
00079
00080 for( ii=0 ; ii < n ; ii++ ){
00081 tt = tb[ii].a ;
00082 tb[ii].a = tb[ii].d ;
00083 tb[ii].d = tt ;
00084 tt = tb[ii].b ;
00085 tb[ii].b = tb[ii].c ;
00086 tb[ii].c = tt ;
00087 }
00088 return 0 ;
00089 }