commit 7c1968fbd88162647a219a6a97575cc7c3411905 Author: Soren Date: Fri Sep 18 18:26:45 2020 -0700 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6115deb --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ + +# Created by https://www.toptal.com/developers/gitignore/api/zig +# Edit at https://www.toptal.com/developers/gitignore?templates=zig + +### zig ### +# Zig programming language + +zig-cache/ +build/ +build-*/ +docgen_tmp/ + +# End of https://www.toptal.com/developers/gitignore/api/zig + diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..3e9ceaf --- /dev/null +++ b/build.zig @@ -0,0 +1,24 @@ +const Builder = @import("std").build.Builder; + +pub fn build(b: *Builder) void { + // Standard target options allows the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + + // Standard release options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. + const mode = b.standardReleaseOptions(); + + const exe = b.addExecutable("dc", "src/main.zig"); + exe.setTarget(target); + exe.setBuildMode(mode); + exe.install(); + + const run_cmd = exe.run(); + run_cmd.step.dependOn(b.getInstallStep()); + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..2c177a4 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,31 @@ +const std = @import("std"); + +pub const linux_sysv_shutdown: []const []const u8 = &[_][]const u8 { "init", "0" }; +pub const linux_systemd_shutdown: []const []const u8 = &[_][]const u8 { "systemctl", "poweroff" }; +pub const windows_shutdown: []const []const u8 = &[_][]const u8 { "shutdown", "/s", "/f" }; + +pub fn main() anyerror!void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + + const shutdown: []const []const u8 = switch(std.builtin.os.tag) { + .linux => blk: { + var systemd_dir_or_error = std.fs.Dir.openDir(std.fs.cwd(), "/run/systemd/system", std.fs.Dir.OpenDirOptions {}); + if(systemd_dir_or_error) |*dir| { + dir.close(); + break :blk linux_systemd_shutdown; + } else |err| { + break :blk linux_sysv_shutdown; + } + }, + .windows => windows_shutdown, + else => return error{OsNotSupported}.OsNotSupported, + }; + + const alloc = &gpa.allocator; + + const child = try std.ChildProcess.init(shutdown, alloc); + try child.spawn(); + _ = try child.wait(); +} +