Home | History | Annotate | Line # | Download | only in rawwrite
rawwrite.c revision 1.2
      1 /*	$NetBSD: rawwrite.c,v 1.2 1996/01/07 22:06:24 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 track 720Kb	*/
     43 #define	NSECT_HD	36		/* Sectors per track 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));
     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.2 $";
     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;
     69 	int		nsect;
     70 
     71 	progname = argv[0];
     72 	init_toslib(argv[0]);
     73 
     74 	while ((ch = getopt(argc, argv, "hvVwo:")) != EOF) {
     75 		switch (ch) {
     76 			case 'h':
     77 				h_flag = 1;
     78 				break;
     79 			case 'o':
     80 				redirect_output(optarg);
     81 				break;
     82 			case 'v':
     83 				v_flag = 1;
     84 				break;
     85 			case 'V':
     86 				V_flag = 1;
     87 				break;
     88 			case 'w':
     89 				set_wait_for_key();
     90 				break;
     91 			default :
     92 				usage();
     93 				break;
     94 		}
     95 	}
     96 	if (h_flag)
     97 		help();
     98 	if (V_flag)
     99 		eprintf("%s\r\n", version);
    100 
    101 	if (optind >= argc)
    102 		usage();
    103 
    104 	infile = argv[optind];
    105 	nsect  = NSECT_DD;
    106 
    107 	if ((fd = open(infile, O_RDONLY)) < 0)
    108 		fatal(-1, "Cannot open '%s'\n", infile);
    109 
    110 	for (i = 0; i < NTRK; i++) {
    111 		if (read(fd, buf, nsect * SECT_SIZE) != (nsect * SECT_SIZE))
    112 		    fatal(-1, "\n\rRead error on '%s'\n", infile);
    113 		if (v_flag) {
    114 			if (i && !(i % 40))
    115 				eprintf("\r\n");
    116 			eprintf(".");
    117 		}
    118 		brwrite(buf, i);
    119 	}
    120 	close(fd);
    121 	if (v_flag)
    122 		eprintf("\r\n");
    123 	xexit(0);
    124 }
    125 
    126 static void
    127 brwrite(buf, trk)
    128 char	*buf;
    129 int	trk;
    130 {
    131 	static u_char	trbuf[NSECT_DD * SECT_SIZE * 2];
    132 	static u_int	sideno  = 0;
    133 
    134 	for (sideno = 0; sideno < 2; sideno++) {
    135 		if (Flopfmt(trbuf, 0, 0, NSECT_DD/2, trk, sideno, 1, 0x87654321,
    136 																 0xe5e5))
    137 			fatal(-1, "Format error");
    138 		if (Flopwr(buf, 0, 0, 1, trk, sideno, NSECT_DD/2))
    139 			fatal(-1, "Write error");
    140 		buf += (NSECT_DD/2) * SECT_SIZE;
    141 	}
    142 }
    143 static void
    144 usage()
    145 {
    146 	eprintf("Usage: %s [-hvVw] [-o <log-file>] <infile>\r\n", progname);
    147 	xexit(1);
    148 }
    149 
    150 static void
    151 help()
    152 {
    153 	eprintf("\r
    154 write a raw floppy-image to disk\r
    155 \r
    156 Usage: %s [-hvVw] [-o <log-file>] <infile>\r
    157 \r
    158 Description of options:\r
    159 \r
    160 \t-h  What your getting right now.\r
    161 \t-o  Write output to both <output file> and stdout.\r
    162 \t-v  Show a '.' for each track written.\r
    163 \t-V  Print program version.\r
    164 \t-w  Wait for a keypress before exiting.\r
    165 ", progname);
    166 	xexit(0);
    167 }
    168