| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 29 | #include <errno.h> | 
| Elliott Hughes | 05fc1d7 | 2015-01-28 18:02:33 -0800 | [diff] [blame] | 30 | #include <malloc.h> | 
|  | 31 | #include <string.h> | 
|  | 32 | #include <unistd.h> | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | extern "C" int __getcwd(char* buf, size_t size); | 
|  | 35 |  | 
| George Burgess IV | 9024235 | 2018-02-06 12:51:31 -0800 | [diff] [blame] | 36 | char* getcwd(char* buf, size_t size) { | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 37 | // You can't specify size 0 unless you're asking us to allocate for you. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 38 | if (buf != nullptr && size == 0) { | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 39 | errno = EINVAL; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 40 | return nullptr; | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 41 | } | 
|  | 42 |  | 
|  | 43 | // Allocate a buffer if necessary. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 44 | char* allocated_buf = nullptr; | 
| Elliott Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 45 | size_t allocated_size = size; | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 46 | if (buf == nullptr) { | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 47 | 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 Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 50 | allocated_size = getpagesize(); | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 51 | } | 
| Elliott Hughes | c4d1fec | 2012-08-28 14:15:04 -0700 | [diff] [blame] | 52 | buf = allocated_buf = static_cast<char*>(malloc(allocated_size)); | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 53 | if (buf == nullptr) { | 
|  | 54 | return nullptr; | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 55 | } | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | // Ask the kernel to fill our buffer. | 
| Elliott Hughes | 156da96 | 2012-10-09 17:14:56 -0700 | [diff] [blame] | 59 | int rc = __getcwd(buf, allocated_size); | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 60 | if (rc == -1) { | 
|  | 61 | free(allocated_buf); | 
|  | 62 | // __getcwd set errno. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 63 | return nullptr; | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
|  | 66 | // If we allocated a whole page, only return as large an allocation as necessary. | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 67 | if (allocated_buf != nullptr) { | 
| Elliott Hughes | 04a83a4 | 2012-08-16 15:59:12 -0700 | [diff] [blame] | 68 | if (size == 0) { | 
|  | 69 | buf = strdup(allocated_buf); | 
|  | 70 | free(allocated_buf); | 
|  | 71 | } else { | 
|  | 72 | buf = allocated_buf; | 
|  | 73 | } | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | return buf; | 
|  | 77 | } |