From bb956254774ed022f29c70e98f7ca95c28f2c86f Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Thu, 16 Feb 2023 18:33:06 +0100 Subject: [PATCH] try 'write' that works on mac, but I didn't know if it works on windows --- exercises/093_hello_c.zig | 20 +++++--------------- patches/patches/093_hello_c.patch | 6 +++--- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/exercises/093_hello_c.zig b/exercises/093_hello_c.zig index d234165..1877e04 100644 --- a/exercises/093_hello_c.zig +++ b/exercises/093_hello_c.zig @@ -42,29 +42,19 @@ const std = @import("std"); // new the import for C const c = @cImport({ - - // we use "standard input/output" from C - @cInclude("stdio.h"); + @cInclude("unistd.h"); }); pub fn main() void { - // Due to a current limitation in the Zig compiler, - // we need a small workaround to make this exercise - // work on mac-os. - const builtin = @import("builtin"); - const stderr = switch (builtin.target.os.tag) { - .macos => 1, - else => c.stderr, - }; - // In order to output a text that can be evaluated by the // Zig Builder, we need to write it to the Error output. // In Zig we do this with "std.debug.print" and in C we can - // specify the file to write to, i.e. "standard error (stderr)". + // specify the file descriptor i.e. 2 for error console. // - // Ups, something is wrong... - const c_res = fprintf(stderr, "Hello C from Zig!"); + // In this case we use 'write' to output 17 chars, + // but something is missing... + const c_res = write(2, "Hello C from Zig!", 17); // let's see what the result from C is: std.debug.print(" - C result ist {d} chars\n", .{c_res}); diff --git a/patches/patches/093_hello_c.patch b/patches/patches/093_hello_c.patch index a0f62c2..60eeaf0 100644 --- a/patches/patches/093_hello_c.patch +++ b/patches/patches/093_hello_c.patch @@ -1,4 +1,4 @@ -63c63 -< const c_res = fprintf(stderr, "Hello C from Zig!"); +57c57 +< const c_res = write(2, "Hello C from Zig!", 17); --- -> const c_res = c.fprintf(stderr, "Hello C from Zig!"); +> const c_res = c.write(2, "Hello C from Zig!", 17);