From 6ae17fe99d649266cf81e76cd146718a1a945f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Mon, 11 Jul 2022 06:07:11 +0300 Subject: [PATCH] wip: turbo-getent --- src/turbo-getent.zig | 50 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/turbo-getent.zig diff --git a/src/turbo-getent.zig b/src/turbo-getent.zig new file mode 100644 index 0000000..b3da3f4 --- /dev/null +++ b/src/turbo-getent.zig @@ -0,0 +1,50 @@ +const std = @import("std"); +const io = std.io; +const os = std.os; +const fmt = std.fmt; + +const flags = @import("flags.zig"); + +const usage = + \\usage: turbo-getent [OPTION]... group|passwd [key...] + \\ + \\ -h Print this help message and exit + \\ --db=PATH Path to the database (default: /etc/turbonss/db.turbo) + \\ + \\turbo-getent resolves group or passwd (a.k.a. user) entries from the + \\database file. If one or more key arguments are provided, then only + \\entries that match the supplied keys will be displayed. If no key is + \\provided, all entries will be displayed. + \\ +; + +pub fn main() !void { + // This line is here because of https://github.com/ziglang/zig/issues/7807 + const argv: []const [*:0]const u8 = os.argv; + + const stderr = io.getStdErr().writer(); + const stdout = io.getStdOut().writer(); + + const return_code = execute(stdout, stderr, argv[1..]); + os.exit(return_code); +} + +fn execute( + stdout: anytype, + stderr: anytype, + argv: []const [*:0]const u8, +) u8 { + const myflags = flags.parse(argv, &[_]flags.Flag{ + .{ .name = "-h", .kind = .boolean }, + }) catch { + stderr.writeAll(usage) catch {}; + return 1; + }; + + if (myflags.boolFlag("-h")) { + stdout.writeAll(usage) catch return 1; + return 0; + } + + return 0; +}