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