xref: /original-bsd/old/flcopy/flcopy.c (revision 7211505a)
1 /*
2  * Copyright (c) 1980 Regents of the University of California.
3  * All rights reserved.  The Berkeley software License Agreement
4  * specifies the terms and conditions for redistribution.
5  */
6 
7 #ifndef lint
8 char copyright[] =
9 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
10  All rights reserved.\n";
11 #endif not lint
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)flcopy.c	5.1 (Berkeley) 06/06/86";
15 #endif not lint
16 
17 #include <sys/file.h>
18 
19 int	floppydes;
20 char	*flopname = "/dev/floppy";
21 long	dsize = 77 * 26 * 128;
22 int	hflag;
23 int	rflag;
24 
25 main(argc, argv)
26 	register char **argv;
27 {
28 	static char buff[512];
29 	register long count;
30 	register startad = -26 * 128;
31 	register int n, file;
32 	register char *cp;
33 
34 	while ((cp = *++argv), --argc > 0) {
35 		while (*cp) {
36 			switch(*cp++) {
37 
38 			case '-':
39 				continue;
40 
41 			case 'h':
42 				hflag++;
43 				printf("Halftime!\n");
44 				if ((file = open("floppy", 0)) < 0) {
45 					printf("can't open \"floppy\"\n");
46 					exit(1);
47 				}
48 				continue;
49 
50 			case 'f':
51 				if (argc < 1) {
52 					printf(
53 					    "flcopy: -f: missing file name\n");
54 					exit(1);
55 				}
56 				flopname = *++argv;
57 				argc--;
58 				break;
59 
60 			case 't':
61 				if (*cp >= '0' && *cp <= '9')
62 					dsize = atoi(cp);
63 				else if (argc > 1) {
64 					dsize = atoi(*++argv);
65 					argc--;
66 				} else
67 					dsize = 77;
68 				if (dsize <= 0 || dsize > 77) {
69 					printf("Bad number of tracks\n");
70 					exit(2);
71 				}
72 				dsize *= 26 * 128;
73 				continue;
74 
75 			case 'r':
76 				rflag++;
77 			}
78 			break;
79 		}
80 	}
81 	if (!hflag) {
82 		file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
83 		if (file < 0) {
84 			printf("can't open \"floppy\"\n");
85 			exit(1);
86 		}
87 		for (count = dsize; count > 0 ; count -= 512) {
88 			n = count > 512 ? 512 : count;
89 			lread(startad, n, buff);
90 			write(file, buff, n);
91 			startad += 512;
92 		}
93 	}
94 	if (rflag)
95 		exit(0);
96 	printf("Change Floppy, Hit return when done.\n");
97 	gets(buff);
98 	lseek(file, 0, 0);
99 	count = dsize;
100 	startad = -26 * 128;
101 	for ( ; count > 0 ; count -= 512) {
102 		n = count > 512 ? 512 : count;
103 		read(file, buff, n);
104 		lwrite(startad, n, buff);
105 		startad += 512;
106 	}
107 	exit(0);
108 }
109 
110 rt_init()
111 {
112 	static initized = 0;
113 	int mode = 2;
114 
115 	if (initized)
116 		return;
117 	if (rflag)
118 		mode = 0;
119 	initized = 1;
120 	if ((floppydes = open(flopname, mode)) < 0) {
121 		printf("Floppy open failed\n");
122 		exit(1);
123 	}
124 }
125 
126 /*
127  * Logical to physical adress translation
128  */
129 long
130 trans(logical)
131 	register int logical;
132 {
133 	register int sector, bytes, track;
134 
135 	logical += 26 * 128;
136 	bytes = (logical & 127);
137 	logical >>= 7;
138 	sector = logical % 26;
139 	if (sector >= 13)
140 		sector = sector*2 +1;
141 	else
142 		sector *= 2;
143 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
144 	sector %= 26;
145 	return ((((track *26) + sector) << 7) + bytes);
146 }
147 
148 lread(startad, count, obuff)
149 	register startad, count;
150 	register char *obuff;
151 {
152 	long trans();
153 	extern floppydes;
154 
155 	rt_init();
156 	while ((count -= 128) >= 0) {
157 		lseek(floppydes, trans(startad), 0);
158 		read(floppydes, obuff, 128);
159 		obuff += 128;
160 		startad += 128;
161 	}
162 }
163 
164 lwrite(startad, count, obuff)
165 	register startad, count;
166 	register char *obuff;
167 {
168 	long trans();
169 	extern floppydes;
170 
171 	rt_init();
172 	while ((count -= 128) >= 0) {
173 		lseek(floppydes, trans(startad), 0);
174 		write(floppydes, obuff, 128);
175 		obuff += 128;
176 		startad += 128;
177 	}
178 }
179