Initial commit

This commit is contained in:
Aly 2020-09-18 18:26:45 -07:00
commit 7c1968fbd8
3 changed files with 69 additions and 0 deletions

14
.gitignore vendored Normal file
View File

@ -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

24
build.zig Normal file
View File

@ -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);
}

31
src/main.zig Normal file
View File

@ -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();
}