-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Put eol() in common * Add assert_same_exit_code() to TestRig * Initial users util * Return in sorted order * Move pointer indexing into unsafe block * Fix test for Windows * Add more detail for some errors to TestRig * v fmt * Nicer small diff reporting in same_results * Correct call to eprintln_small_diff * No eol if empty result returned * Fix quotes in extra operand error
- Loading branch information
Showing
9 changed files
with
143 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import common | ||
|
||
pub struct C.utmpx { | ||
ut_type i16 // Type of login. | ||
ut_pid int // Process ID of login process. | ||
ut_line &char // Devicename. | ||
ut_id &char // Inittab ID. | ||
ut_user &char // Username. | ||
} | ||
|
||
fn utmp_users(filename &char) []string { | ||
mut utmp_buf := []C.utmpx{} | ||
mut users := []string{} | ||
common.read_utmp(filename, mut utmp_buf, .user_process) | ||
unsafe { | ||
for u in utmp_buf { | ||
users << cstring_to_vstring(u.ut_user) | ||
} | ||
} | ||
// Obtain sorted order as GNU coreutils | ||
users.sort() | ||
return users | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import common | ||
import os | ||
|
||
const app = common.CoreutilInfo{ | ||
name: 'users' | ||
description: 'print login names of users currently logged in' | ||
} | ||
|
||
// Settings for Utility: users | ||
struct Settings { | ||
mut: | ||
input_file &char = ''.str | ||
} | ||
|
||
fn users(settings Settings) { | ||
users := utmp_users(settings.input_file).join(' ') | ||
print(users) | ||
if users != '' { | ||
print(common.eol()) | ||
} | ||
} | ||
|
||
fn args() Settings { | ||
mut fp := app.make_flag_parser(os.args) | ||
mut st := Settings{} | ||
mut rem_pars := fp.remaining_parameters() | ||
if rem_pars.len == 0 { | ||
st.input_file = common.utmp_file_charptr | ||
} else if rem_pars.len == 1 { | ||
st.input_file = rem_pars[0].str | ||
} else if rem_pars.len > 1 { | ||
app.quit(message: "extra operand '${rem_pars[1]}'", show_help_advice: true) | ||
} | ||
return st | ||
} | ||
|
||
fn main() { | ||
users(args()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import common | ||
import common.testing | ||
|
||
const supported_platform = $if windows { | ||
false | ||
} $else { | ||
true | ||
} // No utmp in Windows | ||
const rig = testing.prepare_rig(util: 'users', is_supported_platform: supported_platform) | ||
|
||
fn testsuite_begin() { | ||
rig.assert_platform_util() | ||
} | ||
|
||
fn test_help_and_version() { | ||
if !supported_platform { | ||
return | ||
} | ||
rig.assert_help_and_version_options_work() | ||
} | ||
|
||
fn test_compare() { | ||
if !supported_platform { | ||
return | ||
} | ||
rig.assert_same_results('') | ||
rig.assert_same_results('does_not_exist') | ||
// // Don't even try to compile this for Windows | ||
$if !windows { | ||
unsafe { rig.assert_same_results(cstring_to_vstring(common.utmp_file_charptr)) } | ||
unsafe { rig.assert_same_results(cstring_to_vstring(common.wtmp_file_charptr)) } | ||
} | ||
} | ||
|
||
fn test_call_errors() { | ||
if !supported_platform { | ||
return | ||
} | ||
rig.assert_same_exit_code('-x') | ||
rig.assert_same_results('a b c') | ||
} |