30 int i, j, isbig, itmp, argcount=0;
31 int status_only=0, make_big_only=0, make_little_only=0;
36 fprintf(stderr,
"This program flips byte-ordering of DCD files.\n");
38 fprintf(stderr,
"Usage: %s [-s] [-B] [-L] file . . . \n",argv[0]);
39 fprintf(stderr,
" The default behavior is to flip the byte ordering. Other options are:\n");
40 fprintf(stderr,
" -s report the byte-order status of each <file> without changing it\n");
41 fprintf(stderr,
" -B make/keep each <file> big-endian\n");
42 fprintf(stderr,
" -L make/keep each <file> little-endian\n");
43 fprintf(stderr,
" The options are mutually exclusive; the last one read is used.\n\n");
47 while (++argcount < argc){
49 if ((strncmp(argv[argcount],
"-S",2) == 0) || (strncmp(argv[argcount],
"-s",2) == 0)){
50 status_only=1; make_big_only=0; make_little_only=0;
52 else if ((strncmp(argv[argcount],
"-B",2) == 0) || (strncmp(argv[argcount],
"-b",2) == 0)){
53 make_big_only=1; status_only=0; make_little_only=0;
55 else if ((strncmp(argv[argcount],
"-L",2) == 0) || (strncmp(argv[argcount],
"-l",2) == 0)){
56 make_little_only=1; make_big_only=0; status_only=0;
58 else if (strncmp(argv[argcount],
"-",1) == 0){
59 printf(
"\n Error: %s not a valid option. \n\n",argv[argcount]);
63 if ( ( fd = open(argv[argcount], O_RDWR) ) < 0 ) {
64 fprintf(stderr,
"Can't open %s for updating. (File must be read/writeable.)\n",argv[argcount]);
68 if ( fstat(fd,&statbuf) < 0 ) {
69 fprintf(stderr,
"Can't stat %s.\n",argv[1]);
76 fprintf(stderr,
"%s is not in DCD format.\n",argv[argcount]);
80 if ( (
sizeof(
char*) < 8 ) && ( n >> 32 ) ) {
81 fprintf(stderr,
"%s is too large, 64-bit build required\n",argv[argcount]);
86 fprintf(stderr,
"%s is not in DCD format.\n",argv[argcount]);
89 if ( ( d = mmap(0,n,PROT_READ|PROT_WRITE,
MAP_FILE|MAP_SHARED,fd,0) )
91 fprintf(stderr,
"Can't mmap %s.\n",argv[argcount]);
98 fprintf(stderr,
"%s is little-endian.\n",argv[argcount]);
100 else if ( d[3] == 84 ) {
102 fprintf(stderr,
"%s is big-endian.\n",argv[argcount]);
105 fprintf(stderr,
"%s is not in DCD format.\n",argv[argcount]);
112 if ( make_little_only ){
113 fprintf(stderr,
"%s is already little-endian. (No change made.)\n",argv[argcount]);
118 fprintf(stderr,
"%s was little-endian, will be big-endian.\n",argv[argcount]);
121 else if ( d[3] == 84 ) {
122 if ( make_big_only ){
123 fprintf(stderr,
"%s is already big-endian. (No change made.)\n",argv[argcount]);
128 fprintf(stderr,
"%s was big-endian, will be little-endian.\n",argv[argcount]);
132 fprintf(stderr,
"%s is not in DCD format.\n",argv[argcount]);
137 #define FLIPFOUR {for(j=0;j<4;++j)b[j]=d[j];for(j=3;j>=0;--j,++d)*d=b[j];n-=4;}
138 #define FLIPEIGHT {for(j=0;j<8;++j)b[j]=d[j];for(j=7;j>=0;--j,++d)*d=b[j];n-=8;}
139 #define SKIPFOUR {d+=4;n-=4;}
140 #define SKIP(X) {d+=(X);n-=(X);}
141 #define READINT(X) { X=0; if (isbig) { for(j=0;j<4;++j,X<<8) X+=d[j]; } \
142 else { for(j=3;j>=0;--j,X<<8) X+=d[j]; } }
147 for ( i = 0; i < 20; ++i )
FLIPFOUR;
151 if ( n <= (80*(off_t)itmp + 4) ) {
152 fprintf(stderr,
"%s is too short for DCD format.\n",argv[argcount]);