<commit_before>use @import("std");

// TODO var args printing

pub fn main(args: [][]u8) -> %void {
    const exe = args[0];
    var catted_anything = false;
    for (args[1...]) |arg| {
        if (str_eql(arg, "-")) {
            catted_anything = true;
            cat_stream(io.stdin) %% |err| return err;
        } else if (arg[0] == '-') {
            return usage(exe);
        } else {
            var is = io.InStream.open(arg) %% |err| {
                %%io.stderr.write("Unable to open file: ");
                %%io.stderr.write(@err_name(err));
                %%io.stderr.write("\n");
                return err;
            };
            defer %%is.close();

            catted_anything = true;
            cat_stream(is) %% |err| return err;
        }
    }
    if (!catted_anything) {
        cat_stream(io.stdin) %% |err| return err;
    }
}

fn usage(exe: []u8) -> %void {
    %%io.stderr.write("Usage: ");
    %%io.stderr.write(exe);
    %%io.stderr.write(" [FILE]...\n");
    return error.Invalid;
}

fn cat_stream(is: io.InStream) -> %void {
    var buf: [1024 * 4]u8 = undefined;

    while (true) {
        const bytes_read = is.read(buf) %% |err| {
            %%io.stderr.write("Unable to read from stream: ");
            %%io.stderr.write(@err_name(err));
            %%io.stderr.write("\n");
            return err;
        };

        if (bytes_read == 0) {
            break;
        }

        io.stdout.write(buf[0...bytes_read]) %% |err| {
            %%io.stderr.write("Unable to write to stdout: ");
            %%io.stderr.write(@err_name(err));
            %%io.stderr.write("\n");
            return err;
        };
    }
}
<commit_msg>fix cat example by flushing stdout at end<commit_after>use @import("std");

// TODO var args printing

pub fn main(args: [][]u8) -> %void {
    const exe = args[0];
    var catted_anything = false;
    for (args[1...]) |arg| {
        if (str_eql(arg, "-")) {
            catted_anything = true;
            cat_stream(io.stdin) %% |err| return err;
        } else if (arg[0] == '-') {
            return usage(exe);
        } else {
            var is = io.InStream.open(arg) %% |err| {
                %%io.stderr.write("Unable to open file: ");
                %%io.stderr.write(@err_name(err));
                %%io.stderr.write("\n");
                return err;
            };
            defer %%is.close();

            catted_anything = true;
            cat_stream(is) %% |err| return err;
        }
    }
    if (!catted_anything) {
        cat_stream(io.stdin) %% |err| return err;
    }
    io.stdout.flush() %% |err| return err;
}

fn usage(exe: []u8) -> %void {
    %%io.stderr.write("Usage: ");
    %%io.stderr.write(exe);
    %%io.stderr.write(" [FILE]...\n");
    return error.Invalid;
}

fn cat_stream(is: io.InStream) -> %void {
    var buf: [1024 * 4]u8 = undefined;

    while (true) {
        const bytes_read = is.read(buf) %% |err| {
            %%io.stderr.write("Unable to read from stream: ");
            %%io.stderr.write(@err_name(err));
            %%io.stderr.write("\n");
            return err;
        };

        if (bytes_read == 0) {
            break;
        }

        io.stdout.write(buf[0...bytes_read]) %% |err| {
            %%io.stderr.write("Unable to write to stdout: ");
            %%io.stderr.write(@err_name(err));
            %%io.stderr.write("\n");
            return err;
        };
    }
}

{'fraction_non_alphanumeric': 0.1813138686131387, 'fraction_numerical': 0.0075912408759124085, 'mean_word_length': 1.674473067915691, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '727233', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1110, 'n_words': 229}