Home | History | Annotate | Line # | Download | only in rawwrite
rawwrite.c revision 1.7
      1  1.4  lukem /*	$NetBSD: rawwrite.c,v 1.7 1998/01/16 09:19:37 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.3    leo #define	NSECT_DD	18		/* Sectors per cylinder 720Kb	*/
     43  1.3    leo #define	NSECT_HD	36		/* Sectors per cylinder 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.5    leo static void brwrite PROTO((char *, int, 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.7    leo const char version[] = "$Revision: 1.7 $";
     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.5    leo 	int		i, n;
     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.5    leo 	nsect  = NSECT_DD;
     75  1.5    leo 
     76  1.5    leo 	while ((ch = getopt(argc, argv, "hHvVwo:")) != -1) {
     77  1.2    leo 		switch (ch) {
     78  1.2    leo 			case 'h':
     79  1.2    leo 				h_flag = 1;
     80  1.2    leo 				break;
     81  1.5    leo 			case 'H':
     82  1.5    leo 				nsect  = NSECT_HD;
     83  1.5    leo 				break;
     84  1.2    leo 			case 'o':
     85  1.2    leo 				redirect_output(optarg);
     86  1.2    leo 				break;
     87  1.1    leo 			case 'v':
     88  1.2    leo 				v_flag = 1;
     89  1.2    leo 				break;
     90  1.2    leo 			case 'V':
     91  1.2    leo 				V_flag = 1;
     92  1.2    leo 				break;
     93  1.2    leo 			case 'w':
     94  1.2    leo 				set_wait_for_key();
     95  1.1    leo 				break;
     96  1.1    leo 			default :
     97  1.1    leo 				usage();
     98  1.1    leo 				break;
     99  1.1    leo 		}
    100  1.1    leo 	}
    101  1.2    leo 	if (h_flag)
    102  1.2    leo 		help();
    103  1.2    leo 	if (V_flag)
    104  1.2    leo 		eprintf("%s\r\n", version);
    105  1.2    leo 
    106  1.2    leo 	if (optind >= argc)
    107  1.1    leo 		usage();
    108  1.1    leo 
    109  1.1    leo 	infile = argv[optind];
    110  1.1    leo 
    111  1.2    leo 	if ((fd = open(infile, O_RDONLY)) < 0)
    112  1.2    leo 		fatal(-1, "Cannot open '%s'\n", infile);
    113  1.1    leo 
    114  1.2    leo 	for (i = 0; i < NTRK; i++) {
    115  1.5    leo 		n = read(fd, buf, nsect * SECT_SIZE);
    116  1.5    leo 		if (n == 0) {
    117  1.6    leo 			eprintf("Only %d sectors in input file\r\n", i);
    118  1.5    leo 			break;
    119  1.5    leo 		}
    120  1.7    leo 		if (n < 0)
    121  1.7    leo 		    fatal(-1, "\n\rRead error on '%s'\n", infile);
    122  1.5    leo 		if (n != (nsect * SECT_SIZE))
    123  1.7    leo 		    fatal(-1, "\n\rUnexpected short-read on '%s'\n", infile);
    124  1.2    leo 		if (v_flag) {
    125  1.2    leo 			if (i && !(i % 40))
    126  1.2    leo 				eprintf("\r\n");
    127  1.2    leo 			eprintf(".");
    128  1.1    leo 		}
    129  1.5    leo 		brwrite(buf, i, nsect);
    130  1.1    leo 	}
    131  1.1    leo 	close(fd);
    132  1.2    leo 	if (v_flag)
    133  1.2    leo 		eprintf("\r\n");
    134  1.2    leo 	xexit(0);
    135  1.1    leo }
    136  1.1    leo 
    137  1.2    leo static void
    138  1.5    leo brwrite(buf, trk, spt)
    139  1.1    leo char	*buf;
    140  1.5    leo int	trk, spt;
    141  1.1    leo {
    142  1.6    leo 	/*
    143  1.6    leo 	 * These need to be static with my version of osbind.h :-(
    144  1.6    leo 	 */
    145  1.6    leo 	static u_char	trbuf[NSECT_HD * SECT_SIZE * 2];
    146  1.2    leo 	static u_int	sideno  = 0;
    147  1.2    leo 
    148  1.2    leo 	for (sideno = 0; sideno < 2; sideno++) {
    149  1.5    leo 		if (Flopfmt(trbuf, 0, 0, spt/2, trk, sideno, 1,
    150  1.3    leo 						0x87654321, 0xe5e5))
    151  1.2    leo 			fatal(-1, "Format error");
    152  1.5    leo 		if (Flopwr(buf, 0, 0, 1, trk, sideno, spt/2))
    153  1.2    leo 			fatal(-1, "Write error");
    154  1.5    leo 		buf += (spt/2) * SECT_SIZE;
    155  1.1    leo 	}
    156  1.1    leo }
    157  1.2    leo static void
    158  1.2    leo usage()
    159  1.2    leo {
    160  1.2    leo 	eprintf("Usage: %s [-hvVw] [-o <log-file>] <infile>\r\n", progname);
    161  1.2    leo 	xexit(1);
    162  1.2    leo }
    163  1.1    leo 
    164  1.2    leo static void
    165  1.2    leo help()
    166  1.1    leo {
    167  1.2    leo 	eprintf("\r
    168  1.2    leo write a raw floppy-image to disk\r
    169  1.2    leo \r
    170  1.2    leo Usage: %s [-hvVw] [-o <log-file>] <infile>\r
    171  1.2    leo \r
    172  1.2    leo Description of options:\r
    173  1.2    leo \r
    174  1.3    leo \t-h  What you're getting right now.\r
    175  1.5    leo \t-H  Write high density floppies.\r
    176  1.2    leo \t-o  Write output to both <output file> and stdout.\r
    177  1.2    leo \t-v  Show a '.' for each track written.\r
    178  1.2    leo \t-V  Print program version.\r
    179  1.2    leo \t-w  Wait for a keypress before exiting.\r
    180  1.2    leo ", progname);
    181  1.2    leo 	xexit(0);
    182  1.1    leo }
    183