Reworked scanf() testing. General cleanups.
[fw/pdclib] / functions / stdio / gets.c
index ecba23fe51910db0be446597b51265a1ec2525dc..ac93e972585616a0cb79215b14b005ce383494f9 100644 (file)
@@ -1,21 +1,72 @@
-// ----------------------------------------------------------------------------
-// $Id$
-// ----------------------------------------------------------------------------
-// Public Domain C Library - http://pdclib.sourceforge.net
-// This code is Public Domain. Use, modify, and redistribute at will.
-// ----------------------------------------------------------------------------
+/* $Id$ */
 
-char * gets( char * s ) { /* TODO */ };
+/* gets( char * )
 
-/* PDPC code - unreviewed
+   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
+
+#define _PDCLIB_GLUE_H _PDCLIB_GLUE_H
+#include <_PDCLIB_glue.h>
+
+char * gets( char * s )
 {
-    char *ret;
-
-    stdin->quickText = 0;
-    stdin->noNl = 1;
-    ret = fgets(s, INT_MAX, stdin);
-    stdin->noNl = 0;
-    stdin->quickText = 1;
-    return (ret);
+    if ( _PDCLIB_prepread( stdin ) == EOF )
+    {
+        return NULL;
+    }
+    char * dest = s;
+    while ( ( *dest = stdin->buffer[stdin->bufidx++] ) != '\n' )
+    {
+        ++dest;
+        if ( stdin->bufidx == stdin->bufend )
+        {
+            if ( _PDCLIB_fillbuffer( stdin ) == EOF )
+            {
+                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 * 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
+