summaryrefslogtreecommitdiff
path: root/libc/sysdeps
diff options
context:
space:
mode:
Diffstat (limited to 'libc/sysdeps')
-rw-r--r--libc/sysdeps/linux/common/getcwd.c53
1 files changed, 28 insertions, 25 deletions
diff --git a/libc/sysdeps/linux/common/getcwd.c b/libc/sysdeps/linux/common/getcwd.c
index 9cdd84b47..9e64a3dd8 100644
--- a/libc/sysdeps/linux/common/getcwd.c
+++ b/libc/sysdeps/linux/common/getcwd.c
@@ -17,33 +17,36 @@ static inline _syscall2(int, __syscall_getcwd, char *, buf, unsigned long, size)
char *getcwd(char *buf, int size)
{
- int olderrno, ret;
- char *allocbuf;
-
- if (size == 0) {
- __set_errno(EINVAL);
- return NULL;
- }
- if (size < 3) {
- __set_errno(ERANGE);
- return NULL;
- }
- allocbuf=NULL;
- olderrno = errno;
- if (buf == NULL) {
- buf = allocbuf = malloc (size);
- if (buf == NULL)
- return NULL;
+ int olderrno, ret;
+ char *allocbuf;
+
+ if (size == 0) {
+ if (buf != NULL) {
+ __set_errno(EINVAL);
+ return NULL;
}
- ret = INLINE_SYSCALL(getcwd, 2, buf, size);
- if (ret < 0) {
- if (allocbuf) {
- free(allocbuf);
- }
+ size = PATH_MAX;
+ }
+ if (size < 3) {
+ __set_errno(ERANGE);
+ return NULL;
+ }
+ allocbuf=NULL;
+ olderrno = errno;
+ if (buf == NULL) {
+ buf = allocbuf = malloc (size);
+ if (buf == NULL)
return NULL;
- }
- __set_errno(olderrno);
- return buf;
+ }
+ ret = INLINE_SYSCALL(getcwd, 2, buf, size);
+ if (ret < 0) {
+ if (allocbuf) {
+ free(allocbuf);
+ }
+ return NULL;
+ }
+ __set_errno(olderrno);
+ return buf;
}
#else