xref: /csrg-svn/old/flcopy/flcopy.c (revision 42797)
129399Smckusick /*
237979Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337979Sbostic  * All rights reserved.
437979Sbostic  *
5*42797Sbostic  * %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*42797Sbostic static char sccsid[] = "@(#)flcopy.c	5.3 (Berkeley) 06/01/90";
1637979Sbostic #endif /* not lint */
1729399Smckusick 
1813837Ssam #include <sys/file.h>
1937979Sbostic #include "pathnames.h"
2013837Ssam 
215077Ssam int	floppydes;
2237979Sbostic char	*flopname = _PATH_FLOPPY;
235077Ssam long	dsize = 77 * 26 * 128;
245077Ssam int	hflag;
255077Ssam int	rflag;
265077Ssam 
275077Ssam main(argc, argv)
285077Ssam 	register char **argv;
291016Sbill {
301016Sbill 	static char buff[512];
311579Sbill 	register long count;
321579Sbill 	register startad = -26 * 128;
335077Ssam 	register int n, file;
345077Ssam 	register char *cp;
351016Sbill 
365077Ssam 	while ((cp = *++argv), --argc > 0) {
3713837Ssam 		while (*cp) {
385077Ssam 			switch(*cp++) {
395077Ssam 
4013837Ssam 			case '-':
4113837Ssam 				continue;
4213837Ssam 
435077Ssam 			case 'h':
445077Ssam 				hflag++;
455077Ssam 				printf("Halftime!\n");
4613837Ssam 				if ((file = open("floppy", 0)) < 0) {
4713837Ssam 					printf("can't open \"floppy\"\n");
4813837Ssam 					exit(1);
4913837Ssam 				}
505077Ssam 				continue;
515077Ssam 
5213837Ssam 			case 'f':
5313837Ssam 				if (argc < 1) {
5413837Ssam 					printf(
5513837Ssam 					    "flcopy: -f: missing file name\n");
5613837Ssam 					exit(1);
5713837Ssam 				}
5813837Ssam 				flopname = *++argv;
5913837Ssam 				argc--;
6013837Ssam 				break;
6113837Ssam 
625077Ssam 			case 't':
635077Ssam 				if (*cp >= '0' && *cp <= '9')
645077Ssam 					dsize = atoi(cp);
655077Ssam 				else if (argc > 1) {
665077Ssam 					dsize = atoi(*++argv);
675077Ssam 					argc--;
685077Ssam 				} else
695077Ssam 					dsize = 77;
705077Ssam 				if (dsize <= 0 || dsize > 77) {
715077Ssam 					printf("Bad number of tracks\n");
725077Ssam 					exit(2);
735077Ssam 				}
745077Ssam 				dsize *= 26 * 128;
755077Ssam 				continue;
765077Ssam 
775077Ssam 			case 'r':
785077Ssam 				rflag++;
795077Ssam 			}
8013837Ssam 			break;
8113837Ssam 		}
821016Sbill 	}
835077Ssam 	if (!hflag) {
8413837Ssam 		file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
855077Ssam 		if (file < 0) {
865077Ssam 			printf("can't open \"floppy\"\n");
875077Ssam 			exit(1);
885077Ssam 		}
895077Ssam 		for (count = dsize; count > 0 ; count -= 512) {
905077Ssam 			n = count > 512 ? 512 : count;
915077Ssam 			lread(startad, n, buff);
925077Ssam 			write(file, buff, n);
935077Ssam 			startad += 512;
945077Ssam 		}
951016Sbill 	}
965077Ssam 	if (rflag)
975077Ssam 		exit(0);
981016Sbill 	printf("Change Floppy, Hit return when done.\n");
991016Sbill 	gets(buff);
1005077Ssam 	lseek(file, 0, 0);
1015077Ssam 	count = dsize;
1025077Ssam 	startad = -26 * 128;
1035077Ssam 	for ( ; count > 0 ; count -= 512) {
1045077Ssam 		n = count > 512 ? 512 : count;
1055077Ssam 		read(file, buff, n);
1065077Ssam 		lwrite(startad, n, buff);
1071016Sbill 		startad += 512;
1081016Sbill 	}
10913911Ssam 	exit(0);
1101016Sbill }
1115077Ssam 
1121016Sbill rt_init()
1131016Sbill {
1141016Sbill 	static initized = 0;
1151016Sbill 	int mode = 2;
1161016Sbill 
1175077Ssam 	if (initized)
1185077Ssam 		return;
1195077Ssam 	if (rflag)
1205077Ssam 		mode = 0;
1211016Sbill 	initized = 1;
1225077Ssam 	if ((floppydes = open(flopname, mode)) < 0) {
1231016Sbill 		printf("Floppy open failed\n");
1241016Sbill 		exit(1);
1251016Sbill 	}
1261016Sbill }
1275077Ssam 
1285077Ssam /*
1295077Ssam  * Logical to physical adress translation
1305077Ssam  */
1315077Ssam long
1325077Ssam trans(logical)
1335077Ssam 	register int logical;
1341016Sbill {
1351016Sbill 	register int sector, bytes, track;
1361016Sbill 
1371016Sbill 	logical += 26 * 128;
1381016Sbill 	bytes = (logical & 127);
1391016Sbill 	logical >>= 7;
1401016Sbill 	sector = logical % 26;
1415077Ssam 	if (sector >= 13)
1425077Ssam 		sector = sector*2 +1;
1431016Sbill 	else
1441016Sbill 		sector *= 2;
1451016Sbill 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
1461016Sbill 	sector %= 26;
1475077Ssam 	return ((((track *26) + sector) << 7) + bytes);
1481016Sbill }
1495077Ssam 
1505077Ssam lread(startad, count, obuff)
1515077Ssam 	register startad, count;
1525077Ssam 	register char *obuff;
1531016Sbill {
1541016Sbill 	long trans();
1551016Sbill 	extern floppydes;
1565077Ssam 
1571016Sbill 	rt_init();
1585077Ssam 	while ((count -= 128) >= 0) {
1595077Ssam 		lseek(floppydes, trans(startad), 0);
1605077Ssam 		read(floppydes, obuff, 128);
1615077Ssam 		obuff += 128;
1625077Ssam 		startad += 128;
1635077Ssam 	}
1641016Sbill }
1655077Ssam 
1665077Ssam lwrite(startad, count, obuff)
1675077Ssam 	register startad, count;
1685077Ssam 	register char *obuff;
1691016Sbill {
1701016Sbill 	long trans();
1711016Sbill 	extern floppydes;
1725077Ssam 
1731016Sbill 	rt_init();
1745077Ssam 	while ((count -= 128) >= 0) {
1755077Ssam 		lseek(floppydes, trans(startad), 0);
1765077Ssam 		write(floppydes, obuff, 128);
1775077Ssam 		obuff += 128;
1785077Ssam 		startad += 128;
1795077Ssam 	}
1801016Sbill }
181