xref: /csrg-svn/old/flcopy/flcopy.c (revision 37979)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)flcopy.c	5.2 (Berkeley) 05/11/89";
26 #endif /* not lint */
27 
28 #include <sys/file.h>
29 #include "pathnames.h"
30 
31 int	floppydes;
32 char	*flopname = _PATH_FLOPPY;
33 long	dsize = 77 * 26 * 128;
34 int	hflag;
35 int	rflag;
36 
37 main(argc, argv)
38 	register char **argv;
39 {
40 	static char buff[512];
41 	register long count;
42 	register startad = -26 * 128;
43 	register int n, file;
44 	register char *cp;
45 
46 	while ((cp = *++argv), --argc > 0) {
47 		while (*cp) {
48 			switch(*cp++) {
49 
50 			case '-':
51 				continue;
52 
53 			case 'h':
54 				hflag++;
55 				printf("Halftime!\n");
56 				if ((file = open("floppy", 0)) < 0) {
57 					printf("can't open \"floppy\"\n");
58 					exit(1);
59 				}
60 				continue;
61 
62 			case 'f':
63 				if (argc < 1) {
64 					printf(
65 					    "flcopy: -f: missing file name\n");
66 					exit(1);
67 				}
68 				flopname = *++argv;
69 				argc--;
70 				break;
71 
72 			case 't':
73 				if (*cp >= '0' && *cp <= '9')
74 					dsize = atoi(cp);
75 				else if (argc > 1) {
76 					dsize = atoi(*++argv);
77 					argc--;
78 				} else
79 					dsize = 77;
80 				if (dsize <= 0 || dsize > 77) {
81 					printf("Bad number of tracks\n");
82 					exit(2);
83 				}
84 				dsize *= 26 * 128;
85 				continue;
86 
87 			case 'r':
88 				rflag++;
89 			}
90 			break;
91 		}
92 	}
93 	if (!hflag) {
94 		file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
95 		if (file < 0) {
96 			printf("can't open \"floppy\"\n");
97 			exit(1);
98 		}
99 		for (count = dsize; count > 0 ; count -= 512) {
100 			n = count > 512 ? 512 : count;
101 			lread(startad, n, buff);
102 			write(file, buff, n);
103 			startad += 512;
104 		}
105 	}
106 	if (rflag)
107 		exit(0);
108 	printf("Change Floppy, Hit return when done.\n");
109 	gets(buff);
110 	lseek(file, 0, 0);
111 	count = dsize;
112 	startad = -26 * 128;
113 	for ( ; count > 0 ; count -= 512) {
114 		n = count > 512 ? 512 : count;
115 		read(file, buff, n);
116 		lwrite(startad, n, buff);
117 		startad += 512;
118 	}
119 	exit(0);
120 }
121 
122 rt_init()
123 {
124 	static initized = 0;
125 	int mode = 2;
126 
127 	if (initized)
128 		return;
129 	if (rflag)
130 		mode = 0;
131 	initized = 1;
132 	if ((floppydes = open(flopname, mode)) < 0) {
133 		printf("Floppy open failed\n");
134 		exit(1);
135 	}
136 }
137 
138 /*
139  * Logical to physical adress translation
140  */
141 long
142 trans(logical)
143 	register int logical;
144 {
145 	register int sector, bytes, track;
146 
147 	logical += 26 * 128;
148 	bytes = (logical & 127);
149 	logical >>= 7;
150 	sector = logical % 26;
151 	if (sector >= 13)
152 		sector = sector*2 +1;
153 	else
154 		sector *= 2;
155 	sector += 26 + ((track = (logical / 26)) - 1) * 6;
156 	sector %= 26;
157 	return ((((track *26) + sector) << 7) + bytes);
158 }
159 
160 lread(startad, count, obuff)
161 	register startad, count;
162 	register char *obuff;
163 {
164 	long trans();
165 	extern floppydes;
166 
167 	rt_init();
168 	while ((count -= 128) >= 0) {
169 		lseek(floppydes, trans(startad), 0);
170 		read(floppydes, obuff, 128);
171 		obuff += 128;
172 		startad += 128;
173 	}
174 }
175 
176 lwrite(startad, count, obuff)
177 	register startad, count;
178 	register char *obuff;
179 {
180 	long trans();
181 	extern floppydes;
182 
183 	rt_init();
184 	while ((count -= 128) >= 0) {
185 		lseek(floppydes, trans(startad), 0);
186 		write(floppydes, obuff, 128);
187 		obuff += 128;
188 		startad += 128;
189 	}
190 }
191