]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | /* Glibc.c provides access to some libc functions. |
2 | ||
83ffe9cd | 3 | Copyright (C) 2016-2023 Free Software Foundation, Inc. |
1eee94d3 GM |
4 | Contributed by Gaius Mulley <gaius@glam.ac.uk>. |
5 | ||
6 | This file is part of GNU Modula-2. | |
7 | ||
8 | GNU Modula-2 is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU Modula-2 is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GNU Modula-2; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | ||
25 | #if defined(__cplusplus) | |
26 | #define EXTERN extern "C" | |
27 | #else | |
28 | #define EXTERN | |
29 | #endif | |
30 | ||
31 | EXTERN | |
32 | int | |
33 | libc_read (int fd, void *a, int nbytes) | |
34 | { | |
35 | return read (fd, a, nbytes); | |
36 | } | |
37 | ||
38 | EXTERN | |
39 | int | |
40 | libc_write (int fd, void *a, int nbytes) | |
41 | { | |
42 | return write (fd, a, nbytes); | |
43 | } | |
44 | ||
45 | EXTERN | |
46 | int | |
47 | libc_close (int fd) | |
48 | { | |
49 | return close (fd); | |
50 | } | |
51 | ||
52 | EXTERN | |
53 | int | |
54 | libc_exit (int code) | |
55 | { | |
56 | exit (code); | |
57 | } | |
58 | ||
59 | EXTERN | |
60 | void | |
61 | libc_perror (char *s) | |
62 | { | |
63 | perror (s); | |
64 | } | |
65 | ||
66 | EXTERN | |
67 | int | |
68 | libc_abort () | |
69 | { | |
70 | abort (); | |
71 | } | |
72 | ||
73 | EXTERN | |
74 | int | |
75 | libc_strlen (char *s) | |
76 | { | |
77 | return strlen (s); | |
78 | } | |
79 | ||
80 | EXTERN | |
81 | int | |
82 | libc_printf (char *_format, unsigned int _format_high, ...) | |
83 | { | |
84 | va_list arg; | |
85 | int done; | |
86 | char format[_format_high + 1]; | |
87 | unsigned int i = 0; | |
88 | unsigned int j = 0; | |
89 | char *c; | |
90 | ||
91 | do | |
92 | { | |
93 | c = index (&_format[i], '\\'); | |
94 | if (c == NULL) | |
95 | strcpy (&format[j], &_format[i]); | |
96 | else | |
97 | { | |
98 | memcpy (&format[j], &_format[i], (c - _format) - i); | |
99 | i = c - _format; | |
100 | j += c - _format; | |
101 | if (_format[i + 1] == 'n') | |
102 | format[j] = '\n'; | |
103 | else | |
104 | format[j] = _format[i + 1]; | |
105 | j++; | |
106 | i += 2; | |
107 | } | |
108 | } | |
109 | while (c != NULL); | |
110 | ||
111 | va_start (arg, _format_high); | |
112 | done = vfprintf (stdout, format, arg); | |
113 | va_end (arg); | |
114 | ||
115 | return done; | |
116 | } | |
117 | ||
118 | EXTERN | |
119 | void * | |
120 | libc_malloc (unsigned int size) | |
121 | { | |
122 | return malloc (size); | |
123 | } | |
124 | ||
125 | EXTERN | |
126 | void | |
127 | libc_free (void *p) | |
128 | { | |
129 | free (p); | |
130 | } | |
131 | ||
132 | EXTERN | |
133 | char * | |
134 | libc_strcpy (char *dest, char *src) | |
135 | { | |
136 | return strcpy (dest, src); | |
137 | } | |
138 | ||
139 | EXTERN | |
140 | char * | |
141 | libc_strncpy (char *dest, char *src, int n) | |
142 | { | |
143 | return strncpy (dest, src, n); | |
144 | } | |
145 | ||
146 | EXTERN | |
147 | int | |
148 | libc_unlink (char *p) | |
149 | { | |
150 | return unlink (p); | |
151 | } | |
152 | ||
153 | EXTERN | |
154 | int | |
155 | libc_system (char *command) | |
156 | { | |
157 | return system (command); | |
158 | } | |
159 | ||
160 | EXTERN | |
161 | void * | |
162 | libc_memcpy (void *dest, void *src, int n) | |
163 | { | |
164 | return memcpy (dest, src, n); | |
165 | } | |
166 | ||
167 | EXTERN | |
168 | char * | |
169 | libc_getenv (char *name) | |
170 | { | |
171 | return getenv (name); | |
172 | } | |
173 | ||
174 | EXTERN | |
175 | int | |
176 | libc_putenv (char *name) | |
177 | { | |
178 | return putenv (name); | |
179 | } | |
180 | ||
181 | EXTERN | |
182 | int | |
183 | libc_creat (char *p, mode_t mode) | |
184 | { | |
185 | return creat (p, mode); | |
186 | } | |
187 | ||
188 | EXTERN | |
189 | int | |
190 | libc_open (char *p, int flags, mode_t mode) | |
191 | { | |
192 | return open (p, flags, mode); | |
193 | } | |
194 | ||
195 | EXTERN | |
196 | off_t | |
197 | libc_lseek (int fd, off_t offset, int whence) | |
198 | { | |
199 | return lseek (fd, offset, whence); | |
200 | } | |
201 | ||
202 | EXTERN | |
203 | void * | |
204 | libc_realloc (void *ptr, size_t size) | |
205 | { | |
206 | return realloc (ptr, size); | |
207 | } | |
208 | ||
209 | EXTERN | |
210 | void * | |
211 | libc_memset (void *s, int c, size_t n) | |
212 | { | |
213 | return memset (s, c, n); | |
214 | } | |
215 | ||
216 | EXTERN | |
217 | void * | |
218 | libc_memmove (void *dest, void *src, size_t n) | |
219 | { | |
220 | return memmove (dest, src, n); | |
221 | } | |
222 | ||
223 | EXTERN | |
224 | int | |
225 | libc_getpid (void) | |
226 | { | |
227 | return getpid (); | |
228 | } | |
229 | ||
230 | EXTERN | |
231 | unsigned int | |
232 | libc_sleep (unsigned int s) | |
233 | { | |
234 | return sleep (s); | |
235 | } | |
236 | ||
237 | EXTERN | |
238 | int | |
239 | libc_atexit (void (*function) (void)) | |
240 | { | |
241 | return atexit (function); | |
242 | } |