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 | #include "internal/common.h"
|
---|
37 | #include "posix/stdio.h"
|
---|
38 |
|
---|
39 | #include <assert.h>
|
---|
40 |
|
---|
41 | #include <errno.h>
|
---|
42 |
|
---|
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/str.h"
|
---|
51 | #include "libc/malloc.h"
|
---|
52 | #include "libc/adt/list.h"
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Generate a pathname for the controlling terminal.
|
---|
56 | *
|
---|
57 | * @param s Allocated buffer to which the pathname shall be put.
|
---|
58 | * @return Either s or static location filled with the requested pathname.
|
---|
59 | */
|
---|
60 | char *ctermid(char *s)
|
---|
61 | {
|
---|
62 | /* Currently always returns an error value (empty string). */
|
---|
63 | // TODO: return a real terminal path
|
---|
64 |
|
---|
65 | static char dummy_path[L_ctermid] = { '\0' };
|
---|
66 |
|
---|
67 | if (s == NULL) {
|
---|
68 | return dummy_path;
|
---|
69 | }
|
---|
70 |
|
---|
71 | s[0] = '\0';
|
---|
72 | return s;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Read a stream until the delimiter (or EOF) is encountered.
|
---|
77 | *
|
---|
78 | * @param lineptr Pointer to the output buffer in which there will be stored
|
---|
79 | * nul-terminated string together with the delimiter (if encountered).
|
---|
80 | * Will be resized if necessary.
|
---|
81 | * @param n Pointer to the size of the output buffer. Will be increased if
|
---|
82 | * necessary.
|
---|
83 | * @param delimiter Delimiter on which to finish reading the stream.
|
---|
84 | * @param stream Input stream.
|
---|
85 | * @return Number of fetched characters (including delimiter if encountered)
|
---|
86 | * or -1 on error (set in errno).
|
---|
87 | */
|
---|
88 | ssize_t getdelim(char **restrict lineptr, size_t *restrict n,
|
---|
89 | int delimiter, FILE *restrict stream)
|
---|
90 | {
|
---|
91 | /* Check arguments for sanity. */
|
---|
92 | if (!lineptr || !n) {
|
---|
93 | errno = EINVAL;
|
---|
94 | return -1;
|
---|
95 | }
|
---|
96 |
|
---|
97 | size_t alloc_step = 80; /* Buffer size gain during reallocation. */
|
---|
98 | char *pos = *lineptr; /* Next free byte of the output buffer. */
|
---|
99 | size_t cnt = 0; /* Number of fetched characters. */
|
---|
100 | int c = fgetc(stream); /* Current input character. Might be EOF. */
|
---|
101 |
|
---|
102 | do {
|
---|
103 | /* Mask EOF as NUL to terminate string. */
|
---|
104 | if (c == EOF) {
|
---|
105 | c = '\0';
|
---|
106 | }
|
---|
107 |
|
---|
108 | /* Ensure there is still space left in the buffer. */
|
---|
109 | if (pos == *lineptr + *n) {
|
---|
110 | *lineptr = realloc(*lineptr, *n + alloc_step);
|
---|
111 | if (*lineptr) {
|
---|
112 | pos = *lineptr + *n;
|
---|
113 | *n += alloc_step;
|
---|
114 | } else {
|
---|
115 | errno = ENOMEM;
|
---|
116 | return -1;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | /* Store the fetched character. */
|
---|
121 | *pos = c;
|
---|
122 |
|
---|
123 | /* Fetch the next character according to the current character. */
|
---|
124 | if (c != '\0') {
|
---|
125 | ++pos;
|
---|
126 | ++cnt;
|
---|
127 | if (c == delimiter) {
|
---|
128 | /*
|
---|
129 | * Delimiter was just stored. Provide EOF as the next
|
---|
130 | * character - it will be masked as NUL and output string
|
---|
131 | * will be properly terminated.
|
---|
132 | */
|
---|
133 | c = EOF;
|
---|
134 | } else {
|
---|
135 | /*
|
---|
136 | * Neither delimiter nor EOF were encountered. Just fetch
|
---|
137 | * the next character from the stream.
|
---|
138 | */
|
---|
139 | c = fgetc(stream);
|
---|
140 | }
|
---|
141 | }
|
---|
142 | } while (c != '\0');
|
---|
143 |
|
---|
144 | if (errno == EOK && cnt > 0) {
|
---|
145 | return cnt;
|
---|
146 | } else {
|
---|
147 | /* Either some error occured or the stream was already at EOF. */
|
---|
148 | return -1;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Read a stream until the newline (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 stream Input stream.
|
---|
161 | * @return Number of fetched characters (including newline if encountered)
|
---|
162 | * or -1 on error (set in errno).
|
---|
163 | */
|
---|
164 | ssize_t getline(char **restrict lineptr, size_t *restrict n,
|
---|
165 | FILE *restrict stream)
|
---|
166 | {
|
---|
167 | return getdelim(lineptr, n, '\n', stream);
|
---|
168 | }
|
---|
169 |
|
---|
170 | /**
|
---|
171 | * Write error messages to standard error.
|
---|
172 | *
|
---|
173 | * @param s Error message.
|
---|
174 | */
|
---|
175 | void perror(const char *s)
|
---|
176 | {
|
---|
177 | if (s == NULL || s[0] == '\0') {
|
---|
178 | fprintf(stderr, "%s\n", strerror(errno));
|
---|
179 | } else {
|
---|
180 | fprintf(stderr, "%s: %s\n", s, strerror(errno));
|
---|
181 | }
|
---|
182 | }
|
---|
183 |
|
---|
184 | /** Restores stream a to position previously saved with fgetpos().
|
---|
185 | *
|
---|
186 | * @param stream Stream to restore
|
---|
187 | * @param pos Position to restore
|
---|
188 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
189 | */
|
---|
190 | int fsetpos(FILE *stream, const fpos_t *pos)
|
---|
191 | {
|
---|
192 | return fseek64(stream, pos->offset, SEEK_SET);
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Saves the stream's position for later use by fsetpos().
|
---|
196 | *
|
---|
197 | * @param stream Stream to save
|
---|
198 | * @param pos Place to store the position
|
---|
199 | * @return Zero on success, non-zero (with errno set) on failure
|
---|
200 | */
|
---|
201 | int fgetpos(FILE *restrict stream, fpos_t *restrict pos)
|
---|
202 | {
|
---|
203 | off64_t ret = ftell64(stream);
|
---|
204 | if (ret != -1) {
|
---|
205 | pos->offset = ret;
|
---|
206 | return 0;
|
---|
207 | } else {
|
---|
208 | return -1;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * Reposition a file-position indicator in a stream.
|
---|
214 | *
|
---|
215 | * @param stream Stream to seek in.
|
---|
216 | * @param offset Direction and amount of bytes to seek.
|
---|
217 | * @param whence From where to seek.
|
---|
218 | * @return Zero on success, -1 otherwise.
|
---|
219 | */
|
---|
220 | int fseeko(FILE *stream, off_t offset, int whence)
|
---|
221 | {
|
---|
222 | return fseek64(stream, offset, whence);
|
---|
223 | }
|
---|
224 |
|
---|
225 | /**
|
---|
226 | * Discover current file offset in a stream.
|
---|
227 | *
|
---|
228 | * @param stream Stream for which the offset shall be retrieved.
|
---|
229 | * @return Current offset or -1 if not possible.
|
---|
230 | */
|
---|
231 | off_t ftello(FILE *stream)
|
---|
232 | {
|
---|
233 | return ftell64(stream);
|
---|
234 | }
|
---|
235 |
|
---|
236 | /**
|
---|
237 | * Print formatted output to the opened file.
|
---|
238 | *
|
---|
239 | * @param fildes File descriptor of the opened file.
|
---|
240 | * @param format Format description.
|
---|
241 | * @return Either the number of printed characters or negative value on error.
|
---|
242 | */
|
---|
243 | int dprintf(int fildes, const char *restrict format, ...)
|
---|
244 | {
|
---|
245 | va_list list;
|
---|
246 | va_start(list, format);
|
---|
247 | int result = vdprintf(fildes, format, list);
|
---|
248 | va_end(list);
|
---|
249 | return result;
|
---|
250 | }
|
---|
251 |
|
---|
252 | /**
|
---|
253 | * Write ordinary string to the opened file.
|
---|
254 | *
|
---|
255 | * @param str String to be written.
|
---|
256 | * @param size Size of the string (in bytes)..
|
---|
257 | * @param fd File descriptor of the opened file.
|
---|
258 | * @return The number of written characters.
|
---|
259 | */
|
---|
260 | static int _dprintf_str_write(const char *str, size_t size, void *fd)
|
---|
261 | {
|
---|
262 | const int fildes = *(int *) fd;
|
---|
263 | size_t wr;
|
---|
264 | if (failed(vfs_write(fildes, &posix_pos[fildes], str, size, &wr)))
|
---|
265 | return -1;
|
---|
266 | return str_nlength(str, wr);
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Write wide string to the opened file.
|
---|
271 | *
|
---|
272 | * @param str String to be written.
|
---|
273 | * @param size Size of the string (in bytes).
|
---|
274 | * @param fd File descriptor of the opened file.
|
---|
275 | * @return The number of written characters.
|
---|
276 | */
|
---|
277 | static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd)
|
---|
278 | {
|
---|
279 | size_t offset = 0;
|
---|
280 | size_t chars = 0;
|
---|
281 | size_t sz;
|
---|
282 | char buf[4];
|
---|
283 |
|
---|
284 | while (offset < size) {
|
---|
285 | sz = 0;
|
---|
286 | if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
|
---|
287 | break;
|
---|
288 | }
|
---|
289 |
|
---|
290 | const int fildes = *(int *) fd;
|
---|
291 | size_t nwr;
|
---|
292 | if (vfs_write(fildes, &posix_pos[fildes], buf, sz, &nwr) != EOK)
|
---|
293 | break;
|
---|
294 |
|
---|
295 | chars++;
|
---|
296 | offset += sizeof(wchar_t);
|
---|
297 | }
|
---|
298 |
|
---|
299 | return chars;
|
---|
300 | }
|
---|
301 |
|
---|
302 | /**
|
---|
303 | * Print formatted output to the opened file.
|
---|
304 | *
|
---|
305 | * @param fildes File descriptor of the opened file.
|
---|
306 | * @param format Format description.
|
---|
307 | * @param ap Print arguments.
|
---|
308 | * @return Either the number of printed characters or negative value on error.
|
---|
309 | */
|
---|
310 | int vdprintf(int fildes, const char *restrict format, va_list ap)
|
---|
311 | {
|
---|
312 | printf_spec_t spec = {
|
---|
313 | .str_write = _dprintf_str_write,
|
---|
314 | .wstr_write = _dprintf_wstr_write,
|
---|
315 | .data = &fildes
|
---|
316 | };
|
---|
317 |
|
---|
318 | return printf_core(format, &spec, ap);
|
---|
319 | }
|
---|
320 |
|
---|
321 | /**
|
---|
322 | * Print formatted output to the string.
|
---|
323 | *
|
---|
324 | * @param s Output string.
|
---|
325 | * @param format Format description.
|
---|
326 | * @return Either the number of printed characters (excluding null byte) or
|
---|
327 | * negative value on error.
|
---|
328 | */
|
---|
329 | int sprintf(char *s, const char *restrict format, ...)
|
---|
330 | {
|
---|
331 | va_list list;
|
---|
332 | va_start(list, format);
|
---|
333 | int result = vsprintf(s, format, list);
|
---|
334 | va_end(list);
|
---|
335 | return result;
|
---|
336 | }
|
---|
337 |
|
---|
338 | /**
|
---|
339 | * Print formatted output to the string.
|
---|
340 | *
|
---|
341 | * @param s Output string.
|
---|
342 | * @param format Format description.
|
---|
343 | * @param ap Print arguments.
|
---|
344 | * @return Either the number of printed characters (excluding null byte) or
|
---|
345 | * negative value on error.
|
---|
346 | */
|
---|
347 | int vsprintf(char *s, const char *restrict format, va_list ap)
|
---|
348 | {
|
---|
349 | return vsnprintf(s, INT_MAX, format, ap);
|
---|
350 | }
|
---|
351 |
|
---|
352 | /**
|
---|
353 | * Acquire file stream for the thread.
|
---|
354 | *
|
---|
355 | * @param file File stream to lock.
|
---|
356 | */
|
---|
357 | void flockfile(FILE *file)
|
---|
358 | {
|
---|
359 | /* dummy */
|
---|
360 | }
|
---|
361 |
|
---|
362 | /**
|
---|
363 | * Acquire file stream for the thread (non-blocking).
|
---|
364 | *
|
---|
365 | * @param file File stream to lock.
|
---|
366 | * @return Zero for success and non-zero if the lock cannot be acquired.
|
---|
367 | */
|
---|
368 | int ftrylockfile(FILE *file)
|
---|
369 | {
|
---|
370 | /* dummy */
|
---|
371 | return 0;
|
---|
372 | }
|
---|
373 |
|
---|
374 | /**
|
---|
375 | * Relinquish the ownership of the locked file stream.
|
---|
376 | *
|
---|
377 | * @param file File stream to unlock.
|
---|
378 | */
|
---|
379 | void funlockfile(FILE *file)
|
---|
380 | {
|
---|
381 | /* dummy */
|
---|
382 | }
|
---|
383 |
|
---|
384 | /**
|
---|
385 | * Get a byte from a stream (thread-unsafe).
|
---|
386 | *
|
---|
387 | * @param stream Input file stream.
|
---|
388 | * @return Either read byte or EOF.
|
---|
389 | */
|
---|
390 | int getc_unlocked(FILE *stream)
|
---|
391 | {
|
---|
392 | return getc(stream);
|
---|
393 | }
|
---|
394 |
|
---|
395 | /**
|
---|
396 | * Get a byte from the standard input stream (thread-unsafe).
|
---|
397 | *
|
---|
398 | * @return Either read byte or EOF.
|
---|
399 | */
|
---|
400 | int getchar_unlocked(void)
|
---|
401 | {
|
---|
402 | return getchar();
|
---|
403 | }
|
---|
404 |
|
---|
405 | /**
|
---|
406 | * Put a byte on a stream (thread-unsafe).
|
---|
407 | *
|
---|
408 | * @param c Byte to output.
|
---|
409 | * @param stream Output file stream.
|
---|
410 | * @return Either written byte or EOF.
|
---|
411 | */
|
---|
412 | int putc_unlocked(int c, FILE *stream)
|
---|
413 | {
|
---|
414 | return putc(c, stream);
|
---|
415 | }
|
---|
416 |
|
---|
417 | /**
|
---|
418 | * Put a byte on the standard output stream (thread-unsafe).
|
---|
419 | *
|
---|
420 | * @param c Byte to output.
|
---|
421 | * @return Either written byte or EOF.
|
---|
422 | */
|
---|
423 | int putchar_unlocked(int c)
|
---|
424 | {
|
---|
425 | return putchar(c);
|
---|
426 | }
|
---|
427 |
|
---|
428 | /**
|
---|
429 | * Get a unique temporary file name (obsolete).
|
---|
430 | *
|
---|
431 | * @param s Buffer for the file name. Must be at least L_tmpnam bytes long.
|
---|
432 | * @return The value of s on success, NULL on failure.
|
---|
433 | */
|
---|
434 | char *tmpnam(char *s)
|
---|
435 | {
|
---|
436 | assert(L_tmpnam >= strlen("/tmp/tnXXXXXX"));
|
---|
437 |
|
---|
438 | static char buffer[L_tmpnam + 1];
|
---|
439 | if (s == NULL) {
|
---|
440 | s = buffer;
|
---|
441 | }
|
---|
442 |
|
---|
443 | strcpy(s, "/tmp/tnXXXXXX");
|
---|
444 | mktemp(s);
|
---|
445 |
|
---|
446 | if (*s == '\0') {
|
---|
447 | /* Errno set by mktemp(). */
|
---|
448 | return NULL;
|
---|
449 | }
|
---|
450 |
|
---|
451 | return s;
|
---|
452 | }
|
---|
453 |
|
---|
454 | /**
|
---|
455 | * Get an unique temporary file name with additional constraints (obsolete).
|
---|
456 | *
|
---|
457 | * @param dir Path to directory, where the file should be created.
|
---|
458 | * @param pfx Optional prefix up to 5 characters long.
|
---|
459 | * @return Newly allocated unique path for temporary file. NULL on failure.
|
---|
460 | */
|
---|
461 | char *tempnam(const char *dir, const char *pfx)
|
---|
462 | {
|
---|
463 | /* Sequence number of the filename. */
|
---|
464 | static int seq = 0;
|
---|
465 |
|
---|
466 | size_t dir_len = strlen(dir);
|
---|
467 | if (dir[dir_len - 1] == '/') {
|
---|
468 | dir_len--;
|
---|
469 | }
|
---|
470 |
|
---|
471 | size_t pfx_len = strlen(pfx);
|
---|
472 | if (pfx_len > 5) {
|
---|
473 | pfx_len = 5;
|
---|
474 | }
|
---|
475 |
|
---|
476 | char *result = malloc(dir_len + /* slash*/ 1 +
|
---|
477 | pfx_len + /* three-digit seq */ 3 + /* .tmp */ 4 + /* nul */ 1);
|
---|
478 |
|
---|
479 | if (result == NULL) {
|
---|
480 | errno = ENOMEM;
|
---|
481 | return NULL;
|
---|
482 | }
|
---|
483 |
|
---|
484 | char *res_ptr = result;
|
---|
485 | strncpy(res_ptr, dir, dir_len);
|
---|
486 | res_ptr += dir_len;
|
---|
487 | strncpy(res_ptr, pfx, pfx_len);
|
---|
488 | res_ptr += pfx_len;
|
---|
489 |
|
---|
490 | for (; seq < 1000; ++seq) {
|
---|
491 | snprintf(res_ptr, 8, "%03d.tmp", seq);
|
---|
492 |
|
---|
493 | int orig_errno = errno;
|
---|
494 | errno = EOK;
|
---|
495 | /* Check if the file exists. */
|
---|
496 | if (access(result, F_OK) == -1) {
|
---|
497 | if (errno == ENOENT) {
|
---|
498 | errno = orig_errno;
|
---|
499 | break;
|
---|
500 | } else {
|
---|
501 | /* errno set by access() */
|
---|
502 | return NULL;
|
---|
503 | }
|
---|
504 | }
|
---|
505 | }
|
---|
506 |
|
---|
507 | if (seq == 1000) {
|
---|
508 | free(result);
|
---|
509 | errno = EINVAL;
|
---|
510 | return NULL;
|
---|
511 | }
|
---|
512 |
|
---|
513 | return result;
|
---|
514 | }
|
---|
515 |
|
---|
516 | /**
|
---|
517 | * Create and open an unique temporary file.
|
---|
518 | * The file is automatically removed when the stream is closed.
|
---|
519 | *
|
---|
520 | * @param dir Path to directory, where the file should be created.
|
---|
521 | * @param pfx Optional prefix up to 5 characters long.
|
---|
522 | * @return Newly allocated unique path for temporary file. NULL on failure.
|
---|
523 | */
|
---|
524 | FILE *tmpfile(void)
|
---|
525 | {
|
---|
526 | char filename[] = "/tmp/tfXXXXXX";
|
---|
527 | int fd = mkstemp(filename);
|
---|
528 | if (fd == -1) {
|
---|
529 | /* errno set by mkstemp(). */
|
---|
530 | return NULL;
|
---|
531 | }
|
---|
532 |
|
---|
533 | /* Unlink the created file, so that it's removed on close(). */
|
---|
534 | unlink(filename);
|
---|
535 | return fdopen(fd, "w+");
|
---|
536 | }
|
---|
537 |
|
---|
538 | /** @}
|
---|
539 | */
|
---|