104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes/*
204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * Copyright (C) 2008 The Android Open Source Project
304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * All rights reserved.
404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *
504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * Redistribution and use in source and binary forms, with or without
604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * modification, are permitted provided that the following conditions
704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * are met:
804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *  * Redistributions of source code must retain the above copyright
904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *    notice, this list of conditions and the following disclaimer.
1004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *  * Redistributions in binary form must reproduce the above copyright
1104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *    notice, this list of conditions and the following disclaimer in
1204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *    the documentation and/or other materials provided with the
1304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *    distribution.
1404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes *
1504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
1804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
1904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
2504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes * SUCH DAMAGE.
2704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes */
2804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
2904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes#include <unistd.h>
3004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes#include <errno.h>
3104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
3204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughesextern "C" int __getcwd(char* buf, size_t size);
3304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
3404a83a48ed89f433c78e31106ed50059764797a0Elliott Hugheschar* getcwd(char* buf, size_t size) {
3504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  // You can't specify size 0 unless you're asking us to allocate for you.
3604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  if (buf != NULL && size == 0) {
3704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    errno = EINVAL;
3804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    return NULL;
3904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  }
4004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
4104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  // Allocate a buffer if necessary.
4204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  char* allocated_buf = NULL;
43156da966214957c5616a0b83cc84686eedfc4e31Elliott Hughes  size_t allocated_size = size;
4404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  if (buf == NULL) {
4504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    if (size == 0) {
4604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes      // The Linux kernel won't return more than a page, so translate size 0 to 4KiB.
4704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes      // TODO: if we need to support paths longer than that, we'll have to walk the tree ourselves.
48156da966214957c5616a0b83cc84686eedfc4e31Elliott Hughes      allocated_size = getpagesize();
4904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    }
50c4d1fecc105063e68a5090a6900b63d1b9a24287Elliott Hughes    buf = allocated_buf = static_cast<char*>(malloc(allocated_size));
5104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    if (buf == NULL) {
52156da966214957c5616a0b83cc84686eedfc4e31Elliott Hughes      // malloc should set errno, but valgrind's malloc wrapper doesn't.
53156da966214957c5616a0b83cc84686eedfc4e31Elliott Hughes      errno = ENOMEM;
5404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes      return NULL;
5504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    }
5604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  }
5704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
5804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  // Ask the kernel to fill our buffer.
59156da966214957c5616a0b83cc84686eedfc4e31Elliott Hughes  int rc = __getcwd(buf, allocated_size);
6004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  if (rc == -1) {
6104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    free(allocated_buf);
6204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    // __getcwd set errno.
6304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    return NULL;
6404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  }
6504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
6604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  // If we allocated a whole page, only return as large an allocation as necessary.
6704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  if (allocated_buf != NULL) {
6804a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    if (size == 0) {
6904a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes      buf = strdup(allocated_buf);
7004a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes      free(allocated_buf);
7104a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    } else {
7204a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes      buf = allocated_buf;
7304a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes    }
7404a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  }
7504a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes
7604a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes  return buf;
7704a83a48ed89f433c78e31106ed50059764797a0Elliott Hughes}
78