-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprintf_musl.zig
More file actions
49 lines (34 loc) · 1.37 KB
/
printf_musl.zig
File metadata and controls
49 lines (34 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const std = @import("std");
const builtin = @import("builtin");
const dll = @import("dll");
// pub const std_options: std.Options = .{
// .log_scope_levels = &.{.{
// .scope = .dynamic_library_loader,
// .level = .debug,
// }},
// };
pub const debug = struct {
pub const SelfInfo = dll.CustomSelfInfo;
};
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
const io = init.io;
const args = init.minimal.args;
const environ = init.minimal.environ;
try dll.init(.{ .allocator = allocator, .io = io, .args = args, .environ = environ });
defer dll.deinit();
var cwd_buf: [1024]u8 = undefined;
const cwd_idx = try std.process.executableDirPath(io, &cwd_buf);
var lib_path: [1024]u8 = undefined;
const lib_c_path = try std.fmt.bufPrint(&lib_path, "{s}/{s}", .{ cwd_buf[0..cwd_idx], "resources/musl/libc.so" });
std.log.info("loading '{s}'...", .{lib_c_path});
const lib_c = try dll.load(lib_c_path);
std.log.info("testing libc printf...", .{});
const printf_sym = try lib_c.getSymbol("printf");
const printf_addr = printf_sym.addr;
const printf: *const fn ([*:0]const u8, ...) callconv(.c) c_int = @ptrFromInt(printf_addr);
var world: [:0]u8 = try allocator.dupeZ(u8, "World");
defer allocator.free(world);
world.ptr[0] = 'w';
_ = printf("Hello, %s!\n", world.ptr);
}