summaryrefslogtreecommitdiff
path: root/src/stdio/fopen.c
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-15 21:23:11 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-15 21:23:11 -0400
commit8a904ed2241fc8338944cc75db8c488490da56c8 (patch)
tree0bf55c2bb2f564a05ebea1f120592849af94af2d /src/stdio/fopen.c
parent2eff308e962c835a00123292b8e087cea4878c60 (diff)
simplify
Diffstat (limited to 'src/stdio/fopen.c')
-rw-r--r--src/stdio/fopen.c43
1 files changed, 15 insertions, 28 deletions
diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c
index 95130917..4f85f864 100644
--- a/src/stdio/fopen.c
+++ b/src/stdio/fopen.c
@@ -1,40 +1,27 @@
+#include <errno.h>
#include <stdio.h>
-#include "stdlib.h"
+#include <stdlib.h>
#include "_stdio.h"
/** open a file stream **/
+
FILE * fopen(const char * restrict filename, const char * restrict mode)
{
- struct __FILE *base = __stdio.FILES;
- struct __FILE *f = base;
+ FILE *f = NULL;
+ size_t i;
- /* find the next available stream */
- while (f->next != NULL) {
- f = f->next;
+ for (i = 0; i < FOPEN_MAX; i++) {
+ if (__stdio.FILES[i].bmode == 0) {
+ f = &(__stdio.FILES[i]);
+ break;
+ }
}
- /* use a stream from the guaranteed space if possible */
- /* otherwise, allocate a new stream */
- if (f < base + FOPEN_MAX) {
- f->next = f + 1;
- } else {
- f->next = malloc(sizeof(*f->next));
- }
+ if (f == NULL) {
+ #ifdef EMFILE
+ errno = EMFILE;
+ #endif
- /* if we had to allocate, but that failed, we're out of memory */
- if (f->next == NULL) {
- return NULL;
- }
-
- /* open the new stream */
- f->next->prev = f;
- f = f->next;
- f->fd = -1;
- if (freopen(filename, mode, f) == NULL) {
- if (f < base + FOPEN_MAX) {
- } else {
- free(f);
- }
return NULL;
}
@@ -43,7 +30,7 @@ FILE * fopen(const char * restrict filename, const char * restrict mode)
RETURN_FAILURE(CONSTANT(NULL));
*/
- return f;
+ return freopen(filename, mode, f);
}
/***