6262#include <process.h>
6363#define JANET_SPAWN_CHDIR
6464#else
65+ #ifndef JANET_PLAN9
6566#include <spawn.h>
67+ #include <sys/wait.h>
68+ #endif
6669#include <utime.h>
6770#include <unistd.h>
6871#include <dirent.h>
6972#include <sys/types.h>
70- #include <sys/wait.h>
7173#ifdef JANET_APPLE
7274#include <crt_externs.h>
7375#define environ (*_NSGetEnviron())
@@ -293,20 +295,7 @@ JANET_CORE_FN(os_exit,
293295 return janet_wrap_nil ();
294296}
295297
296- #ifdef JANET_PLAN9
297-
298- JANET_CORE_FN (os_isatty ,
299- "(os/isatty &opt file)" ,
300- "Returns true if `file` is a terminal. If `file` is not specified, "
301- "it will default to standard output." ) {
302- janet_arity (argc , 0 , 1 );
303- FILE * f = (argc == 1 ) ? janet_getfile (argv , 0 , NULL ) : stdout ;
304- int fd = fileno (f );
305- if (fd == -1 ) janet_panic (janet_strerror (errno ));
306- return janet_wrap_boolean (isatty (fd ));
307- }
308-
309- #elif !defined(JANET_REDUCED_OS )
298+ #ifndef JANET_REDUCED_OS
310299
311300JANET_CORE_FN (os_cpu_count ,
312301 "(os/cpu-count &opt dflt)" ,
@@ -1621,6 +1610,7 @@ JANET_CORE_FN(os_shell,
16211610
16221611#endif /* JANET_NO_PROCESSES */
16231612
1613+ #ifndef JANET_PLAN9
16241614JANET_CORE_FN (os_environ ,
16251615 "(os/environ)" ,
16261616 "Get a copy of the OS environment table." ) {
@@ -1652,6 +1642,7 @@ JANET_CORE_FN(os_environ,
16521642 janet_unlock_environ ();
16531643 return janet_wrap_table (t );
16541644}
1645+ #endif
16551646
16561647JANET_CORE_FN (os_getenv ,
16571648 "(os/getenv variable &opt dflt)" ,
@@ -1676,6 +1667,9 @@ JANET_CORE_FN(os_setenv,
16761667#ifdef JANET_WINDOWS
16771668#define SETENV (K ,V ) _putenv_s(K, V)
16781669#define UNSETENV (K ) _putenv_s(K, "")
1670+ #elif defined(JANET_PLAN9 )
1671+ #define SETENV (K ,V ) putenv(K, V)
1672+ #define UNSETENV (K ) unsetenv(K)
16791673#else
16801674#define SETENV (K ,V ) setenv(K, V, 1)
16811675#define UNSETENV (K ) unsetenv(K)
@@ -1848,6 +1842,8 @@ static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct
18481842 _tzset ();
18491843 localtime_s (t_infos , & t );
18501844 t_info = t_infos ;
1845+ #elif defined(JANET_PLAN9 )
1846+ t_info = localtime (& t );
18511847#else
18521848 tzset ();
18531849 t_info = localtime_r (& t , t_infos );
@@ -1857,6 +1853,8 @@ static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct
18571853#ifdef JANET_WINDOWS
18581854 gmtime_s (t_infos , & t );
18591855 t_info = t_infos ;
1856+ #elif defined(JANET_PLAN9 )
1857+ t_info = gmtime (& t );
18601858#else
18611859 t_info = gmtime_r (& t , t_infos );
18621860#endif
@@ -2031,6 +2029,7 @@ JANET_CORE_FN(os_mktime,
20312029#define j_symlink symlink
20322030#endif
20332031
2032+ #ifndef JANET_NO_LOCALES
20342033JANET_CORE_FN (os_setlocale ,
20352034 "(os/setlocale &opt locale category)" ,
20362035 "Set the system locale, which affects how dates and numbers are formatted. "
@@ -2067,19 +2066,20 @@ JANET_CORE_FN(os_setlocale,
20672066 if (old == NULL ) return janet_wrap_nil ();
20682067 return janet_cstringv (old );
20692068}
2069+ #endif
20702070
20712071JANET_CORE_FN (os_link ,
20722072 "(os/link oldpath newpath &opt symlink)" ,
20732073 "Create a link at newpath that points to oldpath and returns nil. "
20742074 "Iff symlink is truthy, creates a symlink. "
20752075 "Iff symlink is falsey or not provided, "
2076- "creates a hard link. Does not work on Windows." ) {
2076+ "creates a hard link. Does not work on Windows or Plan 9 ." ) {
20772077 janet_sandbox_assert (JANET_SANDBOX_FS_WRITE );
20782078 janet_arity (argc , 2 , 3 );
2079- #ifdef JANET_WINDOWS
2079+ #if defined( JANET_WINDOWS ) || defined( JANET_PLAN9 )
20802080 (void ) argc ;
20812081 (void ) argv ;
2082- janet_panic ("not supported on Windows" );
2082+ janet_panic ("not supported on Windows or Plan 9 " );
20832083#else
20842084 const char * oldpath = janet_getcstring (argv , 0 );
20852085 const char * newpath = janet_getcstring (argv , 1 );
@@ -2094,10 +2094,10 @@ JANET_CORE_FN(os_symlink,
20942094 "Create a symlink from oldpath to newpath, returning nil. Same as `(os/link oldpath newpath true)`." ) {
20952095 janet_sandbox_assert (JANET_SANDBOX_FS_WRITE );
20962096 janet_fixarity (argc , 2 );
2097- #ifdef JANET_WINDOWS
2097+ #if defined( JANET_WINDOWS ) || defined( JANET_PLAN9 )
20982098 (void ) argc ;
20992099 (void ) argv ;
2100- janet_panic ("not supported on Windows" );
2100+ janet_panic ("not supported on Windows or Plan 9 " );
21012101#else
21022102 const char * oldpath = janet_getcstring (argv , 0 );
21032103 const char * newpath = janet_getcstring (argv , 1 );
@@ -2135,6 +2135,8 @@ JANET_CORE_FN(os_rmdir,
21352135 const char * path = janet_getcstring (argv , 0 );
21362136#ifdef JANET_WINDOWS
21372137 int res = _rmdir (path );
2138+ #elif defined(JANET_PLAN9 )
2139+ int res = remove (path );
21382140#else
21392141 int res = rmdir (path );
21402142#endif
@@ -2261,11 +2263,13 @@ static const uint8_t *janet_decode_mode(mode_t m) {
22612263 const char * str = "other" ;
22622264 if (S_ISREG (m )) str = "file" ;
22632265 else if (S_ISDIR (m )) str = "directory" ;
2266+ #ifndef JANET_PLAN9
22642267 else if (S_ISFIFO (m )) str = "fifo" ;
22652268 else if (S_ISBLK (m )) str = "block" ;
22662269 else if (S_ISSOCK (m )) str = "socket" ;
22672270 else if (S_ISLNK (m )) str = "link" ;
22682271 else if (S_ISCHR (m )) str = "character" ;
2272+ #endif
22692273 return janet_ckeyword (str );
22702274}
22712275
@@ -2430,6 +2434,9 @@ static Janet os_stat_or_lstat(int do_lstat, int32_t argc, Janet *argv) {
24302434#ifdef JANET_WINDOWS
24312435 (void ) do_lstat ;
24322436 int res = _stat (path , & st );
2437+ #elif defined(JANET_PLAN9 )
2438+ (void )do_lstat ;
2439+ int res = stat (path , & st );
24332440#else
24342441 int res ;
24352442 if (do_lstat ) {
@@ -2490,9 +2497,13 @@ JANET_CORE_FN(os_chmod,
24902497 "Change file permissions, where `mode` is a permission string as returned by "
24912498 "`os/perm-string`, or an integer as returned by `os/perm-int`. "
24922499 "When `mode` is an integer, it is interpreted as a Unix permission value, best specified in octal, like "
2493- "8r666 or 8r400. Windows will not differentiate between user, group, and other permissions, and thus will combine all of these permissions. Returns nil." ) {
2500+ "8r666 or 8r400. Windows will not differentiate between user, group, and other permissions, and thus will combine all of these permissions. Returns nil."
2501+ "Unsupported on plan9." ) {
24942502 janet_sandbox_assert (JANET_SANDBOX_FS_WRITE );
24952503 janet_fixarity (argc , 2 );
2504+ #ifdef JANET_PLAN9
2505+ janet_panic ("not supported on Plan 9" );
2506+ #else
24962507 const char * path = janet_getcstring (argv , 0 );
24972508#ifdef JANET_WINDOWS
24982509 int res = _chmod (path , os_getmode (argv , 1 ));
@@ -2501,6 +2512,7 @@ JANET_CORE_FN(os_chmod,
25012512#endif
25022513 if (-1 == res ) janet_panicf ("%s: %s" , janet_strerror (errno ), path );
25032514 return janet_wrap_nil ();
2515+ #endif
25042516}
25052517
25062518#ifndef JANET_NO_UMASK
@@ -2865,9 +2877,7 @@ void janet_lib_os(JanetTable *env) {
28652877 JANET_CORE_REG ("os/which" , os_which ),
28662878 JANET_CORE_REG ("os/arch" , os_arch ),
28672879 JANET_CORE_REG ("os/compiler" , os_compiler ),
2868- #ifdef JANET_PLAN9
2869- JANET_CORE_REG ("os/isatty" , os_isatty ),
2870- #elif !defined (JANET_REDUCED_OS )
2880+ #ifndef JANET_REDUCED_OS
28712881
28722882 /* misc (un-sandboxed) */
28732883 JANET_CORE_REG ("os/cpu-count" , os_cpu_count ),
@@ -2881,10 +2891,14 @@ void janet_lib_os(JanetTable *env) {
28812891 JANET_CORE_REG ("os/strftime" , os_strftime ),
28822892 JANET_CORE_REG ("os/sleep" , os_sleep ),
28832893 JANET_CORE_REG ("os/isatty" , os_isatty ),
2894+ #ifndef JANET_NO_LOCALES
28842895 JANET_CORE_REG ("os/setlocale" , os_setlocale ),
2896+ #endif
28852897
28862898 /* env functions */
2899+ #ifndef JANET_PLAN9
28872900 JANET_CORE_REG ("os/environ" , os_environ ),
2901+ #endif
28882902 JANET_CORE_REG ("os/getenv" , os_getenv ),
28892903 JANET_CORE_REG ("os/setenv" , os_setenv ),
28902904
0 commit comments