mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-09-10 21:40:07 -04:00
tools/nolibc: unistd: Add getcwd()
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 <daniel@thingy.jp> Acked-by: Willy Tarreau <w@1wt.eu> Link: https://patch.msgid.link/20260702085101.3304547-2-daniel@thingy.jp Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
This commit is contained in:
committed by
Thomas Weißschuh
parent
dc59e4fea9
commit
0fbc34f028
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user