xref: /csrg-svn/old/flcopy/flcopy.c (revision 46053)
129399Smckusick /*
237979Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337979Sbostic  * All rights reserved.
437979Sbostic  *
542797Sbostic  * %sccs.include.redist.c%
629399Smckusick  */
729399Smckusick 
813837Ssam #ifndef lint
929399Smckusick char copyright[] =
1037979Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
1129399Smckusick  All rights reserved.\n";
1237979Sbostic #endif /* not lint */
131016Sbill 
1429399Smckusick #ifndef lint
15*46053Sbostic static char sccsid[] = "@(#)flcopy.c	5.4 (Berkeley) 01/20/91";
1637979Sbostic #endif /* not lint */
1729399Smckusick 
1813837Ssam #include <sys/file.h>
19*46053Sbostic #include <stdio.h>
2037979Sbostic #include "pathnames.h"
2113837Ssam 
225077Ssam int	floppydes;
2337979Sbostic char	*flopname = _PATH_FLOPPY;
245077Ssam long	dsize = 77 * 26 * 128;
255077Ssam int	hflag;
265077Ssam int	rflag;
275077Ssam 
main(argc,argv)285077Ssam main(argc, argv)
295077Ssam 	register char **argv;
301016Sbill {
31*46053Sbostic 	extern char *optarg;
32*46053Sbostic 	extern int optind;
331016Sbill 	static char buff[512];
341579Sbill 	register long count;
351579Sbill 	register startad = -26 * 128;
365077Ssam 	register int n, file;
375077Ssam 	register char *cp;
38*46053Sbostic 	int ch;
391016Sbill 
40*46053Sbostic 	while ((ch = getopt(argc, argv, "f:hrt:")) != EOF)
41*46053Sbostic 		switch(ch) {
42*46053Sbostic 		case 'f':
43*46053Sbostic 			flopname = optarg;
44*46053Sbostic 			break;
45*46053Sbostic 		case 'h':
46*46053Sbostic 			hflag = 1;
47*46053Sbostic 			printf("Halftime!\n");
48*46053Sbostic 			if ((file = open("floppy", 0)) < 0) {
49*46053Sbostic 				printf("can't open \"floppy\"\n");
50*46053Sbostic 				exit(1);
515077Ssam 			}
5213837Ssam 			break;
53*46053Sbostic 		case 'r':
54*46053Sbostic 			rflag = 1;
55*46053Sbostic 			break;
56*46053Sbostic 		case 't':
57*46053Sbostic 			dsize = atoi(optarg);
58*46053Sbostic 			if (dsize <= 0 || dsize > 77) {
59*46053Sbostic 				(void)fprintf(stderr,
60*46053Sbostic 				    "flcopy: bad number of tracks (0 - 77).\n");
61*46053Sbostic 				exit(2);
62*46053Sbostic 			}
63*46053Sbostic 			dsize *= 26 * 128;
64*46053Sbostic 			break;
65*46053Sbostic 		case '?':
66*46053Sbostic 		default:
67*46053Sbostic 			usage();
6813837Ssam 		}
69*46053Sbostic 	argc -= optind;
70*46053Sbostic 	argv += optind;
71*46053Sbostic 
725077Ssam 	if (!hflag) {
7313837Ssam 		file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
745077Ssam 		if (file < 0) {
755077Ssam 			printf("can't open \"floppy\"\n");
765077Ssam 			exit(1);
775077Ssam 		}
785077Ssam 		for (count = dsize; count > 0 ; count -= 512) {
795077Ssam 			n = count > 512 ? 512 : count;
805077Ssam 			lread(startad, n, buff);
815077Ssam 			write(file, buff, n);
825077Ssam 			startad += 512;
835077Ssam 		}
841016Sbill 	}
855077Ssam 	if (rflag)
865077Ssam 		exit(0);
871016Sbill 	printf("Change Floppy, Hit return when done.\n");
881016Sbill 	gets(buff);
895077Ssam 	lseek(file, 0, 0);
905077Ssam 	count = dsize;
915077Ssam 	startad = -26 * 128;
925077Ssam 	for ( ; count > 0 ; count -= 512) {
935077Ssam 		n = count > 512 ? 512 : count;
945077Ssam 		read(file, buff, n);
955077Ssam 		lwrite(startad, n, buff);
961016Sbill 		startad += 512;
971016Sbill 	}
9813911Ssam 	exit(0);
991016Sbill }
1005077Ssam 
rt_init()1011016Sbill rt_init()
1021016Sbill {
1031016Sbill 	static initized = 0;
1041016Sbill 	int mode = 2;
1051016Sbill 
1065077Ssam 	if (initized)
1075077Ssam 		return;
1085077Ssam 	if (rflag)
1095077Ssam 		mode = 0;
1101016Sbill 	initized = 1;
1115077Ssam 	if ((floppydes = open(flopname, mode)) < 0) {
1121016Sbill 		printf("Floppy open failed\n");
1131016Sbill 		exit(1);
1141016Sbill 	}
1151016Sbill }
1165077Ssam 
1175077Ssam /*
1185077Ssam  * Logical to physical adress translation
1195077Ssam  */
1205077Ssam long
trans(logical)1215077Ssam trans(logical)
1225077Ssam 	register int logical;
1231016Sbill {
1241016Sbill 	register int sector, bytes, track;
1251016Sbill 
1261016Sbill 	logical += 26 * 128;
1271016Sbill 	bytes = (logical & 127);
1281016Sbill 	logical >>= 7;
1291016Sbill 	sector = logical % 26;
1305077Ssam 	if (sector >= 13)
1315077Ssam 		sector = sector*2 +1;
1321016Sbill 	else
1331016Sbill 		sector *= 2;
1341016Sbill 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
1351016Sbill 	sector %= 26;
1365077Ssam 	return ((((track *26) + sector) << 7) + bytes);
1371016Sbill }
1385077Ssam 
lread(startad,count,obuff)1395077Ssam lread(startad, count, obuff)
1405077Ssam 	register startad, count;
1415077Ssam 	register char *obuff;
1421016Sbill {
1431016Sbill 	long trans();
1441016Sbill 	extern floppydes;
1455077Ssam 
1461016Sbill 	rt_init();
1475077Ssam 	while ((count -= 128) >= 0) {
1485077Ssam 		lseek(floppydes, trans(startad), 0);
1495077Ssam 		read(floppydes, obuff, 128);
1505077Ssam 		obuff += 128;
1515077Ssam 		startad += 128;
1525077Ssam 	}
1531016Sbill }
1545077Ssam 
lwrite(startad,count,obuff)1555077Ssam lwrite(startad, count, obuff)
1565077Ssam 	register startad, count;
1575077Ssam 	register char *obuff;
1581016Sbill {
1591016Sbill 	long trans();
1601016Sbill 	extern floppydes;
1615077Ssam 
1621016Sbill 	rt_init();
1635077Ssam 	while ((count -= 128) >= 0) {
1645077Ssam 		lseek(floppydes, trans(startad), 0);
1655077Ssam 		write(floppydes, obuff, 128);
1665077Ssam 		obuff += 128;
1675077Ssam 		startad += 128;
1685077Ssam 	}
1691016Sbill }
170*46053Sbostic 
usage()171*46053Sbostic usage()
172*46053Sbostic {
173*46053Sbostic 	(void)fprintf(stderr, "usage: flcopy [-hr] [-f file] [-t ntracks]\n");
174*46053Sbostic 	exit(1);
175*46053Sbostic }
176