aboutsummaryrefslogtreecommitdiff
path: root/test/standalone/dirname/has_basename.zig
blob: 49eefa3b48e9b79b5794c17a1334602465a07090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//! Checks that the basename of the given path matches a string.
//!
//! Usage:
//!
//! ```
//! has_basename <path> <basename>
//! ```
//!
//! <path> must be absolute.
//!
//! Returns a non-zero exit code if basename
//! does not match the given string.

const std = @import("std");

pub fn main() !void {
    var arena_state = std.heap.ArenaAllocator.init(std.heap.page_allocator);
    const arena = arena_state.allocator();
    defer arena_state.deinit();

    try run(arena);
}

fn run(allocator: std.mem.Allocator) !void {
    var args = try std.process.argsWithAllocator(allocator);
    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 basename = args.next() orelse {
        std.log.err("missing <basename> argument", .{});
        return error.BadUsage;
    };

    const actual_basename = std.fs.path.basename(path);
    if (std.mem.eql(u8, actual_basename, basename)) {
        return;
    }

    return error.NotEqual;
}