xref: /original-bsd/old/flcopy/flcopy.c (revision 0b685140)
1 static char *sccsid ="@(#)flcopy.c	4.4 (Berkeley) 11/25/81";
2 
3 int	floppydes;
4 char	*flopname = "/dev/floppy";
5 long	dsize = 77 * 26 * 128;
6 int	hflag;
7 int	rflag;
8 
9 main(argc, argv)
10 	register char **argv;
11 {
12 	static char buff[512];
13 	register long count;
14 	register startad = -26 * 128;
15 	register int n, file;
16 	register char *cp;
17 
18 	while ((cp = *++argv), --argc > 0) {
19 		if (*cp++!='-')
20 			continue;
21 		while (*cp)
22 			switch(*cp++) {
23 
24 			case 'h':
25 				hflag++;
26 				printf("Halftime!\n");
27 				if ((file = open("floppy", 0)) < 0)
28 					printf("can't open \"floppy\"\n"),
29 				exit(1);
30 				continue;
31 
32 			case 't':
33 				if (*cp >= '0' && *cp <= '9')
34 					dsize = atoi(cp);
35 				else if (argc > 1) {
36 					dsize = atoi(*++argv);
37 					argc--;
38 				} else
39 					dsize = 77;
40 				if (dsize <= 0 || dsize > 77) {
41 					printf("Bad number of tracks\n");
42 					exit(2);
43 				}
44 				dsize *= 26 * 128;
45 				continue;
46 
47 			case 'r':
48 				rflag++;
49 			}
50 	}
51 	if (!hflag) {
52 		file = creat("floppy", 0666);
53 		close(file);
54 		file = open("floppy", 2);
55 		if (file < 0) {
56 			printf("can't open \"floppy\"\n");
57 			exit(1);
58 		}
59 		for (count = dsize; count > 0 ; count -= 512) {
60 			n = count > 512 ? 512 : count;
61 			lread(startad, n, buff);
62 			write(file, buff, n);
63 			startad += 512;
64 		}
65 	}
66 	if (rflag)
67 		exit(0);
68 	printf("Change Floppy, Hit return when done.\n");
69 	gets(buff);
70 	lseek(file, 0, 0);
71 	count = dsize;
72 	startad = -26 * 128;
73 	for ( ; count > 0 ; count -= 512) {
74 		n = count > 512 ? 512 : count;
75 		read(file, buff, n);
76 		lwrite(startad, n, buff);
77 		startad += 512;
78 	}
79 }
80 
81 rt_init()
82 {
83 	static initized = 0;
84 	int mode = 2;
85 
86 	if (initized)
87 		return;
88 	if (rflag)
89 		mode = 0;
90 	initized = 1;
91 	if ((floppydes = open(flopname, mode)) < 0) {
92 		printf("Floppy open failed\n");
93 		exit(1);
94 	}
95 }
96 
97 /*
98  * Logical to physical adress translation
99  */
100 long
101 trans(logical)
102 	register int logical;
103 {
104 	register int sector, bytes, track;
105 
106 	logical += 26 * 128;
107 	bytes = (logical & 127);
108 	logical >>= 7;
109 	sector = logical % 26;
110 	if (sector >= 13)
111 		sector = sector*2 +1;
112 	else
113 		sector *= 2;
114 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
115 	sector %= 26;
116 	return ((((track *26) + sector) << 7) + bytes);
117 }
118 
119 lread(startad, count, obuff)
120 	register startad, count;
121 	register char *obuff;
122 {
123 	long trans();
124 	extern floppydes;
125 
126 	rt_init();
127 	while ((count -= 128) >= 0) {
128 		lseek(floppydes, trans(startad), 0);
129 		read(floppydes, obuff, 128);
130 		obuff += 128;
131 		startad += 128;
132 	}
133 }
134 
135 lwrite(startad, count, obuff)
136 	register startad, count;
137 	register char *obuff;
138 {
139 	long trans();
140 	extern floppydes;
141 
142 	rt_init();
143 	while ((count -= 128) >= 0) {
144 		lseek(floppydes, trans(startad), 0);
145 		write(floppydes, obuff, 128);
146 		obuff += 128;
147 		startad += 128;
148 	}
149 }
150