codeset
[AROS.git] / workbench / c / CopyToPAR.c
blobb9f79fa10b6b164ac3256115679b793d602deec1
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 <stdlib.h>
52 #include <stdio.h>
54 /****************************************************************************************/
56 #define ARG_TEMPLATE "FILE/A,USB/S,QUIET/S"
57 #define ARG_FILE 0
58 #define ARG_USB 1
59 #define ARG_QUIET 2
60 #define NUM_ARGS 3
62 #define BUFSIZE 4096
64 /****************************************************************************************/
66 struct MsgPort *ParMP;
67 struct IOStdReq *ParIO;
68 BOOL ParOpen;
69 BPTR fh;
70 struct RDArgs *myargs;
71 IPTR args[NUM_ARGS];
72 UBYTE s[256];
73 UBYTE buf[BUFSIZE];
74 STRPTR devicename = "parallel.device";
76 /****************************************************************************************/
78 static void cleanup(char *msg, ULONG retcode)
80 if (msg && !args[ARG_QUIET])
82 fprintf(stderr, "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 exit(retcode);
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 getarguments();
181 openpar();
182 openfile();
183 docopy();
184 cleanup(NULL, 0);
186 return 0;