Stub out most of stdio on AltOS
authorKeith Packard <keithp@keithp.com>
Mon, 19 Mar 2012 05:42:33 +0000 (22:42 -0700)
committerKeith Packard <keithp@keithp.com>
Mon, 19 Mar 2012 05:42:33 +0000 (22:42 -0700)
We're only using stdio for printf support, so stub out most of the
rest of it. A more complete job could be done, but this seems
sufficient for now.

Signed-off-by: Keith Packard <keithp@keithp.com>
16 files changed:
platform/altos/functions/_PDCLIB/fillbuffer.c
platform/altos/functions/_PDCLIB/flushbuffer.c
platform/altos/functions/_PDCLIB/prepread.c [new file with mode: 0644]
platform/altos/functions/_PDCLIB/prepwrite.c [new file with mode: 0644]
platform/altos/functions/stdio/fflush.c [new file with mode: 0644]
platform/altos/functions/stdio/fgetc.c [new file with mode: 0644]
platform/altos/functions/stdio/fgetpos.c [new file with mode: 0644]
platform/altos/functions/stdio/fgets.c [new file with mode: 0644]
platform/altos/functions/stdio/fputc.c [new file with mode: 0644]
platform/altos/functions/stdio/fputs.c [new file with mode: 0644]
platform/altos/functions/stdio/fread.c [new file with mode: 0644]
platform/altos/functions/stdio/fsetpos.c [new file with mode: 0644]
platform/altos/functions/stdio/fwrite.c [new file with mode: 0644]
platform/altos/functions/stdio/gets.c [new file with mode: 0644]
platform/altos/functions/stdio/puts.c [new file with mode: 0644]
platform/altos/functions/stdio/ungetc.c [new file with mode: 0644]

index cf55c58123e1d038e3939c3f637281144582f1a6..6e592fd51e225e16f62a5774f0f59964df3ceada 100644 (file)
 #ifndef REGTEST
 #include <_PDCLIB_glue.h>
 
+extern int PDCLIB_NO_ACTUAL_STDIO();
+
 int _PDCLIB_fillbuffer( struct _PDCLIB_file_t * stream )
 {
-       stream->buffer[0] = inbyte();
-       stream->pos.offset += 1;
-       stream->bufend = 1;
-       stream->bufidx = 0;
-       return 0;
+       return PDCLIB_NO_ACTUAL_STDIO();
 }
 
 #endif
index 2c54ce0d941506d23d565a96b46545d1f638103b..88a9db01b59dc499ea6b9ff36f4dd56d465fadce 100644 (file)
@@ -29,11 +29,7 @@ extern void outbyte(char c);
 
 int _PDCLIB_flushbuffer( struct _PDCLIB_file_t * stream )
 {
-       char *c = stream->buffer;
-       int i = stream->bufidx;
-       while (i--)
-               outbyte(*c++);
-       return 0;
+       return PDCLIB_NO_ACTUAL_STDIO();
 }
 
 #endif
diff --git a/platform/altos/functions/_PDCLIB/prepread.c b/platform/altos/functions/_PDCLIB/prepread.c
new file mode 100644 (file)
index 0000000..7e1dcb3
--- /dev/null
@@ -0,0 +1,28 @@
+/* $Id$ */
+
+/* _PDCLIB_prepread( struct _PDCLIB_file_t * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+extern int PDCLIB_NO_ACTUAL_STDIO();
+
+int _PDCLIB_prepread( struct _PDCLIB_file_t * stream )
+{
+       return PDCLIB_NO_ACTUAL_STDIO();
+}
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/_PDCLIB/prepwrite.c b/platform/altos/functions/_PDCLIB/prepwrite.c
new file mode 100644 (file)
index 0000000..fda144a
--- /dev/null
@@ -0,0 +1,28 @@
+/* $Id$ */
+
+/* _PDCLIB_prepwrite( struct _PDCLIB_file_t * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+extern int PDCLIB_NO_ACTUAL_STDIO();
+
+int _PDCLIB_prepwrite( struct _PDCLIB_file_t * stream )
+{
+       return PDCLIB_NO_ACTUAL_STDIO();
+}
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/fflush.c b/platform/altos/functions/stdio/fflush.c
new file mode 100644 (file)
index 0000000..2e7013e
--- /dev/null
@@ -0,0 +1,30 @@
+/* $Id$ */
+
+/* fflush( FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+int fflush( struct _PDCLIB_file_t * stream )
+{
+    flush();
+}
+                
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/fgetc.c b/platform/altos/functions/stdio/fgetc.c
new file mode 100644 (file)
index 0000000..65aa4a2
--- /dev/null
@@ -0,0 +1,29 @@
+/* $Id$ */
+
+/* fgetc( FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+int fgetc( struct _PDCLIB_file_t * stream )
+{
+    return getchar();
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif
diff --git a/platform/altos/functions/stdio/fgetpos.c b/platform/altos/functions/stdio/fgetpos.c
new file mode 100644 (file)
index 0000000..4c7a382
--- /dev/null
@@ -0,0 +1,43 @@
+/* $Id$ */
+
+/* fgetpos( FILE * , fpos_t * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+int fgetpos( struct _PDCLIB_file_t * _PDCLIB_restrict stream, struct _PDCLIB_fpos_t * _PDCLIB_restrict pos )
+{
+    pos->offset = 0;
+    pos->status = 0;
+    /* TODO: Add mbstate. */
+    return 0;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+#include <string.h>
+
+int main( void )
+{
+    FILE * fh;
+    fpos_t pos1, pos2;
+    TESTCASE( ( fh = tmpfile() ) != NULL );
+    TESTCASE( fgetpos( fh, &pos1 ) == 0 );
+    TESTCASE( fwrite( teststring, 1, strlen( teststring ), fh ) == strlen( teststring ) );
+    TESTCASE( fgetpos( fh, &pos2 ) == 0 );
+    TESTCASE( fsetpos( fh, &pos1 ) == 0 );
+    TESTCASE( ftell( fh ) == 0 );
+    TESTCASE( fsetpos( fh, &pos2 ) == 0 );
+    TESTCASE( (size_t)ftell( fh ) == strlen( teststring ) );
+    TESTCASE( fclose( fh ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
diff --git a/platform/altos/functions/stdio/fgets.c b/platform/altos/functions/stdio/fgets.c
new file mode 100644 (file)
index 0000000..5fd05f7
--- /dev/null
@@ -0,0 +1,79 @@
+/* $Id$ */
+
+/* fgets( char *, int, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+    if ( size == 0 )
+    {
+        return NULL;
+    }
+    if ( size == 1 )
+    {
+        *s = '\0';
+        return s;
+    }
+    char * dest = s;
+    for (;;) {
+       int c = getc(stream);
+       if (c == EOF)
+           break;
+       *dest++ = c;
+       if (c == '\n')
+           break;
+       if (--size <= 0)
+           break;
+    }
+    *dest = '\0';
+    return ( dest == s ) ? NULL : s;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+#include <string.h>
+
+int main( void )
+{
+    FILE * fh;
+    char buffer[10];
+    char const * fgets_test = "foo\nbar\0baz\nweenie";
+    TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
+    TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
+    rewind( fh );
+    TESTCASE( fgets( buffer, 10, fh ) == buffer );
+    TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
+    TESTCASE( fgets( buffer, 10, fh ) == buffer );
+    TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
+    TESTCASE( fgets( buffer, 10, fh ) == buffer );
+    TESTCASE( strcmp( buffer, "weenie" ) == 0 );
+    TESTCASE( feof( fh ) );
+    TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
+    TESTCASE( fgets( buffer, 1, fh ) == buffer );
+    TESTCASE( strcmp( buffer, "" ) == 0 );
+    TESTCASE( fgets( buffer, 0, fh ) == NULL );
+    TESTCASE( ! feof( fh ) );
+    TESTCASE( fgets( buffer, 1, fh ) == buffer );
+    TESTCASE( strcmp( buffer, "" ) == 0 );
+    TESTCASE( ! feof( fh ) );
+    TESTCASE( fgets( buffer, 2, fh ) == buffer );
+    TESTCASE( strcmp( buffer, "e" ) == 0 );
+    TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
+    TESTCASE( fgets( buffer, 2, fh ) == NULL );
+    TESTCASE( feof( fh ) );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( remove( testfile ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/fputc.c b/platform/altos/functions/stdio/fputc.c
new file mode 100644 (file)
index 0000000..059c86f
--- /dev/null
@@ -0,0 +1,36 @@
+/* $Id$ */
+
+/* fputc( int, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+extern void ao_putchar(char c);
+
+/* Write the value c (cast to unsigned char) to the given stream.
+   Returns c if successful, EOF otherwise.
+   If a write error occurs, the error indicator of the stream is set.
+*/
+int fputc( int c, struct _PDCLIB_file_t * stream )
+{
+       ao_putchar(c);
+       return c;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif
diff --git a/platform/altos/functions/stdio/fputs.c b/platform/altos/functions/stdio/fputs.c
new file mode 100644 (file)
index 0000000..f0f3202
--- /dev/null
@@ -0,0 +1,44 @@
+/* $Id$ */
+
+/* fputs( const char *, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+#include <_PDCLIB_glue.h>
+
+int fputs( const char * _PDCLIB_restrict s, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+    char c;
+
+    while ((c = *s++))
+       if (putc(stream, c) != c)
+           return EOF;
+    return 0;
+}
+
+#endif
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    char const * const message = "SUCCESS testing fputs()";
+    FILE * fh;
+    TESTCASE( ( fh = tmpfile() ) != NULL );
+    TESTCASE( fputs( message, fh ) >= 0 );
+    rewind( fh );
+    for ( size_t i = 0; i < 23; ++i )
+    {
+        TESTCASE( fgetc( fh ) == message[i] );
+    }
+    TESTCASE( fclose( fh ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/fread.c b/platform/altos/functions/stdio/fread.c
new file mode 100644 (file)
index 0000000..621ed0a
--- /dev/null
@@ -0,0 +1,78 @@
+/* $Id$ */
+
+/* fwrite( void *, size_t, size_t, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+#include <_PDCLIB_glue.h>
+
+#include <stdbool.h>
+#include <string.h>
+
+size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+    char * dest = (char *)ptr;
+    size_t nmemb_i;
+    for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
+    {
+        for ( size_t size_i = 0; size_i < size; ++size_i )
+        {
+           int c = getc(stream);
+           if (c == EOF)
+           {
+               /* Could not read requested data */
+               return nmemb_i;
+           }
+           *dest++ = c;
+        }
+    }
+    return nmemb_i;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    FILE * fh;
+    char const * message = "Testing fwrite()...\n";
+    char buffer[21];
+    buffer[20] = 'x';
+    TESTCASE( ( fh = tmpfile() ) != NULL );
+    /* fwrite() / readback */
+    TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
+    TESTCASE( memcmp( buffer, message, 20 ) == 0 );
+    TESTCASE( buffer[20] == 'x' );
+    /* same, different nmemb / size settings */
+    rewind( fh );
+    TESTCASE( memset( buffer, '\0', 20 ) == buffer );
+    TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
+    TESTCASE( memcmp( buffer, message, 20 ) == 0 );
+    TESTCASE( buffer[20] == 'x' );
+    /* same... */
+    rewind( fh );
+    TESTCASE( memset( buffer, '\0', 20 ) == buffer );
+    TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
+    TESTCASE( memcmp( buffer, message, 20 ) == 0 );
+    TESTCASE( buffer[20] == 'x' );
+    /* Done. */
+    TESTCASE( fclose( fh ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/fsetpos.c b/platform/altos/functions/stdio/fsetpos.c
new file mode 100644 (file)
index 0000000..23cd844
--- /dev/null
@@ -0,0 +1,31 @@
+/* $Id$ */
+
+/* fsetpos( FILE *, const fpos_t * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+int fsetpos( struct _PDCLIB_file_t * stream, const struct _PDCLIB_fpos_t * pos )
+{
+    stream->pos.status = 0;
+    /* TODO: Add mbstate. */
+    return 0;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* fsetpos() tested together with fsetpos(). */
+    return TEST_RESULTS;
+}
+
+#endif
diff --git a/platform/altos/functions/stdio/fwrite.c b/platform/altos/functions/stdio/fwrite.c
new file mode 100644 (file)
index 0000000..3c1ac29
--- /dev/null
@@ -0,0 +1,46 @@
+/* $Id$ */
+
+/* fwrite( const void *, size_t, size_t, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+#include <_PDCLIB_glue.h>
+
+#include <stdbool.h>
+#include <string.h>
+
+size_t fwrite( const void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
+{
+    size_t total = size * nmemb;
+    size_t written = 0;
+    char *data = ptr;
+
+    while (total-- > 0)
+    {
+       char c = *data++;
+       if (putc(c, stream) != c)
+           break;
+       written++;
+    }
+    return written / size;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by fread(). */
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/gets.c b/platform/altos/functions/stdio/gets.c
new file mode 100644 (file)
index 0000000..1cb92d5
--- /dev/null
@@ -0,0 +1,62 @@
+/* $Id$ */
+
+/* gets( char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+char * gets( char * s )
+{
+    char *dest = s;
+    int c;
+
+    for (;;) {
+       c = getchar();
+       if (c == '\n' || c == EOF)
+           break;
+       *dest++ = c;
+    }
+    *dest = '\0';
+    return ( dest == s ) ? NULL : s;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+#include <string.h>
+
+int main( void )
+{
+    FILE * fh;
+    char buffer[10];
+    char const * gets_test = "foo\nbar\0baz\nweenie";
+    TESTCASE( ( fh = fopen( testfile, "wb" ) ) != NULL );
+    TESTCASE( fwrite( gets_test, 1, 18, fh ) == 18 );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( ( fh = freopen( testfile, "rb", stdin ) ) != NULL );
+    TESTCASE( gets( buffer ) == buffer );
+    TESTCASE( strcmp( buffer, "foo" ) == 0 );
+    TESTCASE( gets( buffer ) == buffer );
+    TESTCASE( memcmp( buffer, "bar\0baz\0", 8 ) == 0 );
+    TESTCASE( gets( buffer ) == buffer );
+    TESTCASE( strcmp( buffer, "weenie" ) == 0 );
+    TESTCASE( feof( fh ) );
+    TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
+    TESTCASE( gets( buffer ) == buffer );
+    TESTCASE( strcmp( buffer, "e" ) == 0 );
+    TESTCASE( feof( fh ) );
+    TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
+    TESTCASE( gets( buffer ) == NULL );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( remove( testfile ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/puts.c b/platform/altos/functions/stdio/puts.c
new file mode 100644 (file)
index 0000000..fb4bdbe
--- /dev/null
@@ -0,0 +1,48 @@
+/* $Id$ */
+
+/* puts( const char * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+int puts( const char * s )
+{
+    char       c;
+
+    while ((c = *s++))
+       if (putchar (c) != c)
+           return EOF;
+    if (putchar('\n') != '\n')
+       return EOF;
+    return 0;
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    FILE * fh;
+    char const * message = "SUCCESS testing puts()";
+    char buffer[23];
+    buffer[22] = 'x';
+    TESTCASE( ( fh = freopen( testfile, "wb+", stdout ) ) != NULL );
+    TESTCASE( puts( message ) >= 0 );
+    rewind( fh );
+    TESTCASE( fread( buffer, 1, 22, fh ) == 22 );
+    TESTCASE( memcmp( buffer, message, 22 ) == 0 );
+    TESTCASE( buffer[22] == 'x' );
+    TESTCASE( fclose( fh ) == 0 );
+    TESTCASE( remove( testfile ) == 0 );
+    return TEST_RESULTS;
+}
+
+#endif
+
diff --git a/platform/altos/functions/stdio/ungetc.c b/platform/altos/functions/stdio/ungetc.c
new file mode 100644 (file)
index 0000000..e7a4315
--- /dev/null
@@ -0,0 +1,31 @@
+/* $Id$ */
+
+/* ungetc( int, FILE * )
+
+   This file is part of the Public Domain C Library (PDCLib).
+   Permission is granted to use, modify, and / or redistribute at will.
+*/
+
+#include <stdio.h>
+
+#ifndef REGTEST
+
+extern int PDCLIB_NO_ACTUAL_STDIO();
+
+int ungetc( int c, struct _PDCLIB_file_t * stream )
+{
+    return PDCLIB_NO_ACTUAL_STDIO();
+}
+
+#endif
+
+#ifdef TEST
+#include <_PDCLIB_test.h>
+
+int main( void )
+{
+    /* Testing covered by ftell.c */
+    return TEST_RESULTS;
+}
+
+#endif