Skip to content

DirCreate

Description

Create a single directory. Direct syscall (mkdir on Linux x86_64 / Darwin, mkdirat on Linux aarch64, CreateDirectoryA on Windows). Mode is 0755 on POSIX. Fails if a parent component is missing – use DirCreateAll for mkdir -p semantics.

Parameters

Name Direction Description
path in Path of the directory. Prefer Str *; const char * accepted.

Success

Returns 1; the directory now exists.

Failure

Returns 0; logs the failing syscall.

Usage example (Cross-references)

Usage examples (Cross-references)
    i8 dir_create(Zstr path) {
        if (!path) {
            LOG_FATAL("DirCreate: NULL path");
        }
    #if PLATFORM_WINDOWS
    #if PLATFORM_WINDOWS
        if (!CreateDirectoryA(path, NULL)) {
            LOG_ERROR("DirCreate(\"{}\"): CreateDirectoryA failed (GetLastError={})", path, (i32)GetLastError());
            return 0;
        }
    #    endif
        if (ret < 0) {
            LOG_SYS_ERROR(ErrnoOf(ret), "DirCreate(\"{}\")", path);
            return 0;
        }
    #else
    #    error                                                                                                             \
            "DirCreate: no direct-syscall path. Add MISRA_SYS_mkdir / MISRA_SYS_mkdirat numbers in _Syscall.h for this arch."
    #endif
    }
                buf[i]     = 0;
                if (!dir_already_exists(buf)) {
                    if (!DirCreate(buf)) {
                        // DirCreate logged the syscall error; re-check
                        // in case a concurrent process beat us to it.
Last updated on