source: mainline/uspace/lib/posix/source/stdio.c@ 163fc09

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 163fc09 was 163fc09, checked in by Jakub Jermar <jakub@…>, 8 years ago

Rename rename() to vfs_rename_path()

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