aboutsummaryrefslogtreecommitdiff
path: root/tests/unistd/io.c
diff options
context:
space:
mode:
authormax99x <max99x@gmail.com>2011-07-23 05:49:48 +0300
committermax99x <max99x@gmail.com>2011-07-23 05:49:48 +0300
commit136756f734ecf14a28736900075d561e981e973e (patch)
treecf33472ea6550c6486ec7722facf34d98a16a51d /tests/unistd/io.c
parent586d229ec311daa5e89781eb6da822989e677789 (diff)
Added unistd tests; fixed a lot of unistd bugs and deficiencies.
Diffstat (limited to 'tests/unistd/io.c')
-rw-r--r--tests/unistd/io.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/tests/unistd/io.c b/tests/unistd/io.c
new file mode 100644
index 00000000..eeb80373
--- /dev/null
+++ b/tests/unistd/io.c
@@ -0,0 +1,101 @@
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <string.h>
+
+int main() {
+ char readBuffer[256] = {0};
+ char writeBuffer[] = "writeme";
+
+ int fl = open("/folder", O_RDWR);
+ printf("read from folder: %d\n", read(fl, readBuffer, sizeof readBuffer));
+ printf("errno: %d\n", errno);
+ errno = 0;
+ printf("write to folder: %d\n", write(fl, writeBuffer, sizeof writeBuffer));
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ int bd = open("/broken-device", O_RDWR);
+ printf("read from broken device: %d\n", read(bd, readBuffer, sizeof readBuffer));
+ printf("errno: %d\n", errno);
+ errno = 0;
+ printf("write to broken device: %d\n", write(bd, writeBuffer, sizeof writeBuffer));
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ int d = open("/device", O_RDWR);
+ printf("read from device: %d\n", read(d, readBuffer, sizeof readBuffer));
+ printf("data: %s\n", readBuffer);
+ memset(readBuffer, 0, sizeof readBuffer);
+ printf("errno: %d\n", errno);
+ errno = 0;
+ printf("write to device: %d\n", write(d, writeBuffer, sizeof writeBuffer));
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ int f = open("/file", O_RDWR);
+ printf("read from file: %d\n", read(f, readBuffer, sizeof readBuffer));
+ printf("data: %s\n", readBuffer);
+ memset(readBuffer, 0, sizeof readBuffer);
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ printf("seek: %d\n", lseek(f, 3, SEEK_SET));
+ printf("errno: %d\n\n", errno);
+ printf("partial read from file: %d\n", read(f, readBuffer, 3));
+ printf("data: %s\n", readBuffer);
+ memset(readBuffer, 0, sizeof readBuffer);
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ printf("seek: %d\n", lseek(f, -2, SEEK_END));
+ printf("errno: %d\n", errno);
+ errno = 0;
+ printf("partial read from end of file: %d\n", read(f, readBuffer, 3));
+ printf("data: %s\n", readBuffer);
+ memset(readBuffer, 0, sizeof readBuffer);
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ printf("seek: %d\n", lseek(f, -15, SEEK_CUR));
+ printf("errno: %d\n", errno);
+ errno = 0;
+ printf("partial read from before start of file: %d\n", read(f, readBuffer, 3));
+ printf("data: %s\n", readBuffer);
+ memset(readBuffer, 0, sizeof readBuffer);
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ printf("seek: %d\n", lseek(f, 0, SEEK_SET));
+ printf("write to start of file: %d\n", write(f, writeBuffer, 3));
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ printf("seek: %d\n", lseek(f, 0, SEEK_END));
+ printf("write to end of file: %d\n", write(f, writeBuffer, 3));
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ printf("seek: %d\n", lseek(f, 10, SEEK_END));
+ printf("write after end of file: %d\n", write(f, writeBuffer, sizeof writeBuffer));
+ printf("errno: %d\n\n", errno);
+ errno = 0;
+
+ int bytesRead;
+ printf("seek: %d\n", lseek(f, 0, SEEK_SET));
+ printf("read after write: %d\n", bytesRead = read(f, readBuffer, sizeof readBuffer));
+ printf("errno: %d\n", errno);
+ errno = 0;
+ printf("final: ");
+ for (int i = 0; i < bytesRead; i++) {
+ if (readBuffer[i] == 0) {
+ printf("\\0");
+ } else {
+ printf("%c", readBuffer[i]);
+ }
+ }
+ printf("\n");
+
+ return 0;
+}