unix/modos: Implement rename().
rename() is considered a part of the baseline API, and should be builtin
to allow to write simple, but useful/reliable utilities.
*
* The MIT License (MIT)
*
- * Copyright (c) 2014-2018 Paul Sokolovsky
+ * Copyright (c) 2014-2019 Paul Sokolovsky
* Copyright (c) 2014-2018 Damien P. George
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
@@ -123,6 +124,18 @@ STATIC mp_obj_t mod_os_remove(mp_obj_t path_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_os_remove_obj, mod_os_remove);
+STATIC mp_obj_t mod_os_rename(mp_obj_t src_in, mp_obj_t dst_in) {
+ const char *src = mp_obj_str_get_str(src_in);
+ const char *dst = mp_obj_str_get_str(dst_in);
+
+ int r = rename(src, dst);
+
+ RAISE_ERRNO(r, errno);
+
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_os_rename_obj, mod_os_rename);
+
STATIC mp_obj_t mod_os_system(mp_obj_t cmd_in) {
const char *cmd = mp_obj_str_get_str(cmd_in);
@@ -236,6 +249,7 @@ STATIC const mp_rom_map_elem_t mp_module_os_globals_table[] = {
#endif
{ MP_ROM_QSTR(MP_QSTR_system), MP_ROM_PTR(&mod_os_system_obj) },
{ MP_ROM_QSTR(MP_QSTR_remove), MP_ROM_PTR(&mod_os_remove_obj) },
+ { MP_ROM_QSTR(MP_QSTR_rename), MP_ROM_PTR(&mod_os_rename_obj) },
{ MP_ROM_QSTR(MP_QSTR_getenv), MP_ROM_PTR(&mod_os_getenv_obj) },
{ MP_ROM_QSTR(MP_QSTR_mkdir), MP_ROM_PTR(&mod_os_mkdir_obj) },
{ MP_ROM_QSTR(MP_QSTR_ilistdir), MP_ROM_PTR(&mod_os_ilistdir_obj) },
