diff options
author | Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> | 2018-08-30 12:08:12 +0200 |
---|---|---|
committer | Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> | 2018-08-30 12:08:12 +0200 |
commit | e6a9894e97ca88c02e6db45ee3aa48064a969bd3 (patch) | |
tree | 0cf6012027ab70b59eb41209d846809da90e7a4f | |
parent | 54e75699b06aa9d2e4f276239bf062ba46142055 (diff) |
Hello, world!
-rw-r--r-- | lib/builtin/std.c | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/lib/builtin/std.c b/lib/builtin/std.c index e7b1dfe..7dee723 100644 --- a/lib/builtin/std.c +++ b/lib/builtin/std.c @@ -94,7 +94,8 @@ int TIO_EXPORT tio_open_std(tio_stream_t **streamp, FILE *filep, int cl) { std_cookie_t *cookie = NULL; unsigned int flags = 0; - tio_off_t off; + tio_off_t off = 0; + long loff; /* Setup the mode. */ @@ -126,6 +127,38 @@ int TIO_EXPORT tio_open_std(tio_stream_t **streamp, FILE *filep, int cl) /* Initialize the stream. */ - off = ftell(filep); + if ((loff = ftell(filep)) >= 0) + off = (tio_off_t)loff; + return (tio_open(streamp, NULL, cookie, flags, &tio_std_functions, off)); } + +/* --- + * Built-in streams. + * --- */ + +#define BUILTIN_STREAM(NAME, GLOBAL, STREAM_NAME) \ +TIO_LOCAL tio_stream_t *GLOBAL = NULL; \ +int TIO_EXPORT NAME(tio_stream_t **streamp) \ +{ \ + int err; \ +\ + if (!streamp) \ + return (tio_error_arg); \ +\ + if (GLOBAL) { \ + *streamp = GLOBAL; \ + return (tio_error_ok); \ + } \ +\ + err = tio_open_std(&GLOBAL, STREAM_NAME, 0); \ + if (err) \ + return (err); \ +\ + *streamp = GLOBAL; \ + return (tio_error_ok); \ +} + +BUILTIN_STREAM(tio_open_stdout, stdout_stream, stdout) +BUILTIN_STREAM(tio_open_stderr, stderr_stream, stderr) +BUILTIN_STREAM(tio_open_stdin, stdin_stream, stdin) |