getcwd

getcwd(): string|false

Returns the absolute path of the process working directory.

C C23

C23
#include <libgen.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>

int main(void) {
    mkdir("sub", 0755);              // fixture directory
    if (chdir("sub") != 0) return 1; // enter it
    char buf[4096];
    char *cwd = getcwd(buf, sizeof buf); // POSIX: buffer you own; NULL asks malloc
    printf("%s\n", basename(cwd));
    return 0;
}
Output
sub

C++ C++26

C++26
#include <filesystem>
#include <iostream>
namespace fs = std::filesystem;
int main() {
    fs::create_directory("sub"); // no-op (returns false) if it already exists
    fs::current_path("sub");     // chdir equivalent
    std::cout << fs::current_path().filename().string() << "\n";
    return 0;
}
Output
sub

C# v11.0.100

v11.0.100
using System;
using System.IO;

Directory.CreateDirectory("sub");
Environment.CurrentDirectory = Path.GetFullPath("sub"); // .NET can change the process cwd
Console.WriteLine(Path.GetFileName(Directory.GetCurrentDirectory()));
Output
sub

Go v1.27.4

v1.27.4
package main

import (
    "fmt"
    "os"
    "path/filepath"
)

func main() {
    if err := os.Mkdir("sub", 0755); err != nil && !os.IsExist(err) {
        panic(err)
    }
    if err := os.Chdir("sub"); err != nil {
        panic(err)
    }
    wd, err := os.Getwd()
    if err != nil {
        panic(err)
    }
    fmt.Println(filepath.Base(wd))
}
Output
sub

Java v27.0.1

v27.0.1
import java.nio.file.Files;
import java.nio.file.Path;

public class Main {
    public static void main(String[] args) throws Exception {
        Path sub = Path.of("sub");
        Files.createDirectories(sub);
        // JVM has no chdir: user.dir is fixed at launch; express the target dir via absolute path
        System.out.println(sub.toAbsolutePath().getFileName());
    }
}
Output
sub

JavaScript v26.13.1

v26.13.1
const fs = require("node:fs");
const path = require("node:path");

fs.mkdirSync("sub", { recursive: true });
process.chdir("sub");
console.log(path.basename(process.cwd()));
Output
sub

PHP v8.6.0

v8.6.0
mkdir("sub"); // fixture directory in the process cwd
chdir("sub");
echo basename(getcwd()); // absolute paths vary per machine; basename keeps output deterministic
Output
sub

Python v3.15.2

v3.15.2
import os

os.makedirs("sub", exist_ok=True)
os.chdir("sub")
print(os.path.basename(os.getcwd()))
Output
sub

Rust v1.99.1

v1.99.1
use std::env;
use std::fs;

fn main() {
    fs::create_dir_all("sub").unwrap();
    env::set_current_dir("sub").unwrap();
    let wd = env::current_dir().unwrap();
    println!("{}", wd.file_name().unwrap().to_str().unwrap());
}
Output
sub

TypeScript v26.13.1

v26.13.1
import * as fs from "node:fs";
import * as path from "node:path";

fs.mkdirSync("sub", { recursive: true });
process.chdir("sub");
console.log(path.basename(process.cwd()));
Output
sub

Language differences

C: getcwd(buf, size) with your own buffer (POSIX.1-2008+ accepts NULL to have libc allocate).

C++: fs::current_path() returns a path object.

Go: os.Getwd() (falls back to inferring from $PWD when the syscall fails, possibly returning the symlinked path).

Java: the biggest divergence is the JVM — no chdir: user.dir is fixed at launch and immutable at runtime (use ProcessBuilder.directory() for child processes), so the Java block expresses the same idea via the target directory's absolute path file name.

JavaScript: Node process.cwd().

PHP: semantics agree closely everywhere — a process-level "current working directory" that all subsequent relative paths resolve against; returns string|false (false when the directory was deleted or permissions are lost); demos enter a subdirectory and print the basename, since absolute paths embed random temp names and the site requires deterministic output.

Python: os.getcwd().

Rust: env::current_dir() returns a Result.