Something went wrong. Try again.
A GPU terminal emulator for Wayland and Windows on libghostty-vt, drawing with Vulkan, running a program on a pty or a serial port.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
//! This machine: its name, and the identity the OS gives it.
const std = @import("std");const builtin = @import("builtin");
/// This process's ID.pub fn pid() u32 { return switch (builtin.os.tag) { .windows => @import("win32").everything.GetCurrentProcessId(), else => @intCast(std.os.linux.getpid()), };}
/// This machine's name, as the network knows it and as `file:` URIs and/// the configuration's `ctx.hostname` name it; in `buf`, cut to fit, and/// empty if it cannot be had.pub fn name(buf: []u8) []const u8 { switch (builtin.os.tag) { .linux => { const linux = std.os.linux; var uts: linux.utsname = undefined; if (linux.errno(linux.uname(&uts)) != .SUCCESS) return ""; const n = std.mem.sliceTo(&uts.nodename, 0); const len = @min(n.len, buf.len); @memcpy(buf[0..len], n[0..len]); return buf[0..len]; }, .windows => { const win32 = @import("win32").everything; var wide: [256:0]u16 = undefined; var len: u32 = wide.len; if (win32.GetComputerNameExW(win32.ComputerNameDnsHostname, &wide, &len) == 0) return ""; return utf16ToUtf8(buf, wide[0..len]); }, else => return "", }}
/// The OS's identity for this machine, as text: `/etc/machine-id` on Linux,/// and on Windows the `MachineGuid` setup gives it. Null where there is/// none.pub fn machineId(buf: []u8) ?[]const u8 { switch (builtin.os.tag) { .linux => { const linux = std.os.linux; const fd = linux.open("/etc/machine-id", .{ .ACCMODE = .RDONLY, .CLOEXEC = true }, 0); if (linux.errno(fd) != .SUCCESS) return null; defer _ = linux.close(@intCast(fd)); const n = linux.read(@intCast(fd), buf.ptr, buf.len); if (linux.errno(n) != .SUCCESS or n == 0) return null; return buf[0..n]; }, .windows => { const win32 = @import("win32").everything; var wide: [64]u16 = undefined; var size: u32 = @sizeOf(@TypeOf(wide)); const rc = win32.RegGetValueW( win32.HKEY_LOCAL_MACHINE, std.unicode.utf8ToUtf16LeStringLiteral("SOFTWARE\\Microsoft\\Cryptography"), std.unicode.utf8ToUtf16LeStringLiteral("MachineGuid"), // The 64-bit view even from a 32-bit build, which would // otherwise be shown a copy without it. .{ .RT_REG_SZ = 1, .SUBKEY_WOW6464KEY = 1 }, null, &wide, &size, ); if (rc != .NO_ERROR) return null; // The size counts the terminating zero. const chars = (size / 2) -| 1; const out = utf16ToUtf8(buf, wide[0..chars]); return if (out.len == 0) null else out; }, else => return null, }}
fn utf16ToUtf8(buf: []u8, wide: []const u16) []const u8 { var out: std.Io.Writer = .fixed(buf); var it = std.unicode.Utf16LeIterator.init(wide); while (it.nextCodepoint() catch return "") |cp| { var enc: [4]u8 = undefined; const n = std.unicode.utf8Encode(cp, &enc) catch return ""; out.writeAll(enc[0..n]) catch break; } return out.buffered();}
test "a name, short or long" { var buf: [256]u8 = undefined; _ = name(&buf); var small: [2]u8 = undefined; try std.testing.expect(name(&small).len <= 2);}