foo
[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 #include "timer.h"
23 #include "asmops.h"
24
25 int chdir(const char *path)
26 {
27         return fs_chdir(path);
28 }
29
30 char *getcwd(char *buf, int sz)
31 {
32         char *cwd = fs_getcwd();
33         int len = strlen(cwd);
34         if(len + 1 > sz) {
35                 errno = ERANGE;
36                 return 0;
37         }
38         memcpy(buf, cwd, len + 1);
39         return buf;
40 }
41
42 int mkdir(const char *path, int mode)
43 {
44         struct fs_node *fsn;
45
46         if(!(fsn = fs_open(path, FSO_CREATE | FSO_DIR | FSO_EXCL))) {
47                 return -1;
48         }
49         fs_close(fsn);
50         return 0;
51 }
52
53 int rmdir(const char *path)
54 {
55         struct fs_node *fsn;
56
57         if(!(fsn = fs_open(path, FSO_DIR))) {
58                 return -1;
59         }
60         fs_remove(fsn);
61         fs_close(fsn);
62         return 0;
63 }
64
65 int usleep(unsigned long usec)
66 {
67         unsigned long wait_ticks = MSEC_TO_TICKS(usec / 1000ul);
68         unsigned long start_ticks = nticks;
69         while(nticks - start_ticks < wait_ticks) {
70                 halt_cpu();
71         }
72         return 0;
73 }