Skip to content

Commit

Permalink
funopen: switch to using a thread behind a pipe() call so the FILE * …
Browse files Browse the repository at this point in the history
…is a real stream ptr
  • Loading branch information
danielhams committed Aug 30, 2020
1 parent 28e6255 commit d03d884
Show file tree
Hide file tree
Showing 13 changed files with 331 additions and 327 deletions.
4 changes: 4 additions & 0 deletions dicltester/src/dicltester/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ dicltester_SOURCES= \
dicl_strtod.c \
dicl_strtof.c \
dicl_strtoll.c \
dicl_funopencommon.c \
dicl_funopenread.c \
dicl_funopenwrite.c \
$(NULL)

AM_CFLAGS= \
Expand All @@ -23,6 +26,7 @@ AM_CFLAGS= \
dicltester_LDADD= \
$(DICLTESTER_DEPS_LIBS) \
-lpthread \
-ldiclfunopen-0.1 \
$(NULL)

CLEANFILES= \
Expand Down
38 changes: 38 additions & 0 deletions dicltester/src/dicltester/dicl_funopencommon.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "config.h"

#include <stdlib.h>

#include <stdio.h>
#include <string.h>

#include <errno.h>

int myreadfn( void * cookie, char * buf, int nmem ) {
static int num_read_total=0;
if( num_read_total > 4096 ) {
printf("myreadfn hit limit\n");
return 0;
}
int cookieasint = (int)cookie;
printf("myreadfn with cookie(%d) asked for %d\n", cookieasint, nmem);
for( int i = 0; i < nmem ; ++i ) {
buf[i] = 'c';
}
num_read_total += nmem;
return nmem;
}

int mywritefn( void * cookie, char * buf, int nmem ) {
int cookieasint = (int)cookie;
printf("mywritefn with cookie(%d) asked for %d\n", cookieasint, nmem);
for( int i = 0; i < nmem ; ++i ) {
printf("%c\n", buf[i]);
}
return nmem;
}

int myclosefn( void * cookie ) {
int cookieasint = (int)cookie;
printf("myclosefn with cookie(%d)\n", cookieasint);
return 0;
}
3 changes: 3 additions & 0 deletions dicltester/src/dicltester/dicl_funopencommon.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
extern int myreadfn( void * cookie, char * buf, int nmem );
extern int mywritefn( void * cookie, char * buf, int nmem );
extern int myclosefn( void * cookie );
63 changes: 63 additions & 0 deletions dicltester/src/dicltester/dicl_funopenread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "config.h"

#include <stdlib.h>

#include <stdio.h>
#include <string.h>

#include <errno.h>

#include <dicl_funopencommon.h>

void dicl_funopenreadtest( char **argv )
{
printf("Beginning funopen read test\n");

void * thecookie = (void*)26;

FILE * the_stream_ptr = funopen( thecookie,
myreadfn,
NULL,
NULL,
myclosefn );

if( errno ) {
printf("Oops\n");
exit(2);
}

char result[2048];
int offset=0;
int num_needed = 20;

while( offset < num_needed ) {
if( errno ) {
printf("FORT (loopstart) errno is set %d\n", errno);
}
int num_this_round = 16;
int num_actually_read = fread( result+offset, 1, num_this_round, the_stream_ptr);
if( num_actually_read == 0 ) {
printf("Nothing returned errno=%d\n", errno);
exit (4);
}
printf("Read OK - have:\n");
for( int i = 0 ; i < num_this_round; ++i ) {
printf("%c\n", *((char*)result+offset+i));
}
offset+=num_this_round;
if( errno ) {
printf("FORT (loopend) errno is set %d\n", errno);
}
}
if( errno ) {
printf("FORT (end-1) errno is set %d\n", errno);
}
//printf("Calling fclose\n");
// fclose(the_stream_ptr);
//printf("Called fclose\n");
if( errno ) {
printf("FORT (end) errno is set %d\n", errno);
}

return;
}
1 change: 1 addition & 0 deletions dicltester/src/dicltester/dicl_funopenread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern void dicl_funopenreadtest();
56 changes: 56 additions & 0 deletions dicltester/src/dicltester/dicl_funopenwrite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "config.h"

#include <stdlib.h>

#include <stdio.h>
#include <string.h>

#include <errno.h>

#include <dicl_funopencommon.h>

void dicl_funopenwritetest( char **argv )
{
void * thecookie = (void*)26;

if( errno ) {
printf("Oops - errno set before call - it is %d\n", errno);
exit(2);
}

FILE * the_stream_ptr = funopen( thecookie,
NULL,
mywritefn,
NULL,
myclosefn );

if( errno ) {
printf("Oops - errno is %d\n", errno);
exit(2);
}

char result[2048];
int offset=0;
int num_needed = 32;
for( int i = 0 ; i < num_needed ; ++i ) {
result[i] = 'b';
}

while( offset < num_needed ) {
int num_this_round = 16;
int num_actually_written = fwrite( result+offset, 1, num_this_round, the_stream_ptr);
if( num_actually_written == 0 ) {
printf("Nothing returned errno=%d\n", errno);
exit (4);
}
printf("Write OK\n");
offset+=num_this_round;
}
fflush(the_stream_ptr);
//fclose(the_stream_ptr);
if( errno ) {
printf("FOWT (end) errno is set %d\n", errno);
}

return;
}
1 change: 1 addition & 0 deletions dicltester/src/dicltester/dicl_funopenwrite.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
extern void dicl_funopenwritetest();
8 changes: 7 additions & 1 deletion dicltester/src/dicltester/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "dicl_strtod.h"
#include "dicl_strtof.h"
#include "dicl_strtoll.h"
#include "dicl_funopenread.h"
#include "dicl_funopenwrite.h"

int main(int argc, char**argv)
{
Expand All @@ -25,7 +27,7 @@ int main(int argc, char**argv)
#else
printf("compiled _without_ libdicl replacements!\n");
#endif

/*
dicl_printftest();
dicl_fprintftest();
Expand All @@ -41,6 +43,10 @@ int main(int argc, char**argv)
dicl_strtoftest();
dicl_strtolltest();
*/
dicl_funopenreadtest();

dicl_funopenwritetest();

return 0;
}
2 changes: 1 addition & 1 deletion libdicl/configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AC_PREREQ(2.65)
AC_INIT([libdicl],
[0.1.31],
[0.1.32],
[[email protected]])

AC_SUBST(ACLOCAL_AMFLAGS, "-I macros")
Expand Down
1 change: 1 addition & 0 deletions libdicl/src/libdicl/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ libdiclfunopen@PACKAGE_SUFFIX@_la_SOURCES= \

libdiclfunopen@PACKAGE_SUFFIX@_la_LIBADD= \
libdicl@[email protected] \
-lnsl \
$(NULL)

if LIBDICL_USING_GNULD
Expand Down
Loading

0 comments on commit d03d884

Please sign in to comment.