summaryrefslogtreecommitdiff
path: root/src/stdio
diff options
context:
space:
mode:
Diffstat (limited to 'src/stdio')
-rw-r--r--src/stdio/fflush.c3
-rw-r--r--src/stdio/fopen.c39
-rw-r--r--src/stdio/fprintf.c1
-rw-r--r--src/stdio/fscanf.c1
-rw-r--r--src/stdio/printf.c1
-rw-r--r--src/stdio/rename.c1
-rw-r--r--src/stdio/scanf.c1
-rw-r--r--src/stdio/snprintf.c12
-rw-r--r--src/stdio/sprintf.c1
-rw-r--r--src/stdio/sscanf.c1
-rw-r--r--src/stdio/vfprintf.c1
-rw-r--r--src/stdio/vfscanf.c7
-rw-r--r--src/stdio/vprintf.c1
-rw-r--r--src/stdio/vsnprintf.c10
-rw-r--r--src/stdio/vsprintf.c1
-rw-r--r--src/stdio/vsscanf.c7
16 files changed, 65 insertions, 23 deletions
diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c
index c7514d10..c4b5ad58 100644
--- a/src/stdio/fflush.c
+++ b/src/stdio/fflush.c
@@ -1,6 +1,5 @@
#include <stdio.h>
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** flush buffered writes **/
int fflush(FILE *stream)
@@ -8,7 +7,7 @@ int fflush(FILE *stream)
flockfile(stream);
if (stream == NULL) {
FILE *p;
- for (p = __libc(FILE_TAIL); p != NULL; p = p->prev) {
+ for (p = __libc(FILE_STREAMS); p != NULL; p = p->prev) {
fflush(p);
}
}
diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c
index 935885d9..1b415969 100644
--- a/src/stdio/fopen.c
+++ b/src/stdio/fopen.c
@@ -1,29 +1,48 @@
#include <stdio.h>
#include "stdlib.h"
-#include "nonstd/internal.h"
#include "nonstd/io.h"
/** open a file stream **/
FILE * fopen(const char * restrict filename, const char * restrict mode)
{
- FILE *f = calloc(1, sizeof(*f));
- f->fd = -1;
- if (freopen(filename, mode, f) == NULL) {
- free(f);
- return NULL;
+ struct __FILE *base = __libc(FILE_STREAMS);
+ struct __FILE *f = base;
+
+ /* find the next available stream */
+ while (f->next != NULL) {
+ f = f->next;
+ }
+
+ /* 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 (__libc(FILE_TAIL)) {
- f->prev = __libc(FILE_TAIL);
- f->prev->next = f;
+ /* if we had to allocate, but that failed, we're out of memory */
+ if (f->next == NULL) {
+ return NULL;
}
- /* __libc(FILE_TAIL) = f; */
+ /* 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;
+ }
/*
RETURN_SUCCESS(a pointer to the new file stream);
RETURN_FAILURE(CONSTANT(NULL));
*/
+
return f;
}
diff --git a/src/stdio/fprintf.c b/src/stdio/fprintf.c
index 053277d7..e6651076 100644
--- a/src/stdio/fprintf.c
+++ b/src/stdio/fprintf.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "stdarg.h"
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** write formatted output to a file stream **/
int fprintf(FILE * restrict stream, const char * restrict format, ...)
diff --git a/src/stdio/fscanf.c b/src/stdio/fscanf.c
index adbb753c..9e027cf5 100644
--- a/src/stdio/fscanf.c
+++ b/src/stdio/fscanf.c
@@ -1,6 +1,5 @@
#include <stdio.h>
#include "stdarg.h"
-#include "nonstd/internal.h"
#include "nonstd/io.h"
/** read formatted input from a file stream **/
diff --git a/src/stdio/printf.c b/src/stdio/printf.c
index 57684158..0a95fbeb 100644
--- a/src/stdio/printf.c
+++ b/src/stdio/printf.c
@@ -1,6 +1,5 @@
#include <stdio.h>
#include "stdarg.h"
-#include "nonstd/internal.h"
#include "nonstd/io.h"
/** write formatted output **/
diff --git a/src/stdio/rename.c b/src/stdio/rename.c
index 10ad7283..068cd73d 100644
--- a/src/stdio/rename.c
+++ b/src/stdio/rename.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "errno.h"
#include "nonstd/syscall.h"
-#include "nonstd/internal.h"
/** rename a file **/
int rename(const char *old, const char *new)
diff --git a/src/stdio/scanf.c b/src/stdio/scanf.c
index 5fbe51ec..cc392373 100644
--- a/src/stdio/scanf.c
+++ b/src/stdio/scanf.c
@@ -1,6 +1,5 @@
#include <stdio.h>
#include "stdarg.h"
-#include "nonstd/internal.h"
#include "nonstd/io.h"
/** read formatted input **/
diff --git a/src/stdio/snprintf.c b/src/stdio/snprintf.c
index bc6df080..acf234b1 100644
--- a/src/stdio/snprintf.c
+++ b/src/stdio/snprintf.c
@@ -1,7 +1,19 @@
#include <stdio.h>
+#include "stdarg.h"
+#include "nonstd/io.h"
int snprintf(char * restrict s, size_t n, const char * restrict format, ...)
{
+ int ret = 0;
+ va_list ap;
+ struct io_options opt = {0};
+ opt.fnname = __func__;
+ opt.string = s;
+ opt.maxlen = n;
+ va_start(ap, format);
+ ret = __printf(&opt, format, ap);
+ va_end(ap);
+ return ret;
}
/*
diff --git a/src/stdio/sprintf.c b/src/stdio/sprintf.c
index babd4ba3..2872331a 100644
--- a/src/stdio/sprintf.c
+++ b/src/stdio/sprintf.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "stdarg.h"
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** write formatted output to a string **/
int sprintf(char * restrict s, const char * restrict format, ...)
diff --git a/src/stdio/sscanf.c b/src/stdio/sscanf.c
index f6637476..02939338 100644
--- a/src/stdio/sscanf.c
+++ b/src/stdio/sscanf.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "stdarg.h"
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** read formatted input from a string **/
int sscanf(const char * restrict s, const char * restrict format, ...)
diff --git a/src/stdio/vfprintf.c b/src/stdio/vfprintf.c
index c6a35edc..450d1383 100644
--- a/src/stdio/vfprintf.c
+++ b/src/stdio/vfprintf.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "stdarg.h"
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** write formatted output to a file stream **/
int vfprintf(FILE * restrict stream, const char * restrict format, va_list arg)
diff --git a/src/stdio/vfscanf.c b/src/stdio/vfscanf.c
index 44ce6ea2..6c0f97ee 100644
--- a/src/stdio/vfscanf.c
+++ b/src/stdio/vfscanf.c
@@ -1,7 +1,14 @@
#include <stdio.h>
+#include "nonstd/io.h"
int vfscanf(FILE * restrict stream, const char * restrict format, va_list arg)
{
+ int ret = 0;
+ struct io_options opt = {0};
+ opt.fnname = __func__;
+ opt.stream = stream;
+ ret = __scanf(&opt, format, arg);
+ return ret;
}
/*
diff --git a/src/stdio/vprintf.c b/src/stdio/vprintf.c
index eb9b4d30..29c1b062 100644
--- a/src/stdio/vprintf.c
+++ b/src/stdio/vprintf.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "stdarg.h"
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** write formatted output **/
int vprintf(const char * restrict format, va_list arg)
diff --git a/src/stdio/vsnprintf.c b/src/stdio/vsnprintf.c
index 7c5a1e03..f54a15a1 100644
--- a/src/stdio/vsnprintf.c
+++ b/src/stdio/vsnprintf.c
@@ -1,8 +1,16 @@
#include <stdio.h>
#include "stdarg.h"
+#include "nonstd/io.h"
-int vsnprintf(char * restrict s, size_t n, va_list ap)
+int vsnprintf(char * restrict s, size_t n, const char *format, va_list arg)
{
+ int ret = 0;
+ struct io_options opt = {0};
+ opt.fnname = "fprintf";
+ opt.string = s;
+ opt.maxlen = n;
+ ret = __printf(&opt, format, arg);
+ return ret;
}
/*
diff --git a/src/stdio/vsprintf.c b/src/stdio/vsprintf.c
index 4a6ec2b8..43caff52 100644
--- a/src/stdio/vsprintf.c
+++ b/src/stdio/vsprintf.c
@@ -1,7 +1,6 @@
#include <stdio.h>
#include "stdarg.h"
#include "nonstd/io.h"
-#include "nonstd/internal.h"
/** write formatted output to a string **/
int vsprintf(char *s, const char *format, va_list arg)
diff --git a/src/stdio/vsscanf.c b/src/stdio/vsscanf.c
index 534f30ea..d5322078 100644
--- a/src/stdio/vsscanf.c
+++ b/src/stdio/vsscanf.c
@@ -1,8 +1,15 @@
#include <stdio.h>
#include "stdarg.h"
+#include "nonstd/io.h"
int vsscanf(const char * restrict s, const char * restrict format, va_list arg)
{
+ int ret = 0;
+ struct io_options opt = {0};
+ opt.fnname = __func__;
+ opt.string = (char*)s;
+ ret = __scanf(&opt, format, arg);
+ return ret;
}
/*