xref: /csrg-svn/old/flcopy/flcopy.c (revision 37979)
129399Smckusick /*
2*37979Sbostic  * Copyright (c) 1989 The Regents of the University of California.
3*37979Sbostic  * All rights reserved.
4*37979Sbostic  *
5*37979Sbostic  * Redistribution and use in source and binary forms are permitted
6*37979Sbostic  * provided that the above copyright notice and this paragraph are
7*37979Sbostic  * duplicated in all such forms and that any documentation,
8*37979Sbostic  * advertising materials, and other materials related to such
9*37979Sbostic  * distribution and use acknowledge that the software was developed
10*37979Sbostic  * by the University of California, Berkeley.  The name of the
11*37979Sbostic  * University may not be used to endorse or promote products derived
12*37979Sbostic  * from this software without specific prior written permission.
13*37979Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*37979Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*37979Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1629399Smckusick  */
1729399Smckusick 
1813837Ssam #ifndef lint
1929399Smckusick char copyright[] =
20*37979Sbostic "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
2129399Smckusick  All rights reserved.\n";
22*37979Sbostic #endif /* not lint */
231016Sbill 
2429399Smckusick #ifndef lint
25*37979Sbostic static char sccsid[] = "@(#)flcopy.c	5.2 (Berkeley) 05/11/89";
26*37979Sbostic #endif /* not lint */
2729399Smckusick 
2813837Ssam #include <sys/file.h>
29*37979Sbostic #include "pathnames.h"
3013837Ssam 
315077Ssam int	floppydes;
32*37979Sbostic char	*flopname = _PATH_FLOPPY;
335077Ssam long	dsize = 77 * 26 * 128;
345077Ssam int	hflag;
355077Ssam int	rflag;
365077Ssam 
375077Ssam main(argc, argv)
385077Ssam 	register char **argv;
391016Sbill {
401016Sbill 	static char buff[512];
411579Sbill 	register long count;
421579Sbill 	register startad = -26 * 128;
435077Ssam 	register int n, file;
445077Ssam 	register char *cp;
451016Sbill 
465077Ssam 	while ((cp = *++argv), --argc > 0) {
4713837Ssam 		while (*cp) {
485077Ssam 			switch(*cp++) {
495077Ssam 
5013837Ssam 			case '-':
5113837Ssam 				continue;
5213837Ssam 
535077Ssam 			case 'h':
545077Ssam 				hflag++;
555077Ssam 				printf("Halftime!\n");
5613837Ssam 				if ((file = open("floppy", 0)) < 0) {
5713837Ssam 					printf("can't open \"floppy\"\n");
5813837Ssam 					exit(1);
5913837Ssam 				}
605077Ssam 				continue;
615077Ssam 
6213837Ssam 			case 'f':
6313837Ssam 				if (argc < 1) {
6413837Ssam 					printf(
6513837Ssam 					    "flcopy: -f: missing file name\n");
6613837Ssam 					exit(1);
6713837Ssam 				}
6813837Ssam 				flopname = *++argv;
6913837Ssam 				argc--;
7013837Ssam 				break;
7113837Ssam 
725077Ssam 			case 't':
735077Ssam 				if (*cp >= '0' && *cp <= '9')
745077Ssam 					dsize = atoi(cp);
755077Ssam 				else if (argc > 1) {
765077Ssam 					dsize = atoi(*++argv);
775077Ssam 					argc--;
785077Ssam 				} else
795077Ssam 					dsize = 77;
805077Ssam 				if (dsize <= 0 || dsize > 77) {
815077Ssam 					printf("Bad number of tracks\n");
825077Ssam 					exit(2);
835077Ssam 				}
845077Ssam 				dsize *= 26 * 128;
855077Ssam 				continue;
865077Ssam 
875077Ssam 			case 'r':
885077Ssam 				rflag++;
895077Ssam 			}
9013837Ssam 			break;
9113837Ssam 		}
921016Sbill 	}
935077Ssam 	if (!hflag) {
9413837Ssam 		file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
955077Ssam 		if (file < 0) {
965077Ssam 			printf("can't open \"floppy\"\n");
975077Ssam 			exit(1);
985077Ssam 		}
995077Ssam 		for (count = dsize; count > 0 ; count -= 512) {
1005077Ssam 			n = count > 512 ? 512 : count;
1015077Ssam 			lread(startad, n, buff);
1025077Ssam 			write(file, buff, n);
1035077Ssam 			startad += 512;
1045077Ssam 		}
1051016Sbill 	}
1065077Ssam 	if (rflag)
1075077Ssam 		exit(0);
1081016Sbill 	printf("Change Floppy, Hit return when done.\n");
1091016Sbill 	gets(buff);
1105077Ssam 	lseek(file, 0, 0);
1115077Ssam 	count = dsize;
1125077Ssam 	startad = -26 * 128;
1135077Ssam 	for ( ; count > 0 ; count -= 512) {
1145077Ssam 		n = count > 512 ? 512 : count;
1155077Ssam 		read(file, buff, n);
1165077Ssam 		lwrite(startad, n, buff);
1171016Sbill 		startad += 512;
1181016Sbill 	}
11913911Ssam 	exit(0);
1201016Sbill }
1215077Ssam 
1221016Sbill rt_init()
1231016Sbill {
1241016Sbill 	static initized = 0;
1251016Sbill 	int mode = 2;
1261016Sbill 
1275077Ssam 	if (initized)
1285077Ssam 		return;
1295077Ssam 	if (rflag)
1305077Ssam 		mode = 0;
1311016Sbill 	initized = 1;
1325077Ssam 	if ((floppydes = open(flopname, mode)) < 0) {
1331016Sbill 		printf("Floppy open failed\n");
1341016Sbill 		exit(1);
1351016Sbill 	}
1361016Sbill }
1375077Ssam 
1385077Ssam /*
1395077Ssam  * Logical to physical adress translation
1405077Ssam  */
1415077Ssam long
1425077Ssam trans(logical)
1435077Ssam 	register int logical;
1441016Sbill {
1451016Sbill 	register int sector, bytes, track;
1461016Sbill 
1471016Sbill 	logical += 26 * 128;
1481016Sbill 	bytes = (logical & 127);
1491016Sbill 	logical >>= 7;
1501016Sbill 	sector = logical % 26;
1515077Ssam 	if (sector >= 13)
1525077Ssam 		sector = sector*2 +1;
1531016Sbill 	else
1541016Sbill 		sector *= 2;
1551016Sbill 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
1561016Sbill 	sector %= 26;
1575077Ssam 	return ((((track *26) + sector) << 7) + bytes);
1581016Sbill }
1595077Ssam 
1605077Ssam lread(startad, count, obuff)
1615077Ssam 	register startad, count;
1625077Ssam 	register char *obuff;
1631016Sbill {
1641016Sbill 	long trans();
1651016Sbill 	extern floppydes;
1665077Ssam 
1671016Sbill 	rt_init();
1685077Ssam 	while ((count -= 128) >= 0) {
1695077Ssam 		lseek(floppydes, trans(startad), 0);
1705077Ssam 		read(floppydes, obuff, 128);
1715077Ssam 		obuff += 128;
1725077Ssam 		startad += 128;
1735077Ssam 	}
1741016Sbill }
1755077Ssam 
1765077Ssam lwrite(startad, count, obuff)
1775077Ssam 	register startad, count;
1785077Ssam 	register char *obuff;
1791016Sbill {
1801016Sbill 	long trans();
1811016Sbill 	extern floppydes;
1825077Ssam 
1831016Sbill 	rt_init();
1845077Ssam 	while ((count -= 128) >= 0) {
1855077Ssam 		lseek(floppydes, trans(startad), 0);
1865077Ssam 		write(floppydes, obuff, 128);
1875077Ssam 		obuff += 128;
1885077Ssam 		startad += 128;
1895077Ssam 	}
1901016Sbill }
191