Use std.mem.span + remove useless dupeZ()

Signed-off-by: AnErrupTion <anerruption@disroot.org>
This commit is contained in:
AnErrupTion
2025-08-30 13:17:19 +02:00
parent 36e220e2ff
commit 5924db58e1
3 changed files with 18 additions and 18 deletions

View File

@@ -305,7 +305,7 @@ fn getXPid(display_num: u8) !i32 {
return std.fmt.parseInt(i32, std.mem.trim(u8, buffer[0..written], " "), 10); return std.fmt.parseInt(i32, std.mem.trim(u8, buffer[0..written], " "), 10);
} }
fn createXauthFile(pwd: [:0]const u8) ![]const u8 { fn createXauthFile(pwd: []const u8) ![]const u8 {
var xauth_buf: [100]u8 = undefined; var xauth_buf: [100]u8 = undefined;
var xauth_dir: []const u8 = undefined; var xauth_dir: []const u8 = undefined;
const xdg_rt_dir = std.posix.getenv("XDG_RUNTIME_DIR"); const xdg_rt_dir = std.posix.getenv("XDG_RUNTIME_DIR");
@@ -365,11 +365,8 @@ fn mcookie() [Md5.digest_length * 2]u8 {
return std.fmt.bytesToHex(&out, .lower); return std.fmt.bytesToHex(&out, .lower);
} }
fn xauth(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, display_name: []u8, shell: [*:0]const u8, pw_dir: [*:0]const u8, options: AuthOptions) !void { fn xauth(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, display_name: []u8, shell: [*:0]const u8, home: []const u8, options: AuthOptions) !void {
var pwd_buf: [100]u8 = undefined; const xauthority = try createXauthFile(home);
const pwd = try std.fmt.bufPrintZ(&pwd_buf, "{s}", .{pw_dir});
const xauthority = try createXauthFile(pwd);
try interop.setEnvironmentVariable(allocator, "XAUTHORITY", xauthority, true); try interop.setEnvironmentVariable(allocator, "XAUTHORITY", xauthority, true);
try interop.setEnvironmentVariable(allocator, "DISPLAY", display_name, true); try interop.setEnvironmentVariable(allocator, "DISPLAY", display_name, true);
@@ -379,6 +376,7 @@ fn xauth(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, display_name:
if (pid == 0) { if (pid == 0) {
var cmd_buffer: [1024]u8 = undefined; var cmd_buffer: [1024]u8 = undefined;
const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} add {s} . {s}", .{ options.xauth_cmd, display_name, magic_cookie }) catch std.process.exit(1); const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} add {s} . {s}", .{ options.xauth_cmd, display_name, magic_cookie }) catch std.process.exit(1);
const args = [_:null]?[*:0]const u8{ shell, "-c", cmd_str }; const args = [_:null]?[*:0]const u8{ shell, "-c", cmd_str };
std.posix.execveZ(shell, &args, std.c.environ) catch {}; std.posix.execveZ(shell, &args, std.c.environ) catch {};
std.process.exit(1); std.process.exit(1);
@@ -399,6 +397,7 @@ fn executeShellCmd(allocator: std.mem.Allocator, shell: []const u8, options: Aut
var cmd_buffer: [1024]u8 = undefined; var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", shell }); const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", shell });
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str }; const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
return std.posix.execveZ(shell_z, &args, std.c.environ); return std.posix.execveZ(shell_z, &args, std.c.environ);
} }
@@ -415,6 +414,7 @@ fn executeWaylandCmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, s
var cmd_buffer: [1024]u8 = undefined; var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", desktop_cmd }); const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", desktop_cmd });
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str }; const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
return std.posix.execveZ(shell_z, &args, std.c.environ); return std.posix.execveZ(shell_z, &args, std.c.environ);
} }
@@ -427,15 +427,13 @@ fn executeX11Cmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell
const shell_z = try allocator.dupeZ(u8, shell); const shell_z = try allocator.dupeZ(u8, shell);
defer allocator.free(shell_z); defer allocator.free(shell_z);
const home_z = try allocator.dupeZ(u8, home); try xauth(log_writer, allocator, display_name, shell_z, home, options);
defer allocator.free(home_z);
try xauth(log_writer, allocator, display_name, shell_z, home_z, options);
const pid = try std.posix.fork(); const pid = try std.posix.fork();
if (pid == 0) { if (pid == 0) {
var cmd_buffer: [1024]u8 = undefined; var cmd_buffer: [1024]u8 = undefined;
const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.x_cmd, display_name, vt }) catch std.process.exit(1); const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.x_cmd, display_name, vt }) catch std.process.exit(1);
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str }; const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
std.posix.execveZ(shell_z, &args, std.c.environ) catch {}; std.posix.execveZ(shell_z, &args, std.c.environ) catch {};
std.process.exit(1); std.process.exit(1);
@@ -459,6 +457,7 @@ fn executeX11Cmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, shell
if (xorg_pid == 0) { if (xorg_pid == 0) {
var cmd_buffer: [1024]u8 = undefined; var cmd_buffer: [1024]u8 = undefined;
const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", desktop_cmd }) catch std.process.exit(1); const cmd_str = std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", desktop_cmd }) catch std.process.exit(1);
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str }; const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
std.posix.execveZ(shell_z, &args, std.c.environ) catch {}; std.posix.execveZ(shell_z, &args, std.c.environ) catch {};
std.process.exit(1); std.process.exit(1);
@@ -500,6 +499,7 @@ fn executeCustomCmd(log_writer: *std.Io.Writer, allocator: std.mem.Allocator, sh
var cmd_buffer: [1024]u8 = undefined; var cmd_buffer: [1024]u8 = undefined;
const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", exec_cmd }); const cmd_str = try std.fmt.bufPrintZ(&cmd_buffer, "{s} {s} {s}", .{ options.setup_cmd, options.login_cmd orelse "", exec_cmd });
const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str }; const args = [_:null]?[*:0]const u8{ shell_z, "-c", cmd_str };
return std.posix.execveZ(shell_z, &args, std.c.environ); return std.posix.execveZ(shell_z, &args, std.c.environ);
} }

View File

@@ -187,7 +187,7 @@ pub fn setUserShell(entry: *UsernameEntry) void {
unistd.setusershell(); unistd.setusershell();
const shell = unistd.getusershell(); const shell = unistd.getusershell();
entry.shell = shell[0..std.mem.len(shell)]; entry.shell = std.mem.span(shell);
unistd.endusershell(); unistd.endusershell();
} }
@@ -213,11 +213,11 @@ pub fn getNextUsernameEntry() ?UsernameEntry {
if (entry == null) return null; if (entry == null) return null;
return .{ return .{
.username = if (entry.*.pw_name) |name| name[0..std.mem.len(name)] else null, .username = if (entry.*.pw_name) |name| std.mem.span(name) else null,
.uid = @intCast(entry.*.pw_uid), .uid = @intCast(entry.*.pw_uid),
.gid = @intCast(entry.*.pw_gid), .gid = @intCast(entry.*.pw_gid),
.home = if (entry.*.pw_dir) |dir| dir[0..std.mem.len(dir)] else null, .home = if (entry.*.pw_dir) |dir| std.mem.span(dir) else null,
.shell = if (entry.*.pw_shell) |shell| shell[0..std.mem.len(shell)] else null, .shell = if (entry.*.pw_shell) |shell| std.mem.span(shell) else null,
.passwd_struct = entry, .passwd_struct = entry,
}; };
} }
@@ -227,11 +227,11 @@ pub fn getUsernameEntry(username: [:0]const u8) ?UsernameEntry {
if (entry == null) return null; if (entry == null) return null;
return .{ return .{
.username = if (entry.*.pw_name) |name| name[0..std.mem.len(name)] else null, .username = if (entry.*.pw_name) |name| std.mem.span(name) else null,
.uid = @intCast(entry.*.pw_uid), .uid = @intCast(entry.*.pw_uid),
.gid = @intCast(entry.*.pw_gid), .gid = @intCast(entry.*.pw_gid),
.home = if (entry.*.pw_dir) |dir| dir[0..std.mem.len(dir)] else null, .home = if (entry.*.pw_dir) |dir| std.mem.span(dir) else null,
.shell = if (entry.*.pw_shell) |shell| shell[0..std.mem.len(shell)] else null, .shell = if (entry.*.pw_shell) |shell| std.mem.span(shell) else null,
.passwd_struct = entry, .passwd_struct = entry,
}; };
} }

View File

@@ -965,7 +965,7 @@ pub fn main() !void {
fn ttyClearScreen() !void { fn ttyClearScreen() !void {
// Clear the TTY because termbox2 doesn't seem to do it properly // Clear the TTY because termbox2 doesn't seem to do it properly
const capability = termbox.global.caps[termbox.TB_CAP_CLEAR_SCREEN]; const capability = termbox.global.caps[termbox.TB_CAP_CLEAR_SCREEN];
const capability_slice = capability[0..std.mem.len(capability)]; const capability_slice = std.mem.span(capability);
_ = try std.posix.write(termbox.global.ttyfd, capability_slice); _ = try std.posix.write(termbox.global.ttyfd, capability_slice);
} }