#how to get username cross-platform?
1 messages · Page 1 of 1 (latest)
easy way: std.process.getEnvVarOwned(gpa, "USER")
will returned the associated string, which you'll need to free with gpa once you're done using it
will this work on windows?
returns error.EnvironmentVariableNotFound in the event that USER isn't an environment variable
it has a codepath on windows so yes, though I don't recall if windows shells define USER
what does even set $USER
usually the shell
most shells, at least on linux, export a bunch of posix-standard variables such as USER and HOME and so on
and usually they get that information from some source of information on the system
I just opened a Windows cmd prompt, and it looks like there the env var is USERNAME
what about USER?
Not present
fair enough, so you'll probably want to either do a switch on the target os or have a fallback when the not found error returns
Instead of HOME, there's USERPROFILE
const username = std.process.getEnvVarOwned(alloc, "USER") catch try std.process.getEnvVarOwned(alloc, "USERNAME");
ie
// switch on os
const user = try std.process.getEnvVarOwned(gpa, switch (@import("builtin").target.os.tag) {
.windows => "USERNAME",
else => "USER",
});
defer gpa.free(user);
// fallback in catch
const user = std.process.getEnvVarOwned(gpa, "USER") catch |err| switch (err) {
error.EnvironmentVariableNotFound => try std.process.getEnvVarOwned(gpa, "USERNAME"),
else => |e| return e,
};
defer gpa.free(user);
yah - better using comptime condition on target
note: it doesn't only return environmentvariablenotfound, it can also return OutOfMemory and InvalidWtf8
so using a catch-all catch is not too prudent
I don;t recall it exactly, but on Widnows there's an API to call that is not dependent on an environment variable.
but then the second one will fail with the same reason
ok but does it really matter
yeah
you don't want to try again if the reason you failed is due to failing due to out of memory or invalid environment
the zig standard library browser is really nice
Windows: GetUserNameExW probably better
W for win
i can probably rely on USERNAME being defined on Windows more than USER being defined on posix OSes
No, it's for the Unicode version, which you want in case the user's name uses characters that aren't in the system codepage.
i know i was
There's also GetUserNameW
pub const HOST_NAME_MAX = switch (native_os) {
.linux => linux.HOST_NAME_MAX,
.macos, .ios, .tvos, .watchos, .visionos => 72,
.openbsd, .haiku, .dragonfly, .netbsd, .solaris, .illumos, .freebsd => 255,
// https://github.com/SerenityOS/serenity/blob/c87557e9c1865fa1a6440de34ff6ce6fc858a2b7/Kernel/API/POSIX/sys/limits.h#L22
.serenity => 64,
else => {},
};
this will be what on windows?
Aren't length of host names established by IETF?
Yes, it's RFC1035: https://datatracker.ietf.org/doc/html/rfc1035
This RFC is the revised specification of the protocol and format used in the implementation of the Domain Name System. It obsoletes RFC-883. This memo documents the details of the domain name client - server communication.
thats from stdlib