From ec18ac2152818d58c3e5f7d9a189642dfe4b1337 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Mon, 13 May 2024 00:28:18 +0500 Subject: [PATCH] Add subdomain constructor --- src/hosts.zig | 12 ++++++++++++ src/util.zig | 2 +- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/hosts.zig b/src/hosts.zig index fdbcb7b..810a895 100644 --- a/src/hosts.zig +++ b/src/hosts.zig @@ -62,14 +62,26 @@ fn append_new(tmp_hosts: std.fs.File, addr: std.net.Address) !void { const writer = tmp_hosts.writer(); var buff = [_]u8{0x00} ** 50; + // write output const header_len = hosts_header.*.len; @memcpy(buff[0..header_len], hosts_header); try writer.writeAll(buff[0..header_len]); try writer.writeByte('\n'); + var subdomains = try get_subdomains(); + const domain = try util.getenv("TARGET_DOMAIN"); + while (subdomains.next()) |subdomain| { + std.debug.print("{s}.{s}\n", .{ subdomain, domain }); + } + // add extra final newline try writer.writeByte('\n'); } +fn get_subdomains() !std.mem.SplitIterator(u8, .scalar) { + const subdomain_list = try util.getenv("SUBDOMAIN_LIST"); + return std.mem.splitScalar(u8, std.mem.span(subdomain_list), ' '); +} + fn create_tmp_hosts(ip: util.IP) !void { var old_hosts = try std.fs.openFileAbsoluteZ(try util.getenv("OLD_HOSTS_PATH"), .{ .mode = .read_only }); defer old_hosts.close(); diff --git a/src/util.zig b/src/util.zig index 48ed5a8..7bb1afc 100644 --- a/src/util.zig +++ b/src/util.zig @@ -64,7 +64,7 @@ fn check_domain(domain_str: [:0]const u8) !Domain { .name = domain_str, .labels = [_][]const u8{&[_]u8{}} ** 5, }; - var labels = std.mem.split(u8, domain_str, "."); + var labels = std.mem.splitScalar(u8, domain_str, '.'); var last: []const u8 = ""; var i: usize = 0; while (labels.next()) |segment| {