import t, c from stdint import * import platmacro import memhub import os.path import os._win32 import os._posix # os package entry point # Dispatches to _win32 or _posix backend based on platmacro.IS_WINDOWS # os.path is for cross-platform pure string operations, can be used directly # Module-level global MBuddy pointer (initialized by the user in main) _mbuddy: memhub.MemBuddy* # ============================================================================= # File/Directory Attribute Query # ============================================================================= def exists(path: str) -> bool: """Check if the file/directory exists""" if platmacro.IS_WINDOWS: return os._win32.exists(path) return os._posix.exists(path) def isdir(path: str) -> bool: """Check if the path is a directory""" if platmacro.IS_WINDOWS: return os._win32.isdir(path) return os._posix.isdir(path) def isfile(path: str) -> bool: """Check if the path is a regular file""" if platmacro.IS_WINDOWS: return os._win32.isfile(path) return os._posix.isfile(path) def getsize(path: str) -> int64: """Returns the file size (in bytes). Returns -1 on failure.""" if platmacro.IS_WINDOWS: return os._win32.getsize(path) return os._posix.getsize(path) # ============================================================================= # Working Directory # ============================================================================= def getcwd() -> str: """Return the current working directory (memory allocated via _mbuddy)""" if platmacro.IS_WINDOWS: return os._win32.getcwd() return os._posix.getcwd_impl() def chdir(path: str) -> int: """Switch the current working directory. Returns 0 if successful, -1 if it fails.""" if platmacro.IS_WINDOWS: return os._win32.chdir(path) return os._posix.chdir_impl(path) # ============================================================================= # File/Directory Operations # ============================================================================= def mkdir(path: str) -> int: """Create a directory. Returns 0 on success, -1 on failure.""" if platmacro.IS_WINDOWS: return os._win32.mkdir(path) return os._posix.mkdir_impl(path) def remove(path: str) -> int: """Delete the file. Returns 0 if successful, -1 if it fails.""" if platmacro.IS_WINDOWS: return os._win32.remove(path) return os._posix.remove(path) def rmdir(path: str) -> int: """Delete empty directories. Returns 0 if successful, -1 if it fails.""" if platmacro.IS_WINDOWS: return os._win32.rmdir(path) return os._posix.rmdir_impl(path) def rename(old_path: str, new_path: str) -> int: """Rename/move a file or directory. Returns 0 on success, -1 on failure.""" if platmacro.IS_WINDOWS: return os._win32.rename(old_path, new_path) return os._posix.rename_impl(old_path, new_path) def chmod(path: str, mode: int) -> int: """Modify file permissions. Returns 0 on success, -1 on failure. On Windows, only the read-only bit (S_IWUSR) is handled; On POSIX, full permission bits are used.""" if platmacro.IS_WINDOWS: return os._win32.chmod(path, mode) return os._posix.chmod_impl(path, mode) # ============================================================================= # Table of Contents Listing # ============================================================================= def listdir(path: str, out_names: str*, max_count: ULONG) -> ULONG: """List file/subdirectory names under a directory. out_names is a caller-provided char** array (each entry points to a filename allocated via _mbuddy); max_count is its capacity. Return the count, or 0 on failure.""" if platmacro.IS_WINDOWS: return os._win32.listdir(path, out_names, max_count) return os._posix.listdir(path, out_names, max_count) # ============================================================================= # Command Execution # ============================================================================= def system(cmd: str) -> int: """Execute a command line command (via the C runtime system()). Output goes to the terminal (inherits parent stdout/stderr). Return the exit code (0 = success).""" if platmacro.IS_WINDOWS: return os._win32.system(cmd) return os._posix.system_impl(cmd)