Merging NList MCC 0.119 into the main branch.
[AROS.git] / test / unixio.c
blob75a6ef15bc7d3ae0969d7a4f505665484aeed1c0
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>
7 #include <stdio.h>
9 #define O_RDONLY 0
10 #define O_WRONLY 1
11 #define O_RDWR 2
13 struct Library *OOPBase = NULL;
15 int main (int argc, char **argv)
17 int failed = 1;
18 struct Library *UnixIOBase = NULL;
19 OOP_Object *unixio = NULL;
20 int fd = -1;
21 int nbytes, ioerr;
22 char buf[1024];
24 if ((OOPBase = OpenLibrary("oop.library", 0)) == NULL) {
25 fprintf(stderr, "can't open oop.library\n");
26 goto exit;
29 UnixIOBase = OpenLibrary("DEVS:Drivers/unixio.hidd", 42);
30 if (!UnixIOBase)
32 fprintf(stderr, "can't open unixio.hidd\n");
33 goto exit;
36 if ((unixio = OOP_NewObject(NULL, CLID_Hidd_UnixIO, NULL)) == NULL) {
37 fprintf(stderr, "can't instantiate unixio hidd\n");
38 goto exit;
42 printf("first, a trivial file read test\n\n");
44 printf("opening /dev/zero for read... ");
45 fd = Hidd_UnixIO_OpenFile(unixio, "/dev/zero", O_RDONLY, 0, &ioerr);
46 if (fd == -1) {
47 printf("failed (ioerr is %d)\n)", ioerr);
48 goto exit;
50 printf("ok (fd is %d)\n", fd);
52 printf("reading... ");
53 nbytes = Hidd_UnixIO_ReadFile(unixio, fd, buf, 1024, &ioerr);
54 if (nbytes == -1) {
55 printf("failed (ioerr is %d\n)", ioerr);
56 goto exit;
58 printf("ok (read %d bytes)\n", nbytes);
60 printf("closing file... ");
61 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
63 printf("failed (ioerr is %d\n)", ioerr);
64 goto exit;
66 printf("ok\n\n\n");
69 printf("next, an equally trivial file write test\n\n");
71 printf("opening /dev/null for write... ");
72 fd = Hidd_UnixIO_OpenFile(unixio, "/dev/null", O_WRONLY, 0, &ioerr);
73 if (fd == -1) {
74 printf("failed (ioerr is %d)\n)", ioerr);
75 goto exit;
77 printf("ok (fd is %d)\n", fd);
79 printf("writing... ");
80 nbytes = Hidd_UnixIO_WriteFile(unixio, fd, buf, 1024, &ioerr);
81 if (nbytes == -1) {
82 printf("failed (ioerr is %d\n)", ioerr);
83 goto exit;
85 printf("ok (wrote %d bytes)\n", nbytes);
87 printf("closing file... ");
88 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
90 printf("failed (ioerr is %d\n)", ioerr);
91 goto exit;
93 printf("ok\n\n\n");
96 printf("just for fun, lets read and print the contents of a file\n\n");
98 printf("opening /etc/hosts for read... ");
99 fd = Hidd_UnixIO_OpenFile(unixio, "/etc/hosts", O_RDONLY, 0, &ioerr);
100 if (fd == -1) {
101 printf("failed (ioerr is %d)\n)", ioerr);
102 goto exit;
104 printf("ok (fd is %d)\n", fd);
106 printf("reading... ");
107 nbytes = Hidd_UnixIO_ReadFile(unixio, fd, buf, 1024, &ioerr);
108 if (nbytes == -1) {
109 printf("failed (ioerr is %d\n)", ioerr);
110 goto exit;
112 printf("ok (read %d bytes)\n", nbytes);
114 printf("system hosts file:\n\n%.*s\n", nbytes, buf);
116 printf("closing file... ");
117 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
119 printf("failed (ioerr is %d\n)", ioerr);
120 goto exit;
122 printf("ok\n\n\n");
124 fd = -1;
127 printf("now type something on the unix console that you\n"
128 "ran aros from, then press enter. I'll wait...\n");
130 Hidd_UnixIO_Wait(unixio, 0, vHidd_UnixIO_Read);
132 printf("reading it... ");
133 nbytes = Hidd_UnixIO_ReadFile(unixio, 0, buf, 1024, &ioerr);
134 if (nbytes == -1) {
135 printf("failed (ioerr is %d\n)", ioerr);
136 goto exit;
138 printf("ok (read %d bytes)\n", nbytes);
140 printf("you typed: %.*s\n\n", nbytes, buf);
143 exit:
144 if (fd >= 0) Hidd_UnixIO_CloseFile(unixio, fd, NULL);
145 if (unixio != NULL) OOP_DisposeObject(unixio);
146 if (UnixIOBase)
147 CloseLibrary(UnixIOBase);
148 if (OOPBase != NULL) CloseLibrary(OOPBase);
150 return failed ? 1 : 0;