2018-03-29 00:50:20 +00:00
|
|
|
/*
|
2019-01-06 08:23:03 +00:00
|
|
|
* Copyright (c) 2019 Calvin Rose
|
2018-03-29 00:50:20 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to
|
|
|
|
* deal in the Software without restriction, including without limitation the
|
|
|
|
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
* sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
* IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
#ifndef JANET_AMALG
|
2019-02-19 01:13:35 +00:00
|
|
|
#include <janet.h>
|
2019-01-24 05:15:58 +00:00
|
|
|
#include "util.h"
|
|
|
|
#endif
|
|
|
|
|
2018-03-29 00:50:20 +00:00
|
|
|
#include <stdlib.h>
|
2019-03-29 03:22:58 +00:00
|
|
|
|
|
|
|
#ifndef JANET_REDUCED_OS
|
|
|
|
|
2018-05-13 00:31:28 +00:00
|
|
|
#include <time.h>
|
2019-03-29 03:34:24 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2019-03-29 03:34:24 +00:00
|
|
|
#include <windows.h>
|
2018-05-19 05:09:56 +00:00
|
|
|
#include <direct.h>
|
2019-03-29 03:34:24 +00:00
|
|
|
#include <sys/utime.h>
|
|
|
|
#include <io.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
#else
|
2019-03-29 03:22:58 +00:00
|
|
|
#include <utime.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
#include <unistd.h>
|
2019-03-29 03:22:58 +00:00
|
|
|
#include <dirent.h>
|
2018-07-07 01:50:59 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
2018-05-13 00:31:28 +00:00
|
|
|
#endif
|
2018-03-29 00:50:20 +00:00
|
|
|
|
2018-09-23 01:46:50 +00:00
|
|
|
/* For macos */
|
|
|
|
#ifdef __MACH__
|
|
|
|
#include <mach/clock.h>
|
|
|
|
#include <mach/mach.h>
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 03:34:24 +00:00
|
|
|
#endif /* JANET_REDCUED_OS */
|
2019-03-29 03:22:58 +00:00
|
|
|
|
|
|
|
/* Core OS functions */
|
|
|
|
|
|
|
|
/* Full OS functions */
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_which(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
(void) argv;
|
2019-02-20 01:51:34 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
return janet_ckeywordv("windows");
|
|
|
|
#elif __APPLE__
|
|
|
|
return janet_ckeywordv("macos");
|
|
|
|
#elif defined(__EMSCRIPTEN__)
|
|
|
|
return janet_ckeywordv("web");
|
|
|
|
#else
|
|
|
|
return janet_ckeywordv("posix");
|
|
|
|
#endif
|
2018-08-07 04:54:47 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 03:22:58 +00:00
|
|
|
static Janet os_exit(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 1);
|
|
|
|
if (argc == 0) {
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
} else if (janet_checkint(argv[0])) {
|
|
|
|
exit(janet_unwrap_integer(argv[0]));
|
|
|
|
} else {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef JANET_REDUCED_OS
|
|
|
|
/* Provide a dud os/getenv so init.janet works, but nothing else */
|
|
|
|
|
|
|
|
static Janet os_getenv(int32_t argc, Janet *argv) {
|
|
|
|
(void) argv;
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
return janet_wrap_nil();
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* Provide full os functionality */
|
|
|
|
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_execute(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, -1);
|
2018-09-06 02:18:42 +00:00
|
|
|
JanetBuffer *buffer = janet_buffer(10);
|
2019-01-06 01:09:03 +00:00
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
|
|
|
const uint8_t *argstring = janet_getstring(argv, i);
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_bytes(buffer, argstring, janet_string_length(argstring));
|
2019-01-06 01:09:03 +00:00
|
|
|
if (i != argc - 1) {
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, ' ');
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-06 02:18:42 +00:00
|
|
|
janet_buffer_push_u8(buffer, 0);
|
2018-07-07 01:50:59 +00:00
|
|
|
|
|
|
|
/* Convert to wide chars */
|
|
|
|
wchar_t *sys_str = malloc(buffer->count * sizeof(wchar_t));
|
|
|
|
if (NULL == sys_str) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
int nwritten = MultiByteToWideChar(
|
2019-02-20 01:51:34 +00:00
|
|
|
CP_UTF8,
|
|
|
|
MB_PRECOMPOSED,
|
|
|
|
buffer->data,
|
|
|
|
buffer->count,
|
|
|
|
sys_str,
|
|
|
|
buffer->count);
|
2018-07-07 01:50:59 +00:00
|
|
|
if (nwritten == 0) {
|
|
|
|
free(sys_str);
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("could not create process");
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
STARTUPINFO si;
|
|
|
|
PROCESS_INFORMATION pi;
|
|
|
|
|
|
|
|
ZeroMemory(&si, sizeof(si));
|
|
|
|
si.cb = sizeof(si);
|
|
|
|
ZeroMemory(&pi, sizeof(pi));
|
|
|
|
|
2018-11-16 21:24:10 +00:00
|
|
|
// Start the child process.
|
2019-02-20 01:51:34 +00:00
|
|
|
if (!CreateProcess(NULL,
|
|
|
|
(LPSTR) sys_str,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
FALSE,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&si,
|
|
|
|
&pi)) {
|
2018-07-07 01:50:59 +00:00
|
|
|
free(sys_str);
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("could not create process");
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
free(sys_str);
|
|
|
|
|
|
|
|
// Wait until child process exits.
|
|
|
|
WaitForSingleObject(pi.hProcess, INFINITE);
|
|
|
|
|
2018-07-08 18:22:40 +00:00
|
|
|
// Close process and thread handles.
|
|
|
|
WORD status;
|
2018-08-06 01:13:14 +00:00
|
|
|
GetExitCodeProcess(pi.hProcess, (LPDWORD)&status);
|
2018-07-07 01:50:59 +00:00
|
|
|
CloseHandle(pi.hProcess);
|
|
|
|
CloseHandle(pi.hThread);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_integer(status);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
#else
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_execute(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, -1);
|
2019-03-29 03:22:58 +00:00
|
|
|
const char **child_argv = malloc(sizeof(char *) * (argc + 1));
|
2019-02-22 17:10:27 +00:00
|
|
|
int status = 0;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (NULL == child_argv) {
|
2018-09-06 02:18:42 +00:00
|
|
|
JANET_OUT_OF_MEMORY;
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
for (int32_t i = 0; i < argc; i++) {
|
2019-03-29 03:22:58 +00:00
|
|
|
child_argv[i] = janet_getcstring(argv, i);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
child_argv[argc] = NULL;
|
2018-07-07 01:50:59 +00:00
|
|
|
|
|
|
|
/* Fork child process */
|
2018-11-16 07:34:50 +00:00
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0) {
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_panic("failed to execute");
|
2018-11-16 07:34:50 +00:00
|
|
|
} else if (pid == 0) {
|
2019-03-29 03:22:58 +00:00
|
|
|
if (-1 == execve(child_argv[0], (char **)child_argv, NULL)) {
|
2018-07-07 01:50:59 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2019-02-22 17:10:27 +00:00
|
|
|
} else {
|
|
|
|
waitpid(pid, &status, 0);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
2019-02-22 17:10:27 +00:00
|
|
|
free(child_argv);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_integer(status);
|
2018-07-07 01:50:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_shell(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 1);
|
|
|
|
const char *cmd = argc
|
2019-03-29 03:22:58 +00:00
|
|
|
? janet_getcstring(argv, 0)
|
2019-02-20 01:51:34 +00:00
|
|
|
: NULL;
|
2018-03-29 00:50:20 +00:00
|
|
|
int stat = system(cmd);
|
2019-01-06 01:09:03 +00:00
|
|
|
return argc
|
2019-02-20 01:51:34 +00:00
|
|
|
? janet_wrap_integer(stat)
|
|
|
|
: janet_wrap_boolean(stat);
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_getenv(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-03-29 03:22:58 +00:00
|
|
|
const char *cstr = janet_getcstring(argv, 0);
|
2018-03-29 00:50:20 +00:00
|
|
|
const char *res = getenv(cstr);
|
2019-03-29 03:22:58 +00:00
|
|
|
return res
|
2019-02-20 01:51:34 +00:00
|
|
|
? janet_cstringv(res)
|
|
|
|
: janet_wrap_nil();
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_setenv(int32_t argc, Janet *argv) {
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-06-08 19:58:23 +00:00
|
|
|
#define SETENV(K,V) _putenv_s(K, V)
|
|
|
|
#define UNSETENV(K) _putenv_s(K, "")
|
2018-03-29 00:50:20 +00:00
|
|
|
#else
|
2018-06-08 19:58:23 +00:00
|
|
|
#define SETENV(K,V) setenv(K, V, 1)
|
|
|
|
#define UNSETENV(K) unsetenv(K)
|
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
janet_arity(argc, 1, 2);
|
2019-03-29 03:22:58 +00:00
|
|
|
const char *ks = janet_getcstring(argv, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
if (argc == 1 || janet_checktype(argv[1], JANET_NIL)) {
|
2018-06-08 19:58:23 +00:00
|
|
|
UNSETENV(ks);
|
2018-03-29 00:50:20 +00:00
|
|
|
} else {
|
2019-03-29 03:22:58 +00:00
|
|
|
SETENV(ks, janet_getcstring(argv, 1));
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_time(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
(void) argv;
|
2018-09-23 01:46:50 +00:00
|
|
|
double dtime = (double)(time(NULL));
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_number(dtime);
|
2018-09-23 01:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Clock shims */
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-09-23 01:46:50 +00:00
|
|
|
static int gettime(struct timespec *spec) {
|
2018-07-09 01:10:15 +00:00
|
|
|
int64_t wintime = 0LL;
|
2019-02-20 01:51:34 +00:00
|
|
|
GetSystemTimeAsFileTime((FILETIME *)&wintime);
|
2018-07-09 01:10:15 +00:00
|
|
|
/* Windows epoch is January 1, 1601 apparently*/
|
|
|
|
wintime -= 116444736000000000LL;
|
2018-07-08 23:27:11 +00:00
|
|
|
spec->tv_sec = wintime / 10000000LL;
|
|
|
|
/* Resolution is 100 nanoseconds. */
|
|
|
|
spec->tv_nsec = wintime % 10000000LL * 100;
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-23 01:46:50 +00:00
|
|
|
#elif defined(__MACH__)
|
|
|
|
static int gettime(struct timespec *spec) {
|
|
|
|
clock_serv_t cclock;
|
|
|
|
mach_timespec_t mts;
|
|
|
|
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
|
|
|
|
clock_get_time(cclock, &mts);
|
|
|
|
mach_port_deallocate(mach_task_self(), cclock);
|
|
|
|
spec->tv_sec = mts.tv_sec;
|
|
|
|
spec->tv_nsec = mts.tv_nsec;
|
|
|
|
return 0;
|
2018-09-12 01:33:50 +00:00
|
|
|
}
|
2018-11-16 21:24:10 +00:00
|
|
|
#else
|
2018-09-23 01:46:50 +00:00
|
|
|
#define gettime(TV) clock_gettime(CLOCK_MONOTONIC, (TV))
|
|
|
|
#endif
|
2018-09-12 01:33:50 +00:00
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_clock(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
(void) argv;
|
2018-07-08 23:27:11 +00:00
|
|
|
struct timespec tv;
|
2019-01-06 01:09:03 +00:00
|
|
|
if (gettime(&tv)) janet_panic("could not get time");
|
2018-07-08 23:27:11 +00:00
|
|
|
double dtime = tv.tv_sec + (tv.tv_nsec / 1E9);
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_number(dtime);
|
2018-05-13 00:31:28 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_sleep(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 1);
|
2019-01-06 01:09:03 +00:00
|
|
|
double delay = janet_getnumber(argv, 0);
|
|
|
|
if (delay < 0) janet_panic("invalid argument to sleep");
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2019-02-20 01:51:34 +00:00
|
|
|
Sleep((DWORD)(delay * 1000));
|
2018-05-13 00:31:28 +00:00
|
|
|
#else
|
2018-07-07 01:50:59 +00:00
|
|
|
struct timespec ts;
|
|
|
|
ts.tv_sec = (time_t) delay;
|
|
|
|
ts.tv_nsec = (delay <= UINT32_MAX)
|
2019-02-20 01:51:34 +00:00
|
|
|
? (long)((delay - ((uint32_t)delay)) * 1000000000)
|
|
|
|
: 0;
|
2018-07-07 01:50:59 +00:00
|
|
|
nanosleep(&ts, NULL);
|
2018-05-13 00:31:28 +00:00
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
return janet_wrap_nil();
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-06 01:09:03 +00:00
|
|
|
static Janet os_cwd(int32_t argc, Janet *argv) {
|
2019-01-06 01:45:24 +00:00
|
|
|
janet_fixarity(argc, 0);
|
2019-01-06 01:09:03 +00:00
|
|
|
(void) argv;
|
2018-05-19 05:09:56 +00:00
|
|
|
char buf[FILENAME_MAX];
|
|
|
|
char *ptr;
|
2018-09-06 02:18:42 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
2018-05-19 05:09:56 +00:00
|
|
|
ptr = _getcwd(buf, FILENAME_MAX);
|
|
|
|
#else
|
|
|
|
ptr = getcwd(buf, FILENAME_MAX);
|
|
|
|
#endif
|
2019-01-06 01:09:03 +00:00
|
|
|
if (NULL == ptr) janet_panic("could not get current directory");
|
|
|
|
return janet_cstringv(ptr);
|
2018-05-19 05:09:56 +00:00
|
|
|
}
|
|
|
|
|
2019-01-20 19:34:33 +00:00
|
|
|
static Janet os_date(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 0, 1);
|
|
|
|
(void) argv;
|
|
|
|
time_t t;
|
|
|
|
struct tm *t_info;
|
|
|
|
if (argc) {
|
|
|
|
t = (time_t) janet_getinteger64(argv, 0);
|
|
|
|
} else {
|
|
|
|
time(&t);
|
|
|
|
}
|
|
|
|
t_info = localtime(&t);
|
|
|
|
JanetKV *st = janet_struct_begin(9);
|
|
|
|
janet_struct_put(st, janet_ckeywordv("seconds"), janet_wrap_number(t_info->tm_sec));
|
|
|
|
janet_struct_put(st, janet_ckeywordv("minutes"), janet_wrap_number(t_info->tm_min));
|
|
|
|
janet_struct_put(st, janet_ckeywordv("hours"), janet_wrap_number(t_info->tm_hour));
|
2019-01-20 21:49:39 +00:00
|
|
|
janet_struct_put(st, janet_ckeywordv("month-day"), janet_wrap_number(t_info->tm_mday - 1));
|
2019-01-20 19:34:33 +00:00
|
|
|
janet_struct_put(st, janet_ckeywordv("month"), janet_wrap_number(t_info->tm_mon));
|
2019-01-20 21:49:39 +00:00
|
|
|
janet_struct_put(st, janet_ckeywordv("year"), janet_wrap_number(t_info->tm_year + 1900));
|
2019-01-20 19:34:33 +00:00
|
|
|
janet_struct_put(st, janet_ckeywordv("week-day"), janet_wrap_number(t_info->tm_wday));
|
|
|
|
janet_struct_put(st, janet_ckeywordv("year-day"), janet_wrap_number(t_info->tm_yday));
|
|
|
|
janet_struct_put(st, janet_ckeywordv("dst"), janet_wrap_boolean(t_info->tm_isdst));
|
|
|
|
return janet_wrap_struct(janet_struct_end(st));
|
|
|
|
}
|
|
|
|
|
2019-03-29 03:22:58 +00:00
|
|
|
static Janet os_link(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 2, 3);
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
janet_panic("os/link not supported on Windows");
|
|
|
|
return janet_wrap_nil();
|
|
|
|
#else
|
|
|
|
const char *oldpath = janet_getcstring(argv, 0);
|
|
|
|
const char *newpath = janet_getcstring(argv, 1);
|
|
|
|
int res = ((argc == 3 && janet_getboolean(argv, 2)) ? symlink : link)(oldpath, newpath);
|
|
|
|
if (res == -1) janet_panicv(janet_cstringv(strerror(errno)));
|
|
|
|
return janet_wrap_integer(res);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet os_mkdir(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
const char *path = janet_getcstring(argv, 0);
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
int res = _mkdir(path);
|
|
|
|
#else
|
|
|
|
int res = mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
|
|
|
|
#endif
|
|
|
|
return janet_wrap_boolean(res != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet os_cd(int32_t argc, Janet *argv) {
|
|
|
|
janet_fixarity(argc, 1);
|
|
|
|
const char *path = janet_getcstring(argv, 0);
|
2019-03-29 03:34:24 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
int res = _chdir(path);
|
|
|
|
#else
|
2019-03-29 03:22:58 +00:00
|
|
|
int res = chdir(path);
|
2019-03-29 03:34:24 +00:00
|
|
|
#endif
|
2019-03-29 03:22:58 +00:00
|
|
|
return janet_wrap_boolean(res != -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Janet os_touch(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 3);
|
|
|
|
const char *path = janet_getcstring(argv, 0);
|
|
|
|
struct utimbuf timebuf, *bufp;
|
|
|
|
if (argc >= 2) {
|
|
|
|
bufp = &timebuf;
|
|
|
|
timebuf.actime = (time_t) janet_getnumber(argv, 1);
|
|
|
|
if (argc >= 3) {
|
|
|
|
timebuf.modtime = (time_t) janet_getnumber(argv, 2);
|
|
|
|
} else {
|
|
|
|
timebuf.modtime = timebuf.actime;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bufp = NULL;
|
|
|
|
}
|
|
|
|
int res = utime(path, bufp);
|
|
|
|
return janet_wrap_boolean(res != -1);
|
|
|
|
}
|
|
|
|
|
2019-03-30 16:06:14 +00:00
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
static const uint8_t *janet_decode_permissions(unsigned short m) {
|
|
|
|
uint8_t flags[9] = {0};
|
|
|
|
flags[0] = flags[3] = flags[6] = (m & S_IREAD) ? 'r' : '-';
|
|
|
|
flags[1] = flags[4] = flags[7] = (m & S_IWRITE) ? 'w' : '-';
|
|
|
|
flags[2] = flags[5] = flags[8] = (m & S_IEXEC) ? 'x' : '-';
|
|
|
|
return janet_string(flags, sizeof(flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t *janet_decode_mode(unsigned short m) {
|
|
|
|
const char *str = "other";
|
|
|
|
if (_S_ISREG(m)) str = "file";
|
|
|
|
else if (_S_ISDIR(m)) str = "directory";
|
|
|
|
return janet_ckeyword(str);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static const uint8_t *janet_decode_permissions(mode_t m) {
|
|
|
|
uint8_t flags[9] = {0};
|
|
|
|
flags[0] = (m & S_IRUSR) ? 'r' : '-';
|
|
|
|
flags[1] = (m & S_IWUSR) ? 'w' : '-';
|
|
|
|
flags[2] = (m & S_IXUSR) ? 'x' : '-';
|
|
|
|
flags[3] = (m & S_IRGRP) ? 'r' : '-';
|
|
|
|
flags[4] = (m & S_IWGRP) ? 'w' : '-';
|
|
|
|
flags[5] = (m & S_IXGRP) ? 'x' : '-';
|
|
|
|
flags[6] = (m & S_IROTH) ? 'r' : '-';
|
|
|
|
flags[7] = (m & S_IWOTH) ? 'w' : '-';
|
|
|
|
flags[8] = (m & S_IXOTH) ? 'x' : '-';
|
|
|
|
return janet_string(flags, sizeof(flags));
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t *janet_decode_mode(mode_t m) {
|
|
|
|
const char *str = "other";
|
|
|
|
if (S_ISREG(m)) str = "file";
|
|
|
|
else if (S_ISDIR(m)) str = "directory";
|
|
|
|
else if (S_ISFIFO(m)) str = "fifo";
|
|
|
|
else if (S_ISBLK(m)) str = "block";
|
|
|
|
else if (S_ISSOCK(m)) str = "socket";
|
|
|
|
else if (S_ISLNK(m)) str = "link";
|
|
|
|
else if (S_ISCHR(m)) str = "character";
|
|
|
|
return janet_ckeyword(str);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static Janet os_stat(int32_t argc, Janet *argv) {
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
const char *path = janet_getcstring(argv, 0);
|
|
|
|
JanetTable *tab;
|
|
|
|
if (argc == 2) {
|
|
|
|
tab = janet_gettable(argv, 1);
|
|
|
|
} else {
|
|
|
|
tab = janet_table(0);
|
|
|
|
}
|
|
|
|
/* Build result */
|
|
|
|
#ifdef JANET_WINDOWS
|
|
|
|
struct _stat st;
|
|
|
|
int res = _stat(path, &st);
|
|
|
|
#else
|
|
|
|
struct stat st;
|
|
|
|
int res = stat(path, &st);
|
|
|
|
#endif
|
|
|
|
if (-1 == res) {
|
|
|
|
janet_panicv(janet_cstringv(strerror(errno)));
|
|
|
|
}
|
|
|
|
janet_table_put(tab, janet_ckeywordv("dev"), janet_wrap_number(st.st_dev));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("inode"), janet_wrap_number(st.st_ino));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("mode"), janet_wrap_keyword(janet_decode_mode(st.st_mode)));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("permissions"), janet_wrap_string(janet_decode_permissions(st.st_mode)));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("uid"), janet_wrap_number(st.st_uid));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("gid"), janet_wrap_number(st.st_gid));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("size"), janet_wrap_number(st.st_size));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("nlink"), janet_wrap_number(st.st_nlink));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("rdev"), janet_wrap_number(st.st_rdev));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("blocksize"), janet_wrap_number(st.st_blksize));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("blocks"), janet_wrap_number(st.st_blocks));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("accessed"), janet_wrap_number(st.st_atime));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("modified"), janet_wrap_number(st.st_mtime));
|
|
|
|
janet_table_put(tab, janet_ckeywordv("changed"), janet_wrap_number(st.st_ctime));
|
|
|
|
return janet_wrap_table(tab);
|
|
|
|
}
|
|
|
|
|
2019-03-30 16:36:27 +00:00
|
|
|
static Janet os_dir(int32_t argc, Janet *argv) {
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dfd;
|
|
|
|
janet_arity(argc, 1, 2);
|
|
|
|
const char *dir = janet_getcstring(argv, 0);
|
|
|
|
JanetArray *paths = (argc == 2) ? janet_getarray(argv, 1) : janet_array(0);
|
|
|
|
if ((dfd = opendir(dir)) == NULL) janet_panicf("cannot open directory %s", dir);
|
|
|
|
/* Read directory items */
|
|
|
|
while ((dp = readdir(dfd)) != NULL) {
|
|
|
|
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
janet_array_push(paths, janet_cstringv(dp->d_name));
|
|
|
|
}
|
|
|
|
return janet_wrap_array(paths);
|
|
|
|
}
|
|
|
|
|
2019-03-29 03:22:58 +00:00
|
|
|
#endif /* JANET_REDUCED_OS */
|
|
|
|
|
2019-01-24 05:15:58 +00:00
|
|
|
static const JanetReg os_cfuns[] = {
|
2019-03-29 03:22:58 +00:00
|
|
|
{
|
|
|
|
"os/exit", os_exit,
|
|
|
|
JDOC("(os/exit x)\n\n"
|
|
|
|
"Exit from janet with an exit code equal to x. If x is not an integer, "
|
|
|
|
"the exit with status equal the hash of x.")
|
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/which", os_which,
|
|
|
|
JDOC("(os/which)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Check the current operating system. Returns one of:\n\n"
|
|
|
|
"\t:windows - Microsoft Windows\n"
|
|
|
|
"\t:macos - Apple macos\n"
|
|
|
|
"\t:posix - A POSIX compatible system (default)")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-03-29 03:22:58 +00:00
|
|
|
{
|
|
|
|
"os/getenv", os_getenv,
|
|
|
|
JDOC("(os/getenv variable)\n\n"
|
|
|
|
"Get the string value of an environment variable.")
|
|
|
|
},
|
|
|
|
#ifndef JANET_REDUCED_OS
|
2019-03-30 16:36:27 +00:00
|
|
|
{
|
|
|
|
"os/dir", os_dir,
|
|
|
|
JDOC("(os/stat dir [, array])\n\n"
|
|
|
|
"Iterate over files and subdirectories in a directory. Returns an array of paths parts, "
|
|
|
|
"with only the filename or directory name and no prefix.")
|
|
|
|
},
|
2019-03-29 03:22:58 +00:00
|
|
|
{
|
2019-03-30 16:06:14 +00:00
|
|
|
"os/stat", os_stat,
|
2019-03-30 16:36:27 +00:00
|
|
|
JDOC("(os/stat path [, tab])\n\n"
|
2019-03-30 16:06:14 +00:00
|
|
|
"Gets information about a file or directory. Returns a table.")
|
|
|
|
},
|
|
|
|
{
|
2019-03-29 03:22:58 +00:00
|
|
|
"os/touch", os_touch,
|
|
|
|
JDOC("(os/touch path [, actime [, modtime]])\n\n"
|
|
|
|
"Update the access time and modification times for a file. By default, sets "
|
|
|
|
"times to the current time.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"os/cd", os_cd,
|
|
|
|
JDOC("(os/cd path)\n\n"
|
|
|
|
"Change current directory to path. Returns true on success, false on failure.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"os/mkdir", os_mkdir,
|
|
|
|
JDOC("(os/mkdir path)\n\n"
|
|
|
|
"Create a new directory. The path will be relative to the current directory if relative, otherwise "
|
|
|
|
"it will be an absolute path.")
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"os/link", os_link,
|
|
|
|
JDOC("(os/link oldpath newpath [, symlink])\n\n"
|
|
|
|
"Create a symlink from oldpath to newpath. The 3 optional paramater "
|
|
|
|
"enables a hard link over a soft link. Does not work on Windows.")
|
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/execute", os_execute,
|
|
|
|
JDOC("(os/execute program & args)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Execute a program on the system and pass it string arguments. Returns "
|
|
|
|
"the exit status of the program.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/shell", os_shell,
|
|
|
|
JDOC("(os/shell str)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Pass a command string str directly to the system shell.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/setenv", os_setenv,
|
|
|
|
JDOC("(os/setenv variable value)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Set an environment variable.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/time", os_time,
|
|
|
|
JDOC("(os/time)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Get the current time expressed as the number of seconds since "
|
|
|
|
"January 1, 1970, the Unix epoch. Returns a real number.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/clock", os_clock,
|
|
|
|
JDOC("(os/clock)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Return the number of seconds since some fixed point in time. The clock "
|
|
|
|
"is guaranteed to be non decreasing in real time.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/sleep", os_sleep,
|
|
|
|
JDOC("(os/sleep nsec)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Suspend the program for nsec seconds. 'nsec' can be a real number. Returns "
|
|
|
|
"nil.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-06 06:49:56 +00:00
|
|
|
{
|
|
|
|
"os/cwd", os_cwd,
|
|
|
|
JDOC("(os/cwd)\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns the current working directory.")
|
2018-11-16 07:34:50 +00:00
|
|
|
},
|
2019-01-20 19:34:33 +00:00
|
|
|
{
|
|
|
|
"os/date", os_date,
|
|
|
|
JDOC("(os/date [,time])\n\n"
|
2019-02-20 01:51:34 +00:00
|
|
|
"Returns the given time as a date struct, or the current time if no time is given. "
|
|
|
|
"Returns a struct with following key values. Note that all numbers are 0-indexed.\n\n"
|
|
|
|
"\t:seconds - number of seconds [0-61]\n"
|
|
|
|
"\t:minutes - number of minutes [0-59]\n"
|
|
|
|
"\t:seconds - number of hours [0-23]\n"
|
|
|
|
"\t:month-day - day of month [0-30]\n"
|
|
|
|
"\t:month - month of year [0, 11]\n"
|
|
|
|
"\t:year - years since year 0 (e.g. 2019)\n"
|
|
|
|
"\t:week-day - day of the week [0-6]\n"
|
|
|
|
"\t:year-day - day of the year [0-365]\n"
|
|
|
|
"\t:dst - If Day Light Savings is in effect")
|
2019-01-20 19:34:33 +00:00
|
|
|
},
|
2019-03-29 03:22:58 +00:00
|
|
|
#endif
|
2018-11-15 20:45:41 +00:00
|
|
|
{NULL, NULL, NULL}
|
2018-03-29 00:50:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Module entry point */
|
2019-01-06 01:09:03 +00:00
|
|
|
void janet_lib_os(JanetTable *env) {
|
2019-02-08 05:44:30 +00:00
|
|
|
janet_core_cfuns(env, NULL, os_cfuns);
|
2018-03-29 00:50:20 +00:00
|
|
|
}
|