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

os: add os.uname()

This commit is contained in:
Mike 'Fuzzy' Partin 2020-06-14 06:46:30 -07:00 committed by GitHub
parent 2ef0f15b34
commit 206e64d72c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 1 deletions

View File

@ -1358,3 +1358,12 @@ pub fn create(path string) ?File {
opened: true
}
}
pub struct Uname {
pub mut:
sysname string
nodename string
release string
version string
machine string
}

View File

@ -5,6 +5,7 @@ import strings
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>
pub const (
path_separator = '/'
@ -17,8 +18,32 @@ const (
stderr_value = 2
)
struct C.utsname {
mut:
sysname charptr
nodename charptr
release charptr
version charptr
machine charptr
}
fn C.uname(name voidptr) int
fn C.symlink(charptr, charptr) int
pub fn uname() Uname {
mut u := Uname{}
d := &C.utsname( malloc(int(sizeof(C.utsname))) )
if C.uname(d) == 0 {
u.sysname = cstring_to_vstring(d.sysname)
u.nodename = cstring_to_vstring(d.nodename)
u.release = cstring_to_vstring(d.release)
u.version = cstring_to_vstring(d.version)
u.machine = cstring_to_vstring(d.machine)
}
free(d)
return u
}
fn init_os_args(argc int, argv &&byte) []string {
mut args := []string{}
//mut args := []string(make(0, argc, sizeof(string)))

View File

@ -343,3 +343,12 @@ fn test_basedir() {
}
assert os.base_dir('filename') == 'filename'
}
fn test_uname() {
u := os.uname()
assert u.sysname.len > 0
assert u.nodename.len > 0
assert u.release.len > 0
assert u.version.len > 0
assert u.machine.len > 0
}

View File

@ -317,7 +317,7 @@ pub:
// status_ constants
code u32
flags u32
record &ExceptionRecord
address voidptr
param_count u32
@ -353,3 +353,15 @@ pub fn add_vectored_exception_handler(first bool, handler VectoredExceptionHandl
pub fn debugger_present() bool {
return C.IsDebuggerPresent()
}
pub fn uname() Uname {
// TODO: implement `os.uname()` for windows
unknown := 'unknown'
return Uname{
sysname: unknown
nodename: unknown
release: unknown
version: unknown
machine: unknown
}
}