1fc05425ca354ac8ec9bd549ddc36d74ff804bb0
[bootcensus] / src / libc / unistd.c
1 /*
2 pcboot - bootable PC demo/game kernel
3 Copyright (C) 2018-2019  John Tsiombikas <nuclear@member.fsf.org>
4
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.
9
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.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include <errno.h>
20 #include "unistd.h"
21 #include "fs.h"
22
23 int chdir(const char *path)
24 {
25         return fs_chdir(path);
26 }
27
28 char *getcwd(char *buf, int sz)
29 {
30         char *cwd = fs_getcwd();
31         int len = strlen(cwd);
32         if(len + 1 > sz) {
33                 errno = ERANGE;
34                 return 0;
35         }
36         memcpy(buf, cwd, len + 1);
37         return buf;
38 }
39
40 int mkdir(const char *path, int mode)
41 {
42         struct fs_node *fsn;
43
44         if(!(fsn = fs_open(path, FSO_CREATE | FSO_DIR | FSO_EXCL))) {
45                 return -1;
46         }
47         fs_close(fsn);
48         return 0;
49 }
50
51 int rmdir(const char *path)
52 {
53         struct fs_node *fsn;
54
55         if(!(fsn = fs_open(path, FSO_DIR))) {
56                 return -1;
57         }
58         fs_remove(fsn);
59         fs_close(fsn);
60         return 0;
61 }