lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* Get current working directory. NaCl version. |
| 2 | Copyright (C) 2015 Free Software Foundation, Inc. |
| 3 | This file is part of the GNU C Library. |
| 4 | |
| 5 | The GNU C Library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | The GNU C Library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with the GNU C Library; if not, see |
| 17 | <http://www.gnu.org/licenses/>. */ |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | #include <limits.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <nacl-interfaces.h> |
| 23 | |
| 24 | /* Get the pathname of the current working directory, |
| 25 | and put it in SIZE bytes of BUF. Returns NULL if the |
| 26 | directory couldn't be determined or SIZE was too small. |
| 27 | If successful, returns BUF. In GNU, if BUF is NULL, |
| 28 | an array is allocated with `malloc'; the array is SIZE |
| 29 | bytes long, unless SIZE <= 0, in which case it is as |
| 30 | big as necessary. */ |
| 31 | char * |
| 32 | __getcwd (char *buf, size_t size) |
| 33 | { |
| 34 | char *use_buf = buf; |
| 35 | |
| 36 | if (buf == NULL) |
| 37 | { |
| 38 | if (size == 0) |
| 39 | size = PATH_MAX; |
| 40 | use_buf = malloc (size); |
| 41 | if (__glibc_unlikely (use_buf == NULL)) |
| 42 | return NULL; |
| 43 | } |
| 44 | |
| 45 | int error = __nacl_irt_dev_filename.getcwd (use_buf, size); |
| 46 | if (__glibc_unlikely (error)) |
| 47 | { |
| 48 | if (use_buf != buf) |
| 49 | free (use_buf); |
| 50 | errno = error; |
| 51 | return NULL; |
| 52 | } |
| 53 | |
| 54 | return use_buf; |
| 55 | } |
| 56 | weak_alias (__getcwd, getcwd) |