source: mainline/uspace/lib/posix/stdio.c@ 2a53f71

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

Added missing comments to the newly implemented functions.

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