diff --git a/stdlib/os/fs.aspl b/stdlib/os/fs.aspl index 0e6750b..0b2caaf 100644 --- a/stdlib/os/fs.aspl +++ b/stdlib/os/fs.aspl @@ -14,4 +14,10 @@ function chdir(string path){ [public] function chmod(string path, int mode){ implement("os.change_mode", path, mode) +} + +// create_temp_dir creates and returns a unique ephemeral directory suitable for storing temporary files +[public] +function create_temp_dir() returns string{ + return string(implement("os.create_temporary_directory")) } \ No newline at end of file diff --git a/stdlib/os/implementations/implementations.c b/stdlib/os/implementations/implementations.c index 38d5908..c7502f3 100644 --- a/stdlib/os/implementations/implementations.c +++ b/stdlib/os/implementations/implementations.c @@ -6,6 +6,7 @@ #include #include #include +#include #endif ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$get_current_program_arguments() @@ -123,6 +124,39 @@ ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$change_mode(ASPL_OBJECT_TYPE* path, ASPL_OBJE return ASPL_UNINITIALIZED; } +ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$create_temporary_directory() +{ + char* tempDirPath; +#ifdef _WIN32 + char tempPath[MAX_PATH]; + if (GetTempPath(MAX_PATH, tempPath) == 0) + { + ASPL_PANIC("Failed to retrieve temporary path."); + } + + tempDirPath = ASPL_MALLOC(MAX_PATH); + if (GetTempFileName(tempPath, "", 0, tempDirPath) == 0) + { + ASPL_PANIC("Failed to create temporary directory name."); + } + + DeleteFile(tempDirPath); + if (!CreateDirectory(tempDirPath, NULL)) + { + ASPL_PANIC("Failed to create temporary directory."); + } +#else + tempDirPath = ASPL_MALLOC(PATH_MAX); + snprintf(tempDirPath, PATH_MAX, "/tmp/XXXXXX"); + + if (mkdtemp(tempDirPath) == NULL) + { + ASPL_PANIC("Failed to create temporary directory."); + } +#endif + return ASPL_STRING_LITERAL_NO_COPY(tempDirPath); +} + ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$get_current_runtime_os_name() { // TODO