<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <archive.h>
#include <archive_entry.h>

int main(int argc, char *argv[]) {
    struct archive *inarc;
    struct archive *outarc;
    struct archive_entry *entry;
    FILE *infile;
    FILE *outfile;
    char *inname;
    char *outname;
    char *fullpath;
    char *path;
    char *filename;
    void *buff;
    FILE *file;
    int size;

    if (argc == 1) {
        printf("usage: rebuild-epub file\n");
        return 1;
    }

    inname = argv[1];

    outname = malloc(strlen(inname) + 4);
    strcpy(outname, inname);
    strcat(outname, ".new");

    infile = fopen(inname, "r");
    inarc = archive_read_new();
    archive_read_support_format_zip(inarc);
    archive_read_open_FILE(inarc, infile);

    outfile = fopen(outname, "w");
    outarc = archive_write_new();
    archive_write_set_format_zip(outarc);
    archive_write_open_FILE(outarc, outfile);

    while (archive_read_next_header(inarc, &entry) != ARCHIVE_EOF) {
        archive_write_header(outarc, entry);

        fullpath = strdup(archive_entry_pathname(entry));
        filename = basename(fullpath);
        path = dirname(fullpath);

        if (strcmp(path, "OEBPS/Text") == 0
            && (file = fopen(filename, "rb")) != NULL) {
            fseek(file, 0L, SEEK_END);
            size = ftell(file);
            fseek(file, 0L, SEEK_SET);
            buff = malloc(size);
            fread(buff, size, 1, file);
            fclose(file);
            printf("Replaced %s\n", filename);
        }
        else {
            size = archive_entry_size(entry);
            buff = malloc(size);
            archive_read_data(inarc, buff, size);
        }
        free(fullpath); 
        archive_write_data(outarc, buff, size);
        free(buff);
    }
<commit_msg>Use stat instead of fseek to find file size<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include <archive.h>
#include <archive_entry.h>
#include <sys/stat.h>

int main(int argc, char *argv[]) {
    struct archive *inarc;
    struct archive *outarc;
    struct archive_entry *entry;
    FILE *infile;
    FILE *outfile;
    char *inname;
    char *outname;
    char *fullpath;
    char *path;
    char *filename;
    void *buff;
    FILE *file;
    int size;
    struct stat st;

    if (argc == 1) {
        printf("usage: rebuild-epub file\n");
        return 1;
    }

    inname = argv[1];

    outname = malloc(strlen(inname) + 4);
    strcpy(outname, inname);
    strcat(outname, ".new");

    infile = fopen(inname, "r");
    inarc = archive_read_new();
    archive_read_support_format_zip(inarc);
    archive_read_open_FILE(inarc, infile);

    outfile = fopen(outname, "w");
    outarc = archive_write_new();
    archive_write_set_format_zip(outarc);
    archive_write_open_FILE(outarc, outfile);

    while (archive_read_next_header(inarc, &entry) != ARCHIVE_EOF) {
        archive_write_header(outarc, entry);

        fullpath = strdup(archive_entry_pathname(entry));
        filename = basename(fullpath);
        path = dirname(fullpath);

        if (strcmp(path, "OEBPS/Text") == 0
            && (file = fopen(filename, "rb")) != NULL) {
            stat(filename, &st);
	    size = st.st_size;

            buff = malloc(size);
            fread(buff, size, 1, file);
            fclose(file);
            printf("Replaced %s\n", filename);
        }
        else {
            size = archive_entry_size(entry);
            buff = malloc(size);
            archive_read_data(inarc, buff, size);
        }
        free(fullpath); 
        archive_write_data(outarc, buff, size);
        free(buff);
    }

{'fraction_non_alphanumeric': 0.13179128563743947, 'fraction_numerical': 0.0037654653039268424, 'mean_word_length': 2.4985888993414864, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1341208', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1245, 'n_words': 269}