blob: a8bbcf31b4fba261b025e67985c2d5dd5444c0a9 [file] [log] [blame]
Elliott Hughes04a83a42012-08-16 15:59:12 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Elliott Hughes04a83a42012-08-16 15:59:12 -070029#include <errno.h>
Elliott Hughes05fc1d72015-01-28 18:02:33 -080030#include <malloc.h>
31#include <string.h>
32#include <unistd.h>
Elliott Hughes04a83a42012-08-16 15:59:12 -070033
34extern "C" int __getcwd(char* buf, size_t size);
35
36char* getcwd(char* buf, size_t size) {
37 // You can't specify size 0 unless you're asking us to allocate for you.
38 if (buf != NULL && size == 0) {
39 errno = EINVAL;
40 return NULL;
41 }
42
43 // Allocate a buffer if necessary.
44 char* allocated_buf = NULL;
Elliott Hughes156da962012-10-09 17:14:56 -070045 size_t allocated_size = size;
Elliott Hughes04a83a42012-08-16 15:59:12 -070046 if (buf == NULL) {
Elliott Hughes04a83a42012-08-16 15:59:12 -070047 if (size == 0) {
48 // The Linux kernel won't return more than a page, so translate size 0 to 4KiB.
49 // TODO: if we need to support paths longer than that, we'll have to walk the tree ourselves.
Elliott Hughes156da962012-10-09 17:14:56 -070050 allocated_size = getpagesize();
Elliott Hughes04a83a42012-08-16 15:59:12 -070051 }
Elliott Hughesc4d1fec2012-08-28 14:15:04 -070052 buf = allocated_buf = static_cast<char*>(malloc(allocated_size));
Elliott Hughes04a83a42012-08-16 15:59:12 -070053 if (buf == NULL) {
Elliott Hughes156da962012-10-09 17:14:56 -070054 // malloc should set errno, but valgrind's malloc wrapper doesn't.
55 errno = ENOMEM;
Elliott Hughes04a83a42012-08-16 15:59:12 -070056 return NULL;
57 }
58 }
59
60 // Ask the kernel to fill our buffer.
Elliott Hughes156da962012-10-09 17:14:56 -070061 int rc = __getcwd(buf, allocated_size);
Elliott Hughes04a83a42012-08-16 15:59:12 -070062 if (rc == -1) {
63 free(allocated_buf);
64 // __getcwd set errno.
65 return NULL;
66 }
67
68 // If we allocated a whole page, only return as large an allocation as necessary.
69 if (allocated_buf != NULL) {
70 if (size == 0) {
71 buf = strdup(allocated_buf);
72 free(allocated_buf);
73 } else {
74 buf = allocated_buf;
75 }
76 }
77
78 return buf;
79}