home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <fcntl.h>
- #include <time.h>
-
- #ifndef O_BINARY
- #define O_BINARY 0
- #endif
-
- #ifndef __GNUC__
- unsigned long htonl(unsigned long x)
- {
- unsigned char *cp, *dp;
- unsigned long y;
- dp = &y;
- cp = &x;
- dp += 3;
- *dp-- = *cp++;
- *dp-- = *cp++;
- *dp-- = *cp++;
- *dp-- = *cp++;
- return y;
- }
-
- unsigned short htons(unsigned short x)
- {
- unsigned char *cp, *dp;
- unsigned short y;
- dp = &y;
- cp = &x;
- dp += 1;
- *dp-- = *cp++;
- *dp-- = *cp++;
- return y;
- }
- #else
- #include <unistd.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #endif
-
-
- #pragma pack(2)
-
- struct prchead {
- char name[32]; //0-32
- short int attr; //32-33
- short int vers; //34-35
- long int cr, md, bkt; //times 36-47
- long int mn, app, sort; // zero 48-59 - zero for prcs.
- long int type, crea; //60-67
- long int uidseed, nxrec; //68-75 - uidseed rand, nxrec zero;
- short int nrecs; //76-78
- } head;
-
- int main(int argc, char *argv[])
- {
-
- unsigned long cofst, xlong, xlong2;
- unsigned char *bbuf;
- long bbuflen;
- int outfd, infd, i, maxsect, argp;
- char title[128];
-
- if (argc < 2) {
- fprintf(stderr, "Usage: %s prcfile\n", argv[0]);
- exit(1);
- }
-
- for (argp = 1; argp < argc; argp++) {
-
- if ((infd = open(argv[argp], O_BINARY | O_RDONLY)) < 1) {
- fprintf(stderr, "Can't open file %s\n", argv[argp]);
- exit(-2);
- }
-
- if (!(bbuf = malloc(8400)))
- exit(-3);
-
- read(infd, &head, sizeof(head));
- read(infd, &xlong, 4);
-
- maxsect = htons(head.nrecs);
-
- cofst = htonl(xlong);
-
- strcpy(title, argv[1]);
-
- if (!strcmp(&title[strlen(title) - 4], ".pdb"))
- title[strlen(title) - 4] = 0;
- else
- strcat(title, ".out");
-
- fprintf(stderr, "%s -> %s\n", argv[argp], title);
-
- outfd = open(title, O_BINARY | O_RDWR | O_CREAT, 0644);
-
- for (i = 0; i < maxsect; i++) {
-
- read(infd, title, 4);
-
- if (i + 1 != maxsect) {
-
- read(infd, &xlong2, 4);
-
- bbuflen = htonl(xlong2);
- bbuflen = bbuflen - cofst;
- } else
- bbuflen = 4096 + 8;
-
- if (bbuflen > 8400 || bbuflen < 8)
- exit(-3);
-
- xlong = lseek(infd, 0, SEEK_CUR);
- lseek(infd, cofst, SEEK_SET);
-
- read(infd, bbuf, 4);
- if (strncmp(bbuf, "DBLK", 4))
- exit(-2);
- read(infd, &cofst, 4);
- bbuflen -= 8;
- bbuflen = read(infd, bbuf, bbuflen);
-
- if (i != maxsect - 1 && htonl(cofst) != bbuflen)
- fprintf(stderr, "Length mismatch %d v.s. %ld\n", htonl(cofst), bbuflen);
-
- lseek(infd, xlong, SEEK_SET);
- cofst = htonl(xlong2);
- write(outfd, bbuf, bbuflen);
-
- }
-
- close(outfd);
- }
-
- return 0;
- }
-