From 4b72e9529287740e110a687cf33cdd7e7e3c9cec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Wed, 16 Feb 2005 03:51:34 +0000 Subject: [PATCH] getcwd() is actually implemented like elsewhere, and not strictly after POSIX demands: if you pass in a NULL pointer, a buffer will now be allocated for you. git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11388 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/kernel/libroot/posix/unistd/directory.c | 27 +++++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/kernel/libroot/posix/unistd/directory.c b/src/kernel/libroot/posix/unistd/directory.c index 54de82c661..482ec1a1b1 100644 --- a/src/kernel/libroot/posix/unistd/directory.c +++ b/src/kernel/libroot/posix/unistd/directory.c @@ -1,12 +1,13 @@ -/* -** Copyright 2002-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved. -** Distributed under the terms of the Haiku License. -*/ +/* + * Copyright 2002-2005, Axel Dörfler, axeld@pinc-software.de. All rights reserved. + * Distributed under the terms of the MIT License. + */ #include #include +#include #include @@ -39,8 +40,24 @@ fchdir(int fd) char * getcwd(char *buffer, size_t size) { - int status = _kern_getcwd(buffer, size); + bool allocated = false; + status_t status; + + if (buffer == NULL) { + buffer = malloc(size = PATH_MAX); + if (buffer == NULL) { + errno = B_NO_MEMORY; + return NULL; + } + + allocated = true; + } + + status = _kern_getcwd(buffer, size); if (status < B_OK) { + if (allocated) + free(buffer); + errno = status; return NULL; }