/* Copyright (C) 2008 Emmanuel Varoquaux This file is part of XOS. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ #include "unistd.h" #include "fcntl.h" #include <limits.h> #include <stdio.h> #include <errno.h> char __attribute__ ((weak, visibility ("default"))) *getcwd(char *buf, size_t size) { char cwd_filename[PATH_MAX]; int fd; ssize_t n; int errnum; if (!buf || !size) { errno = EINVAL; return NULL; } snprintf(cwd_filename, PATH_MAX, "/proc/%d/cwd", __getpid()); if ((fd = __open(cwd_filename, O_RDONLY)) == -1) return NULL; if ((n = __read(fd, buf, size)) == -1) { errnum = errno; __close(fd); errno = errnum; return NULL; } __close(fd); if ((size_t)n >= size) { errno = ERANGE; return NULL; } buf[n] = '\0'; return buf; }