Skip to content

Commit 0c6ab61

Browse files
committed
tests passing on linux
1 parent 2b42e91 commit 0c6ab61

38 files changed

+348
-298
lines changed

build.zig

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,8 @@ fn dependOnLib(b: *Builder, lib_exe_obj: var, dep: LibraryDep) void {
166166
}
167167

168168
fn fileExists(filename: []const u8) !bool {
169-
fs.File.exists(filename) catch |err| switch (err) {
170-
error.PermissionDenied,
171-
error.FileNotFound,
172-
=> return false,
169+
fs.File.access(filename) catch |err| switch (err) {
170+
error.FileNotFound => return false,
173171
else => return err,
174172
};
175173
return true;

doc/langref.html.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,8 +796,8 @@ const assert = std.debug.assert;
796796
threadlocal var x: i32 = 1234;
797797

798798
test "thread local storage" {
799-
const thread1 = try std.os.spawnThread({}, testTls);
800-
const thread2 = try std.os.spawnThread({}, testTls);
799+
const thread1 = try std.Thread.spawn({}, testTls);
800+
const thread2 = try std.Thread.spawn({}, testTls);
801801
testTls({});
802802
thread1.wait();
803803
thread2.wait();

example/cat/main.zig

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
const std = @import("std");
22
const io = std.io;
3+
const process = std.process;
4+
const File = std.fs.File;
35
const mem = std.mem;
4-
const os = std.os;
56
const warn = std.debug.warn;
67
const allocator = std.debug.global_allocator;
78

89
pub fn main() !void {
9-
var args_it = os.args();
10+
var args_it = process.args();
1011
const exe = try unwrapArg(args_it.next(allocator).?);
1112
var catted_anything = false;
1213
var stdout_file = try io.getStdOut();
@@ -20,7 +21,7 @@ pub fn main() !void {
2021
} else if (arg[0] == '-') {
2122
return usage(exe);
2223
} else {
23-
var file = os.File.openRead(arg) catch |err| {
24+
var file = File.openRead(arg) catch |err| {
2425
warn("Unable to open file: {}\n", @errorName(err));
2526
return err;
2627
};
@@ -41,7 +42,7 @@ fn usage(exe: []const u8) !void {
4142
return error.Invalid;
4243
}
4344

44-
fn cat_file(stdout: *os.File, file: *os.File) !void {
45+
fn cat_file(stdout: *File, file: *File) !void {
4546
var buf: [1024 * 4]u8 = undefined;
4647

4748
while (true) {

example/guess_number/main.zig

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const builtin = @import("builtin");
22
const std = @import("std");
33
const io = std.io;
44
const fmt = std.fmt;
5-
const os = std.os;
65

76
pub fn main() !void {
87
var stdout_file = try io.getStdOut();
@@ -11,7 +10,7 @@ pub fn main() !void {
1110
try stdout.print("Welcome to the Guess Number Game in Zig.\n");
1211

1312
var seed_bytes: [@sizeOf(u64)]u8 = undefined;
14-
os.getRandomBytes(seed_bytes[0..]) catch |err| {
13+
std.crypto.randomBytes(seed_bytes[0..]) catch |err| {
1514
std.debug.warn("unable to seed random number generator: {}", err);
1615
return err;
1716
};

example/hello_world/hello_libc.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ const c = @cImport({
55
});
66

77
export fn main(argc: c_int, argv: [*]?[*]u8) c_int {
8-
c.fprintf(c.stderr, c"Hello, world!\n");
8+
_ = c.fprintf(c.stderr, c"Hello, world!\n");
99
return 0;
1010
}

src-self-hosted/compilation.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ pub const Compilation = struct {
301301
InvalidUtf8,
302302
BadPathName,
303303
DeviceBusy,
304+
CurrentWorkingDirectoryUnlinked,
304305
};
305306

306307
pub const Event = union(enum) {

src-self-hosted/libc_installation.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub const LibCInstallation = struct {
182182
}
183183

184184
async fn findNativeIncludeDirLinux(self: *LibCInstallation, loop: *event.Loop) !void {
185-
const cc_exe = std.process.getEnvPosix("CC") orelse "cc";
185+
const cc_exe = std.os.getenv("CC") orelse "cc";
186186
const argv = []const []const u8{
187187
cc_exe,
188188
"-E",
@@ -392,7 +392,7 @@ pub const LibCInstallation = struct {
392392

393393
/// caller owns returned memory
394394
async fn ccPrintFileName(loop: *event.Loop, o_file: []const u8, want_dirname: bool) ![]u8 {
395-
const cc_exe = std.process.getEnvPosix("CC") orelse "cc";
395+
const cc_exe = std.os.getenv("CC") orelse "cc";
396396
const arg1 = try std.fmt.allocPrint(loop.allocator, "-print-file-name={}", o_file);
397397
defer loop.allocator.free(arg1);
398398
const argv = []const []const u8{ cc_exe, arg1 };
@@ -463,7 +463,7 @@ fn fileExists(path: []const u8) !bool {
463463
if (fs.File.access(path)) |_| {
464464
return true;
465465
} else |err| switch (err) {
466-
error.FileNotFound, error.PermissionDenied => return false,
466+
error.FileNotFound => return false,
467467
else => return error.FileSystem,
468468
}
469469
}

src-self-hosted/main.zig

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ const FmtError = error{
702702
ReadOnlyFileSystem,
703703
LinkQuotaExceeded,
704704
FileBusy,
705+
CurrentWorkingDirectoryUnlinked,
705706
} || fs.File.OpenError;
706707

707708
async fn asyncFmtMain(
@@ -851,7 +852,7 @@ fn cmdTargets(allocator: *Allocator, args: []const []const u8) !void {
851852
}
852853

853854
fn cmdVersion(allocator: *Allocator, args: []const []const u8) !void {
854-
try stdout.print("{}\n", std.cstr.toSliceConst(c.ZIG_VERSION_STRING));
855+
try stdout.print("{}\n", std.mem.toSliceConst(u8, c.ZIG_VERSION_STRING));
855856
}
856857

857858
const args_test_spec = []Flag{Flag.Bool("--help")};
@@ -924,14 +925,14 @@ fn cmdInternalBuildInfo(allocator: *Allocator, args: []const []const u8) !void {
924925
\\ZIG_DIA_GUIDS_LIB {}
925926
\\
926927
,
927-
std.cstr.toSliceConst(c.ZIG_CMAKE_BINARY_DIR),
928-
std.cstr.toSliceConst(c.ZIG_CXX_COMPILER),
929-
std.cstr.toSliceConst(c.ZIG_LLVM_CONFIG_EXE),
930-
std.cstr.toSliceConst(c.ZIG_LLD_INCLUDE_PATH),
931-
std.cstr.toSliceConst(c.ZIG_LLD_LIBRARIES),
932-
std.cstr.toSliceConst(c.ZIG_STD_FILES),
933-
std.cstr.toSliceConst(c.ZIG_C_HEADER_FILES),
934-
std.cstr.toSliceConst(c.ZIG_DIA_GUIDS_LIB),
928+
std.mem.toSliceConst(u8, c.ZIG_CMAKE_BINARY_DIR),
929+
std.mem.toSliceConst(u8, c.ZIG_CXX_COMPILER),
930+
std.mem.toSliceConst(u8, c.ZIG_LLVM_CONFIG_EXE),
931+
std.mem.toSliceConst(u8, c.ZIG_LLD_INCLUDE_PATH),
932+
std.mem.toSliceConst(u8, c.ZIG_LLD_LIBRARIES),
933+
std.mem.toSliceConst(u8, c.ZIG_STD_FILES),
934+
std.mem.toSliceConst(u8, c.ZIG_C_HEADER_FILES),
935+
std.mem.toSliceConst(u8, c.ZIG_DIA_GUIDS_LIB),
935936
);
936937
}
937938

std/c.zig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ pub extern "c" fn open(path: [*]const u8, oflag: c_uint, ...) c_int;
3939
pub extern "c" fn raise(sig: c_int) c_int;
4040
pub extern "c" fn read(fd: fd_t, buf: [*]u8, nbyte: usize) isize;
4141
pub extern "c" fn pread(fd: fd_t, buf: [*]u8, nbyte: usize, offset: u64) isize;
42-
pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize;
43-
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec, iovcnt: c_int, offset: usize) isize;
42+
pub extern "c" fn preadv(fd: c_int, iov: [*]const iovec, iovcnt: c_uint, offset: usize) isize;
43+
pub extern "c" fn pwritev(fd: c_int, iov: [*]const iovec_const, iovcnt: c_uint, offset: usize) isize;
4444
pub extern "c" fn stat(noalias path: [*]const u8, noalias buf: *Stat) c_int;
4545
pub extern "c" fn write(fd: fd_t, buf: [*]const u8, nbyte: usize) isize;
4646
pub extern "c" fn pwrite(fd: fd_t, buf: [*]const u8, nbyte: usize, offset: u64) isize;
@@ -49,7 +49,7 @@ pub extern "c" fn munmap(addr: *align(page_size) c_void, len: usize) c_int;
4949
pub extern "c" fn mprotect(addr: *align(page_size) c_void, len: usize, prot: c_uint) c_int;
5050
pub extern "c" fn unlink(path: [*]const u8) c_int;
5151
pub extern "c" fn getcwd(buf: [*]u8, size: usize) ?[*]u8;
52-
pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_int, options: c_int) c_int;
52+
pub extern "c" fn waitpid(pid: c_int, stat_loc: *c_uint, options: c_uint) c_int;
5353
pub extern "c" fn fork() c_int;
5454
pub extern "c" fn access(path: [*]const u8, mode: c_uint) c_int;
5555
pub extern "c" fn pipe(fds: *[2]fd_t) c_int;
@@ -76,7 +76,12 @@ pub extern "c" fn sysctlbyname(name: [*]const u8, oldp: ?*c_void, oldlenp: ?*usi
7676
pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usize) c_int;
7777

7878
pub extern "c" fn bind(socket: fd_t, address: ?*const sockaddr, address_len: socklen_t) c_int;
79-
pub extern "c" fn socket(domain: c_int, sock_type: c_int, protocol: c_int) c_int;
79+
pub extern "c" fn socket(domain: c_uint, sock_type: c_uint, protocol: c_uint) c_int;
80+
pub extern "c" fn listen(sockfd: fd_t, backlog: c_uint) c_int;
81+
pub extern "c" fn getsockname(sockfd: fd_t, noalias addr: *sockaddr, noalias addrlen: *socklen_t) c_int;
82+
pub extern "c" fn connect(sockfd: fd_t, sock_addr: *const sockaddr, addrlen: socklen_t) c_int;
83+
pub extern "c" fn accept4(sockfd: fd_t, addr: *sockaddr, addrlen: *socklen_t, flags: c_uint) c_int;
84+
pub extern "c" fn getsockopt(sockfd: fd_t, level: c_int, optname: c_int, optval: *c_void, optlen: *socklen_t) c_int;
8085
pub extern "c" fn kill(pid: pid_t, sig: c_int) c_int;
8186
pub extern "c" fn getdirentries(fd: fd_t, buf_ptr: [*]u8, nbytes: usize, basep: *i64) isize;
8287
pub extern "c" fn openat(fd: c_int, path: [*]const u8, flags: c_int) c_int;

std/c/linux.zig

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
const std = @import("../std.zig");
22
use std.c;
33

4-
pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
5-
pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
64
extern "c" fn __errno_location() *c_int;
75
pub const _errno = __errno_location;
86

7+
pub extern "c" fn getrandom(buf_ptr: [*]u8, buf_len: usize, flags: c_uint) c_int;
8+
pub extern "c" fn sched_getaffinity(pid: c_int, size: usize, set: *cpu_set_t) c_int;
9+
pub extern "c" fn eventfd(initval: c_uint, flags: c_uint) c_int;
10+
pub extern "c" fn epoll_ctl(epfd: fd_t, op: c_uint, fd: fd_t, event: *epoll_event) c_int;
11+
pub extern "c" fn epoll_create1(flags: c_uint) c_int;
12+
pub extern "c" fn epoll_wait(epfd: fd_t, events: [*]epoll_event, maxevents: c_uint, timeout: c_int) c_int;
13+
pub extern "c" fn epoll_pwait(
14+
epfd: fd_t,
15+
events: [*]epoll_event,
16+
maxevents: c_int,
17+
timeout: c_int,
18+
sigmask: *const sigset_t,
19+
) c_int;
20+
pub extern "c" fn inotify_init1(flags: c_uint) c_int;
21+
pub extern "c" fn inotify_add_watch(fd: fd_t, pathname: [*]const u8, mask: u32) c_int;
22+
923
/// See std.elf for constants for this
10-
pub extern fn getauxval(__type: c_ulong) c_ulong;
24+
pub extern "c" fn getauxval(__type: c_ulong) c_ulong;
1125

1226
pub const dl_iterate_phdr_callback = extern fn (info: *dl_phdr_info, size: usize, data: ?*c_void) c_int;
13-
pub extern fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;
27+
pub extern "c" fn dl_iterate_phdr(callback: dl_iterate_phdr_callback, data: ?*c_void) c_int;

std/child_process.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ pub const ChildProcess = struct {
5454
os.ChangeCurDirError || windows.CreateProcessError;
5555

5656
pub const Term = union(enum) {
57-
Exited: i32,
58-
Signal: i32,
59-
Stopped: i32,
60-
Unknown: i32,
57+
Exited: u32,
58+
Signal: u32,
59+
Stopped: u32,
60+
Unknown: u32,
6161
};
6262

6363
pub const StdIo = enum {
@@ -155,7 +155,7 @@ pub const ChildProcess = struct {
155155
}
156156

157157
pub const ExecResult = struct {
158-
term: os.ChildProcess.Term,
158+
term: Term,
159159
stdout: []u8,
160160
stderr: []u8,
161161
};
@@ -224,7 +224,7 @@ pub const ChildProcess = struct {
224224
if (windows.GetExitCodeProcess(self.handle, &exit_code) == 0) {
225225
break :x Term{ .Unknown = 0 };
226226
} else {
227-
break :x Term{ .Exited = @bitCast(i32, exit_code) };
227+
break :x Term{ .Exited = exit_code };
228228
}
229229
});
230230

@@ -240,7 +240,7 @@ pub const ChildProcess = struct {
240240
self.handleWaitResult(status);
241241
}
242242

243-
fn handleWaitResult(self: *ChildProcess, status: i32) void {
243+
fn handleWaitResult(self: *ChildProcess, status: u32) void {
244244
self.term = self.cleanupAfterWait(status);
245245
}
246246

@@ -259,7 +259,7 @@ pub const ChildProcess = struct {
259259
}
260260
}
261261

262-
fn cleanupAfterWait(self: *ChildProcess, status: i32) !Term {
262+
fn cleanupAfterWait(self: *ChildProcess, status: u32) !Term {
263263
defer {
264264
os.close(self.err_pipe[0]);
265265
os.close(self.err_pipe[1]);
@@ -281,7 +281,7 @@ pub const ChildProcess = struct {
281281
return statusToTerm(status);
282282
}
283283

284-
fn statusToTerm(status: i32) Term {
284+
fn statusToTerm(status: u32) Term {
285285
return if (os.WIFEXITED(status))
286286
Term{ .Exited = os.WEXITSTATUS(status) }
287287
else if (os.WIFSIGNALED(status))

std/cstr.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test "cstr fns" {
2828

2929
fn testCStrFnsImpl() void {
3030
testing.expect(cmp(c"aoeu", c"aoez") == -1);
31-
testing.expect(len(c"123456789") == 9);
31+
testing.expect(mem.len(u8, c"123456789") == 9);
3232
}
3333

3434
/// Returns a mutable slice with 1 more byte of length which is a null byte.

std/dynamic_library.zig

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ const os = std.os;
66
const assert = std.debug.assert;
77
const testing = std.testing;
88
const elf = std.elf;
9-
const windows = os.windows;
10-
const win_util = @import("os/windows/util.zig");
9+
const windows = std.os.windows;
1110
const maxInt = std.math.maxInt;
1211

1312
pub const DynLib = switch (builtin.os) {
@@ -102,39 +101,35 @@ pub fn linkmap_iterator(phdrs: []elf.Phdr) !LinkMap.Iterator {
102101
pub const LinuxDynLib = struct {
103102
elf_lib: ElfLib,
104103
fd: i32,
105-
map_addr: usize,
106-
map_size: usize,
104+
memory: []align(mem.page_size) u8,
107105

108106
/// Trusts the file
109107
pub fn open(allocator: *mem.Allocator, path: []const u8) !DynLib {
110108
const fd = try os.open(path, 0, os.O_RDONLY | os.O_CLOEXEC);
111-
errdefer std.os.close(fd);
109+
errdefer os.close(fd);
112110

113-
const size = @intCast(usize, (try std.os.posixFStat(fd)).size);
111+
const size = @intCast(usize, (try os.fstat(fd)).size);
114112

115-
const addr = os.mmap(
113+
const bytes = try os.mmap(
116114
null,
117115
size,
118116
os.PROT_READ | os.PROT_EXEC,
119117
os.MAP_PRIVATE | os.MAP_LOCKED,
120118
fd,
121119
0,
122120
);
123-
errdefer os.munmap(addr, size);
124-
125-
const bytes = @intToPtr([*]align(mem.page_size) u8, addr)[0..size];
121+
errdefer os.munmap(bytes);
126122

127123
return DynLib{
128124
.elf_lib = try ElfLib.init(bytes),
129125
.fd = fd,
130-
.map_addr = addr,
131-
.map_size = size,
126+
.memory = bytes,
132127
};
133128
}
134129

135130
pub fn close(self: *DynLib) void {
136-
os.munmap(self.map_addr, self.map_size);
137-
std.os.close(self.fd);
131+
os.munmap(self.memory);
132+
os.close(self.fd);
138133
self.* = undefined;
139134
}
140135

@@ -253,28 +248,21 @@ pub const WindowsDynLib = struct {
253248
dll: windows.HMODULE,
254249

255250
pub fn open(allocator: *mem.Allocator, path: []const u8) !WindowsDynLib {
256-
const wpath = try win_util.sliceToPrefixedFileW(path);
251+
const wpath = try windows.sliceToPrefixedFileW(path);
257252

258253
return WindowsDynLib{
259254
.allocator = allocator,
260-
.dll = windows.LoadLibraryW(&wpath) orelse {
261-
switch (windows.GetLastError()) {
262-
windows.ERROR.FILE_NOT_FOUND => return error.FileNotFound,
263-
windows.ERROR.PATH_NOT_FOUND => return error.FileNotFound,
264-
windows.ERROR.MOD_NOT_FOUND => return error.FileNotFound,
265-
else => |err| return windows.unexpectedError(err),
266-
}
267-
},
255+
.dll = try windows.LoadLibraryW(&wpath),
268256
};
269257
}
270258

271259
pub fn close(self: *WindowsDynLib) void {
272-
assert(windows.FreeLibrary(self.dll) != 0);
260+
windows.FreeLibrary(self.dll);
273261
self.* = undefined;
274262
}
275263

276264
pub fn lookup(self: *WindowsDynLib, name: []const u8) ?usize {
277-
return @ptrToInt(windows.GetProcAddress(self.dll, name.ptr));
265+
return @ptrToInt(windows.kernel32.GetProcAddress(self.dll, name.ptr));
278266
}
279267
};
280268

0 commit comments

Comments
 (0)