Fix IO memory access .. SB128 driver makes noises in VMWare - CMI is untested (Curren...
[AROS.git] / workbench / c / CopyToPAR.c
blobbd11db0f96859bd9062b808f95fcd936e089f4c2
1 /*
2 Copyright © 1995-2010, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Copy file to parallel.device
6 Lang: English
7 */
8 /*****************************************************************************
10 NAME
12 CopyToPAR
14 SYNOPSIS
16 FILE/A,USB/S,QUIET/S
18 LOCATION
22 FUNCTION
24 Copies (or sends) a file to parallel.device or usbparallel.device.
26 INPUTS
28 FILE -- Either a file, a directory or a pattern to match.
30 USB -- Use usbparallel.device.
32 QUIET -- Suppresses any output to the shell.
34 RESULT
36 Standard DOS return codes.
38 NOTES
40 BUGS
42 INTERNALS
44 ******************************************************************************/
45 #include <exec/io.h>
46 #include <dos/dos.h>
47 #include <devices/parallel.h>
48 #include <proto/exec.h>
49 #include <proto/dos.h>
51 #include <setjmp.h>
53 /****************************************************************************************/
55 #define ARG_TEMPLATE "FILE/A,USB/S,QUIET/S"
56 #define ARG_FILE 0
57 #define ARG_USB 1
58 #define ARG_QUIET 2
59 #define NUM_ARGS 3
61 #define BUFSIZE 4096
63 /****************************************************************************************/
65 struct MsgPort *ParMP;
66 struct IOStdReq *ParIO;
67 BOOL ParOpen;
68 BPTR fh;
69 struct RDArgs *myargs;
70 IPTR args[NUM_ARGS];
71 UBYTE s[256];
72 UBYTE buf[BUFSIZE];
73 STRPTR devicename = "parallel.device";
74 jmp_buf exit_buf;
76 /****************************************************************************************/
78 static void cleanup(char *msg, ULONG retcode)
80 if (msg && !args[ARG_QUIET])
82 Printf("CopyToPAR: %s\n", msg);
85 if (fh) Close(fh);
86 if (myargs) FreeArgs(myargs);
88 if (ParOpen) CloseDevice((struct IORequest *)ParIO);
89 if (ParIO) DeleteIORequest((struct IORequest *)ParIO);
90 if (ParMP) DeleteMsgPort(ParMP);
92 longjmp(exit_buf, retcode | (1 << 31));
95 /****************************************************************************************/
97 static void getarguments(void)
99 if (!(myargs = ReadArgs(ARG_TEMPLATE, args, 0)))
101 Fault(IoErr(), 0, s, 255);
102 cleanup(s, RETURN_FAIL);
106 /****************************************************************************************/
108 static void openpar(void)
110 ParMP = CreateMsgPort();
111 if (!ParMP) cleanup("Failed to create msgport", RETURN_ERROR);
113 ParIO = (struct IOStdReq *)CreateIORequest(ParMP, sizeof(struct IOExtPar));
114 if (!ParIO) cleanup("Failed to create IO request", RETURN_ERROR);
116 if (args[ARG_USB])
118 devicename = "usbparallel.device";
121 if (OpenDevice(devicename, 0, (struct IORequest *)ParIO, 0))
123 cleanup("Failed to open (usb)parallel.device", RETURN_ERROR);
126 ParOpen = TRUE;
129 /****************************************************************************************/
131 static void openfile(void)
133 fh = Open((STRPTR)args[ARG_FILE], MODE_OLDFILE);
134 if (!fh)
136 Fault(IoErr(), 0, s, 255);
137 cleanup(s, RETURN_FAIL);
141 /****************************************************************************************/
143 static BOOL WritePAR(APTR buf, ULONG size)
145 ParIO->io_Command = CMD_WRITE;
146 ParIO->io_Data = buf;
147 ParIO->io_Length = size;
149 return (DoIO((struct IORequest *)ParIO) == 0) ? TRUE : FALSE;
152 /****************************************************************************************/
154 static void docopy(void)
156 LONG size;
160 size = Read(fh, buf, BUFSIZE);
161 if (size == -1)
163 Fault(IoErr(), 0, s, 255);
164 cleanup(s, RETURN_FAIL);
167 if (!WritePAR(buf, size))
169 cleanup("Error writing to (usb)parallel.device", RETURN_FAIL);
172 } while (size == BUFSIZE);
176 /****************************************************************************************/
178 int main(void)
180 int rc;
182 if ((rc = setjmp(exit_buf)) != 0) {
183 return rc & ~(1 << 31);
186 getarguments();
187 openpar();
188 openfile();
189 docopy();
190 cleanup(NULL, 0);
192 return 0;