From: cuz Date: Sun, 17 Nov 2002 22:44:55 +0000 (+0000) Subject: Added posixio-test.c X-Git-Tag: V2.12.0~2100 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=43d5800702086d92fb5d150238928e78d8178280;p=cc65 Added posixio-test.c git-svn-id: svn://svn.cc65.org/cc65/trunk@1532 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- diff --git a/testcode/lib/files.txt b/testcode/lib/files.txt index ef60b3502..5ed341334 100644 --- a/testcode/lib/files.txt +++ b/testcode/lib/files.txt @@ -4,12 +4,12 @@ Test programs for the runtime lib: clock.c - test program for clock() and CLOCKS_PER_SEC cprintf.c - test program for cprintf \n and \r operators +cursor.c - test the cursor() function deb.c - test program for the library debugger div-test.c - test division/modulus and the div() routine ft.c - file I/O test program (open + read functions) getsp.s - helper routine for ft.c joytest.c - readjoy function test program -time-test.c - test the time/mktime/gmtime/asctime functions -cursor.c - test the cursor() function +posixio-test.c - test POSIX file i/o routines (open/read/write/close) seek.c - test lseek()/fseek()/fell() - +time-test.c - test the time/mktime/gmtime/asctime functions diff --git a/testcode/lib/posixio-test.c b/testcode/lib/posixio-test.c new file mode 100644 index 000000000..eaa60d357 --- /dev/null +++ b/testcode/lib/posixio-test.c @@ -0,0 +1,86 @@ +#include +#include +#include + + + +int Open (const char* Name, int Flags) +{ + int fd; + printf ("Opening %s: ", Name); + fd = open (Name, Flags); + printf ("%d\n", fd); + return fd; +} + + + +void Write (int fd, const void* Buf, unsigned Size) +{ + int Res; + Res = write (fd, Buf, Size); + printf ("Writing %u bytes to %d: %d\n", Size, fd, Res); +} + + + +int Read (int fd, void* Buf, unsigned Size) +{ + int Res; + Res = read (fd, Buf, Size); + printf ("Reading %u bytes from %d: %d\n", Size, fd, Res); + return Res > 0? Res : 0; +} + + + +void Close (int fd) +{ + printf ("Closing %d: %d\n", fd, close (fd)); +} + + + +int main (void) +{ + int fd1, fd2; + int Res; + static const char text1[] = "This goes into file #1\n"; + static const char text2[] = "This goes into file #2\n"; + static const char text3[] = "This goes into file #3\n"; + static const char text4[] = "This goes into file #4\n"; + static char Buf[200]; + + + fd1 = Open ("foobar1", O_WRONLY|O_CREAT|O_TRUNC); + fd2 = Open ("foobar2", O_WRONLY|O_CREAT|O_TRUNC); + + Write (fd1, text1, sizeof (text1) - 1); + Write (fd2, text2, sizeof (text2) - 1); + Write (fd1, text1, sizeof (text1) - 1); + Write (fd2, text2, sizeof (text2) - 1); + + Close (fd1); + Close (fd2); + + fd1 = Open ("foobar3", O_WRONLY|O_CREAT|O_TRUNC); + fd2 = Open ("foobar4", O_WRONLY|O_CREAT|O_TRUNC); + + Write (fd1, text3, sizeof (text3) - 1); + Write (fd2, text4, sizeof (text4) - 1); + Write (fd1, text3, sizeof (text3) - 1); + Write (fd2, text4, sizeof (text4) - 1); + + Close (fd1); + Close (fd2); + + fd1 = Open ("foobar1", O_RDONLY); + Res = Read (fd1, Buf, sizeof (Buf)); + printf ("%.*s", Res, Buf); + Res = Read (fd1, Buf, sizeof (Buf)); + Close (fd1); + + return 0; +} + +