2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi│
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/alg/arraylist.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/bits/bits.h"
|
|
|
|
#include "libc/bits/pushpop.h"
|
|
|
|
#include "libc/calls/calls.h"
|
2021-03-27 05:31:41 +00:00
|
|
|
#include "libc/errno.h"
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/mem/mem.h"
|
|
|
|
#include "libc/runtime/runtime.h"
|
2021-01-28 03:34:02 +00:00
|
|
|
#include "libc/stdio/fflush.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/stdio/internal.h"
|
|
|
|
#include "libc/stdio/stdio.h"
|
|
|
|
#include "libc/sysv/consts/o.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Blocks until data from stream buffer is written out.
|
|
|
|
*
|
|
|
|
* @param f is the stream handle
|
2021-03-17 20:02:15 +00:00
|
|
|
* @return is 0 on success or -1 on error
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
|
|
|
int fflush(FILE *f) {
|
|
|
|
size_t i;
|
|
|
|
if (!f) {
|
2021-03-01 07:42:35 +00:00
|
|
|
for (i = __fflush.handles.i; i; --i) {
|
|
|
|
if ((f = __fflush.handles.p[i - 1])) {
|
2021-03-27 05:31:41 +00:00
|
|
|
if (fflush(f) == -1) return -1;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-28 20:01:51 +00:00
|
|
|
} else if (f->fd != -1) {
|
2021-04-23 17:45:19 +00:00
|
|
|
if (__fflush_impl(f) == -1) return -1;
|
2020-11-28 20:01:51 +00:00
|
|
|
} else if (f->beg && f->beg < f->size) {
|
|
|
|
f->buf[f->beg] = 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2021-03-27 05:31:41 +00:00
|
|
|
return 0;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-01 07:42:35 +00:00
|
|
|
textstartup int __fflush_register(FILE *f) {
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i;
|
|
|
|
struct StdioFlush *sf;
|
2021-03-01 07:42:35 +00:00
|
|
|
sf = &__fflush;
|
2020-06-15 14:18:57 +00:00
|
|
|
if (!sf->handles.p) {
|
2022-03-19 10:37:00 +00:00
|
|
|
sf->handles.p = sf->handles_initmem;
|
2020-06-15 14:18:57 +00:00
|
|
|
pushmov(&sf->handles.n, ARRAYLEN(sf->handles_initmem));
|
2022-03-19 10:37:00 +00:00
|
|
|
__cxa_atexit(fflush, 0, 0);
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
for (i = sf->handles.i; i; --i) {
|
|
|
|
if (!sf->handles.p[i - 1]) {
|
|
|
|
sf->handles.p[i - 1] = f;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return append(&sf->handles, &f);
|
|
|
|
}
|
|
|
|
|
2021-03-01 07:42:35 +00:00
|
|
|
void __fflush_unregister(FILE *f) {
|
2020-06-15 14:18:57 +00:00
|
|
|
size_t i;
|
|
|
|
struct StdioFlush *sf;
|
2021-03-01 07:42:35 +00:00
|
|
|
sf = &__fflush;
|
2020-06-15 14:18:57 +00:00
|
|
|
sf = pushpop(sf);
|
|
|
|
for (i = sf->handles.i; i; --i) {
|
|
|
|
if (sf->handles.p[i - 1] == f) {
|
2022-03-19 10:37:00 +00:00
|
|
|
pushmov(&sf->handles.p[i - 1], 0);
|
2020-06-15 14:18:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|