Hint added.
[AROS.git] / workbench / c / CopyToPAR.c
blob4bdf20c0cfbb6dfba878da37511769d8a57bfe6e
1 /*
2 Copyright © 1995-2011, 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 /****************************************************************************************/
53 #define ARG_TEMPLATE "FILE/A,USB/S,QUIET/S"
54 #define ARG_FILE 0
55 #define ARG_USB 1
56 #define ARG_QUIET 2
57 #define NUM_ARGS 3
59 #define BUFSIZE 4096
61 /****************************************************************************************/
63 struct MsgPort *ParMP;
64 struct IOStdReq *ParIO;
65 BOOL ParOpen;
66 BPTR fh;
67 struct RDArgs *myargs;
68 IPTR args[NUM_ARGS];
69 UBYTE s[256];
70 UBYTE buf[BUFSIZE];
71 STRPTR devicename = "parallel.device";
73 /****************************************************************************************/
75 static void cleanup(char *msg)
77 if (msg && !args[ARG_QUIET])
79 Printf("CopyToPAR: %s\n", msg);
82 if (fh) Close(fh);
83 if (myargs) FreeArgs(myargs);
85 if (ParOpen) CloseDevice((struct IORequest *)ParIO);
86 if (ParIO) DeleteIORequest((struct IORequest *)ParIO);
87 if (ParMP) DeleteMsgPort(ParMP);
90 /****************************************************************************************/
92 static ULONG getarguments(void)
94 if (!(myargs = ReadArgs(ARG_TEMPLATE, args, 0)))
96 Fault(IoErr(), 0, s, 255);
97 cleanup(s);
98 return RETURN_FAIL;
100 return 0;
103 /****************************************************************************************/
105 static ULONG openpar(void)
107 ParMP = CreateMsgPort();
108 if (!ParMP)
110 cleanup("Failed to create msgport");
111 return RETURN_ERROR;
114 ParIO = (struct IOStdReq *)CreateIORequest(ParMP, sizeof(struct IOExtPar));
115 if (!ParIO)
117 cleanup("Failed to create IO request");
118 return RETURN_ERROR;
121 if (args[ARG_USB])
123 devicename = "usbparallel.device";
126 if (OpenDevice(devicename, 0, (struct IORequest *)ParIO, 0))
128 cleanup("Failed to open (usb)parallel.device");
129 return RETURN_ERROR;
132 ParOpen = TRUE;
133 return 0;
136 /****************************************************************************************/
138 static ULONG openfile(void)
140 fh = Open((STRPTR)args[ARG_FILE], MODE_OLDFILE);
141 if (!fh)
143 Fault(IoErr(), 0, s, 255);
144 cleanup(s);
145 return RETURN_FAIL;
147 return 0;
150 /****************************************************************************************/
152 static BOOL WritePAR(APTR buf, ULONG size)
154 ParIO->io_Command = CMD_WRITE;
155 ParIO->io_Data = buf;
156 ParIO->io_Length = size;
158 return (DoIO((struct IORequest *)ParIO) == 0) ? TRUE : FALSE;
161 /****************************************************************************************/
163 static ULONG docopy(void)
165 LONG size;
169 size = Read(fh, buf, BUFSIZE);
170 if (size == -1)
172 Fault(IoErr(), 0, s, 255);
173 cleanup(s);
174 return RETURN_FAIL;
177 if (!WritePAR(buf, size))
179 cleanup("Error writing to (usb)parallel.device");
180 return RETURN_FAIL;
183 } while (size == BUFSIZE);
185 return 0;
188 /****************************************************************************************/
190 int main(void)
192 int rc;
194 rc = getarguments();
195 if (rc)
196 return rc;
198 rc = openpar();
199 if (rc)
200 return rc;
202 rc = openfile();
203 if (rc)
204 return rc;
206 rc = docopy();
207 if (rc)
208 return rc;
210 cleanup(NULL);
211 return 0;