From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pg1-f173.google.com (mail-pg1-f173.google.com [209.85.215.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 404F533AD8B for ; Thu, 2 Jul 2026 08:51:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782982278; cv=none; b=Hfj+l26aqT3IdugLOUGEIdxAvYDWEEG9aKQwbXBd98HTkfSk+tO4xNZCzMD4zUbdDzh/BEaax8RbZ3CFU8dw/KuxrNxFhPk6F6ps3pK2I09XEPfTItVV369Y5UgUzCHKv+B/gv0TyXqyeSs4/kB062Xk0xmJPZSYQaPiTjb9PMI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782982278; c=relaxed/simple; bh=QMvRVQgQtQX0n0VHGZqFMtSZhmFmRxnWOpd7pWVQUDI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dgiaqgoSbe+FMU+AT9a5IVDwT04rl1jwiKJNWc4YrF28mz83XdwJFTlFX8LwzFj9rtmchR19bD8RjPQgkqmtV5hl9jouHwFL5WRXhbiSIjU+kPHd3ysCE92S8QJjzMoMGk7/hqXIzYH8D3mwEu0c7Y7wQIlfuMUcT7JYBZRNFmM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thingy.jp; spf=pass smtp.mailfrom=0x0f.com; dkim=pass (1024-bit key) header.d=thingy.jp header.i=@thingy.jp header.b=kQw38k6N; arc=none smtp.client-ip=209.85.215.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=thingy.jp Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=0x0f.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=thingy.jp header.i=@thingy.jp header.b="kQw38k6N" Received: by mail-pg1-f173.google.com with SMTP id 41be03b00d2f7-c8fee9f63d5so789542a12.0 for ; Thu, 02 Jul 2026 01:51:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thingy.jp; s=google; t=1782982276; x=1783587076; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DSr8UQySB5Q1j3yFwGeLY/i/7RPjQgKqmdsQZapQabY=; b=kQw38k6NB7S3e+5ltkvcPek3u77noV5TJcu3xSH+CfbRmpXju0CgBAXEzNlKZeDvoW 644UeQnum8ZPU7ngZOpWprwDcXQGIQ4kszEJn0NS20ngb9fdRfXMXGeysluGrKp6E+AW jQxaAWoxR4ZnHtNI2zRxYB2BpRVYaslwckrDk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782982276; x=1783587076; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=DSr8UQySB5Q1j3yFwGeLY/i/7RPjQgKqmdsQZapQabY=; b=OEH1jSxXZ13Vc84gzTV4CzmkeEoIPPJzkkXTVbSwcrNykINBWMAlJFsNrD91ngCKmq 408lA7DSjv1Nca7HSzuce7z0IEW0rp3IUzX82kH7nOMl68XioMFwlJN2yUjbl0QncTCM GpmCGpa80UVicagRdKzDmzxSkEdeKmyQ5dxSCJGlk454cvNsptLEuynUtzC8iCZZejWy fqhylCH9516S+hcX3Yoe0+dCMV6V7dmP7rrIB18Iq+JdBmj1ikCDncHoPiah2PVj+EWn 4ctp2fNrB7OMPzJS1hnYZCMLbjqsK7paw13XD0ue+fK5xbzupnrHsWqfy6yqaq4NMidz b78A== X-Gm-Message-State: AOJu0YypMq47KMrRaP1dSc+QpodzKpFC5p4m/XxAdZklOVL4CvSr+jKu O12ls965DHfsNOo/Xv2bIK37O5so7uNCBcuH0wcZM9tqIBEQ6fIACpQ+qZsifxXw2fM= X-Gm-Gg: AfdE7ckc+GDomJ0E9ZARVC8TK7BH8j5QMn2xlOJxef/giCy3Uu/S+z2pv0F8sat/Y7c OR0zHVmqpooI37vXB15n2UFMkOQQCYwUi7VfRu1GISbcY9ifthwJU7XLReRu6O+NUVVxFVqH8QP xIj4nPSaCnuK/6LjVWgyJDHafi/UGFS96qlxvlPngYffWgErjXyewsBHP/GwQbtNAtXr25KEM65 h8TcJnjfQoROQuB41fiPOpvsFSQgAj4t7sHbN2OETKtaJ8Dxqf/jUE6n8NzYazSyAJU31pdV/N5 EtpdBzoCQ8vcy0LYJ09RlwYqhX77zzgsS0I0RngMt11IUPyHnaM49L9FDIJYzoyTmeSnprKymsl Jkx0JOCe2OPomzvMfG/IOu4nq29IGXssIWT35ESe/cya1W2vt7HAfexYpQYCc466cf6Cye9vJOU JxN8NCTdOPXaRWfzJAf8QK19Ewaw== X-Received: by 2002:a05:6a20:6a26:b0:3bf:a698:ce4d with SMTP id adf61e73a8af0-3bff42ef857mr5081186637.54.1782982276068; Thu, 02 Jul 2026 01:51:16 -0700 (PDT) Received: from kinako.work.home.arpa ([2400:4162:2428:2ffe:a973:53e4:1a28:8545]) by smtp.googlemail.com with ESMTPSA id 41be03b00d2f7-c9e8f4cd4b1sm944649a12.12.2026.07.02.01.51.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Jul 2026 01:51:15 -0700 (PDT) From: Daniel Palmer To: w@1wt.eu, linux@weissschuh.net Cc: linux-kernel@vger.kernel.org, Daniel Palmer Subject: [PATCH v3 1/3] tools/nolibc: unistd: Add getcwd() Date: Thu, 2 Jul 2026 17:50:59 +0900 Message-ID: <20260702085101.3304547-2-daniel@thingy.jp> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260702085101.3304547-1-daniel@thingy.jp> References: <20260702085101.3304547-1-daniel@thingy.jp> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Add getcwd() for getting the current working directory. The behaviour matches what musl is doing except for one important difference: If the passed buf is NULL musl (and glibc) uses a big buffer on the stack and that is then strdup()'d and returned. According to the man page for getcwd() this is a glibc extension and I don't think we need it in nolibc. Signed-off-by: Daniel Palmer --- tools/include/nolibc/unistd.h | 42 +++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index 79599ceef45d..2e0edbc26315 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -73,6 +73,48 @@ int ftruncate(int fd, off_t length) return __sysret(_sys_ftruncate(fd, length)); } +/* + * char *getcwd(char *buf, size_t size); + */ + +static __attribute__((unused)) +int _sys_getcwd(char *buf, size_t size) +{ + return __nolibc_syscall2(__NR_getcwd, buf, size); +} + +static __attribute__((unused)) +char *getcwd(char *buf, size_t size) +{ + int ret; + + /* Unlike other libc's we don't handle passing NULL for buf */ + if (!buf || !size) { + SET_ERRNO(EINVAL); + return NULL; + } + + ret = __sysret(_sys_getcwd(buf, size)); + + /* On error return NULL, __sysret() above will have set errno */ + if (ret < 0) + return NULL; + + /* Handle no path being written or the kernel putting + * "(unreachable)" into the buffer instead of a path. + * This matches what musl is doing. + */ + if (ret == 0 || buf[0] != '/') { + SET_ERRNO(ENOENT); + return NULL; + } + + /* ret must be the number of bytes written at this point, + * so return the pointer to buf. + */ + return buf; +} + static __attribute__((unused)) int msleep(unsigned int msecs) { -- 2.53.0