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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d948095 was 32b3a12, checked in by Vojtech Horky <vojtechhorky@…>, 13 years ago

libposix: even less includes from libc

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