zig/test/standalone/cat/main.zig
Andrew Kelley 429a219f42 stage2: fix not detecting all dynamic libraries
Positional shared library arguments were not being detected as causing
dynamic linking, resulting in invalid linker lines. LLD did not have an
error message for this when targeting x86_64-linux but it did emit an
error message when targeting aarch64-linux, which is how I noticed the
problem.

This surfaced an error having to do with fifo.pipe() in the cat example
which I did not diagnose but solved the issue by doing the revamp that
was already overdue for that example.

It appears that the zig-window project was exploiting the previous
behavior for it to function properly, so this prompts the question, is
there some kind of static/dynamic executable hybrid that the compiler
should recognize? Unclear - but we can discuss that in #7240.
2020-11-30 20:25:28 -07:00

47 lines
1.2 KiB
Zig

const std = @import("std");
const io = std.io;
const process = std.process;
const fs = std.fs;
const mem = std.mem;
const warn = std.log.warn;
pub fn main() !void {
var arena_instance = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena_instance.deinit();
const arena = &arena_instance.allocator;
const args = try process.argsAlloc(arena);
const exe = args[0];
var catted_anything = false;
const stdout_file = io.getStdOut();
const cwd = fs.cwd();
for (args[1..]) |arg| {
if (mem.eql(u8, arg, "-")) {
catted_anything = true;
try stdout_file.writeFileAll(io.getStdIn(), .{});
} else if (mem.startsWith(u8, arg, "-")) {
return usage(exe);
} else {
const file = cwd.openFile(arg, .{}) catch |err| {
warn("Unable to open file: {s}\n", .{@errorName(err)});
return err;
};
defer file.close();
catted_anything = true;
try stdout_file.writeFileAll(file, .{});
}
}
if (!catted_anything) {
try stdout_file.writeFileAll(io.getStdIn(), .{});
}
}
fn usage(exe: []const u8) !void {
warn("Usage: {} [FILE]...\n", .{exe});
return error.Invalid;
}