Skip to content

Commit 0fdb9c6

Browse files
committed
Merge branch 'master' into common-impl-strcasecmp
2 parents 2d60b68 + 6eb5e56 commit 0fdb9c6

File tree

9 files changed

+112
-54
lines changed

9 files changed

+112
-54
lines changed

lib/c/string.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ comptime {
88
@export(&strncmp, .{ .name = "strncmp", .linkage = common.linkage, .visibility = common.visibility });
99
@export(&strcasecmp, .{ .name = "strcasecmp", .linkage = common.linkage, .visibility = common.visibility });
1010
@export(&strncasecmp, .{ .name = "strncasecmp", .linkage = common.linkage, .visibility = common.visibility });
11+
@export(&__strcasecmp_l, .{ .name = "__strcasecmp_l", .linkage = common.linkage, .visibility = common.visibility });
12+
@export(&__strncasecmp_l, .{ .name = "__strncasecmp_l", .linkage = common.linkage, .visibility = common.visibility });
1113
}
1214

1315
fn strcmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {
@@ -48,6 +50,11 @@ fn strcasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char) callconv(.c) c_int {
4850
return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0]));
4951
}
5052

53+
fn __strcasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, locale: *anyopaque) callconv(.c) c_int {
54+
_ = locale;
55+
return strcasecmp(s1, s2);
56+
}
57+
5158
fn strncasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(.c) c_int {
5259
const toLower = std.ascii.toLower;
5360
var l: [*:0]const u8 = @ptrCast(s1);
@@ -63,6 +70,11 @@ fn strncasecmp(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize) callconv(
6370
return @as(c_int, toLower(l[0])) - @as(c_int, toLower(r[0]));
6471
}
6572

73+
fn __strncasecmp_l(s1: [*:0]const c_char, s2: [*:0]const c_char, n: usize, locale: *anyopaque) callconv(.c) c_int {
74+
_ = locale;
75+
return strncasecmp(s1, s2, n);
76+
}
77+
6678
test strcasecmp {
6779
try std.testing.expect(strcasecmp(@ptrCast("a"), @ptrCast("b")) < 0);
6880
try std.testing.expect(strcasecmp(@ptrCast("b"), @ptrCast("a")) > 0);

lib/std/c.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10347,6 +10347,16 @@ pub const sigaction = switch (native_os) {
1034710347
else => private.sigaction,
1034810348
};
1034910349

10350+
/// Zig's version of SIGRTMIN. Actually a function.
10351+
pub fn sigrtmin() u8 {
10352+
return @truncate(@as(c_uint, @bitCast(private.__libc_current_sigrtmin())));
10353+
}
10354+
10355+
/// Zig's version of SIGRTMAX. Actually a function.
10356+
pub fn sigrtmax() u8 {
10357+
return @truncate(@as(c_uint, @bitCast(private.__libc_current_sigrtmax())));
10358+
}
10359+
1035010360
pub const sigfillset = switch (native_os) {
1035110361
.netbsd => private.__sigfillset14,
1035210362
else => private.sigfillset,
@@ -11213,6 +11223,9 @@ const private = struct {
1121311223
extern "c" fn __getdents30(fd: c_int, buf_ptr: [*]u8, nbytes: usize) c_int;
1121411224
extern "c" fn __sigaltstack14(ss: ?*stack_t, old_ss: ?*stack_t) c_int;
1121511225

11226+
extern "c" fn __libc_current_sigrtmin() c_int;
11227+
extern "c" fn __libc_current_sigrtmax() c_int;
11228+
1121611229
// Don't forget to add another clown when an OS picks yet another unique
1121711230
// symbol name for errno location!
1121811231
// 🤡🤡🤡🤡🤡🤡

lib/std/os.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ pub fn getFdPath(fd: std.posix.fd_t, out_buffer: *[max_path_bytes]u8) std.posix.
121121
.SUCCESS => {},
122122
.BADF => return error.FileNotFound,
123123
.NOSPC => return error.NameTooLong,
124+
.NOENT => return error.FileNotFound,
124125
// TODO man pages for fcntl on macOS don't really tell you what
125126
// errno values to expect when command is F.GETPATH...
126127
else => |err| return posix.unexpectedErrno(err),

lib/std/os/linux.zig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,19 @@ const SigsetElement = c_ulong;
18011801

18021802
const sigset_len = @typeInfo(sigset_t).array.len;
18031803

1804+
/// Zig's SIGRTMIN, but is a function for compatibility with glibc
1805+
pub fn sigrtmin() u8 {
1806+
// Default is 32 in the kernel UAPI: https://github.com/torvalds/linux/blob/78109c591b806e41987e0b83390e61d675d1f724/include/uapi/asm-generic/signal.h#L50
1807+
// AFAICT, all architectures that override this also set it to 32:
1808+
// https://github.com/search?q=repo%3Atorvalds%2Flinux+sigrtmin+path%3Auapi&type=code
1809+
return 32;
1810+
}
1811+
1812+
/// Zig's SIGRTMAX, but is a function for compatibility with glibc
1813+
pub fn sigrtmax() u8 {
1814+
return NSIG - 1;
1815+
}
1816+
18041817
/// Zig's version of sigemptyset. Returns initialized sigset_t.
18051818
pub fn sigemptyset() sigset_t {
18061819
return [_]SigsetElement{0} ** sigset_len;

lib/std/os/windows.zig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ pub const ReadFileError = error{
605605
/// Known to be possible when:
606606
/// - Unable to read from disconnected virtual com port (Windows)
607607
AccessDenied,
608+
NotOpenForReading,
608609
Unexpected,
609610
};
610611

@@ -638,6 +639,7 @@ pub fn ReadFile(in_hFile: HANDLE, buffer: []u8, offset: ?u64) ReadFileError!usiz
638639
.NETNAME_DELETED => return error.ConnectionResetByPeer,
639640
.LOCK_VIOLATION => return error.LockViolation,
640641
.ACCESS_DENIED => return error.AccessDenied,
642+
.INVALID_HANDLE => return error.NotOpenForReading,
641643
else => |err| return unexpectedError(err),
642644
}
643645
}
@@ -1152,7 +1154,10 @@ pub fn GetStdHandle(handle_id: DWORD) GetStdHandleError!HANDLE {
11521154
return handle;
11531155
}
11541156

1155-
pub const SetFilePointerError = error{Unexpected};
1157+
pub const SetFilePointerError = error{
1158+
Unseekable,
1159+
Unexpected,
1160+
};
11561161

11571162
/// The SetFilePointerEx function with the `dwMoveMethod` parameter set to `FILE_BEGIN`.
11581163
pub fn SetFilePointerEx_BEGIN(handle: HANDLE, offset: u64) SetFilePointerError!void {
@@ -1162,6 +1167,8 @@ pub fn SetFilePointerEx_BEGIN(handle: HANDLE, offset: u64) SetFilePointerError!v
11621167
const ipos = @as(LARGE_INTEGER, @bitCast(offset));
11631168
if (kernel32.SetFilePointerEx(handle, ipos, null, FILE_BEGIN) == 0) {
11641169
switch (GetLastError()) {
1170+
.INVALID_FUNCTION => return error.Unseekable,
1171+
.NEGATIVE_SEEK => return error.Unseekable,
11651172
.INVALID_PARAMETER => unreachable,
11661173
.INVALID_HANDLE => unreachable,
11671174
else => |err| return unexpectedError(err),
@@ -1173,6 +1180,8 @@ pub fn SetFilePointerEx_BEGIN(handle: HANDLE, offset: u64) SetFilePointerError!v
11731180
pub fn SetFilePointerEx_CURRENT(handle: HANDLE, offset: i64) SetFilePointerError!void {
11741181
if (kernel32.SetFilePointerEx(handle, offset, null, FILE_CURRENT) == 0) {
11751182
switch (GetLastError()) {
1183+
.INVALID_FUNCTION => return error.Unseekable,
1184+
.NEGATIVE_SEEK => return error.Unseekable,
11761185
.INVALID_PARAMETER => unreachable,
11771186
.INVALID_HANDLE => unreachable,
11781187
else => |err| return unexpectedError(err),
@@ -1184,6 +1193,8 @@ pub fn SetFilePointerEx_CURRENT(handle: HANDLE, offset: i64) SetFilePointerError
11841193
pub fn SetFilePointerEx_END(handle: HANDLE, offset: i64) SetFilePointerError!void {
11851194
if (kernel32.SetFilePointerEx(handle, offset, null, FILE_END) == 0) {
11861195
switch (GetLastError()) {
1196+
.INVALID_FUNCTION => return error.Unseekable,
1197+
.NEGATIVE_SEEK => return error.Unseekable,
11871198
.INVALID_PARAMETER => unreachable,
11881199
.INVALID_HANDLE => unreachable,
11891200
else => |err| return unexpectedError(err),
@@ -1196,6 +1207,8 @@ pub fn SetFilePointerEx_CURRENT_get(handle: HANDLE) SetFilePointerError!u64 {
11961207
var result: LARGE_INTEGER = undefined;
11971208
if (kernel32.SetFilePointerEx(handle, 0, &result, FILE_CURRENT) == 0) {
11981209
switch (GetLastError()) {
1210+
.INVALID_FUNCTION => return error.Unseekable,
1211+
.NEGATIVE_SEEK => return error.Unseekable,
11991212
.INVALID_PARAMETER => unreachable,
12001213
.INVALID_HANDLE => unreachable,
12011214
else => |err| return unexpectedError(err),

lib/std/posix.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ pub const rusage = system.rusage;
151151
pub const sa_family_t = system.sa_family_t;
152152
pub const siginfo_t = system.siginfo_t;
153153
pub const sigset_t = system.sigset_t;
154+
pub const sigrtmin = system.sigrtmin;
155+
pub const sigrtmax = system.sigrtmax;
154156
pub const sockaddr = system.sockaddr;
155157
pub const socklen_t = system.socklen_t;
156158
pub const stack_t = system.stack_t;

lib/std/posix/test.zig

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,17 @@ test "shutdown socket" {
859859
std.net.Stream.close(.{ .handle = sock });
860860
}
861861

862+
test "sigrtmin/max" {
863+
if (native_os == .wasi or native_os == .windows or native_os == .macos) {
864+
return error.SkipZigTest;
865+
}
866+
867+
try std.testing.expect(posix.sigrtmin() >= 32);
868+
try std.testing.expect(posix.sigrtmin() >= posix.system.sigrtmin());
869+
try std.testing.expect(posix.sigrtmin() < posix.system.sigrtmax());
870+
try std.testing.expect(posix.sigrtmax() < posix.NSIG);
871+
}
872+
862873
test "sigset empty/full" {
863874
if (native_os == .wasi or native_os == .windows)
864875
return error.SkipZigTest;
@@ -875,10 +886,13 @@ test "sigset empty/full" {
875886
try expectEqual(true, posix.sigismember(&set, @truncate(posix.SIG.INT)));
876887
}
877888

878-
// Some signals (32 - 34 on glibc/musl) are not allowed to be added to a
889+
// Some signals (i.e., 32 - 34 on glibc/musl) are not allowed to be added to a
879890
// sigset by the C library, so avoid testing them.
880891
fn reserved_signo(i: usize) bool {
881-
return builtin.link_libc and (i >= 32 and i <= 34);
892+
if (native_os == .macos) {
893+
return false;
894+
}
895+
return builtin.link_libc and (i >= 32 and i < posix.sigrtmin());
882896
}
883897

884898
test "sigset add/del" {

src/Compilation.zig

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5174,11 +5174,13 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: std.Pr
51745174
}
51755175

51765176
// Just to save disk space, we delete the files that are never needed again.
5177-
defer if (out_diag_path) |diag_file_path| zig_cache_tmp_dir.deleteFile(std.fs.path.basename(diag_file_path)) catch |err| {
5178-
log.warn("failed to delete '{s}': {s}", .{ diag_file_path, @errorName(err) });
5177+
defer if (out_diag_path) |diag_file_path| zig_cache_tmp_dir.deleteFile(std.fs.path.basename(diag_file_path)) catch |err| switch (err) {
5178+
error.FileNotFound => {}, // the file wasn't created due to an error we reported
5179+
else => log.warn("failed to delete '{s}': {s}", .{ diag_file_path, @errorName(err) }),
51795180
};
5180-
defer if (out_dep_path) |dep_file_path| zig_cache_tmp_dir.deleteFile(std.fs.path.basename(dep_file_path)) catch |err| {
5181-
log.warn("failed to delete '{s}': {s}", .{ dep_file_path, @errorName(err) });
5181+
defer if (out_dep_path) |dep_file_path| zig_cache_tmp_dir.deleteFile(std.fs.path.basename(dep_file_path)) catch |err| switch (err) {
5182+
error.FileNotFound => {}, // the file wasn't created due to an error we reported
5183+
else => log.warn("failed to delete '{s}': {s}", .{ dep_file_path, @errorName(err) }),
51825184
};
51835185
if (std.process.can_spawn) {
51845186
var child = std.process.Child.init(argv.items, arena);

src/glibc.zig

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -864,8 +864,8 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
864864
// Example:
865865
// .balign 4
866866
// .globl _Exit_2_2_5
867-
// .type _Exit_2_2_5, %function;
868-
// .symver _Exit_2_2_5, _Exit@@GLIBC_2.2.5
867+
// .type _Exit_2_2_5, %function
868+
// .symver _Exit_2_2_5, _Exit@@GLIBC_2.2.5, remove
869869
// _Exit_2_2_5: .long 0
870870
const ver_index = versions_buffer[ver_buf_i];
871871
const ver = metadata.all_versions[ver_index];
@@ -876,19 +876,16 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
876876
const want_default = chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index;
877877
const at_sign_str: []const u8 = if (want_default) "@@" else "@";
878878
if (ver.patch == 0) {
879-
const sym_plus_ver = if (want_default)
880-
sym_name
881-
else
882-
try std.fmt.allocPrint(
883-
arena,
884-
"{s}_GLIBC_{d}_{d}",
885-
.{ sym_name, ver.major, ver.minor },
886-
);
879+
const sym_plus_ver = try std.fmt.allocPrint(
880+
arena,
881+
"{s}_{d}_{d}",
882+
.{ sym_name, ver.major, ver.minor },
883+
);
887884
try stubs_asm.writer().print(
888885
\\.balign {d}
889886
\\.globl {s}
890-
\\.type {s}, %function;
891-
\\.symver {s}, {s}{s}GLIBC_{d}.{d}
887+
\\.type {s}, %function
888+
\\.symver {s}, {s}{s}GLIBC_{d}.{d}, remove
892889
\\{s}: {s} 0
893890
\\
894891
, .{
@@ -904,19 +901,16 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
904901
wordDirective(target),
905902
});
906903
} else {
907-
const sym_plus_ver = if (want_default)
908-
sym_name
909-
else
910-
try std.fmt.allocPrint(
911-
arena,
912-
"{s}_GLIBC_{d}_{d}_{d}",
913-
.{ sym_name, ver.major, ver.minor, ver.patch },
914-
);
904+
const sym_plus_ver = try std.fmt.allocPrint(
905+
arena,
906+
"{s}_{d}_{d}_{d}",
907+
.{ sym_name, ver.major, ver.minor, ver.patch },
908+
);
915909
try stubs_asm.writer().print(
916910
\\.balign {d}
917911
\\.globl {s}
918-
\\.type {s}, %function;
919-
\\.symver {s}, {s}{s}GLIBC_{d}.{d}.{d}
912+
\\.type {s}, %function
913+
\\.symver {s}, {s}{s}GLIBC_{d}.{d}.{d}, remove
920914
\\{s}: {s} 0
921915
\\
922916
, .{
@@ -1041,9 +1035,9 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
10411035
// Example:
10421036
// .balign 4
10431037
// .globl environ_2_2_5
1044-
// .type environ_2_2_5, %object;
1045-
// .size environ_2_2_5, 4;
1046-
// .symver environ_2_2_5, environ@@GLIBC_2.2.5
1038+
// .type environ_2_2_5, %object
1039+
// .size environ_2_2_5, 4
1040+
// .symver environ_2_2_5, environ@@GLIBC_2.2.5, remove
10471041
// environ_2_2_5: .fill 4, 1, 0
10481042
const ver_index = versions_buffer[ver_buf_i];
10491043
const ver = metadata.all_versions[ver_index];
@@ -1054,20 +1048,17 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
10541048
const want_default = chosen_def_ver_index != 255 and ver_index == chosen_def_ver_index;
10551049
const at_sign_str: []const u8 = if (want_default) "@@" else "@";
10561050
if (ver.patch == 0) {
1057-
const sym_plus_ver = if (want_default)
1058-
sym_name
1059-
else
1060-
try std.fmt.allocPrint(
1061-
arena,
1062-
"{s}_GLIBC_{d}_{d}",
1063-
.{ sym_name, ver.major, ver.minor },
1064-
);
1051+
const sym_plus_ver = try std.fmt.allocPrint(
1052+
arena,
1053+
"{s}_{d}_{d}",
1054+
.{ sym_name, ver.major, ver.minor },
1055+
);
10651056
try stubs_asm.writer().print(
10661057
\\.balign {d}
10671058
\\.globl {s}
1068-
\\.type {s}, %object;
1069-
\\.size {s}, {d};
1070-
\\.symver {s}, {s}{s}GLIBC_{d}.{d}
1059+
\\.type {s}, %object
1060+
\\.size {s}, {d}
1061+
\\.symver {s}, {s}{s}GLIBC_{d}.{d}, remove
10711062
\\{s}: .fill {d}, 1, 0
10721063
\\
10731064
, .{
@@ -1085,20 +1076,17 @@ pub fn buildSharedObjects(comp: *Compilation, prog_node: std.Progress.Node) anye
10851076
size,
10861077
});
10871078
} else {
1088-
const sym_plus_ver = if (want_default)
1089-
sym_name
1090-
else
1091-
try std.fmt.allocPrint(
1092-
arena,
1093-
"{s}_GLIBC_{d}_{d}_{d}",
1094-
.{ sym_name, ver.major, ver.minor, ver.patch },
1095-
);
1079+
const sym_plus_ver = try std.fmt.allocPrint(
1080+
arena,
1081+
"{s}_{d}_{d}_{d}",
1082+
.{ sym_name, ver.major, ver.minor, ver.patch },
1083+
);
10961084
try stubs_asm.writer().print(
10971085
\\.balign {d}
10981086
\\.globl {s}
1099-
\\.type {s}, %object;
1100-
\\.size {s}, {d};
1101-
\\.symver {s}, {s}{s}GLIBC_{d}.{d}.{d}
1087+
\\.type {s}, %object
1088+
\\.size {s}, {d}
1089+
\\.symver {s}, {s}{s}GLIBC_{d}.{d}.{d}, remove
11021090
\\{s}: .fill {d}, 1, 0
11031091
\\
11041092
, .{

0 commit comments

Comments
 (0)