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