authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-22 02:23:52+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2026-01-22 02:23:52+01:00
log4b2eae32f1401f302648de13c6cae60c88cf0bc7
treedc3adab17e3db2e14a17572bcdd17bbc6ee0ccbd
parent200fb7c2ac460ea40fc032cf95d1bd78e72b046a
parent1ab6bf59a6722f816edce15574c2dfcaefeebd31

Merge pull request 'libc: use common implementation for `sys/utsname.h`' (#30904) from GasInfinity/zig:libc-uname into master

Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30904 Reviewed-by: Andrew Kelley <andrew@ziglang.org>

12 files changed, 59 insertions(+), 62 deletions(-)

lib/c.zig+2
...@@ -25,6 +25,8 @@ comptime {...@@ -25,6 +25,8 @@ comptime {
25 _ = @import("c/strings.zig");25 _ = @import("c/strings.zig");
26 _ = @import("c/wchar.zig");26 _ = @import("c/wchar.zig");
2727
28 _ = @import("c/sys.zig");
29
28 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {30 if (builtin.target.isMuslLibC() or builtin.target.isWasiLibC()) {
29 // Files specific to musl and wasi-libc.31 // Files specific to musl and wasi-libc.
30 }32 }
lib/c/common.zig+14
...@@ -13,3 +13,17 @@ pub const visibility: std.builtin.SymbolVisibility = if (linkage != .internal)...@@ -13,3 +13,17 @@ pub const visibility: std.builtin.SymbolVisibility = if (linkage != .internal)
13 .hidden13 .hidden
14else14else
15 .default;15 .default;
16
17/// Checks whether the syscall has had an error, storing it in `std.c.errno` and returning -1.
18/// Otherwise returns the result.
19pub fn linuxErrno(r: usize) isize {
20 const linux = std.os.linux;
21
22 return switch (linux.errno(r)) {
23 .SUCCESS => @bitCast(r),
24 else => |err| blk: {
25 std.c._errno().* = @intFromEnum(err);
26 break :blk -1;
27 },
28 };
29}
lib/c/sys.zig created+3
...@@ -0,0 +1,3 @@
1comptime {
2 _ = @import("sys/utsname.zig");
3}
lib/c/sys/utsname.zig created+32
...@@ -0,0 +1,32 @@
1const std = @import("std");
2const common = @import("../common.zig");
3const builtin = @import("builtin");
4
5comptime {
6 if (builtin.target.isMuslLibC()) {
7 @export(&unameLinux, .{ .name = "uname", .linkage = common.linkage, .visibility = common.visibility });
8 }
9
10 if (builtin.target.isWasiLibC()) {
11 @export(&unameWasi, .{ .name = "uname", .linkage = common.linkage, .visibility = common.visibility });
12 }
13}
14
15fn unameLinux(uts: *std.os.linux.utsname) callconv(.c) c_int {
16 return @intCast(common.linuxErrno(std.os.linux.uname(uts)));
17}
18
19fn unameWasi(uts: *std.c.utsname) callconv(.c) c_int {
20 // note the @bitCast's for NUL termination!
21 uts.sysname[0..5].* = @bitCast("wasi".*);
22 uts.nodename[0..7].* = @bitCast("(none)".*);
23 uts.release[0..6].* = @bitCast("0.0.0".*);
24 uts.version[0..6].* = @bitCast("0.0.0".*);
25 uts.machine[0..7].* = @bitCast(switch (builtin.target.cpu.arch) {
26 .wasm32 => "wasm32",
27 .wasm64 => "wasm64",
28 else => comptime unreachable,
29 }.*);
30 uts.domainname[0..7].* = @bitCast("(none)".*);
31 return 0;
32}
lib/libc/musl/src/misc/ffs.c deleted-7
...@@ -1,7 +0,0 @@
1#include <strings.h>
2#include "atomic.h"
3
4int ffs(int i)
5{
6 return i ? a_ctz_l(i)+1 : 0;
7}
lib/libc/musl/src/misc/ffsl.c deleted-7
...@@ -1,7 +0,0 @@
1#include <strings.h>
2#include "atomic.h"
3
4int ffsl(long i)
5{
6 return i ? a_ctz_l(i)+1 : 0;
7}
lib/libc/musl/src/misc/ffsll.c deleted-7
...@@ -1,7 +0,0 @@
1#include <strings.h>
2#include "atomic.h"
3
4int ffsll(long long i)
5{
6 return i ? a_ctz_64(i)+1 : 0;
7}
lib/libc/musl/src/misc/uname.c deleted-7
...@@ -1,7 +0,0 @@
1#include <sys/utsname.h>
2#include "syscall.h"
3
4int uname(struct utsname *uts)
5{
6 return syscall(SYS_uname, uts);
7}
lib/libc/wasi/libc-top-half/musl/src/misc/uname.c deleted-32
...@@ -1,32 +0,0 @@
1#include <sys/utsname.h>
2#ifdef __wasilibc_unmodified_upstream // Implement uname with placeholders
3#include "syscall.h"
4#else
5#include <string.h>
6#endif
7
8int uname(struct utsname *uts)
9{
10#ifdef __wasilibc_unmodified_upstream // Implement uname with placeholders
11 return syscall(SYS_uname, uts);
12#else
13 // Just fill in the fields with placeholder values.
14 strcpy(uts->sysname, "wasi");
15 strcpy(uts->nodename, "(none)");
16 strcpy(uts->release, "0.0.0");
17 strcpy(uts->version, "0.0.0");
18#if defined(__wasm32__)
19 strcpy(uts->machine, "wasm32");
20#elif defined(__wasm64__)
21 strcpy(uts->machine, "wasm64");
22#else
23 strcpy(uts->machine, "unknown");
24#endif
25#ifdef _GNU_SOURCE
26 strcpy(uts->domainname, "(none)");
27#else
28 strcpy(uts->__domainname, "(none)");
29#endif
30 return 0;
31#endif
32}
lib/std/c.zig+8
...@@ -7069,6 +7069,14 @@ pub const user_desc = switch (native_os) {...@@ -7069,6 +7069,14 @@ pub const user_desc = switch (native_os) {
7069pub const utsname = switch (native_os) {7069pub const utsname = switch (native_os) {
7070 .linux => linux.utsname,7070 .linux => linux.utsname,
7071 .emscripten => emscripten.utsname,7071 .emscripten => emscripten.utsname,
7072 .wasi => extern struct {
7073 sysname: [64:0]u8,
7074 nodename: [64:0]u8,
7075 release: [64:0]u8,
7076 version: [64:0]u8,
7077 machine: [64:0]u8,
7078 domainname: [64:0]u8,
7079 },
7072 .illumos => extern struct {7080 .illumos => extern struct {
7073 sysname: [256:0]u8,7081 sysname: [256:0]u8,
7074 nodename: [256:0]u8,7082 nodename: [256:0]u8,
src/libs/musl.zig-1
...@@ -1156,7 +1156,6 @@ const src_files = [_][]const u8{...@@ -1156,7 +1156,6 @@ const src_files = [_][]const u8{
1156 "musl/src/misc/setrlimit.c",1156 "musl/src/misc/setrlimit.c",
1157 "musl/src/misc/syscall.c",1157 "musl/src/misc/syscall.c",
1158 "musl/src/misc/syslog.c",1158 "musl/src/misc/syslog.c",
1159 "musl/src/misc/uname.c",
1160 "musl/src/misc/wordexp.c",1159 "musl/src/misc/wordexp.c",
1161 "musl/src/mman/madvise.c",1160 "musl/src/mman/madvise.c",
1162 "musl/src/mman/mincore.c",1161 "musl/src/mman/mincore.c",
src/libs/wasi_libc.zig-1
...@@ -1054,7 +1054,6 @@ const libc_top_half_src_files = [_][]const u8{...@@ -1054,7 +1054,6 @@ const libc_top_half_src_files = [_][]const u8{
1054 "wasi/libc-top-half/musl/src/math/sinhf.c",1054 "wasi/libc-top-half/musl/src/math/sinhf.c",
1055 "wasi/libc-top-half/musl/src/misc/fmtmsg.c",1055 "wasi/libc-top-half/musl/src/misc/fmtmsg.c",
1056 "wasi/libc-top-half/musl/src/misc/nftw.c",1056 "wasi/libc-top-half/musl/src/misc/nftw.c",
1057 "wasi/libc-top-half/musl/src/misc/uname.c",
1058 "wasi/libc-top-half/musl/src/prng/random.c",1057 "wasi/libc-top-half/musl/src/prng/random.c",
1059 "wasi/libc-top-half/musl/src/regex/glob.c",1058 "wasi/libc-top-half/musl/src/regex/glob.c",
1060 "wasi/libc-top-half/musl/src/regex/regcomp.c",1059 "wasi/libc-top-half/musl/src/regex/regcomp.c",