1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

os: add fn user_names() (#14424)

This commit is contained in:
Adam Oates 2022-05-18 10:37:34 +00:00 committed by GitHub
parent 417a6dc506
commit a786c58d0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -343,6 +343,28 @@ pub fn user_os() string {
return 'unknown' return 'unknown'
} }
// user_names returns an array of the name of every user on the system.
pub fn user_names() ?[]string {
$if windows {
result := execute('wmic useraccount get name')
if result.exit_code != 0 {
return error('Failed to get user names. Exited with code $result.exit_code: $result.output')
}
mut users := result.output.split_into_lines()
// windows command prints an empty line at the end of output
users.delete(users.len - 1)
return users
} $else {
lines := read_lines('/etc/passwd')?
mut users := []string{cap: lines.len}
for line in lines {
end_name := line.index(':') or { line.len }
users << line[0..end_name]
}
return users
}
}
// home_dir returns path to the user's home directory. // home_dir returns path to the user's home directory.
pub fn home_dir() string { pub fn home_dir() string {
$if windows { $if windows {