zig/test/standalone/dirname/touch.zig
Andrew Kelley 0403f9647b std: different mechanism for disabling network dependency
On Windows, it is sometimes problematic to depend on ws2_32.dll. Before,
users of std.Io.Threaded would have to call ioBasic() rather than io()
in order to avoid unnecessary dependencies on ws2_32.dll. Now, the
application can disable networking with std.Options.

This change is necessary due to moving networking functionality to
be based on Io.Operation, which is a tagged union.
2026-03-05 22:36:33 -08:00

33 lines
904 B
Zig

//! Creates a file at the given path, if it doesn't already exist.
//!
//! ```
//! touch <path>
//! ```
//!
//! Path must be absolute.
const std = @import("std");
pub fn main(init: std.process.Init) !void {
var args = try init.minimal.args.iterateAllocator(init.gpa);
defer args.deinit();
_ = args.next() orelse unreachable; // skip binary name
const path = args.next() orelse {
std.log.err("missing <path> argument", .{});
return error.BadUsage;
};
const dir_path = std.Io.Dir.path.dirname(path) orelse unreachable;
const basename = std.Io.Dir.path.basename(path);
const io = std.Io.Threaded.global_single_threaded.io();
var dir = try std.Io.Dir.cwd().openDir(io, dir_path, .{});
defer dir.close(io);
_ = dir.statFile(io, basename, .{}) catch {
var file = try dir.createFile(io, basename, .{});
file.close(io);
};
}