]> git.sur5r.net Git - cc65/blob - testcode/lib/posixio-test.c
include unistd.h -- fix by Daniel Serpell
[cc65] / testcode / lib / posixio-test.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5
6
7 int Open (const char* Name, int Flags)
8 {
9     int fd;
10     printf ("Opening %s: ", Name);
11     fd = open (Name, Flags);
12     printf ("%d\n", fd);
13     return fd;
14 }
15
16
17
18 void Write (int fd, const void* Buf, unsigned Size)
19 {
20     int Res;
21     Res = write (fd, Buf, Size);
22     printf ("Writing %u bytes to %d: %d\n", Size, fd, Res);
23 }
24
25
26
27 int Read (int fd, void* Buf, unsigned Size)
28 {
29     int Res;
30     Res = read (fd, Buf, Size);
31     printf ("Reading %u bytes from %d: %d\n", Size, fd, Res);
32     return Res > 0? Res : 0;
33 }
34
35
36
37 void Close (int fd)
38 {
39     printf ("Closing %d: %d\n", fd, close (fd));
40 }
41
42
43
44 int main (void)
45 {
46     int fd1, fd2;
47     int Res;
48     static const char text1[] = "This goes into file #1\n";
49     static const char text2[] = "This goes into file #2\n";
50     static const char text3[] = "This goes into file #3\n";
51     static const char text4[] = "This goes into file #4\n";
52     static char Buf[200];
53
54
55     fd1 = Open ("foobar1", O_WRONLY|O_CREAT|O_TRUNC);
56     fd2 = Open ("foobar2", O_WRONLY|O_CREAT|O_TRUNC);
57
58     Write (fd1, text1, sizeof (text1) - 1);
59     Write (fd2, text2, sizeof (text2) - 1);
60     Write (fd1, text1, sizeof (text1) - 1);
61     Write (fd2, text2, sizeof (text2) - 1);
62
63     Close (fd1);
64     Close (fd2);
65
66     fd1 = Open ("foobar3", O_WRONLY|O_CREAT|O_TRUNC);
67     fd2 = Open ("foobar4", O_WRONLY|O_CREAT|O_TRUNC);
68
69     Write (fd1, text3, sizeof (text3) - 1);
70     Write (fd2, text4, sizeof (text4) - 1);
71     Write (fd1, text3, sizeof (text3) - 1);
72     Write (fd2, text4, sizeof (text4) - 1);
73
74     Close (fd1);
75     Close (fd2);
76
77     fd1 = Open ("foobar1", O_RDONLY);
78     Res = Read (fd1, Buf, sizeof (Buf));
79     printf ("%.*s", Res, Buf);
80     Res = Read (fd1, Buf, sizeof (Buf));
81     Close (fd1);
82
83     return 0;
84 }
85
86