Skip to content

Commit

Permalink
backmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
igrekus committed Jun 3, 2024
2 parents 952bbdf + 7c68314 commit 508b79b
Show file tree
Hide file tree
Showing 81 changed files with 4,444 additions and 289 deletions.
10 changes: 7 additions & 3 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* eol=lf
*.vsh linguist-language=V text=auto
v.mod linguist-language=Text
* text=auto eol=lf
*.bat eol=crlf

**/*.v linguist-language=V
**/*.vv linguist-language=V
**/*.vsh linguist-language=V
**/v.mod linguist-language=V
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ jobs:
run: cd coreutils && make testfmt
- name: Build all
run: cd coreutils && v run build.vsh
- name: Debug uptime_test.v
run: cd coreutils && v -d trace_same_results -stats src/uptime/uptime_test.v
- name: Debug uptime_nix_test.v
run: cd coreutils && v -d trace_same_results -stats src/uptime/uptime_nix_test.v
- name: Run tests
run: cd coreutils && make test

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ build
.idea/
*.iml
build
src/touch/touch

20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ count below and mark it as done in this README.md. Thanks!
GNU coreutils. They are not 100% compatiable. If you encounter different behaviors,
compare against the true GNU coreutils version on the Linux-based tests first.

## Completed (51/109)
## Completed (60/109)

| Done | Cmd | Descripton |
| :-----: | --------- | ------------------------------------------------ |
Expand All @@ -68,7 +68,7 @@ compare against the true GNU coreutils version on the Linux-based tests first.
| | coreutils | Multi-call program |
| ✓ | cp | Copy files and directories |
| | csplit | Split a file into context-determined pieces |
| | cut | Print selected parts of lines |
| ✓ | cut | Print selected parts of lines |
| | date | Print or set system date and time |
| | dd | Convert and copy a file |
| | df | Report file system disk space usage |
Expand All @@ -82,7 +82,7 @@ compare against the true GNU coreutils version on the Linux-based tests first.
| ✓ | expr | Evaluate expressions |
| ✓ | factor | Print prime factors |
| ✓ | false | Do nothing, unsuccessfully |
| | fmt | Reformat paragraph text |
| ✓ | fmt | Reformat paragraph text |
| ✓ | fold | Wrap input lines to fit in specified width |
| | groups | Print group names a user is in |
| ✓ | head | Output the first part of files |
Expand Down Expand Up @@ -116,7 +116,7 @@ compare against the true GNU coreutils version on the Linux-based tests first.
| ✓ | printf | Format and print data |
| | ptx | Produce permuted indexes |
| ✓ | pwd | Print working directory |
| | readlink | Print value of a symlink or canonical file name |
| ✓ | readlink | Print value of a symlink or canonical file name |
| | realpath | Print the resolved file name |
| ✓ | rm | Remove files or directories |
| ✓ | rmdir | Remove empty directories |
Expand All @@ -132,28 +132,28 @@ compare against the true GNU coreutils version on the Linux-based tests first.
| ✓ | sleep | Delay for a specified time |
| | sort | Sort text files |
| | split | Split a file into pieces |
| | stat | Report file or file system status |
| ✓ | stat | Report file or file system status |
| | stdbuf | Run a command with modified I/O stream buffering |
| | stty | Print or change terminal characteristics |
| | sum | Print checksum and block counts |
| ✓ | sum | Print checksum and block counts |
| ✓ | sync | Synchronize cached writes to persistent storage |
| ✓ | tac | Concatenate and write files in reverse |
| | tail | Output the last part of files |
| ✓ | tail | Output the last part of files |
| | tee | Redirect output to multiple files or processes |
| ✓ | test | Check file types and compare values |
| | timeout | Run a command with a time limit |
| | touch | Change file timestamps |
| ✓ | touch | Change file timestamps |
| | tr | Translate, squeeze, and/or delete characters |
| ✓ | true | Do nothing, successfully |
| ✓ | truncate | Shrink or extend the size of a file |
| | tsort | Topological sort |
| | tty | Print file name of terminal on standard input |
| ✓ | tty | Print file name of terminal on standard input |
| ✓ | uname | Print system information |
| | unexpand | Convert spaces to tabs |
| ✓ | uniq | Uniquify files |
| ✓ | unlink | Remove files via the unlink syscall |
| ✓ | uptime | Print system uptime and load |
| | users | Print login names of users currently logged in |
| ✓ | users | Print login names of users currently logged in |
| | vdir | Verbosely list directory contents |
| ✓ | wc | Print newline, word, and byte counts |
| | who | Print who is currently logged in |
Expand Down
16 changes: 10 additions & 6 deletions build.vsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@

import os // v has a bug that you can't use args

const ignore_dirs = $if windows {
[
const ignore_dirs = {
'windows': [
// avoid *nix-dependent utils
'nohup',
'stat',
'tty',
// avoid utmp-dependent utils (WinOS has no utmp support)
'uptime',
'users',
'who',
]
} $else {
[]string{}
}
'macos': ['stat', 'sync', 'uptime']
}[os.user_os()] or { [] }

dump(os.user_os())
dump(ignore_dirs)

vargs := if os.args.len > 1 { os.args[1..] } else { []string{} }

curdir := getwd()
chdir('src')!

dirs := ls('.')!.filter(is_dir(it))
dirs := ls('.')!.filter(is_dir(it)).sorted()

if !exists('${curdir}/bin') {
mkdir('${curdir}/bin')!
Expand Down
43 changes: 41 additions & 2 deletions common/common.v
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
module common

import flag
import os

pub const version = '0.0.1'
pub const err_programming_error = 0x7c
pub const err_not_implemented = 0x7d
pub const err_platform_not_supported = 0x7F

pub struct CoreutilInfo {
pub:
name string
description string
help string
}

pub struct CoreutilExitDetail {
pub:
message string
mut:
return_code int = 1
pub mut:
show_help_advice bool // defaults to false
return_code int = 1
}

// coreutils_version returns formatted coreutils tool version
Expand Down Expand Up @@ -72,15 +78,48 @@ pub fn (app CoreutilInfo) quit(detail CoreutilExitDetail) {
exit(detail.return_code)
}

// strip_error_code_from_msg strips the final semicolon and code
// ("<msg>; code: <code>") away from a POSIX and Win32 error to make
// it match what GNU coreutils return
pub fn strip_error_code_from_msg(msg string) string {
j := msg.last_index('; code: ') or { -1 }
if j > 0 {
return msg[0..j]
} else {
return msg
}
}

pub fn (app CoreutilInfo) eprintln(message string) {
eprintln('${app.name}: ${strip_error_code_from_msg(message)}')
}

pub fn (app CoreutilInfo) eprintln_posix(message string) {
app.eprintln('${message}: ${os.error_posix()}')
}

// flag_parser returns a flag.FlagParser, with the common
// options already set, reducing the boilerplate code in
// each individual utility.
pub fn (app CoreutilInfo) make_flag_parser(args []string) &flag.FlagParser {
mut fp := flag.new_flag_parser(args)
fp.version(coreutils_version())
if app.help != '' {
fp.footer(app.help)
}
fp.footer(coreutils_footer())
fp.skip_executable()
fp.application(app.name)
fp.description(app.description)
return fp
}

@[inline]
pub fn eol() string {
$if windows {
// WinOS => CRLF
return '\r\n'
}
// POSIX => LF
return '\n'
}
55 changes: 55 additions & 0 deletions common/pwd/pwd_nix.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module pwd

import os

#include <pwd.h>
#include <grp.h>

struct C.passwd {
pw_name &char
pw_passwd &char
pw_uid u32
pw_gid u32
pw_gecos &char
pw_dir &char
pw_shell &char
}

fn C.getpwuid(int) &C.passwd
fn C.getgrgid(int) &C.passwd

pub fn get_name_for_gid(gid int) !string {
pwd := C.getgrgid(gid)
unsafe {
if isnil(pwd) {
// Call succeeded but user not found
if C.errno == 0 {
return ''
}
return os.error_posix()
}
return cstring_to_vstring(pwd.pw_name)
}
}

pub fn get_name_for_uid(uid int) !string {
pwd := C.getpwuid(uid)
unsafe {
if isnil(pwd) {
// Call succeeded but user not found
if C.errno == 0 {
return ''
}
return os.error_posix()
}
return cstring_to_vstring(pwd.pw_name)
}
}

pub fn whoami() !string {
uid := os.geteuid()
if uid == -1 {
return error('no user name')
}
return get_name_for_uid(uid)
}
19 changes: 19 additions & 0 deletions common/pwd/pwd_windows.c.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module pwd

import os

pub fn get_name_for_gid(gid int) !string {
return error('Not supported on Windows')
}

pub fn get_name_for_uid(uid int) !string {
return error('Not supported on Windows')
}

pub fn whoami() !string {
username := os.loginname() or { '' }
if username == '' {
return error('no user name')
}
return username
}
5 changes: 4 additions & 1 deletion common/readutmp_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct C.timeval {

// <time.h>
pub struct C.timeval {
pub:
tv_sec u64 // Seconds.
tv_usec u64 // Microseconds.
}
Expand Down Expand Up @@ -55,7 +56,9 @@ pub enum ReadUtmpOptions {
// readutmp.h : IS_USER_PROCESS(U)
pub fn is_user_process(u &C.utmpx) bool {
// C.USER_PROCESS = 7
return !isnil(u.ut_user[0]) && u.ut_type == C.USER_PROCESS
unsafe {
return !isnil(u.ut_user[0]) && u.ut_type == C.USER_PROCESS
}
}

fn desirable_utmp_entry(u &C.utmpx, options ReadUtmpOptions) bool {
Expand Down
2 changes: 1 addition & 1 deletion common/sums/sums.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn sum(args []string, sum_name string, sum_type string, num_chars_in_sum int
warn := fp.bool('warn', `w`, false, '(only with -c) warn about improperly formatted checksum lines')

mut files := fp.finalize() or {
eprintln("${args[0]}: ${err.msg}\nTry '${args[0]} --help' for more information.")
eprintln("${args[0]}: ${err.msg()}\nTry '${args[0]} --help' for more information.")
exit(1)
}

Expand Down
14 changes: 5 additions & 9 deletions common/testing/testing.v
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module testing

import common
import os
import regex

Expand All @@ -13,7 +14,7 @@ struct DidNotFailError {
}

pub fn (err DidNotFailError) msg() string {
return err.msg
return err.msg()
}

pub fn (err DidNotFailError) code() int {
Expand All @@ -27,7 +28,7 @@ struct DoesNotWorkError {
}

pub fn (err DoesNotWorkError) msg() string {
return err.msg
return err.msg()
}

pub fn (err DoesNotWorkError) code() int {
Expand All @@ -41,7 +42,7 @@ struct ExitCodesDifferError {
}

pub fn (err ExitCodesDifferError) msg() string {
return err.msg
return err.msg()
}

pub fn (err ExitCodesDifferError) code() int {
Expand Down Expand Up @@ -225,10 +226,5 @@ pub fn check_dir_exists(d string) bool {
}

pub fn output_eol() string {
$if windows {
// WinOS => CRLF
return '\r\n'
}
// POSIX => LF
return '\n'
return common.eol()
}
Loading

0 comments on commit 508b79b

Please sign in to comment.