/* Program to put a column of a fixed-width database into a binary * column file. Rather slow, only processes 6 million records a * second. findcuit.c is the program that queries it. */ #include #include #include #include #include #include #include #include enum { recsize = 52, keysize = 11 }; int main(int argc, char **argv) { if (argc != 2) { fprintf(stderr, "Usage: %s utlfile/padr/SELE-SAL-CONSTA.p20out1.20230415.tmp\n", argv[0]); return 1; } int bufsize = strlen(argv[1]) + 10; char tmpfilenamebuf[bufsize], filenamebuf[bufsize]; strcpy(filenamebuf, argv[1]); strcat(filenamebuf, ".cuit"); strcpy(tmpfilenamebuf, filenamebuf); strcat(tmpfilenamebuf, ".tmp"); int fd = open(argv[1], O_RDONLY); if (fd < 0) { perror(argv[1]); return 1; } FILE *out = fopen(tmpfilenamebuf, "w"); if (!out) { perror(tmpfilenamebuf); return 1; } off_t size = lseek(fd, 0, SEEK_END); printf("%s is %ld bytes\n", argv[1], size); char *mem = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, (off_t)0); if (mem == MAP_FAILED) { perror("mmap"); return 1; } close(fd); for (int off = 0; off <= size - recsize; off += recsize) { char *end = mem + off + keysize; unsigned long cuit = strtol(mem + off, &end, 10); unsigned char cuitbuf[8]; for (int i = 0; i < 8; i++) { cuitbuf[i] = cuit & 255; cuit >>= 8; } fwrite(cuitbuf, 8, 1, out); } fflush(out); printf("Wrote %ld bytes\n", ftell(out)); fsync(fileno(out)); fclose(out); if (rename(tmpfilenamebuf, filenamebuf) < 0) { perror("rename"); return 1; } return 0; }