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())
@@ -296,20 +298,7 @@ JANET_CORE_FN(os_exit,
296298 return janet_wrap_nil ();
297299}
298300
299- #ifdef JANET_PLAN9
300-
301- JANET_CORE_FN (os_isatty ,
302- "(os/isatty &opt file)" ,
303- "Returns true if `file` is a terminal. If `file` is not specified, "
304- "it will default to standard output." ) {
305- janet_arity (argc , 0 , 1 );
306- FILE * f = (argc == 1 ) ? janet_getfile (argv , 0 , NULL ) : stdout ;
307- int fd = fileno (f );
308- if (fd == -1 ) janet_panic (janet_strerror (errno ));
309- return janet_wrap_boolean (isatty (fd ));
310- }
311-
312- #elif !defined(JANET_REDUCED_OS )
301+ #ifndef JANET_REDUCED_OS
313302
314303JANET_CORE_FN (os_cpu_count ,
315304 "(os/cpu-count &opt dflt)" ,
@@ -1626,6 +1615,7 @@ JANET_CORE_FN(os_shell,
16261615
16271616#endif /* JANET_NO_PROCESSES */
16281617
1618+ #ifndef JANET_PLAN9
16291619JANET_CORE_FN (os_environ ,
16301620 "(os/environ)" ,
16311621 "Get a copy of the OS environment table." ) {
@@ -1657,6 +1647,7 @@ JANET_CORE_FN(os_environ,
16571647 janet_unlock_environ ();
16581648 return janet_wrap_table (t );
16591649}
1650+ #endif
16601651
16611652JANET_CORE_FN (os_getenv ,
16621653 "(os/getenv variable &opt dflt)" ,
@@ -1681,6 +1672,9 @@ JANET_CORE_FN(os_setenv,
16811672#ifdef JANET_WINDOWS
16821673#define SETENV (K ,V ) _putenv_s(K, V)
16831674#define UNSETENV (K ) _putenv_s(K, "")
1675+ #elif defined(JANET_PLAN9 )
1676+ #define SETENV (K ,V ) putenv(K, V)
1677+ #define UNSETENV (K ) unsetenv(K)
16841678#else
16851679#define SETENV (K ,V ) setenv(K, V, 1)
16861680#define UNSETENV (K ) unsetenv(K)
@@ -1853,6 +1847,8 @@ static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct
18531847 _tzset ();
18541848 localtime_s (t_infos , & t );
18551849 t_info = t_infos ;
1850+ #elif defined(JANET_PLAN9 )
1851+ t_info = localtime (& t );
18561852#else
18571853 tzset ();
18581854 t_info = localtime_r (& t , t_infos );
@@ -1862,6 +1858,8 @@ static struct tm *time_to_tm(const Janet *argv, int32_t argc, int32_t n, struct
18621858#ifdef JANET_WINDOWS
18631859 gmtime_s (t_infos , & t );
18641860 t_info = t_infos ;
1861+ #elif defined(JANET_PLAN9 )
1862+ t_info = gmtime (& t );
18651863#else
18661864 t_info = gmtime_r (& t , t_infos );
18671865#endif
@@ -2036,6 +2034,7 @@ JANET_CORE_FN(os_mktime,
20362034#define j_symlink symlink
20372035#endif
20382036
2037+ #ifndef JANET_NO_LOCALES
20392038JANET_CORE_FN (os_setlocale ,
20402039 "(os/setlocale &opt locale category)" ,
20412040 "Set the system locale, which affects how dates and numbers are formatted. "
@@ -2072,19 +2071,20 @@ JANET_CORE_FN(os_setlocale,
20722071 if (old == NULL ) return janet_wrap_nil ();
20732072 return janet_cstringv (old );
20742073}
2074+ #endif
20752075
20762076JANET_CORE_FN (os_link ,
20772077 "(os/link oldpath newpath &opt symlink)" ,
20782078 "Create a link at newpath that points to oldpath and returns nil. "
20792079 "Iff symlink is truthy, creates a symlink. "
20802080 "Iff symlink is falsey or not provided, "
2081- "creates a hard link. Does not work on Windows." ) {
2081+ "creates a hard link. Does not work on Windows or Plan 9 ." ) {
20822082 janet_sandbox_assert (JANET_SANDBOX_FS_WRITE );
20832083 janet_arity (argc , 2 , 3 );
2084- #ifdef JANET_WINDOWS
2084+ #if defined( JANET_WINDOWS ) || defined( JANET_PLAN9 )
20852085 (void ) argc ;
20862086 (void ) argv ;
2087- janet_panic ("not supported on Windows" );
2087+ janet_panic ("not supported on Windows or Plan 9 " );
20882088#else
20892089 const char * oldpath = janet_getcstring (argv , 0 );
20902090 const char * newpath = janet_getcstring (argv , 1 );
@@ -2099,10 +2099,10 @@ JANET_CORE_FN(os_symlink,
20992099 "Create a symlink from oldpath to newpath, returning nil. Same as `(os/link oldpath newpath true)`." ) {
21002100 janet_sandbox_assert (JANET_SANDBOX_FS_WRITE );
21012101 janet_fixarity (argc , 2 );
2102- #ifdef JANET_WINDOWS
2102+ #if defined( JANET_WINDOWS ) || defined( JANET_PLAN9 )
21032103 (void ) argc ;
21042104 (void ) argv ;
2105- janet_panic ("not supported on Windows" );
2105+ janet_panic ("not supported on Windows or Plan 9 " );
21062106#else
21072107 const char * oldpath = janet_getcstring (argv , 0 );
21082108 const char * newpath = janet_getcstring (argv , 1 );
@@ -2140,6 +2140,8 @@ JANET_CORE_FN(os_rmdir,
21402140 const char * path = janet_getcstring (argv , 0 );
21412141#ifdef JANET_WINDOWS
21422142 int res = _rmdir (path );
2143+ #elif defined(JANET_PLAN9 )
2144+ int res = remove (path );
21432145#else
21442146 int res = rmdir (path );
21452147#endif
@@ -2266,11 +2268,13 @@ static const uint8_t *janet_decode_mode(mode_t m) {
22662268 const char * str = "other" ;
22672269 if (S_ISREG (m )) str = "file" ;
22682270 else if (S_ISDIR (m )) str = "directory" ;
2271+ #ifndef JANET_PLAN9
22692272 else if (S_ISFIFO (m )) str = "fifo" ;
22702273 else if (S_ISBLK (m )) str = "block" ;
22712274 else if (S_ISSOCK (m )) str = "socket" ;
22722275 else if (S_ISLNK (m )) str = "link" ;
22732276 else if (S_ISCHR (m )) str = "character" ;
2277+ #endif
22742278 return janet_ckeyword (str );
22752279}
22762280
@@ -2435,6 +2439,9 @@ static Janet os_stat_or_lstat(int do_lstat, int32_t argc, Janet *argv) {
24352439#ifdef JANET_WINDOWS
24362440 (void ) do_lstat ;
24372441 int res = _stat (path , & st );
2442+ #elif defined(JANET_PLAN9 )
2443+ (void )do_lstat ;
2444+ int res = stat (path , & st );
24382445#else
24392446 int res ;
24402447 if (do_lstat ) {
@@ -2495,9 +2502,13 @@ JANET_CORE_FN(os_chmod,
24952502 "Change file permissions, where `mode` is a permission string as returned by "
24962503 "`os/perm-string`, or an integer as returned by `os/perm-int`. "
24972504 "When `mode` is an integer, it is interpreted as a Unix permission value, best specified in octal, like "
2498- "8r666 or 8r400. Windows will not differentiate between user, group, and other permissions, and thus will combine all of these permissions. Returns nil." ) {
2505+ "8r666 or 8r400. Windows will not differentiate between user, group, and other permissions, and thus will combine all of these permissions. Returns nil."
2506+ "Unsupported on plan9." ) {
24992507 janet_sandbox_assert (JANET_SANDBOX_FS_WRITE );
25002508 janet_fixarity (argc , 2 );
2509+ #ifdef JANET_PLAN9
2510+ janet_panic ("not supported on Plan 9" );
2511+ #else
25012512 const char * path = janet_getcstring (argv , 0 );
25022513#ifdef JANET_WINDOWS
25032514 int res = _chmod (path , os_getmode (argv , 1 ));
@@ -2506,6 +2517,7 @@ JANET_CORE_FN(os_chmod,
25062517#endif
25072518 if (-1 == res ) janet_panicf ("%s: %s" , janet_strerror (errno ), path );
25082519 return janet_wrap_nil ();
2520+ #endif
25092521}
25102522
25112523#ifndef JANET_NO_UMASK
@@ -2870,9 +2882,7 @@ void janet_lib_os(JanetTable *env) {
28702882 JANET_CORE_REG ("os/which" , os_which ),
28712883 JANET_CORE_REG ("os/arch" , os_arch ),
28722884 JANET_CORE_REG ("os/compiler" , os_compiler ),
2873- #ifdef JANET_PLAN9
2874- JANET_CORE_REG ("os/isatty" , os_isatty ),
2875- #elif !defined (JANET_REDUCED_OS )
2885+ #ifndef JANET_REDUCED_OS
28762886
28772887 /* misc (un-sandboxed) */
28782888 JANET_CORE_REG ("os/cpu-count" , os_cpu_count ),
@@ -2886,10 +2896,14 @@ void janet_lib_os(JanetTable *env) {
28862896 JANET_CORE_REG ("os/strftime" , os_strftime ),
28872897 JANET_CORE_REG ("os/sleep" , os_sleep ),
28882898 JANET_CORE_REG ("os/isatty" , os_isatty ),
2899+ #ifndef JANET_NO_LOCALES
28892900 JANET_CORE_REG ("os/setlocale" , os_setlocale ),
2901+ #endif
28902902
28912903 /* env functions */
2904+ #ifndef JANET_PLAN9
28922905 JANET_CORE_REG ("os/environ" , os_environ ),
2906+ #endif
28932907 JANET_CORE_REG ("os/getenv" , os_getenv ),
28942908 JANET_CORE_REG ("os/setenv" , os_setenv ),
28952909
0 commit comments