flipdcd.c File Reference

#include "largefiles.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Defines

#define FLIPFOUR   {for(j=0;j<4;++j)b[j]=d[j];for(j=3;j>=0;--j,++d)*d=b[j];n-=4;}
#define FLIPEIGHT   {for(j=0;j<8;++j)b[j]=d[j];for(j=7;j>=0;--j,++d)*d=b[j];n-=8;}
#define SKIPFOUR   {d+=4;n-=4;}
#define SKIP(X)   {d+=(X);n-=(X);}
#define READINT(X)

Functions

int main (int argc, char *argv[])

Define Documentation

#define FLIPEIGHT   {for(j=0;j<8;++j)b[j]=d[j];for(j=7;j>=0;--j,++d)*d=b[j];n-=8;}
#define FLIPFOUR   {for(j=0;j<4;++j)b[j]=d[j];for(j=3;j>=0;--j,++d)*d=b[j];n-=4;}

Referenced by main().

#define READINT (  ) 
Value:
{ X=0; if (isbig) { for(j=0;j<4;++j,X<<8) X+=d[j]; } \
           else { for(j=3;j>=0;--j,X<<8) X+=d[j]; } }
#define SKIP (  )     {d+=(X);n-=(X);}
#define SKIPFOUR   {d+=4;n-=4;}

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 24 of file flipdcd.c.

References FLIPFOUR, j, MAP_FILE, READINT, SKIP, and SKIPFOUR.

00024                                  {
00025 
00026 int fd;
00027 struct stat statbuf;
00028 off_t n;
00029 int i, j, isbig, itmp, argcount=0;
00030 int status_only=0, make_big_only=0, make_little_only=0;
00031 char b[8];
00032 char *d;
00033 
00034 if ( argc < 2 ) {
00035   fprintf(stderr,"This program flips byte-ordering of DCD files.\n");
00036   usage:
00037   fprintf(stderr,"Usage: %s [-s] [-B] [-L] file . . . \n",argv[0]);
00038   fprintf(stderr,"      The default behavior is to flip the byte ordering. Other options are:\n");
00039   fprintf(stderr,"      -s report the byte-order status of each <file> without changing it\n");
00040   fprintf(stderr,"      -B make/keep each <file> big-endian\n");
00041   fprintf(stderr,"      -L make/keep each <file> little-endian\n");
00042   fprintf(stderr,"      The options are mutually exclusive; the last one read is used.\n\n");
00043   exit(-1);
00044 }
00045 
00046 while (++argcount < argc){
00047    /* debug: printf("Current argument %d (out of %d): %s\n",argcount,argc-1,argv[argcount]); */ 
00048    if ((strncmp(argv[argcount],"-S",2) == 0) || (strncmp(argv[argcount],"-s",2) == 0)){
00049       status_only=1; make_big_only=0; make_little_only=0;
00050    }
00051    else if ((strncmp(argv[argcount],"-B",2) == 0) || (strncmp(argv[argcount],"-b",2) == 0)){
00052       make_big_only=1; status_only=0; make_little_only=0;
00053    }
00054    else if ((strncmp(argv[argcount],"-L",2) == 0) || (strncmp(argv[argcount],"-l",2) == 0)){
00055       make_little_only=1; make_big_only=0; status_only=0; 
00056    }
00057    else if (strncmp(argv[argcount],"-",1) == 0){
00058       printf("\n Error: %s not a valid option. \n\n",argv[argcount]);
00059       goto usage;
00060    }
00061    else{
00062       if ( ( fd = open(argv[argcount], O_RDWR) ) < 0 ) {
00063         fprintf(stderr,"Can't open %s for updating. (File must be read/writeable.)\n",argv[argcount]);
00064         goto end;
00065       }
00066 
00067       if ( fstat(fd,&statbuf) < 0 ) {
00068         fprintf(stderr,"Can't stat %s.\n",argv[1]);
00069         goto end;
00070       }
00071       
00072       n = statbuf.st_size;
00073       
00074       if ( n <= 104 ) {
00075         fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00076         goto end;
00077       }
00078 
00079       if ( ( sizeof(char*) < 8 ) && ( n >> 32 ) ) {
00080         fprintf(stderr,"%s is too large, 64-bit build required\n",argv[argcount]);
00081         goto end;
00082       }
00083       
00084       if ( n % 4 ) {
00085         fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00086         goto end;
00087       }
00088       if ( ( d = mmap(0,n,PROT_READ|PROT_WRITE,MAP_FILE|MAP_SHARED,fd,0) )
00089                                                               == (caddr_t) -1 ) {
00090         fprintf(stderr,"Can't mmap %s.\n",argv[argcount]);
00091         goto end;
00092       }
00093       
00094       if ( status_only ){
00095         if ( d[0] == 84 ) {
00096           isbig = 0;
00097           fprintf(stderr,"%s is little-endian.\n",argv[argcount]);
00098         }
00099         else if ( d[3] == 84 ) {
00100           isbig = 1;
00101           fprintf(stderr,"%s is big-endian.\n",argv[argcount]);
00102         }
00103         else {
00104           fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00105         }
00106         goto end;  /* Done if only status is requested */
00107         
00108       }
00109       else {
00110         if ( d[0] == 84 ) {
00111           if ( make_little_only ){
00112             fprintf(stderr,"%s is already little-endian. (No change made.)\n",argv[argcount]);
00113             goto end;
00114           }
00115           else {   
00116             isbig = 0;
00117             fprintf(stderr,"%s was little-endian, will be big-endian.\n",argv[argcount]);
00118           }
00119         }
00120         else if ( d[3] == 84 ) {
00121           if ( make_big_only ){
00122             fprintf(stderr,"%s is already big-endian. (No change made.)\n",argv[argcount]);
00123             goto end;
00124           }
00125           else {   
00126             isbig = 1;
00127             fprintf(stderr,"%s was big-endian, will be little-endian.\n",argv[argcount]);
00128           }
00129         }
00130         else {
00131           fprintf(stderr,"%s is not in DCD format.\n",argv[argcount]);
00132           goto end;
00133         }
00134       }
00135       
00136 #define FLIPFOUR {for(j=0;j<4;++j)b[j]=d[j];for(j=3;j>=0;--j,++d)*d=b[j];n-=4;}
00137 #define FLIPEIGHT {for(j=0;j<8;++j)b[j]=d[j];for(j=7;j>=0;--j,++d)*d=b[j];n-=8;}
00138 #define SKIPFOUR {d+=4;n-=4;}
00139 #define SKIP(X) {d+=(X);n-=(X);}
00140 #define READINT(X) { X=0; if (isbig) { for(j=0;j<4;++j,X<<8) X+=d[j]; } \
00141            else { for(j=3;j>=0;--j,X<<8) X+=d[j]; } }
00142 
00143 
00144       FLIPFOUR;  /* 84 */
00145       SKIPFOUR;  /* "CORD" */
00146       for ( i = 0; i < 20; ++i ) FLIPFOUR;
00147       FLIPFOUR;  /* 84 */
00148       FLIPFOUR;  /* TITLE SIZE */
00149       READINT(itmp); FLIPFOUR;  /* NTITLE */
00150       if ( n <= (80*(off_t)itmp + 4) ) {
00151         fprintf(stderr,"%s is too short for DCD format.\n",argv[argcount]);
00152         goto end;
00153       }
00154       SKIP(80*itmp);
00155       FLIPFOUR;  /* TITLE SIZE */
00156       
00157       while ( n ) FLIPFOUR;  /* WHATEVER UNTIL END */
00158         ;
00159       
00160       }
00161    end:
00162       ;
00163    }
00164 }


Generated on 21 Sep 2019 for NAMD by  doxygen 1.6.1