/[osn-commons]/trunk/plibc/lib/unistd.c
ViewVC logotype

Annotation of /trunk/plibc/lib/unistd.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 51 - (hide annotations)
Fri Aug 16 18:34:43 2024 UTC (7 months, 2 weeks ago) by rakinar2
File MIME type: text/x-c
File size: 1802 byte(s)
feat: add basic utilities and more system calls

1 rakinar2 51 #include "unistd.h"
2     #include "errno.h"
3     #include "stdbool.h"
4     #include "stdio.h"
5     #include "syscalls.h"
6    
7     volatile bool plibc_sfds_status_array[3] = { true, true, true };
8    
9     static inline int
10     syscall_wrapper (int ret)
11     {
12     if (ret < 0)
13     {
14     errno = -ret;
15     return -1;
16     }
17    
18     return ret;
19     }
20    
21     int
22     write (int fd, const void *buf, size_t count)
23     {
24     return syscall_wrapper (sys_write (fd, buf, count));
25     }
26    
27     void *
28     sbrk (intptr_t increment)
29     {
30     void *ptr = sys_sbrk (increment);
31    
32     if (ptr == (void *) -1)
33     {
34     errno = ENOMEM;
35     return (void *) -1;
36     }
37    
38     return ptr;
39     }
40    
41     void *
42     brk (void)
43     {
44     void *ptr = sys_brk ();
45    
46     if (ptr == (void *) -1)
47     {
48     errno = ENOMEM;
49     return (void *) -1;
50     }
51    
52     return ptr;
53     }
54    
55     void
56     exit (int status)
57     {
58     sys_exit (status);
59     }
60    
61     void *
62     mmap (void *addr, size_t len, int prot, int flags, int fd, off_t offset)
63     {
64     void *ptr = sys_mmap (addr, len, prot, flags, fd, offset);
65    
66     if (ptr == (void *) -1)
67     {
68     errno = ENOMEM;
69     return (void *) -1;
70     }
71    
72     return ptr;
73     }
74    
75     void *
76     sysinfo (void *buffer)
77     {
78     void *ptr = sys_sysinfo (buffer);
79    
80     if (ptr == (void *) -1)
81     {
82     errno = ENOMEM;
83     return (void *) -1;
84     }
85    
86     return ptr;
87     }
88    
89     int
90     kill (pid_t pid, int sig)
91     {
92     return syscall_wrapper (sys_kill (pid, sig));
93     }
94    
95     int
96     open (const char *pathname, int flags)
97     {
98     return syscall_wrapper (sys_open (pathname, flags, 0644));
99     }
100    
101     int
102     close (int fd)
103     {
104     int ret = syscall_wrapper (sys_close (fd));
105    
106     if (ret < 0)
107     return ret;
108    
109     if (fd < 3)
110     plibc_sfds_status_array[fd] = false;
111    
112     return ret;
113     }
114    
115     const volatile bool *
116     plibc_sfds_status ()
117     {
118     return plibc_sfds_status_array;
119     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26