killを追加

このコミットが含まれているのは:
守矢諏訪子 2023-08-14 16:09:18 +09:00
コミット 45cb2b7e7a
4個のファイルの変更84行の追加1行の削除

ファイルの表示

@ -14,3 +14,4 @@
* wcを追加(完了)
* basenameを追加(完了)
* dirnameを追加(完了)
* killを追加(完了)

ファイルの表示

@ -1,6 +1,7 @@
* [ ] awk
* [x] basename
* [x] cat
* [ ] cd
* [ ] chmod
* [ ] chown
* [ ] cksum
@ -21,7 +22,7 @@
* [ ] hostname
* [ ] id
* [ ] install
* [ ] kill
* [x] kill
* [ ] ln
* [-] ls
* [ ] md5sum

ファイルの表示

@ -30,6 +30,9 @@ pub fn build(b: *std.Build) !void {
const bin_groups = b.addExecutable(.{ .name = "groups", .root_source_file = .{ .path = "src/groups.zig" }, .target = target, .optimize = optimize });
b.installArtifact(bin_groups);
const bin_kill = b.addExecutable(.{ .name = "kill", .root_source_file = .{ .path = "src/kill.zig" }, .target = target, .optimize = optimize });
b.installArtifact(bin_kill);
const bin_ls = b.addExecutable(.{ .name = "ls", .root_source_file = .{ .path = "src/ls.zig" }, .target = target, .optimize = optimize });
bin_ls.addModule("toki", lib_toki.module("toki"));
bin_ls.linkLibrary(lib_toki.artifact("toki"));

78
src/kill.zig ノーマルファイル
ファイルの表示

@ -0,0 +1,78 @@
const std = @import("std");
const fs = std.fs;
const io = std.io;
const os = std.os;
const version = @import("version.zig");
fn help() !void {
const stdof = io.getStdOut().writer();
var bw = io.bufferedWriter(stdof);
const stdout = bw.writer();
try stdout.print("076 coreutils\n", .{});
try stdout.print("使用法: kill [オプション]... [PID]...\n", .{});
try stdout.print("プロセスを終了\n\n", .{});
try stdout.print("-h ヘルプを表示\n", .{});
try stdout.print("-v バージョンを表示\n", .{});
try bw.flush();
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const alloc = gpa.allocator();
var option = std.ArrayList(u8).init(alloc);
defer option.deinit();
var pids = std.ArrayList([]const u8).init(alloc);
defer pids.deinit();
const args = try std.process.argsAlloc(alloc);
defer std.process.argsFree(alloc, args);
const stdof = io.getStdOut().writer();
var bw = io.bufferedWriter(stdof);
const stdout = bw.writer();
for (args, 0..) |arg, i| {
if (i == 0) continue;
var m: [1]u8 = [_]u8{'-'};
if (std.mem.eql(u8, arg[0..1], m[0..])) {
for (arg, 0..) |a, j| {
if (j == 0) continue;
try option.append(a);
}
} else {
try pids.append(arg);
}
}
var signal: u8 = 0;
for (option.items) |i| {
if (i == 'h') {
try help();
return;
} else if (i == 'v') {
try version.ver("kill");
return;
} else {
signal = i;
}
}
if (pids.items.len == 0) {
try help();
} else {
for (pids.items) |item| {
const pid = try std.fmt.parseInt(os.pid_t, item, 10);
_ = try os.kill(pid, signal);
try stdout.print("酷いよ!!プロセス {} を殺しました。まぁ、しょうがないね…\n", .{pid});
}
}
try bw.flush();
}