1 /* Copyright 1999-2003,2007,2009 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
18 * Same thing as cat /dev/fd0 or cat file >/dev/fd0
19 * Something, that isn't possible with floppyd anymore.
22 #include "sysincludes.h"
28 #include "floppyd_io.h"
31 static void usage(void)
33 fprintf(stderr, "Mtools version %s, dated %s\n",
35 fprintf(stderr, "Usage: mcat [-V] [-w] device\n");
36 fprintf(stderr, " -w write on device else read\n");
43 #define BUF_SIZE 16000
46 static size_t bufLen(size_t blocksize, mt_size_t totalSize, mt_off_t address)
50 if(address + blocksize > totalSize)
51 return totalSize - address;
55 void mcat(int argc, char **argv, int type)
58 struct device out_dev;
59 char drive, name[EXPAND_BUF];
76 if (argv[1][0] == '-') {
77 if (argv[1][1] != 'w') {
84 if (argc - optindex < 1)
88 if (!argv[optindex][0] || argv[optindex][1] != ':'
89 || argv[optindex][2]) {
93 drive = toupper(argv[optindex][0]);
95 /* check out a drive whose letter and parameters match */
96 sprintf(errmsg, "Drive '%c:' not supported", drive);
98 for (dev=devices; dev->name; dev++) {
100 if (dev->drive != drive)
103 expand(dev->name,name);
104 #ifdef USING_NEW_VOLD
105 strcpy(name, getVoldName(dev, name));
110 Stream = XdfOpen(&out_dev, name, mode, errmsg, 0);
112 out_dev.use_2m = 0x7f;
118 Stream = FloppydOpen(&out_dev, dev, name,
119 mode, errmsg, 0, 1, NULL);
124 Stream = SimpleFileOpen(&out_dev, dev, name, mode,
132 /* print error msg if needed */
133 if ( dev->drive == 0 ){
135 fprintf(stderr,"%s\n",errmsg);
140 if (mode == O_WRONLY) {
142 size = out_dev.sectors * out_dev.heads * out_dev.tracks;
144 while ((len = fread(buf, 1,
145 bufLen(BUF_SIZE, size, address),
147 int r = WRITES(Stream, buf, address, len);
148 fprintf(stderr, "Wrote to %d\n", (int) address);
154 while ((len = READS(Stream, buf, address, BUF_SIZE)) > 0) {
155 fwrite(buf, 1, len, stdout);