xref: /csrg-svn/old/flcopy/flcopy.c (revision 5077)
1*5077Ssam static char *sccsid ="@(#)flcopy.c	4.4 (Berkeley) 11/25/81";
21016Sbill 
3*5077Ssam int	floppydes;
4*5077Ssam char	*flopname = "/dev/floppy";
5*5077Ssam long	dsize = 77 * 26 * 128;
6*5077Ssam int	hflag;
7*5077Ssam int	rflag;
8*5077Ssam 
9*5077Ssam main(argc, argv)
10*5077Ssam 	register char **argv;
111016Sbill {
121016Sbill 	static char buff[512];
131579Sbill 	register long count;
141579Sbill 	register startad = -26 * 128;
15*5077Ssam 	register int n, file;
16*5077Ssam 	register char *cp;
171016Sbill 
18*5077Ssam 	while ((cp = *++argv), --argc > 0) {
19*5077Ssam 		if (*cp++!='-')
20*5077Ssam 			continue;
21*5077Ssam 		while (*cp)
22*5077Ssam 			switch(*cp++) {
23*5077Ssam 
24*5077Ssam 			case 'h':
25*5077Ssam 				hflag++;
26*5077Ssam 				printf("Halftime!\n");
27*5077Ssam 				if ((file = open("floppy", 0)) < 0)
28*5077Ssam 					printf("can't open \"floppy\"\n"),
29*5077Ssam 				exit(1);
30*5077Ssam 				continue;
31*5077Ssam 
32*5077Ssam 			case 't':
33*5077Ssam 				if (*cp >= '0' && *cp <= '9')
34*5077Ssam 					dsize = atoi(cp);
35*5077Ssam 				else if (argc > 1) {
36*5077Ssam 					dsize = atoi(*++argv);
37*5077Ssam 					argc--;
38*5077Ssam 				} else
39*5077Ssam 					dsize = 77;
40*5077Ssam 				if (dsize <= 0 || dsize > 77) {
41*5077Ssam 					printf("Bad number of tracks\n");
42*5077Ssam 					exit(2);
43*5077Ssam 				}
44*5077Ssam 				dsize *= 26 * 128;
45*5077Ssam 				continue;
46*5077Ssam 
47*5077Ssam 			case 'r':
48*5077Ssam 				rflag++;
49*5077Ssam 			}
501016Sbill 	}
51*5077Ssam 	if (!hflag) {
52*5077Ssam 		file = creat("floppy", 0666);
53*5077Ssam 		close(file);
54*5077Ssam 		file = open("floppy", 2);
55*5077Ssam 		if (file < 0) {
56*5077Ssam 			printf("can't open \"floppy\"\n");
57*5077Ssam 			exit(1);
58*5077Ssam 		}
59*5077Ssam 		for (count = dsize; count > 0 ; count -= 512) {
60*5077Ssam 			n = count > 512 ? 512 : count;
61*5077Ssam 			lread(startad, n, buff);
62*5077Ssam 			write(file, buff, n);
63*5077Ssam 			startad += 512;
64*5077Ssam 		}
651016Sbill 	}
66*5077Ssam 	if (rflag)
67*5077Ssam 		exit(0);
681016Sbill 	printf("Change Floppy, Hit return when done.\n");
691016Sbill 	gets(buff);
70*5077Ssam 	lseek(file, 0, 0);
71*5077Ssam 	count = dsize;
72*5077Ssam 	startad = -26 * 128;
73*5077Ssam 	for ( ; count > 0 ; count -= 512) {
74*5077Ssam 		n = count > 512 ? 512 : count;
75*5077Ssam 		read(file, buff, n);
76*5077Ssam 		lwrite(startad, n, buff);
771016Sbill 		startad += 512;
781016Sbill 	}
791016Sbill }
80*5077Ssam 
811016Sbill rt_init()
821016Sbill {
831016Sbill 	static initized = 0;
841016Sbill 	int mode = 2;
851016Sbill 
86*5077Ssam 	if (initized)
87*5077Ssam 		return;
88*5077Ssam 	if (rflag)
89*5077Ssam 		mode = 0;
901016Sbill 	initized = 1;
91*5077Ssam 	if ((floppydes = open(flopname, mode)) < 0) {
921016Sbill 		printf("Floppy open failed\n");
931016Sbill 		exit(1);
941016Sbill 	}
951016Sbill }
96*5077Ssam 
97*5077Ssam /*
98*5077Ssam  * Logical to physical adress translation
99*5077Ssam  */
100*5077Ssam long
101*5077Ssam trans(logical)
102*5077Ssam 	register int logical;
1031016Sbill {
1041016Sbill 	register int sector, bytes, track;
1051016Sbill 
1061016Sbill 	logical += 26 * 128;
1071016Sbill 	bytes = (logical & 127);
1081016Sbill 	logical >>= 7;
1091016Sbill 	sector = logical % 26;
110*5077Ssam 	if (sector >= 13)
111*5077Ssam 		sector = sector*2 +1;
1121016Sbill 	else
1131016Sbill 		sector *= 2;
1141016Sbill 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
1151016Sbill 	sector %= 26;
116*5077Ssam 	return ((((track *26) + sector) << 7) + bytes);
1171016Sbill }
118*5077Ssam 
119*5077Ssam lread(startad, count, obuff)
120*5077Ssam 	register startad, count;
121*5077Ssam 	register char *obuff;
1221016Sbill {
1231016Sbill 	long trans();
1241016Sbill 	extern floppydes;
125*5077Ssam 
1261016Sbill 	rt_init();
127*5077Ssam 	while ((count -= 128) >= 0) {
128*5077Ssam 		lseek(floppydes, trans(startad), 0);
129*5077Ssam 		read(floppydes, obuff, 128);
130*5077Ssam 		obuff += 128;
131*5077Ssam 		startad += 128;
132*5077Ssam 	}
1331016Sbill }
134*5077Ssam 
135*5077Ssam lwrite(startad, count, obuff)
136*5077Ssam 	register startad, count;
137*5077Ssam 	register char *obuff;
1381016Sbill {
1391016Sbill 	long trans();
1401016Sbill 	extern floppydes;
141*5077Ssam 
1421016Sbill 	rt_init();
143*5077Ssam 	while ((count -= 128) >= 0) {
144*5077Ssam 		lseek(floppydes, trans(startad), 0);
145*5077Ssam 		write(floppydes, obuff, 128);
146*5077Ssam 		obuff += 128;
147*5077Ssam 		startad += 128;
148*5077Ssam 	}
1491016Sbill }
150