1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Zarevucky
|
---|
3 | * Copyright (c) 2011 Petr Koupy
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * Redistribution and use in source and binary forms, with or without
|
---|
7 | * modification, are permitted provided that the following conditions
|
---|
8 | * are met:
|
---|
9 | *
|
---|
10 | * - Redistributions of source code must retain the above copyright
|
---|
11 | * notice, this list of conditions and the following disclaimer.
|
---|
12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer in the
|
---|
14 | * documentation and/or other materials provided with the distribution.
|
---|
15 | * - The name of the author may not be used to endorse or promote products
|
---|
16 | * derived from this software without specific prior written permission.
|
---|
17 | *
|
---|
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
28 | */
|
---|
29 |
|
---|
30 | /** @addtogroup libposix
|
---|
31 | * @{
|
---|
32 | */
|
---|
33 | /** @file Standard buffered input/output.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #define LIBPOSIX_INTERNAL
|
---|
37 |
|
---|
38 | /* Has to be first. */
|
---|
39 | #include "stdbool.h"
|
---|
40 |
|
---|
41 | #include "internal/common.h"
|
---|
42 | #include "stdio.h"
|
---|
43 |
|
---|
44 | #include "assert.h"
|
---|
45 | #include "errno.h"
|
---|
46 | #include "string.h"
|
---|
47 | #include "sys/types.h"
|
---|
48 |
|
---|
49 | #include "libc/io/printf_core.h"
|
---|
50 | #include "libc/str.h"
|
---|
51 | #include "libc/malloc.h"
|
---|
52 | #include "libc/adt/list.h"
|
---|
53 | #include "libc/sys/stat.h"
|
---|
54 |
|
---|
55 |
|
---|
56 | /* not the best of solutions, but freopen and ungetc will eventually
|
---|
57 | * need to be implemented in libc anyway
|
---|
58 | */
|
---|
59 | #include "../c/generic/private/stdio.h"
|
---|
60 |
|
---|
61 | /** Clears the stream's error and end-of-file indicators.
|
---|
62 | *
|
---|
63 | * @param stream Stream whose indicators shall be cleared.
|
---|
64 | */
|
---|
65 | void posix_clearerr(FILE *stream)
|
---|
66 | {
|
---|
67 | stream->error = 0;
|
---|
68 | stream->eof = 0;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Generate a pathname for the controlling terminal.
|
---|
73 | *
|
---|
74 | * @param s Allocated buffer to which the pathname shall be put.
|
---|
75 | * @return Either s or static location filled with the requested pathname.
|
---|
76 | */
|
---|
77 | char *posix_ctermid(char *s)
|
---|
78 | {
|
---|
79 | /* Currently always returns an error value (empty string). */
|
---|
80 | // TODO: return a real terminal path
|
---|
81 |
|
---|
82 | static char dummy_path[L_ctermid] = {'\0'};
|
---|
83 |
|
---|
84 | if (s == NULL) {
|
---|
85 | return dummy_path;
|
---|
86 | }
|
---|
87 |
|
---|
88 | s[0] = '\0';
|
---|
89 | return s;
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Put a string on the stream.
|
---|
94 | *
|
---|
95 | * @param s String to be written.
|
---|
96 | * @param stream Output stream.
|
---|
97 | * @return Non-negative on success, EOF on failure.
|
---|
98 | */
|
---|
99 | int posix_fputs(const char *restrict s, FILE *restrict stream)
|
---|
100 | {
|
---|
101 | int rc = fputs(s, stream);
|
---|
102 | if (rc == 0) {
|
---|
103 | return EOF;
|
---|
104 | } else {
|
---|
105 | return 0;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Push byte back into input stream.
|
---|
111 | *
|
---|
112 | * @param c Byte to be pushed back.
|
---|
113 | * @param stream Stream to where the byte shall be pushed.
|
---|
114 | * @return Provided byte on success or EOF if not possible.
|
---|
115 | */
|
---|
116 | int posix_ungetc(int c, FILE *stream)
|
---|
117 | {
|
---|
118 | uint8_t b = (uint8_t) c;
|
---|
119 |
|
---|
120 | bool can_unget =
|
---|
121 | /* Provided character is legal. */
|
---|
122 | c != EOF &&
|
---|
123 | /* Stream is consistent. */
|
---|
124 | !stream->error &&
|
---|
125 | /* Stream is buffered. */
|
---|
126 | stream->btype != _IONBF &&
|
---|
127 | /* Last operation on the stream was a read operation. */
|
---|
128 | stream->buf_state == _bs_read &&
|
---|
129 | /* Stream buffer is already allocated (i.e. there was already carried
|
---|
130 | * out either write or read operation on the stream). This is probably
|
---|
131 | * redundant check but let's be safe. */
|
---|
132 | stream->buf != NULL &&
|
---|
133 | /* There is still space in the stream to retreat. POSIX demands the
|
---|
134 | * possibility to unget at least 1 character. It should be always
|
---|
135 | * possible, assuming the last operation on the stream read at least 1
|
---|
136 | * character, because the buffer is refilled in the lazily manner. */
|
---|
137 | stream->buf_tail > stream->buf;
|
---|
138 |
|
---|
139 | if (can_unget) {
|
---|
140 | --stream->buf_tail;
|
---|
141 | stream->buf_tail[0] = b;
|
---|
142 | stream->eof = false;
|
---|
143 | return (int) b;
|
---|
144 | } else {
|
---|
145 | return EOF;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Read a stream until the delimiter (or EOF) is encountered.
|
---|
151 | *
|
---|
152 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
153 | * nul-terminated string together with the delimiter (if encountered).
|
---|
154 | * Will be resized if necessary.
|
---|
155 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
156 | * necessary.
|
---|
157 | * @param delimiter Delimiter on which to finish reading the stream.
|
---|
158 | * @param stream Input stream.
|
---|
159 | * @return Number of fetched characters (including delimiter if encountered)
|
---|
160 | * or -1 on error (set in errno).
|
---|
161 | */
|
---|
162 | ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
|
---|
163 | int delimiter, FILE *restrict stream)
|
---|
164 | {
|
---|
165 | /* Check arguments for sanity. */
|
---|
166 | if (!lineptr || !n) {
|
---|
167 | errno = EINVAL;
|
---|
168 | return -1;
|
---|
169 | }
|
---|
170 |
|
---|
171 | size_t alloc_step = 80; /* Buffer size gain during reallocation. */
|
---|
172 | char *pos = *lineptr; /* Next free byte of the output buffer. */
|
---|
173 | size_t cnt = 0; /* Number of fetched characters. */
|
---|
174 | int c = fgetc(stream); /* Current input character. Might be EOF. */
|
---|
175 |
|
---|
176 | do {
|
---|
177 | /* Mask EOF as NUL to terminate string. */
|
---|
178 | if (c == EOF) {
|
---|
179 | c = '\0';
|
---|
180 | }
|
---|
181 |
|
---|
182 | /* Ensure there is still space left in the buffer. */
|
---|
183 | if (pos == *lineptr + *n) {
|
---|
184 | *lineptr = realloc(*lineptr, *n + alloc_step);
|
---|
185 | if (*lineptr) {
|
---|
186 | pos = *lineptr + *n;
|
---|
187 | *n += alloc_step;
|
---|
188 | } else {
|
---|
189 | errno = ENOMEM;
|
---|
190 | return -1;
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* Store the fetched character. */
|
---|
195 | *pos = c;
|
---|
196 |
|
---|
197 | /* Fetch the next character according to the current character. */
|
---|
198 | if (c != '\0') {
|
---|
199 | ++pos;
|
---|
200 | ++cnt;
|
---|
201 | if (c == delimiter) {
|
---|
202 | /* Delimiter was just stored. Provide EOF as the next
|
---|
203 | * character - it will be masked as NUL and output string
|
---|
204 | * will be properly terminated. */
|
---|
205 | c = EOF;
|
---|
206 | } else {
|
---|
207 | /* Neither delimiter nor EOF were encountered. Just fetch
|
---|
208 | * the next character from the stream. */
|
---|
209 | c = fgetc(stream);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | } while (c != '\0');
|
---|
213 |
|
---|
214 | if (errno == EOK && cnt > 0) {
|
---|
215 | return cnt;
|
---|
216 | } else {
|
---|
217 | /* Either some error occured or the stream was already at EOF. */
|
---|
218 | return -1;
|
---|
219 | }
|
---|
220 | }
|
---|
221 |
|
---|
222 | /**
|
---|
223 | * Read a stream until the newline (or EOF) is encountered.
|
---|
224 | *
|
---|
225 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
226 | * nul-terminated string together with the delimiter (if encountered).
|
---|
227 | * Will be resized if necessary.
|
---|
228 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
229 | * necessary.
|
---|
230 | * @param stream Input stream.
|
---|
231 | * @return Number of fetched characters (including newline if encountered)
|
---|
232 | * or -1 on error (set in errno).
|
---|
233 | */
|
---|
234 | ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
|
---|
235 | FILE *restrict stream)
|
---|
236 | {
|
---|
237 | return posix_getdelim(lineptr, n, '\n', stream);
|
---|
238 | }
|
---|
239 |
|
---|
240 | /**
|
---|
241 | * Reopen a file stream.
|
---|
242 | *
|
---|
243 | * @param filename Pathname of a file to be reopened or NULL for changing
|
---|
244 | * the mode of the stream.
|
---|
245 | * @param mode Mode to be used for reopening the file or changing current
|
---|
246 | * mode of the stream.
|
---|
247 | * @param stream Current stream associated with the opened file.
|
---|
248 | * @return On success, either a stream of the reopened file or the provided
|
---|
249 | * stream with a changed mode. NULL otherwise.
|
---|
250 | */
|
---|
251 | FILE *posix_freopen(const char *restrict filename,
|
---|
252 | const char *restrict mode, FILE *restrict stream)
|
---|
253 | {
|
---|
254 | assert(mode != NULL);
|
---|
255 | assert(stream != NULL);
|
---|
256 |
|
---|
257 | /* Retieve the node. */
|
---|
258 | struct stat st;
|
---|
259 | int rc;
|
---|
260 |
|
---|
261 | if (filename == NULL) {
|
---|
262 | rc = fstat(stream->fd, &st);
|
---|
263 | } else {
|
---|
264 | rc = stat(filename, &st);
|
---|
265 | }
|
---|
266 |
|
---|
267 | if (rc != EOK) {
|
---|
268 | fclose(stream);
|
---|
269 | errno = -rc;
|
---|
270 | return NULL;
|
---|
271 | }
|
---|
272 |
|
---|
273 | fdi_node_t node = {
|
---|
274 | .fs_handle = st.fs_handle,
|
---|
275 | .devmap_handle = st.devmap_handle,
|
---|
276 | .index = st.index
|
---|
277 | };
|
---|
278 |
|
---|
279 | /* Open a new stream. */
|
---|
280 | FILE* new = fopen_node(&node, mode);
|
---|
281 | if (new == NULL) {
|
---|
282 | fclose(stream);
|
---|
283 | /* fopen_node() sets errno. */
|
---|
284 | return NULL;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /* Close the original stream without freeing it (ignoring errors). */
|
---|
288 | if (stream->buf != NULL) {
|
---|
289 | fflush(stream);
|
---|
290 | }
|
---|
291 | if (stream->sess != NULL) {
|
---|
292 | async_hangup(stream->sess);
|
---|
293 | }
|
---|
294 | if (stream->fd >= 0) {
|
---|
295 | close(stream->fd);
|
---|
296 | }
|
---|
297 | list_remove(&stream->link);
|
---|
298 |
|
---|
299 | /* Move the new stream to the original location. */
|
---|
300 | memcpy(stream, new, sizeof (FILE));
|
---|
301 | free(new);
|
---|
302 |
|
---|
303 | /* Update references in the file list. */
|
---|
304 | stream->link.next->prev = &stream->link;
|
---|
305 | stream->link.prev->next = &stream->link;
|
---|
306 |
|
---|
307 | return stream;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /**
|
---|
311 | *
|
---|
312 | * @param buf
|
---|
313 | * @param size
|
---|
314 | * @param mode
|
---|
315 | * @return
|
---|
316 | */
|
---|
317 | FILE *posix_fmemopen(void *restrict buf, size_t size,
|
---|
318 | const char *restrict mode)
|
---|
319 | {
|
---|
320 | // TODO
|
---|
321 | not_implemented();
|
---|
322 | }
|
---|
323 |
|
---|
324 | /**
|
---|
325 | *
|
---|
326 | * @param bufp
|
---|
327 | * @param sizep
|
---|
328 | * @return
|
---|
329 | */
|
---|
330 | FILE *posix_open_memstream(char **bufp, size_t *sizep)
|
---|
331 | {
|
---|
332 | // TODO
|
---|
333 | not_implemented();
|
---|
334 | }
|
---|
335 |
|
---|
336 | /**
|
---|
337 | * Write error messages to standard error.
|
---|
338 | *
|
---|
339 | * @param s Error message.
|
---|
340 | */
|
---|
341 | void posix_perror(const char *s)
|
---|
342 | {
|
---|
343 | if (s == NULL || s[0] == '\0') {
|
---|
344 | fprintf(stderr, "%s\n", posix_strerror(errno));
|
---|
345 | } else {
|
---|
346 | fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
|
---|
347 | }
|
---|
348 | }
|
---|
349 |
|
---|
350 | struct _posix_fpos {
|
---|
351 | off64_t offset;
|
---|
352 | };
|
---|
353 |
|
---|
354 | /** Restores stream a to position previously saved with fgetpos().
|
---|
355 | *
|
---|
356 | * @param stream Stream to restore
|
---|
357 | * @param pos Position to restore
|
---|
358 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
359 | */
|
---|
360 | int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
|
---|
361 | {
|
---|
362 | return fseek(stream, pos->offset, SEEK_SET);
|
---|
363 | }
|
---|
364 |
|
---|
365 | /** Saves the stream's position for later use by fsetpos().
|
---|
366 | *
|
---|
367 | * @param stream Stream to save
|
---|
368 | * @param pos Place to store the position
|
---|
369 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
370 | */
|
---|
371 | int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
|
---|
372 | {
|
---|
373 | off64_t ret = ftell(stream);
|
---|
374 | if (ret != -1) {
|
---|
375 | pos->offset = ret;
|
---|
376 | return 0;
|
---|
377 | } else {
|
---|
378 | return -1;
|
---|
379 | }
|
---|
380 | }
|
---|
381 |
|
---|
382 | /**
|
---|
383 | * Reposition a file-position indicator in a stream.
|
---|
384 | *
|
---|
385 | * @param stream Stream to seek in.
|
---|
386 | * @param offset Direction and amount of bytes to seek.
|
---|
387 | * @param whence From where to seek.
|
---|
388 | * @return Zero on success, -1 otherwise.
|
---|
389 | */
|
---|
390 | int posix_fseek(FILE *stream, long offset, int whence)
|
---|
391 | {
|
---|
392 | return fseek(stream, (off64_t) offset, whence);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Reposition a file-position indicator in a stream.
|
---|
397 | *
|
---|
398 | * @param stream Stream to seek in.
|
---|
399 | * @param offset Direction and amount of bytes to seek.
|
---|
400 | * @param whence From where to seek.
|
---|
401 | * @return Zero on success, -1 otherwise.
|
---|
402 | */
|
---|
403 | int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
|
---|
404 | {
|
---|
405 | return fseek(stream, (off64_t) offset, whence);
|
---|
406 | }
|
---|
407 |
|
---|
408 | /**
|
---|
409 | * Discover current file offset in a stream.
|
---|
410 | *
|
---|
411 | * @param stream Stream for which the offset shall be retrieved.
|
---|
412 | * @return Current offset or -1 if not possible.
|
---|
413 | */
|
---|
414 | long posix_ftell(FILE *stream)
|
---|
415 | {
|
---|
416 | return (long) ftell(stream);
|
---|
417 | }
|
---|
418 |
|
---|
419 | /**
|
---|
420 | * Discover current file offset in a stream.
|
---|
421 | *
|
---|
422 | * @param stream Stream for which the offset shall be retrieved.
|
---|
423 | * @return Current offset or -1 if not possible.
|
---|
424 | */
|
---|
425 | posix_off_t posix_ftello(FILE *stream)
|
---|
426 | {
|
---|
427 | return (posix_off_t) ftell(stream);
|
---|
428 | }
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * Discard prefetched data or write unwritten data.
|
---|
432 | *
|
---|
433 | * @param stream Stream that shall be flushed.
|
---|
434 | * @return Zero on success, EOF on failure.
|
---|
435 | */
|
---|
436 | int posix_fflush(FILE *stream)
|
---|
437 | {
|
---|
438 | int rc = fflush(stream);
|
---|
439 | if (rc < 0) {
|
---|
440 | errno = -rc;
|
---|
441 | return EOF;
|
---|
442 | } else {
|
---|
443 | return 0;
|
---|
444 | }
|
---|
445 | }
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Print formatted output to the opened file.
|
---|
449 | *
|
---|
450 | * @param fildes File descriptor of the opened file.
|
---|
451 | * @param format Format description.
|
---|
452 | * @return Either the number of printed characters or negative value on error.
|
---|
453 | */
|
---|
454 | int posix_dprintf(int fildes, const char *restrict format, ...)
|
---|
455 | {
|
---|
456 | va_list list;
|
---|
457 | va_start(list, format);
|
---|
458 | int result = posix_vdprintf(fildes, format, list);
|
---|
459 | va_end(list);
|
---|
460 | return result;
|
---|
461 | }
|
---|
462 |
|
---|
463 | /**
|
---|
464 | * Write ordinary string to the opened file.
|
---|
465 | *
|
---|
466 | * @param str String to be written.
|
---|
467 | * @param size Size of the string (in bytes)..
|
---|
468 | * @param fd File descriptor of the opened file.
|
---|
469 | * @return The number of written characters.
|
---|
470 | */
|
---|
471 | static int _dprintf_str_write(const char *str, size_t size, void *fd)
|
---|
472 | {
|
---|
473 | ssize_t wr = write(*(int *) fd, str, size);
|
---|
474 | return str_nlength(str, wr);
|
---|
475 | }
|
---|
476 |
|
---|
477 | /**
|
---|
478 | * Write wide string to the opened file.
|
---|
479 | *
|
---|
480 | * @param str String to be written.
|
---|
481 | * @param size Size of the string (in bytes).
|
---|
482 | * @param fd File descriptor of the opened file.
|
---|
483 | * @return The number of written characters.
|
---|
484 | */
|
---|
485 | static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
|
---|
486 | {
|
---|
487 | size_t offset = 0;
|
---|
488 | size_t chars = 0;
|
---|
489 | size_t sz;
|
---|
490 | char buf[4];
|
---|
491 |
|
---|
492 | while (offset < size) {
|
---|
493 | sz = 0;
|
---|
494 | if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
|
---|
495 | break;
|
---|
496 | }
|
---|
497 |
|
---|
498 | if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
|
---|
499 | break;
|
---|
500 | }
|
---|
501 |
|
---|
502 | chars++;
|
---|
503 | offset += sizeof(wchar_t);
|
---|
504 | }
|
---|
505 |
|
---|
506 | return chars;
|
---|
507 | }
|
---|
508 |
|
---|
509 | /**
|
---|
510 | * Print formatted output to the opened file.
|
---|
511 | *
|
---|
512 | * @param fildes File descriptor of the opened file.
|
---|
513 | * @param format Format description.
|
---|
514 | * @param ap Print arguments.
|
---|
515 | * @return Either the number of printed characters or negative value on error.
|
---|
516 | */
|
---|
517 | int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
|
---|
518 | {
|
---|
519 | printf_spec_t spec = {
|
---|
520 | .str_write = _dprintf_str_write,
|
---|
521 | .wstr_write = _dprintf_wstr_write,
|
---|
522 | .data = &fildes
|
---|
523 | };
|
---|
524 |
|
---|
525 | return printf_core(format, &spec, ap);
|
---|
526 | }
|
---|
527 |
|
---|
528 | /**
|
---|
529 | * Print formatted output to the string.
|
---|
530 | *
|
---|
531 | * @param s Output string.
|
---|
532 | * @param format Format description.
|
---|
533 | * @return Either the number of printed characters (excluding null byte) or
|
---|
534 | * negative value on error.
|
---|
535 | */
|
---|
536 | int posix_sprintf(char *s, const char *restrict format, ...)
|
---|
537 | {
|
---|
538 | va_list list;
|
---|
539 | va_start(list, format);
|
---|
540 | int result = posix_vsprintf(s, format, list);
|
---|
541 | va_end(list);
|
---|
542 | return result;
|
---|
543 | }
|
---|
544 |
|
---|
545 | /**
|
---|
546 | * Print formatted output to the string.
|
---|
547 | *
|
---|
548 | * @param s Output string.
|
---|
549 | * @param format Format description.
|
---|
550 | * @param ap Print arguments.
|
---|
551 | * @return Either the number of printed characters (excluding null byte) or
|
---|
552 | * negative value on error.
|
---|
553 | */
|
---|
554 | int posix_vsprintf(char *s, const char *restrict format, va_list ap)
|
---|
555 | {
|
---|
556 | return vsnprintf(s, STR_NO_LIMIT, format, ap);
|
---|
557 | }
|
---|
558 |
|
---|
559 | /**
|
---|
560 | * Convert formatted input from the stream.
|
---|
561 | *
|
---|
562 | * @param stream Input stream.
|
---|
563 | * @param format Format description.
|
---|
564 | * @return The number of converted output items or EOF on failure.
|
---|
565 | */
|
---|
566 | int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
|
---|
567 | {
|
---|
568 | va_list list;
|
---|
569 | va_start(list, format);
|
---|
570 | int result = posix_vfscanf(stream, format, list);
|
---|
571 | va_end(list);
|
---|
572 | return result;
|
---|
573 | }
|
---|
574 |
|
---|
575 | /**
|
---|
576 | * Convert formatted input from the standard input.
|
---|
577 | *
|
---|
578 | * @param format Format description.
|
---|
579 | * @return The number of converted output items or EOF on failure.
|
---|
580 | */
|
---|
581 | int posix_scanf(const char *restrict format, ...)
|
---|
582 | {
|
---|
583 | va_list list;
|
---|
584 | va_start(list, format);
|
---|
585 | int result = posix_vscanf(format, list);
|
---|
586 | va_end(list);
|
---|
587 | return result;
|
---|
588 | }
|
---|
589 |
|
---|
590 | /**
|
---|
591 | * Convert formatted input from the standard input.
|
---|
592 | *
|
---|
593 | * @param format Format description.
|
---|
594 | * @param arg Output items.
|
---|
595 | * @return The number of converted output items or EOF on failure.
|
---|
596 | */
|
---|
597 | int posix_vscanf(const char *restrict format, va_list arg)
|
---|
598 | {
|
---|
599 | return posix_vfscanf(stdin, format, arg);
|
---|
600 | }
|
---|
601 |
|
---|
602 | /**
|
---|
603 | * Convert formatted input from the string.
|
---|
604 | *
|
---|
605 | * @param s Input string.
|
---|
606 | * @param format Format description.
|
---|
607 | * @return The number of converted output items or EOF on failure.
|
---|
608 | */
|
---|
609 | int posix_sscanf(const char *restrict s, const char *restrict format, ...)
|
---|
610 | {
|
---|
611 | va_list list;
|
---|
612 | va_start(list, format);
|
---|
613 | int result = posix_vsscanf(s, format, list);
|
---|
614 | va_end(list);
|
---|
615 | return result;
|
---|
616 | }
|
---|
617 |
|
---|
618 | /**
|
---|
619 | * Acquire file stream for the thread.
|
---|
620 | *
|
---|
621 | * @param file File stream to lock.
|
---|
622 | */
|
---|
623 | void posix_flockfile(FILE *file)
|
---|
624 | {
|
---|
625 | /* dummy */
|
---|
626 | }
|
---|
627 |
|
---|
628 | /**
|
---|
629 | * Acquire file stream for the thread (non-blocking).
|
---|
630 | *
|
---|
631 | * @param file File stream to lock.
|
---|
632 | * @return Zero for success and non-zero if the lock cannot be acquired.
|
---|
633 | */
|
---|
634 | int posix_ftrylockfile(FILE *file)
|
---|
635 | {
|
---|
636 | /* dummy */
|
---|
637 | return 0;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /**
|
---|
641 | * Relinquish the ownership of the locked file stream.
|
---|
642 | *
|
---|
643 | * @param file File stream to unlock.
|
---|
644 | */
|
---|
645 | void posix_funlockfile(FILE *file)
|
---|
646 | {
|
---|
647 | /* dummy */
|
---|
648 | }
|
---|
649 |
|
---|
650 | /**
|
---|
651 | * Get a byte from a stream (thread-unsafe).
|
---|
652 | *
|
---|
653 | * @param stream Input file stream.
|
---|
654 | * @return Either read byte or EOF.
|
---|
655 | */
|
---|
656 | int posix_getc_unlocked(FILE *stream)
|
---|
657 | {
|
---|
658 | return getc(stream);
|
---|
659 | }
|
---|
660 |
|
---|
661 | /**
|
---|
662 | * Get a byte from the standard input stream (thread-unsafe).
|
---|
663 | *
|
---|
664 | * @return Either read byte or EOF.
|
---|
665 | */
|
---|
666 | int posix_getchar_unlocked(void)
|
---|
667 | {
|
---|
668 | return getchar();
|
---|
669 | }
|
---|
670 |
|
---|
671 | /**
|
---|
672 | * Put a byte on a stream (thread-unsafe).
|
---|
673 | *
|
---|
674 | * @param c Byte to output.
|
---|
675 | * @param stream Output file stream.
|
---|
676 | * @return Either written byte or EOF.
|
---|
677 | */
|
---|
678 | int posix_putc_unlocked(int c, FILE *stream)
|
---|
679 | {
|
---|
680 | return putc(c, stream);
|
---|
681 | }
|
---|
682 |
|
---|
683 | /**
|
---|
684 | * Put a byte on the standard output stream (thread-unsafe).
|
---|
685 | *
|
---|
686 | * @param c Byte to output.
|
---|
687 | * @return Either written byte or EOF.
|
---|
688 | */
|
---|
689 | int posix_putchar_unlocked(int c)
|
---|
690 | {
|
---|
691 | return putchar(c);
|
---|
692 | }
|
---|
693 |
|
---|
694 | /**
|
---|
695 | * Remove a file or directory.
|
---|
696 | *
|
---|
697 | * @param path Pathname of the file that shall be removed.
|
---|
698 | * @return Zero on success, -1 (with errno set) otherwise.
|
---|
699 | */
|
---|
700 | int posix_remove(const char *path)
|
---|
701 | {
|
---|
702 | struct stat st;
|
---|
703 | int rc = stat(path, &st);
|
---|
704 |
|
---|
705 | if (rc != EOK) {
|
---|
706 | errno = -rc;
|
---|
707 | return -1;
|
---|
708 | }
|
---|
709 |
|
---|
710 | if (st.is_directory) {
|
---|
711 | rc = rmdir(path);
|
---|
712 | } else {
|
---|
713 | rc = unlink(path);
|
---|
714 | }
|
---|
715 |
|
---|
716 | if (rc != EOK) {
|
---|
717 | errno = -rc;
|
---|
718 | return -1;
|
---|
719 | }
|
---|
720 | return 0;
|
---|
721 | }
|
---|
722 |
|
---|
723 | /**
|
---|
724 | *
|
---|
725 | * @param s
|
---|
726 | * @return
|
---|
727 | */
|
---|
728 | char *posix_tmpnam(char *s)
|
---|
729 | {
|
---|
730 | // TODO: low priority, just a compile-time dependency of binutils
|
---|
731 | not_implemented();
|
---|
732 | }
|
---|
733 |
|
---|
734 | /** @}
|
---|
735 | */
|
---|