source: mainline/uspace/lib/posix/stdio.c@ 1978a5f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1978a5f was 1978a5f, checked in by Petr Koupy <petr.koupy@…>, 14 years ago

Implemented ungetc() and commented some functions from stdio.c.

  • Property mode set to 100644
File size: 14.5 KB
Line 
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
34 */
35
36#define LIBPOSIX_INTERNAL
37
38#include <assert.h>
39#include <errno.h>
40#include <bool.h>
41
42#include "internal/common.h"
43#include "stdio.h"
44#include "libc/io/printf_core.h"
45#include "string.h"
46#include "libc/str.h"
47#include "sys/types.h"
48
49/* not the best of solutions, but freopen and ungetc will eventually
50 * need to be implemented in libc anyway
51 */
52#include "../c/generic/private/stdio.h"
53
54/** Clears the stream's error and end-of-file indicators.
55 *
56 * @param stream Stream whose indicators shall be cleared.
57 */
58void posix_clearerr(FILE *stream)
59{
60 stream->error = 0;
61 stream->eof = 0;
62}
63
64/**
65 * Generate a pathname for the controlling terminal.
66 *
67 * @param s Allocated buffer to which the pathname shall be put.
68 * @return Either s or static location filled with the requested pathname.
69 */
70char *posix_ctermid(char *s)
71{
72 /* Currently always returns an error value (empty string). */
73 // TODO: return a real terminal path
74
75 static char dummy_path[L_ctermid] = {'\0'};
76
77 if (s == NULL) {
78 return dummy_path;
79 }
80
81 s[0] = '\0';
82 return s;
83}
84
85/**
86 * Push byte back into input stream.
87 *
88 * @param c Byte to be pushed back.
89 * @param stream Stream to where the byte shall be pushed.
90 * @return Provided byte on succes or EOF if not possible.
91 */
92int posix_ungetc(int c, FILE *stream)
93{
94 uint8_t b = (uint8_t) c;
95
96 bool can_unget =
97 /* Provided character is legal. */
98 c != EOF &&
99 /* Stream is consistent. */
100 !stream->error &&
101 /* Stream is buffered. */
102 stream->btype != _IONBF &&
103 /* Last operation on the stream was a read operation. */
104 stream->buf_state == _bs_read &&
105 /* Stream buffer is already allocated (i.e. there was already carried
106 * out either write or read operation on the stream). This is probably
107 * redundant check but let's be safe. */
108 stream->buf != NULL &&
109 /* There is still space in the stream to retreat. POSIX demands the
110 * possibility to unget at least 1 character. It should be always
111 * possible, assuming the last operation on the stream read at least 1
112 * character, because the buffer is refilled in the lazily manner. */
113 stream->buf_tail > stream->buf;
114
115 if (can_unget) {
116 --stream->buf_tail;
117 stream->buf_tail[0] = b;
118 stream->eof = false;
119 return (int) b;
120 } else {
121 return EOF;
122 }
123}
124
125/**
126 *
127 * @param lineptr
128 * @param n
129 * @param delimiter
130 * @param stream
131 * @return
132 */
133ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
134 int delimiter, FILE *restrict stream)
135{
136 // TODO
137 not_implemented();
138}
139
140/**
141 *
142 * @param lineptr
143 * @param n
144 * @param stream
145 * @return
146 */
147ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
148 FILE *restrict stream)
149{
150 // TODO
151 not_implemented();
152}
153
154/**
155 * Reopen a file stream.
156 *
157 * @param filename Pathname of a file to be reopened or NULL for changing
158 * the mode of the stream.
159 * @param mode Mode to be used for reopening the file or changing current
160 * mode of the stream.
161 * @param stream Current stream associated with the opened file.
162 * @return On success, either a stream of the reopened file or the provided
163 * stream with a changed mode. NULL otherwise.
164 */
165FILE *posix_freopen(
166 const char *restrict filename,
167 const char *restrict mode,
168 FILE *restrict stream)
169{
170 assert(mode != NULL);
171 assert(stream != NULL);
172
173 if (filename == NULL) {
174 // TODO
175
176 /* print error to stderr as well, to avoid hard to find problems
177 * with buggy apps that expect this to work
178 */
179 fprintf(stderr,
180 "ERROR: Application wants to use freopen() to change mode of opened stream.\n"
181 " libposix does not support that yet, the application may function improperly.\n");
182 errno = ENOTSUP;
183 return NULL;
184 }
185
186 FILE* copy = malloc(sizeof(FILE));
187 if (copy == NULL) {
188 errno = ENOMEM;
189 return NULL;
190 }
191 memcpy(copy, stream, sizeof(FILE));
192 fclose(copy); /* copy is now freed */
193
194 copy = fopen(filename, mode); /* open new stream */
195 if (copy == NULL) {
196 /* fopen() sets errno */
197 return NULL;
198 }
199
200 /* move the new stream to the original location */
201 memcpy(stream, copy, sizeof (FILE));
202 free(copy);
203
204 /* update references in the file list */
205 stream->link.next->prev = &stream->link;
206 stream->link.prev->next = &stream->link;
207
208 return stream;
209}
210
211/**
212 *
213 * @param buf
214 * @param size
215 * @param mode
216 * @return
217 */
218FILE *posix_fmemopen(void *restrict buf, size_t size,
219 const char *restrict mode)
220{
221 // TODO
222 not_implemented();
223}
224
225/**
226 *
227 * @param bufp
228 * @param sizep
229 * @return
230 */
231FILE *posix_open_memstream(char **bufp, size_t *sizep)
232{
233 // TODO
234 not_implemented();
235}
236
237/**
238 * Write error messages to standard error.
239 *
240 * @param s Error message.
241 */
242void posix_perror(const char *s)
243{
244 if (s == NULL || s[0] == '\0') {
245 fprintf(stderr, "%s\n", posix_strerror(errno));
246 } else {
247 fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
248 }
249}
250
251struct _posix_fpos {
252 off64_t offset;
253};
254
255/** Restores stream a to position previously saved with fgetpos().
256 *
257 * @param stream Stream to restore
258 * @param pos Position to restore
259 * @return Zero on success, non-zero (with errno set) on failure
260 */
261int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
262{
263 return fseek(stream, pos->offset, SEEK_SET);
264}
265
266/** Saves the stream's position for later use by fsetpos().
267 *
268 * @param stream Stream to save
269 * @param pos Place to store the position
270 * @return Zero on success, non-zero (with errno set) on failure
271 */
272int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
273{
274 off64_t ret = ftell(stream);
275 if (ret == -1) {
276 return errno;
277 }
278 pos->offset = ret;
279 return 0;
280}
281
282/**
283 * Reposition a file-position indicator in a stream.
284 *
285 * @param stream Stream to seek in.
286 * @param offset Direction and amount of bytes to seek.
287 * @param whence From where to seek.
288 * @return Zero on success, -1 otherwise.
289 */
290int posix_fseek(FILE *stream, long offset, int whence)
291{
292 return fseek(stream, (off64_t) offset, whence);
293}
294
295/**
296 * Reposition a file-position indicator in a stream.
297 *
298 * @param stream Stream to seek in.
299 * @param offset Direction and amount of bytes to seek.
300 * @param whence From where to seek.
301 * @return Zero on success, -1 otherwise.
302 */
303int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
304{
305 return fseek(stream, (off64_t) offset, whence);
306}
307
308/**
309 * Discover current file offset in a stream.
310 *
311 * @param stream Stream for which the offset shall be retrieved.
312 * @return Current offset or -1 if not possible.
313 */
314long posix_ftell(FILE *stream)
315{
316 return (long) ftell(stream);
317}
318
319/**
320 * Discover current file offset in a stream.
321 *
322 * @param stream Stream for which the offset shall be retrieved.
323 * @return Current offset or -1 if not possible.
324 */
325posix_off_t posix_ftello(FILE *stream)
326{
327 return (posix_off_t) ftell(stream);
328}
329
330/**
331 * Print formatted output to the opened file.
332 *
333 * @param fildes File descriptor of the opened file.
334 * @param format Format description.
335 * @return Either the number of printed characters or negative value on error.
336 */
337int posix_dprintf(int fildes, const char *restrict format, ...)
338{
339 va_list list;
340 va_start(list, format);
341 int result = posix_vdprintf(fildes, format, list);
342 va_end(list);
343 return result;
344}
345
346/**
347 * Write ordinary string to the opened file.
348 *
349 * @param str String to be written.
350 * @param size Size of the string (in bytes)..
351 * @param fd File descriptor of the opened file.
352 * @return The number of written characters.
353 */
354static int _dprintf_str_write(const char *str, size_t size, void *fd)
355{
356 ssize_t wr = write(*(int *) fd, str, size);
357 return str_nlength(str, wr);
358}
359
360/**
361 * Write wide string to the opened file.
362 *
363 * @param str String to be written.
364 * @param size Size of the string (in bytes).
365 * @param fd File descriptor of the opened file.
366 * @return The number of written characters.
367 */
368static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
369{
370 size_t offset = 0;
371 size_t chars = 0;
372 size_t sz;
373 char buf[4];
374
375 while (offset < size) {
376 sz = 0;
377 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
378 break;
379 }
380
381 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
382 break;
383 }
384
385 chars++;
386 offset += sizeof(wchar_t);
387 }
388
389 return chars;
390}
391
392/**
393 * Print formatted output to the opened file.
394 *
395 * @param fildes File descriptor of the opened file.
396 * @param format Format description.
397 * @param ap Print arguments.
398 * @return Either the number of printed characters or negative value on error.
399 */
400int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
401{
402 printf_spec_t spec = {
403 .str_write = _dprintf_str_write,
404 .wstr_write = _dprintf_wstr_write,
405 .data = &fildes
406 };
407
408 return printf_core(format, &spec, ap);
409}
410
411/**
412 * Print formatted output to the string.
413 *
414 * @param s Output string.
415 * @param format Format description.
416 * @return Either the number of printed characters (excluding null byte) or
417 * negative value on error.
418 */
419int posix_sprintf(char *s, const char *format, ...)
420{
421 va_list list;
422 va_start(list, format);
423 int result = posix_vsprintf(s, format, list);
424 va_end(list);
425 return result;
426}
427
428/**
429 * Print formatted output to the string.
430 *
431 * @param s Output string.
432 * @param format Format description.
433 * @param ap Print arguments.
434 * @return Either the number of printed characters (excluding null byte) or
435 * negative value on error.
436 */
437int posix_vsprintf(char *s, const char *format, va_list ap)
438{
439 return vsnprintf(s, STR_NO_LIMIT, format, ap);
440}
441
442/**
443 * Convert formatted input from the stream.
444 *
445 * @param stream Input stream.
446 * @param format Format description.
447 * @return The number of converted output items or EOF on failure.
448 */
449int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
450{
451 va_list list;
452 va_start(list, format);
453 int result = posix_vfscanf(stream, format, list);
454 va_end(list);
455 return result;
456}
457
458/**
459 * Convert formatted input from the stream.
460 *
461 * @param stream Input stream.
462 * @param format Format description.
463 * @param arg Output items.
464 * @return The number of converted output items or EOF on failure.
465 */
466int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg)
467{
468 // TODO
469 not_implemented();
470}
471
472/**
473 * Convert formatted input from the standard input.
474 *
475 * @param format Format description.
476 * @return The number of converted output items or EOF on failure.
477 */
478int posix_scanf(const char *restrict format, ...)
479{
480 va_list list;
481 va_start(list, format);
482 int result = posix_vscanf(format, list);
483 va_end(list);
484 return result;
485}
486
487/**
488 * Convert formatted input from the standard input.
489 *
490 * @param format Format description.
491 * @param arg Output items.
492 * @return The number of converted output items or EOF on failure.
493 */
494int posix_vscanf(const char *restrict format, va_list arg)
495{
496 return posix_vfscanf(stdin, format, arg);
497}
498
499/**
500 * Convert formatted input from the string.
501 *
502 * @param s Input string.
503 * @param format Format description.
504 * @return The number of converted output items or EOF on failure.
505 */
506int posix_sscanf(const char *s, const char *format, ...)
507{
508 va_list list;
509 va_start(list, format);
510 int result = posix_vsscanf(s, format, list);
511 va_end(list);
512 return result;
513}
514
515/**
516 * Convert formatted input from the string.
517 *
518 * @param s Input string.
519 * @param format Format description.
520 * @param arg Output items.
521 * @return The number of converted output items or EOF on failure.
522 */
523int posix_vsscanf(
524 const char *restrict s, const char *restrict format, va_list arg)
525{
526 // TODO
527 not_implemented();
528}
529
530/**
531 * Acquire file stream for the thread.
532 *
533 * @param file File stream to lock.
534 */
535void posix_flockfile(FILE *file)
536{
537 /* dummy */
538}
539
540/**
541 * Acquire file stream for the thread (non-blocking).
542 *
543 * @param file File stream to lock.
544 * @return Zero for success and non-zero if the lock cannot be acquired.
545 */
546int posix_ftrylockfile(FILE *file)
547{
548 /* dummy */
549 return 0;
550}
551
552/**
553 * Relinquish the ownership of the locked file stream.
554 *
555 * @param file File stream to unlock.
556 */
557void posix_funlockfile(FILE *file)
558{
559 /* dummy */
560}
561
562/**
563 * Get a byte from a stream (thread-unsafe).
564 *
565 * @param stream Input file stream.
566 * @return Either read byte or EOF.
567 */
568int posix_getc_unlocked(FILE *stream)
569{
570 return getc(stream);
571}
572
573/**
574 * Get a byte from the standard input stream (thread-unsafe).
575 *
576 * @return Either read byte or EOF.
577 */
578int posix_getchar_unlocked(void)
579{
580 return getchar();
581}
582
583/**
584 * Put a byte on a stream (thread-unsafe).
585 *
586 * @param c Byte to output.
587 * @param stream Output file stream.
588 * @return Either written byte or EOF.
589 */
590int posix_putc_unlocked(int c, FILE *stream)
591{
592 return putc(c, stream);
593}
594
595/**
596 * Put a byte on the standard output stream (thread-unsafe).
597 *
598 * @param c Byte to output.
599 * @return Either written byte or EOF.
600 */
601int posix_putchar_unlocked(int c)
602{
603 return putchar(c);
604}
605
606/**
607 * Remove a file.
608 *
609 * @param path Pathname of the file that shall be removed.
610 * @return Zero on success, -1 otherwise.
611 */
612int posix_remove(const char *path)
613{
614 // FIXME: unlink() and rmdir() seem to be equivalent at the moment,
615 // but that does not have to be true forever
616 return unlink(path);
617}
618
619/**
620 *
621 * @param s
622 * @return
623 */
624char *posix_tmpnam(char *s)
625{
626 // TODO: low priority, just a compile-time dependency of binutils
627 not_implemented();
628}
629
630/** @}
631 */
Note: See TracBrowser for help on using the repository browser.