Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / libc / unistd / getcwd.c
blobb975c87bad49eb0c3c7264c34c49ed8757d2475e
1 /*
2 * ZeX/OS
3 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program 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
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
25 char *getcwd (char *buf, size_t size)
27 char *cwd = getenv ("PWD");
29 if (!cwd) {
30 errno = ENOENT;
31 return 0;
34 unsigned l = strlen (cwd);
36 if (!buf) {
37 if (!size)
38 size = l;
39 else {
40 errno = EINVAL;
41 return 0;
44 buf = (char *) malloc (sizeof (char) * size);
46 if (!buf) {
47 errno = EFAULT;
48 return 0;
53 if (l > size) {
54 errno = ERANGE;
55 return 0;
58 memcpy (buf, cwd, l);
59 buf[l] = '\0';
61 return buf;