From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f177.google.com (mail-pl1-f177.google.com [209.85.214.177]) (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 9718540B38C for ; Tue, 30 Jun 2026 13:24:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.177 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782825875; cv=none; b=glJLlV0ChBfrw8WTwaapxzqRZRUnwpUQUBkezd1mbZp0PPLu3Hhn0OLZfToRkurYrey0fa4nBUNHzvruOvcfwEcY5THhQCrh3zbp6+ODiU8RzySrpAHkL45CT7o0uxK1YQiDJbiO+kbdnTvYknDD3aiocuOUic0HzL65K+WlQ9M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782825875; c=relaxed/simple; bh=eDF5Tjq3li/8JjbfP4dMK+RweBX8Y2ybHL/47acQfhA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u87fEfFuQK3HryV2RCHsCT0Gjo6lha5COUfGfiCk7/dTP7aHQXJZ8klAso5u5PqMDkYvWel92OGu93537YV0MjKvlpqmtgpguOZFIX9l97fw3RqP0lGHJtyfmVOhYK4YIYKhvE1HxJRwYGZwTa26L63dviDpQddN3amYo0WGxks= 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=pcowkiBr; arc=none smtp.client-ip=209.85.214.177 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="pcowkiBr" Received: by mail-pl1-f177.google.com with SMTP id d9443c01a7336-2c82da9504cso13124305ad.2 for ; Tue, 30 Jun 2026 06:24:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=thingy.jp; s=google; t=1782825873; x=1783430673; 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; bh=B/1/XnLwqPJJgXgzDJA+4dmEkMNHYSqlmNKvjE7Qf3Q=; b=pcowkiBr2+KcZqsicQvEw0p1NRxEwmCzEvQ7CboyvU+ZrhTLLcOfEegHjDIZGrHYeH DNlj4Yq60gk7GhKyZ35nLWwK8fs4fm/AJuSNp/iabSk0TIufPcqXpC6rzN2Lm06sF6yp iOSn1Rb4eTvt3N2jWN9iOy02RFfht6IYmTrBw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1782825873; x=1783430673; 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; bh=B/1/XnLwqPJJgXgzDJA+4dmEkMNHYSqlmNKvjE7Qf3Q=; b=pVbRFu2uA0ylmvtoULjLJQZOqSP1nxYlGovtU3v+blduayjiC6H1RyW9Nx8MMvmy7B FkFQnrDZbVHXwtXxR7trkpEytQ/VTFmwxsKUVynVYPdvZw9KZ3Hy5CIa3y+RSV8bw0+a yhGL1ODM1elN2qDEFcvvfwGiJTYAwIzD7Voi+ehouxwzRgwY+Tx1+5wTvMXu07rhZlm1 rHlFH32zf7LRA251js7LSpgQiGZ9GqA1k3fMjDappPvukyNFmCC5AmcibPsTlcQ9HrCo ONFXfoF0I6X7wpdAaV67QB0x7WBmirZHgf4gVU9D1ItwGoLdGAG9S31owhUaGLMTa1we 1toA== X-Gm-Message-State: AOJu0Yy00BhTjwHrZ/WR6buvNbBXK8tuBAFpaK/ufHww03dUUrwdf3n6 brCLMnzHNA7pwDrpBRLf62jpDOvBQz3dLH2tNp1iZmkSyAeIKU06CIb/XrIUoTjzrd19DZz08U3 KTCpR X-Gm-Gg: AfdE7cnEHXS/U2JyJw/2LBhHcddgXuz+R6eTwObAjfYI17+fGJf1f9BuzrH+ZFrSMBU ypkygu3gLbCHAcRE2P3Qd8BiXbojUeIssS8HRvJgmyq34prfUJ5s8cWUdVvxCtw4AN7FpWB9BRm Mn5qUWUxeWabTmdi81vhAvd4H7QXe9J1Txcew/fVubZRcKj60LgaSB8bupB8ISvmVz16xsPcLD+ hzT1I/2rqxkZpQSq9iHcoIT994TAI9k4IKoGO24otuvsq5YwV3a1/QLxsdLMB8UkFKhW7JZtbIC I+hGIYupRuUaviZ1k0VIto3A4B7bo9fxpTQEUusRHLb4vFbAucXbqkbQ+iDKtNBvYifwE1dF79d ee8GBmcAe15gsleHW0m1cxH2jsrz1yyEUXo9jXjuGZBAzXLKDGbXfGP1Uv16qQT9//BVc8xCD5s NVP8pMsuHmzcpKVf8hWWfbDdvKKQ== X-Received: by 2002:a17:903:388c:b0:2c0:f807:9bf3 with SMTP id d9443c01a7336-2ca2d580258mr27666885ad.10.1782825872674; Tue, 30 Jun 2026 06:24:32 -0700 (PDT) Received: from kinako.work.home.arpa ([2400:4162:2428:2ffe:a973:53e4:1a28:8545]) by smtp.googlemail.com with ESMTPSA id d9443c01a7336-2ca382abefdsm14716155ad.59.2026.06.30.06.24.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jun 2026 06:24:32 -0700 (PDT) From: Daniel Palmer To: w@1wt.eu, linux@weissschuh.net Cc: linux-kernel@vger.kernel.org, Daniel Palmer Subject: [PATCH v2 1/3] tools/nolibc: unistd: Add getcwd() Date: Tue, 30 Jun 2026 22:24:17 +0900 Message-ID: <20260630132419.2523890-2-daniel@thingy.jp> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260630132419.2523890-1-daniel@thingy.jp> References: <20260630132419.2523890-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 | 40 +++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tools/include/nolibc/unistd.h b/tools/include/nolibc/unistd.h index 79599ceef45d..057fc3a94179 100644 --- a/tools/include/nolibc/unistd.h +++ b/tools/include/nolibc/unistd.h @@ -73,6 +73,46 @@ 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, + * we just call the syscall, if you passed NULL you'll get + * an error. + */ + 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