source: mainline/uspace/lib/posix/source/stdio.c@ acdb5bac

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since acdb5bac was 3e6a98c5, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Standards-compliant boolean type.

  • Property mode set to 100644
File size: 19.1 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 Standard buffered input/output.
34 */
35
36#define LIBPOSIX_INTERNAL
37
38#include "internal/common.h"
39#include "posix/stdio.h"
40
41#include "posix/assert.h"
42#include "posix/errno.h"
43#include "posix/stdlib.h"
44#include "posix/string.h"
45#include "posix/sys/types.h"
46#include "posix/unistd.h"
47
48#include "libc/stdio.h"
49#include "libc/io/printf_core.h"
50#include "libc/stdbool.h"
51#include "libc/str.h"
52#include "libc/malloc.h"
53#include "libc/adt/list.h"
54#include "libc/sys/stat.h"
55
56
57/* not the best of solutions, but freopen and ungetc will eventually
58 * need to be implemented in libc anyway
59 */
60#include "../../c/generic/private/stdio.h"
61
62/** Clears the stream's error and end-of-file indicators.
63 *
64 * @param stream Stream whose indicators shall be cleared.
65 */
66void posix_clearerr(FILE *stream)
67{
68 stream->error = 0;
69 stream->eof = 0;
70}
71
72/**
73 * Generate a pathname for the controlling terminal.
74 *
75 * @param s Allocated buffer to which the pathname shall be put.
76 * @return Either s or static location filled with the requested pathname.
77 */
78char *posix_ctermid(char *s)
79{
80 /* Currently always returns an error value (empty string). */
81 // TODO: return a real terminal path
82
83 static char dummy_path[L_ctermid] = {'\0'};
84
85 if (s == NULL) {
86 return dummy_path;
87 }
88
89 s[0] = '\0';
90 return s;
91}
92
93/**
94 * Put a string on the stream.
95 *
96 * @param s String to be written.
97 * @param stream Output stream.
98 * @return Non-negative on success, EOF on failure.
99 */
100int posix_fputs(const char *restrict s, FILE *restrict stream)
101{
102 int rc = fputs(s, stream);
103 if (rc == 0) {
104 return EOF;
105 } else {
106 return 0;
107 }
108}
109
110/**
111 * Push byte back into input stream.
112 *
113 * @param c Byte to be pushed back.
114 * @param stream Stream to where the byte shall be pushed.
115 * @return Provided byte on success or EOF if not possible.
116 */
117int posix_ungetc(int c, FILE *stream)
118{
119 uint8_t b = (uint8_t) c;
120
121 bool can_unget =
122 /* Provided character is legal. */
123 c != EOF &&
124 /* Stream is consistent. */
125 !stream->error &&
126 /* Stream is buffered. */
127 stream->btype != _IONBF &&
128 /* Last operation on the stream was a read operation. */
129 stream->buf_state == _bs_read &&
130 /* Stream buffer is already allocated (i.e. there was already carried
131 * out either write or read operation on the stream). This is probably
132 * redundant check but let's be safe. */
133 stream->buf != NULL &&
134 /* There is still space in the stream to retreat. POSIX demands the
135 * possibility to unget at least 1 character. It should be always
136 * possible, assuming the last operation on the stream read at least 1
137 * character, because the buffer is refilled in the lazily manner. */
138 stream->buf_tail > stream->buf;
139
140 if (can_unget) {
141 --stream->buf_tail;
142 stream->buf_tail[0] = b;
143 stream->eof = false;
144 return (int) b;
145 } else {
146 return EOF;
147 }
148}
149
150/**
151 * Read a stream until the delimiter (or EOF) is encountered.
152 *
153 * @param lineptr Pointer to the output buffer in which there will be stored
154 * nul-terminated string together with the delimiter (if encountered).
155 * Will be resized if necessary.
156 * @param n Pointer to the size of the output buffer. Will be increased if
157 * necessary.
158 * @param delimiter Delimiter on which to finish reading the stream.
159 * @param stream Input stream.
160 * @return Number of fetched characters (including delimiter if encountered)
161 * or -1 on error (set in errno).
162 */
163ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n,
164 int delimiter, FILE *restrict stream)
165{
166 /* Check arguments for sanity. */
167 if (!lineptr || !n) {
168 errno = EINVAL;
169 return -1;
170 }
171
172 size_t alloc_step = 80; /* Buffer size gain during reallocation. */
173 char *pos = *lineptr; /* Next free byte of the output buffer. */
174 size_t cnt = 0; /* Number of fetched characters. */
175 int c = fgetc(stream); /* Current input character. Might be EOF. */
176
177 do {
178 /* Mask EOF as NUL to terminate string. */
179 if (c == EOF) {
180 c = '\0';
181 }
182
183 /* Ensure there is still space left in the buffer. */
184 if (pos == *lineptr + *n) {
185 *lineptr = realloc(*lineptr, *n + alloc_step);
186 if (*lineptr) {
187 pos = *lineptr + *n;
188 *n += alloc_step;
189 } else {
190 errno = ENOMEM;
191 return -1;
192 }
193 }
194
195 /* Store the fetched character. */
196 *pos = c;
197
198 /* Fetch the next character according to the current character. */
199 if (c != '\0') {
200 ++pos;
201 ++cnt;
202 if (c == delimiter) {
203 /* Delimiter was just stored. Provide EOF as the next
204 * character - it will be masked as NUL and output string
205 * will be properly terminated. */
206 c = EOF;
207 } else {
208 /* Neither delimiter nor EOF were encountered. Just fetch
209 * the next character from the stream. */
210 c = fgetc(stream);
211 }
212 }
213 } while (c != '\0');
214
215 if (errno == EOK && cnt > 0) {
216 return cnt;
217 } else {
218 /* Either some error occured or the stream was already at EOF. */
219 return -1;
220 }
221}
222
223/**
224 * Read a stream until the newline (or EOF) is encountered.
225 *
226 * @param lineptr Pointer to the output buffer in which there will be stored
227 * nul-terminated string together with the delimiter (if encountered).
228 * Will be resized if necessary.
229 * @param n Pointer to the size of the output buffer. Will be increased if
230 * necessary.
231 * @param stream Input stream.
232 * @return Number of fetched characters (including newline if encountered)
233 * or -1 on error (set in errno).
234 */
235ssize_t posix_getline(char **restrict lineptr, size_t *restrict n,
236 FILE *restrict stream)
237{
238 return posix_getdelim(lineptr, n, '\n', stream);
239}
240
241/**
242 * Reopen a file stream.
243 *
244 * @param filename Pathname of a file to be reopened or NULL for changing
245 * the mode of the stream.
246 * @param mode Mode to be used for reopening the file or changing current
247 * mode of the stream.
248 * @param stream Current stream associated with the opened file.
249 * @return On success, either a stream of the reopened file or the provided
250 * stream with a changed mode. NULL otherwise.
251 */
252FILE *posix_freopen(const char *restrict filename,
253 const char *restrict mode, FILE *restrict stream)
254{
255 assert(mode != NULL);
256 assert(stream != NULL);
257
258 if (filename == NULL) {
259 /* POSIX allows this to be imlementation-defined. HelenOS currently
260 * does not support changing the mode. */
261 // FIXME: handle mode change once it is supported
262 return stream;
263 }
264
265 /* Open a new stream. */
266 FILE* new = fopen(filename, mode);
267 if (new == NULL) {
268 fclose(stream);
269 /* errno was set by fopen() */
270 return NULL;
271 }
272
273 /* Close the original stream without freeing it (ignoring errors). */
274 if (stream->buf != NULL) {
275 fflush(stream);
276 }
277 if (stream->sess != NULL) {
278 async_hangup(stream->sess);
279 }
280 if (stream->fd >= 0) {
281 close(stream->fd);
282 }
283 list_remove(&stream->link);
284
285 /* Move the new stream to the original location. */
286 memcpy(stream, new, sizeof (FILE));
287 free(new);
288
289 /* Update references in the file list. */
290 stream->link.next->prev = &stream->link;
291 stream->link.prev->next = &stream->link;
292
293 return stream;
294}
295
296/**
297 * Write error messages to standard error.
298 *
299 * @param s Error message.
300 */
301void posix_perror(const char *s)
302{
303 if (s == NULL || s[0] == '\0') {
304 fprintf(stderr, "%s\n", posix_strerror(errno));
305 } else {
306 fprintf(stderr, "%s: %s\n", s, posix_strerror(errno));
307 }
308}
309
310struct _posix_fpos {
311 off64_t offset;
312};
313
314/** Restores stream a to position previously saved with fgetpos().
315 *
316 * @param stream Stream to restore
317 * @param pos Position to restore
318 * @return Zero on success, non-zero (with errno set) on failure
319 */
320int posix_fsetpos(FILE *stream, const posix_fpos_t *pos)
321{
322 return fseek(stream, pos->offset, SEEK_SET);
323}
324
325/** Saves the stream's position for later use by fsetpos().
326 *
327 * @param stream Stream to save
328 * @param pos Place to store the position
329 * @return Zero on success, non-zero (with errno set) on failure
330 */
331int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos)
332{
333 off64_t ret = ftell(stream);
334 if (ret != -1) {
335 pos->offset = ret;
336 return 0;
337 } else {
338 return -1;
339 }
340}
341
342/**
343 * Reposition a file-position indicator in a stream.
344 *
345 * @param stream Stream to seek in.
346 * @param offset Direction and amount of bytes to seek.
347 * @param whence From where to seek.
348 * @return Zero on success, -1 otherwise.
349 */
350int posix_fseek(FILE *stream, long offset, int whence)
351{
352 return fseek(stream, (off64_t) offset, whence);
353}
354
355/**
356 * Reposition a file-position indicator in a stream.
357 *
358 * @param stream Stream to seek in.
359 * @param offset Direction and amount of bytes to seek.
360 * @param whence From where to seek.
361 * @return Zero on success, -1 otherwise.
362 */
363int posix_fseeko(FILE *stream, posix_off_t offset, int whence)
364{
365 return fseek(stream, (off64_t) offset, whence);
366}
367
368/**
369 * Discover current file offset in a stream.
370 *
371 * @param stream Stream for which the offset shall be retrieved.
372 * @return Current offset or -1 if not possible.
373 */
374long posix_ftell(FILE *stream)
375{
376 return (long) ftell(stream);
377}
378
379/**
380 * Discover current file offset in a stream.
381 *
382 * @param stream Stream for which the offset shall be retrieved.
383 * @return Current offset or -1 if not possible.
384 */
385posix_off_t posix_ftello(FILE *stream)
386{
387 return (posix_off_t) ftell(stream);
388}
389
390/**
391 * Discard prefetched data or write unwritten data.
392 *
393 * @param stream Stream that shall be flushed.
394 * @return Zero on success, EOF on failure.
395 */
396int posix_fflush(FILE *stream)
397{
398 int rc = fflush(stream);
399 if (rc < 0) {
400 errno = -rc;
401 return EOF;
402 } else {
403 return 0;
404 }
405}
406
407/**
408 * Print formatted output to the opened file.
409 *
410 * @param fildes File descriptor of the opened file.
411 * @param format Format description.
412 * @return Either the number of printed characters or negative value on error.
413 */
414int posix_dprintf(int fildes, const char *restrict format, ...)
415{
416 va_list list;
417 va_start(list, format);
418 int result = posix_vdprintf(fildes, format, list);
419 va_end(list);
420 return result;
421}
422
423/**
424 * Write ordinary string to the opened file.
425 *
426 * @param str String to be written.
427 * @param size Size of the string (in bytes)..
428 * @param fd File descriptor of the opened file.
429 * @return The number of written characters.
430 */
431static int _dprintf_str_write(const char *str, size_t size, void *fd)
432{
433 ssize_t wr = write(*(int *) fd, str, size);
434 return str_nlength(str, wr);
435}
436
437/**
438 * Write wide string to the opened file.
439 *
440 * @param str String to be written.
441 * @param size Size of the string (in bytes).
442 * @param fd File descriptor of the opened file.
443 * @return The number of written characters.
444 */
445static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
446{
447 size_t offset = 0;
448 size_t chars = 0;
449 size_t sz;
450 char buf[4];
451
452 while (offset < size) {
453 sz = 0;
454 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
455 break;
456 }
457
458 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) {
459 break;
460 }
461
462 chars++;
463 offset += sizeof(wchar_t);
464 }
465
466 return chars;
467}
468
469/**
470 * Print formatted output to the opened file.
471 *
472 * @param fildes File descriptor of the opened file.
473 * @param format Format description.
474 * @param ap Print arguments.
475 * @return Either the number of printed characters or negative value on error.
476 */
477int posix_vdprintf(int fildes, const char *restrict format, va_list ap)
478{
479 printf_spec_t spec = {
480 .str_write = _dprintf_str_write,
481 .wstr_write = _dprintf_wstr_write,
482 .data = &fildes
483 };
484
485 return printf_core(format, &spec, ap);
486}
487
488/**
489 * Print formatted output to the string.
490 *
491 * @param s Output string.
492 * @param format Format description.
493 * @return Either the number of printed characters (excluding null byte) or
494 * negative value on error.
495 */
496int posix_sprintf(char *s, const char *restrict format, ...)
497{
498 va_list list;
499 va_start(list, format);
500 int result = posix_vsprintf(s, format, list);
501 va_end(list);
502 return result;
503}
504
505/**
506 * Print formatted output to the string.
507 *
508 * @param s Output string.
509 * @param format Format description.
510 * @param ap Print arguments.
511 * @return Either the number of printed characters (excluding null byte) or
512 * negative value on error.
513 */
514int posix_vsprintf(char *s, const char *restrict format, va_list ap)
515{
516 return vsnprintf(s, STR_NO_LIMIT, format, ap);
517}
518
519/**
520 * Convert formatted input from the stream.
521 *
522 * @param stream Input stream.
523 * @param format Format description.
524 * @return The number of converted output items or EOF on failure.
525 */
526int posix_fscanf(FILE *restrict stream, const char *restrict format, ...)
527{
528 va_list list;
529 va_start(list, format);
530 int result = posix_vfscanf(stream, format, list);
531 va_end(list);
532 return result;
533}
534
535/**
536 * Convert formatted input from the standard input.
537 *
538 * @param format Format description.
539 * @return The number of converted output items or EOF on failure.
540 */
541int posix_scanf(const char *restrict format, ...)
542{
543 va_list list;
544 va_start(list, format);
545 int result = posix_vscanf(format, list);
546 va_end(list);
547 return result;
548}
549
550/**
551 * Convert formatted input from the standard input.
552 *
553 * @param format Format description.
554 * @param arg Output items.
555 * @return The number of converted output items or EOF on failure.
556 */
557int posix_vscanf(const char *restrict format, va_list arg)
558{
559 return posix_vfscanf(stdin, format, arg);
560}
561
562/**
563 * Convert formatted input from the string.
564 *
565 * @param s Input string.
566 * @param format Format description.
567 * @return The number of converted output items or EOF on failure.
568 */
569int posix_sscanf(const char *restrict s, const char *restrict format, ...)
570{
571 va_list list;
572 va_start(list, format);
573 int result = posix_vsscanf(s, format, list);
574 va_end(list);
575 return result;
576}
577
578/**
579 * Acquire file stream for the thread.
580 *
581 * @param file File stream to lock.
582 */
583void posix_flockfile(FILE *file)
584{
585 /* dummy */
586}
587
588/**
589 * Acquire file stream for the thread (non-blocking).
590 *
591 * @param file File stream to lock.
592 * @return Zero for success and non-zero if the lock cannot be acquired.
593 */
594int posix_ftrylockfile(FILE *file)
595{
596 /* dummy */
597 return 0;
598}
599
600/**
601 * Relinquish the ownership of the locked file stream.
602 *
603 * @param file File stream to unlock.
604 */
605void posix_funlockfile(FILE *file)
606{
607 /* dummy */
608}
609
610/**
611 * Get a byte from a stream (thread-unsafe).
612 *
613 * @param stream Input file stream.
614 * @return Either read byte or EOF.
615 */
616int posix_getc_unlocked(FILE *stream)
617{
618 return getc(stream);
619}
620
621/**
622 * Get a byte from the standard input stream (thread-unsafe).
623 *
624 * @return Either read byte or EOF.
625 */
626int posix_getchar_unlocked(void)
627{
628 return getchar();
629}
630
631/**
632 * Put a byte on a stream (thread-unsafe).
633 *
634 * @param c Byte to output.
635 * @param stream Output file stream.
636 * @return Either written byte or EOF.
637 */
638int posix_putc_unlocked(int c, FILE *stream)
639{
640 return putc(c, stream);
641}
642
643/**
644 * Put a byte on the standard output stream (thread-unsafe).
645 *
646 * @param c Byte to output.
647 * @return Either written byte or EOF.
648 */
649int posix_putchar_unlocked(int c)
650{
651 return putchar(c);
652}
653
654/**
655 * Remove a file or directory.
656 *
657 * @param path Pathname of the file that shall be removed.
658 * @return Zero on success, -1 (with errno set) otherwise.
659 */
660int posix_remove(const char *path)
661{
662 struct stat st;
663 int rc = stat(path, &st);
664
665 if (rc != EOK) {
666 errno = -rc;
667 return -1;
668 }
669
670 if (st.is_directory) {
671 rc = rmdir(path);
672 } else {
673 rc = unlink(path);
674 }
675
676 if (rc != EOK) {
677 errno = -rc;
678 return -1;
679 }
680 return 0;
681}
682
683/**
684 * Rename a file or directory.
685 *
686 * @param old Old pathname.
687 * @param new New pathname.
688 * @return Zero on success, -1 (with errno set) otherwise.
689 */
690int posix_rename(const char *old, const char *new)
691{
692 return errnify(rename, old, new);
693}
694
695/**
696 * Get a unique temporary file name (obsolete).
697 *
698 * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
699 * @return The value of s on success, NULL on failure.
700 */
701char *posix_tmpnam(char *s)
702{
703 assert(L_tmpnam >= posix_strlen("/tmp/tnXXXXXX"));
704
705 static char buffer[L_tmpnam + 1];
706 if (s == NULL) {
707 s = buffer;
708 }
709
710 posix_strcpy(s, "/tmp/tnXXXXXX");
711 posix_mktemp(s);
712
713 if (*s == '\0') {
714 /* Errno set by mktemp(). */
715 return NULL;
716 }
717
718 return s;
719}
720
721/**
722 * Get an unique temporary file name with additional constraints (obsolete).
723 *
724 * @param dir Path to directory, where the file should be created.
725 * @param pfx Optional prefix up to 5 characters long.
726 * @return Newly allocated unique path for temporary file. NULL on failure.
727 */
728char *posix_tempnam(const char *dir, const char *pfx)
729{
730 /* Sequence number of the filename. */
731 static int seq = 0;
732
733 size_t dir_len = posix_strlen(dir);
734 if (dir[dir_len - 1] == '/') {
735 dir_len--;
736 }
737
738 size_t pfx_len = posix_strlen(pfx);
739 if (pfx_len > 5) {
740 pfx_len = 5;
741 }
742
743 char *result = malloc(dir_len + /* slash*/ 1 +
744 pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
745
746 if (result == NULL) {
747 errno = ENOMEM;
748 return NULL;
749 }
750
751 char *res_ptr = result;
752 posix_strncpy(res_ptr, dir, dir_len);
753 res_ptr += dir_len;
754 posix_strncpy(res_ptr, pfx, pfx_len);
755 res_ptr += pfx_len;
756
757 for (; seq < 1000; ++seq) {
758 snprintf(res_ptr, 8, "%03d.tmp", seq);
759
760 int orig_errno = errno;
761 errno = 0;
762 /* Check if the file exists. */
763 if (posix_access(result, F_OK) == -1) {
764 if (errno == ENOENT) {
765 errno = orig_errno;
766 break;
767 } else {
768 /* errno set by access() */
769 return NULL;
770 }
771 }
772 }
773
774 if (seq == 1000) {
775 free(result);
776 errno = EINVAL;
777 return NULL;
778 }
779
780 return result;
781}
782
783/**
784 * Create and open an unique temporary file.
785 * The file is automatically removed when the stream is closed.
786 *
787 * @param dir Path to directory, where the file should be created.
788 * @param pfx Optional prefix up to 5 characters long.
789 * @return Newly allocated unique path for temporary file. NULL on failure.
790 */
791FILE *posix_tmpfile(void)
792{
793 char filename[] = "/tmp/tfXXXXXX";
794 int fd = posix_mkstemp(filename);
795 if (fd == -1) {
796 /* errno set by mkstemp(). */
797 return NULL;
798 }
799
800 /* Unlink the created file, so that it's removed on close(). */
801 posix_unlink(filename);
802 return fdopen(fd, "w+");
803}
804
805/** @}
806 */
Note: See TracBrowser for help on using the repository browser.