Skip to content

Commit

Permalink
getlogin in (liii os) and fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Aug 9, 2024
1 parent 43d0f79 commit 3749b6e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
7 changes: 6 additions & 1 deletion goldfish/liii/os.scm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
(define-library (liii os)
(export
os-call os-arch os-type os-windows? os-linux? os-macos? os-temp-dir
isdir mkdir rmdir getenv getcwd listdir access)
isdir mkdir rmdir getenv getcwd listdir access getlogin)
(import (scheme process-context)
(liii error))
(begin
Expand Down Expand Up @@ -82,5 +82,10 @@
(define (getcwd)
(g_getcwd))

(define (getlogin)
(if (os-windows?)
(getenv "USERNAME")
(g_getlogin)))

) ; end of begin
) ; end of define-library
19 changes: 18 additions & 1 deletion src/goldfish.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifdef TB_CONFIG_OS_WINDOWS
#include <io.h>
#else
#include <pwd.h>
#include <unistd.h>
#endif

Expand Down Expand Up @@ -276,11 +277,22 @@ f_access (s7_scheme* sc, s7_pointer args) {
#ifdef TB_CONFIG_OS_WINDOWS
bool ret= (_access (path_c, mode) == 0);
#else
bool ret= (access (path_c, mode) == 0);
bool ret= (access (path_c, mode) == 0);
#endif
return s7_make_boolean (sc, ret);
}

static s7_pointer
f_getlogin (s7_scheme* sc, s7_pointer args) {
#ifdef TB_CONFIG_OS_WINDOWS
return s7_make_boolean (sc, false);
#else
uid_t uid= getuid ();
struct passwd* pwd= getpwuid (uid);
return s7_make_string (sc, pwd->pw_name);
#endif
}

inline void
glue_liii_os (s7_scheme* sc) {
s7_pointer cur_env= s7_curlet (sc);
Expand All @@ -303,6 +315,8 @@ glue_liii_os (s7_scheme* sc) {
const char* d_getcwd = "(g_getcwd) => string";
const char* s_access = "g_access";
const char* d_access = "(g_access string integer) => boolean";
const char* s_getlogin = "g_getlogin";
const char* d_getlogin = "(g_getlogin) => string";

s7_define (sc, cur_env, s7_make_symbol (sc, s_os_type),
s7_make_typed_function (sc, s_os_type, f_os_type, 0, 0, false,
Expand Down Expand Up @@ -331,6 +345,9 @@ glue_liii_os (s7_scheme* sc) {
s7_define (sc, cur_env, s7_make_symbol (sc, s_access),
s7_make_typed_function (sc, s_access, f_access, 2, 0, false,
d_access, NULL));
s7_define (sc, cur_env, s7_make_symbol (sc, s_getlogin),
s7_make_typed_function (sc, s_getlogin, f_getlogin, 0, 0, false,
d_access, NULL));
}

static s7_pointer
Expand Down
10 changes: 4 additions & 6 deletions tests/scheme/boot-test.scm
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
; under the License.
;

(import (srfi srfi-78)
(srfi srfi-1)
(import (liii list)
(liii check)
(liii os))

Expand All @@ -25,13 +24,13 @@
(check (file-exists? "/tmp") => #t)
(check (file-exists? "/not_exists") => #f))

(when (and (os-linux?) (not (string=? "/root" (getenv "HOME"))))
(when (and (os-linux?) (not (string=? "root" (getlogin))))
(check-catch 'permission-error (lambda () (file-exists? "/root"))))

(when (os-windows?)
(check (file-exists? "C:") => #t))

(when (and (os-linux?) (not (string=? "/root" (getenv "HOME"))))
(when (and (os-linux?) (not (string=? "root" (getlogin))))
(check-catch 'permission-error (lambda () (delete-file "/root"))))

(when (not (os-windows?))
Expand All @@ -42,5 +41,4 @@
(delete-file "/tmp/test_delete_file")
(check (file-exists? "/tmp/test_delete_file") => #f))

(check-report)
(if (check-failed?) (exit -1))
(check-report "\n\nCheck report of boot-test.scm => ")

0 comments on commit 3749b6e

Please sign in to comment.