librom.a - Add some more 'clean' string functions
[AROS.git] / test / unixio.c
blobd10f728fc6f5cf02b9072ef4b45ab01e88c92770
1 #include <hidd/unixio.h>
2 #include <hidd/unixio_inline.h>
3 #include <proto/exec.h>
4 #include <proto/dos.h>
5 #include <proto/oop.h>
6 #include <oop/oop.h>
8 #include <stdio.h>
10 #define O_RDONLY 0
11 #define O_WRONLY 1
12 #define O_RDWR 2
14 struct Library *OOPBase = NULL;
16 int main (int argc, char **argv)
18 int failed = 1;
19 struct Library *UnixIOBase = NULL;
20 OOP_Object *unixio = NULL;
21 int fd = -1;
22 int nbytes, ioerr;
23 char buf[1024];
25 if ((OOPBase = OpenLibrary("oop.library", 0)) == NULL) {
26 fprintf(stderr, "can't open oop.library\n");
27 goto exit;
30 UnixIOBase = OpenLibrary("DEVS:Drivers/unixio.hidd", 42);
31 if (!UnixIOBase)
33 fprintf(stderr, "can't open unixio.hidd\n");
34 goto exit;
37 if ((unixio = OOP_NewObject(NULL, CLID_Hidd_UnixIO, NULL)) == NULL) {
38 fprintf(stderr, "can't instantiate unixio hidd\n");
39 goto exit;
43 printf("first, a trivial file read test\n\n");
45 printf("opening /dev/zero for read... ");
46 fd = Hidd_UnixIO_OpenFile(unixio, "/dev/zero", O_RDONLY, 0, &ioerr);
47 if (fd == -1) {
48 printf("failed (ioerr is %d)\n)", ioerr);
49 goto exit;
51 printf("ok (fd is %d)\n", fd);
53 printf("reading... ");
54 nbytes = Hidd_UnixIO_ReadFile(unixio, fd, buf, 1024, &ioerr);
55 if (nbytes == -1) {
56 printf("failed (ioerr is %d\n)", ioerr);
57 goto exit;
59 printf("ok (read %d bytes)\n", nbytes);
61 printf("closing file... ");
62 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
64 printf("failed (ioerr is %d\n)", ioerr);
65 goto exit;
67 printf("ok\n\n\n");
70 printf("next, an equally trivial file write test\n\n");
72 printf("opening /dev/null for write... ");
73 fd = Hidd_UnixIO_OpenFile(unixio, "/dev/null", O_WRONLY, 0, &ioerr);
74 if (fd == -1) {
75 printf("failed (ioerr is %d)\n)", ioerr);
76 goto exit;
78 printf("ok (fd is %d)\n", fd);
80 printf("writing... ");
81 nbytes = Hidd_UnixIO_WriteFile(unixio, fd, buf, 1024, &ioerr);
82 if (nbytes == -1) {
83 printf("failed (ioerr is %d\n)", ioerr);
84 goto exit;
86 printf("ok (wrote %d bytes)\n", nbytes);
88 printf("closing file... ");
89 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
91 printf("failed (ioerr is %d\n)", ioerr);
92 goto exit;
94 printf("ok\n\n\n");
97 printf("just for fun, lets read and print the contents of a file\n\n");
99 printf("opening /etc/hosts for read... ");
100 fd = Hidd_UnixIO_OpenFile(unixio, "/etc/hosts", O_RDONLY, 0, &ioerr);
101 if (fd == -1) {
102 printf("failed (ioerr is %d)\n)", ioerr);
103 goto exit;
105 printf("ok (fd is %d)\n", fd);
107 printf("reading... ");
108 nbytes = Hidd_UnixIO_ReadFile(unixio, fd, buf, 1024, &ioerr);
109 if (nbytes == -1) {
110 printf("failed (ioerr is %d\n)", ioerr);
111 goto exit;
113 printf("ok (read %d bytes)\n", nbytes);
115 printf("system hosts file:\n\n%.*s\n", nbytes, buf);
117 printf("closing file... ");
118 if (Hidd_UnixIO_CloseFile(unixio, fd, &ioerr) == -1)
120 printf("failed (ioerr is %d\n)", ioerr);
121 goto exit;
123 printf("ok\n\n\n");
125 fd = -1;
128 printf("now type something on the unix console that you\n"
129 "ran aros from, then press enter. I'll wait...\n");
131 Hidd_UnixIO_Wait(unixio, 0, vHidd_UnixIO_Read);
133 printf("reading it... ");
134 nbytes = Hidd_UnixIO_ReadFile(unixio, 0, buf, 1024, &ioerr);
135 if (nbytes == -1) {
136 printf("failed (ioerr is %d\n)", ioerr);
137 goto exit;
139 printf("ok (read %d bytes)\n", nbytes);
141 printf("you typed: %.*s\n\n", nbytes, buf);
144 exit:
145 if (fd >= 0) Hidd_UnixIO_CloseFile(unixio, fd, NULL);
146 if (unixio != NULL) OOP_DisposeObject(unixio);
147 if (UnixIOBase)
148 CloseLibrary(UnixIOBase);
149 if (OOPBase != NULL) CloseLibrary(OOPBase);
151 return failed ? 1 : 0;