Sam and ppc-efika build fixes.
[AROS.git] / test / unixio.c
blobd1a9a0625b32aba9c44f6a16dbc80b9532039657
1 #include <hidd/unixio.h>
2 #include <proto/exec.h>
3 #include <proto/dos.h>
4 #include <proto/oop.h>
5 #include <oop/oop.h>
6 #include <stdio.h>
8 #define O_RDONLY 0
9 #define O_WRONLY 1
10 #define O_RDWR 2
12 struct Library *OOPBase = NULL;
14 int main (int argc, char **argv)
16 int failed = 1;
17 struct Library *UnixIOBase = NULL;
18 OOP_Object *unixio = NULL;
19 int fd = -1;
20 int nbytes, ioerr;
21 char buf[1024];
23 if ((OOPBase = OpenLibrary("oop.library", 0)) == NULL) {
24 fprintf(stderr, "can't open oop.library\n");
25 goto exit;
28 UnixIOBase = OpenLibrary("DEVS:Drivers/unixio.hidd", 42);
29 if (!UnixIOBase)
31 fprintf(stderr, "can't open unixio.hidd\n");
32 goto exit;
35 if ((unixio = OOP_NewObject(NULL, CLID_Hidd_UnixIO, NULL)) == NULL) {
36 fprintf(stderr, "can't instantiate unixio hidd\n");
37 goto exit;
41 printf("first, a trivial file read test\n\n");
43 printf("opening /dev/zero for read... ");
44 fd = Hidd_UnixIO_OpenFile(unixio, "/dev/zero", O_RDONLY, 0, &ioerr);
45 if (fd == -1) {
46 printf("failed (ioerr is %d)\n)", ioerr);
47 goto exit;
49 printf("ok (fd is %d)\n", fd);
51 printf("reading... ");
52 nbytes = Hidd_UnixIO_ReadFile(unixio, fd, buf, 1024, &ioerr);
53 if (nbytes == -1) {
54 printf("failed (ioerr is %d\n)", ioerr);
55 goto exit;
57 printf("ok (read %d bytes)\n", nbytes);
59 printf("closing file... ");
60 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
62 printf("failed (ioerr is %d\n)", ioerr);
63 goto exit;
65 printf("ok\n\n\n");
68 printf("next, an equally trivial file write test\n\n");
70 printf("opening /dev/null for write... ");
71 fd = Hidd_UnixIO_OpenFile(unixio, "/dev/null", O_WRONLY, 0, &ioerr);
72 if (fd == -1) {
73 printf("failed (ioerr is %d)\n)", ioerr);
74 goto exit;
76 printf("ok (fd is %d)\n", fd);
78 printf("writing... ");
79 nbytes = Hidd_UnixIO_WriteFile(unixio, fd, buf, 1024, &ioerr);
80 if (nbytes == -1) {
81 printf("failed (ioerr is %d\n)", ioerr);
82 goto exit;
84 printf("ok (wrote %d bytes)\n", nbytes);
86 printf("closing file... ");
87 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
89 printf("failed (ioerr is %d\n)", ioerr);
90 goto exit;
92 printf("ok\n\n\n");
95 printf("just for fun, lets read and print the contents of a file\n\n");
97 printf("opening /etc/hosts for read... ");
98 fd = Hidd_UnixIO_OpenFile(unixio, "/etc/hosts", O_RDONLY, 0, &ioerr);
99 if (fd == -1) {
100 printf("failed (ioerr is %d)\n)", ioerr);
101 goto exit;
103 printf("ok (fd is %d)\n", fd);
105 printf("reading... ");
106 nbytes = Hidd_UnixIO_ReadFile(unixio, fd, buf, 1024, &ioerr);
107 if (nbytes == -1) {
108 printf("failed (ioerr is %d\n)", ioerr);
109 goto exit;
111 printf("ok (read %d bytes)\n", nbytes);
113 printf("system hosts file:\n\n%.*s\n", nbytes, buf);
115 printf("closing file... ");
116 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
118 printf("failed (ioerr is %d\n)", ioerr);
119 goto exit;
121 printf("ok\n\n\n");
123 fd = -1;
126 printf("now type something on the unix console that you\n"
127 "ran aros from, then press enter. I'll wait...\n");
129 Hidd_UnixIO_Wait(unixio, 0, vHidd_UnixIO_Read);
131 printf("reading it... ");
132 nbytes = Hidd_UnixIO_ReadFile(unixio, 0, buf, 1024, &ioerr);
133 if (nbytes == -1) {
134 printf("failed (ioerr is %d\n)", ioerr);
135 goto exit;
137 printf("ok (read %d bytes)\n", nbytes);
139 printf("you typed: %.*s\n\n", nbytes, buf);
142 exit:
143 if (fd >= 0) Hidd_UnixIO_CloseFile(unixio, fd, NULL);
144 if (unixio != NULL) OOP_DisposeObject(unixio);
145 if (UnixIOBase)
146 CloseLibrary(UnixIOBase);
147 if (OOPBase != NULL) CloseLibrary(OOPBase);
149 return failed ? 1 : 0;