rawwrite.c revision 1.2 1 1.2 leo /* $NetBSD: rawwrite.c,v 1.2 1996/01/07 22:06:24 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Leo Weppelman.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo #include <osbind.h>
34 1.2 leo #include <stdlib.h>
35 1.2 leo #include <unistd.h>
36 1.1 leo #include <stdio.h>
37 1.1 leo #include <fcntl.h>
38 1.1 leo #include <string.h>
39 1.2 leo #include "libtos.h"
40 1.1 leo
41 1.1 leo #define SECT_SIZE 512 /* Sector size */
42 1.1 leo #define NSECT_DD 18 /* Sectors per track 720Kb */
43 1.1 leo #define NSECT_HD 36 /* Sectors per track 1.44Mb */
44 1.1 leo #define NTRK 80 /* Number of tracks */
45 1.1 leo
46 1.2 leo static void help PROTO((void));
47 1.2 leo static void usage PROTO((void));
48 1.2 leo static void brwrite PROTO((char *, int));
49 1.1 leo
50 1.1 leo char buf[NSECT_HD * SECT_SIZE];
51 1.2 leo int h_flag = 0; /* Show help */
52 1.2 leo int v_flag = 0; /* Verbose (a dot for each track copied) */
53 1.2 leo int V_flag = 0; /* Show version */
54 1.1 leo char *progname;
55 1.1 leo
56 1.2 leo const char version[] = "$Revision: 1.2 $";
57 1.2 leo
58 1.2 leo int
59 1.1 leo main(argc, argv)
60 1.1 leo int argc;
61 1.1 leo char *argv[];
62 1.1 leo {
63 1.1 leo extern int optind;
64 1.1 leo extern char *optarg;
65 1.1 leo int ch;
66 1.1 leo char *infile;
67 1.1 leo int fd;
68 1.1 leo int i;
69 1.1 leo int nsect;
70 1.1 leo
71 1.1 leo progname = argv[0];
72 1.2 leo init_toslib(argv[0]);
73 1.2 leo
74 1.2 leo while ((ch = getopt(argc, argv, "hvVwo:")) != EOF) {
75 1.2 leo switch (ch) {
76 1.2 leo case 'h':
77 1.2 leo h_flag = 1;
78 1.2 leo break;
79 1.2 leo case 'o':
80 1.2 leo redirect_output(optarg);
81 1.2 leo break;
82 1.1 leo case 'v':
83 1.2 leo v_flag = 1;
84 1.2 leo break;
85 1.2 leo case 'V':
86 1.2 leo V_flag = 1;
87 1.2 leo break;
88 1.2 leo case 'w':
89 1.2 leo set_wait_for_key();
90 1.1 leo break;
91 1.1 leo default :
92 1.1 leo usage();
93 1.1 leo break;
94 1.1 leo }
95 1.1 leo }
96 1.2 leo if (h_flag)
97 1.2 leo help();
98 1.2 leo if (V_flag)
99 1.2 leo eprintf("%s\r\n", version);
100 1.2 leo
101 1.2 leo if (optind >= argc)
102 1.1 leo usage();
103 1.1 leo
104 1.1 leo infile = argv[optind];
105 1.1 leo nsect = NSECT_DD;
106 1.1 leo
107 1.2 leo if ((fd = open(infile, O_RDONLY)) < 0)
108 1.2 leo fatal(-1, "Cannot open '%s'\n", infile);
109 1.1 leo
110 1.2 leo for (i = 0; i < NTRK; i++) {
111 1.2 leo if (read(fd, buf, nsect * SECT_SIZE) != (nsect * SECT_SIZE))
112 1.2 leo fatal(-1, "\n\rRead error on '%s'\n", infile);
113 1.2 leo if (v_flag) {
114 1.2 leo if (i && !(i % 40))
115 1.2 leo eprintf("\r\n");
116 1.2 leo eprintf(".");
117 1.1 leo }
118 1.2 leo brwrite(buf, i);
119 1.1 leo }
120 1.1 leo close(fd);
121 1.2 leo if (v_flag)
122 1.2 leo eprintf("\r\n");
123 1.2 leo xexit(0);
124 1.1 leo }
125 1.1 leo
126 1.2 leo static void
127 1.2 leo brwrite(buf, trk)
128 1.1 leo char *buf;
129 1.2 leo int trk;
130 1.1 leo {
131 1.2 leo static u_char trbuf[NSECT_DD * SECT_SIZE * 2];
132 1.2 leo static u_int sideno = 0;
133 1.2 leo
134 1.2 leo for (sideno = 0; sideno < 2; sideno++) {
135 1.2 leo if (Flopfmt(trbuf, 0, 0, NSECT_DD/2, trk, sideno, 1, 0x87654321,
136 1.2 leo 0xe5e5))
137 1.2 leo fatal(-1, "Format error");
138 1.2 leo if (Flopwr(buf, 0, 0, 1, trk, sideno, NSECT_DD/2))
139 1.2 leo fatal(-1, "Write error");
140 1.2 leo buf += (NSECT_DD/2) * SECT_SIZE;
141 1.1 leo }
142 1.1 leo }
143 1.2 leo static void
144 1.2 leo usage()
145 1.2 leo {
146 1.2 leo eprintf("Usage: %s [-hvVw] [-o <log-file>] <infile>\r\n", progname);
147 1.2 leo xexit(1);
148 1.2 leo }
149 1.1 leo
150 1.2 leo static void
151 1.2 leo help()
152 1.1 leo {
153 1.2 leo eprintf("\r
154 1.2 leo write a raw floppy-image to disk\r
155 1.2 leo \r
156 1.2 leo Usage: %s [-hvVw] [-o <log-file>] <infile>\r
157 1.2 leo \r
158 1.2 leo Description of options:\r
159 1.2 leo \r
160 1.2 leo \t-h What your getting right now.\r
161 1.2 leo \t-o Write output to both <output file> and stdout.\r
162 1.2 leo \t-v Show a '.' for each track written.\r
163 1.2 leo \t-V Print program version.\r
164 1.2 leo \t-w Wait for a keypress before exiting.\r
165 1.2 leo ", progname);
166 1.2 leo xexit(0);
167 1.1 leo }
168