Commit | Line | Data |
---|---|---|
04972d3c KM |
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 | ||
9397a91f | 13 | #ifndef lint |
1189369a | 14 | static char sccsid[] = "@(#)flcopy.c 5.2 (Berkeley) %G%"; |
04972d3c | 15 | #endif not lint |
9397a91f SL |
16 | |
17 | #include <sys/file.h> | |
f7e35322 | 18 | |
fb60a39a SL |
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; | |
f7e35322 BJ |
27 | { |
28 | static char buff[512]; | |
58bb1161 BJ |
29 | register long count; |
30 | register startad = -26 * 128; | |
fb60a39a SL |
31 | register int n, file; |
32 | register char *cp; | |
33 | ||
34 | while ((cp = *++argv), --argc > 0) { | |
9397a91f | 35 | while (*cp) { |
fb60a39a SL |
36 | switch(*cp++) { |
37 | ||
9397a91f SL |
38 | case '-': |
39 | continue; | |
40 | ||
fb60a39a SL |
41 | case 'h': |
42 | hflag++; | |
43 | printf("Halftime!\n"); | |
9397a91f SL |
44 | if ((file = open("floppy", 0)) < 0) { |
45 | printf("can't open \"floppy\"\n"); | |
46 | exit(1); | |
47 | } | |
fb60a39a | 48 | continue; |
f7e35322 | 49 | |
9397a91f SL |
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 | ||
fb60a39a SL |
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 | } | |
9397a91f SL |
78 | break; |
79 | } | |
f7e35322 | 80 | } |
fb60a39a | 81 | if (!hflag) { |
9397a91f | 82 | file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666); |
fb60a39a SL |
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 | } | |
f7e35322 | 93 | } |
fb60a39a SL |
94 | if (rflag) |
95 | exit(0); | |
f7e35322 BJ |
96 | printf("Change Floppy, Hit return when done.\n"); |
97 | gets(buff); | |
fb60a39a SL |
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); | |
f7e35322 BJ |
105 | startad += 512; |
106 | } | |
a7cb025c | 107 | exit(0); |
f7e35322 | 108 | } |
fb60a39a | 109 | |
f7e35322 BJ |
110 | rt_init() |
111 | { | |
112 | static initized = 0; | |
113 | int mode = 2; | |
114 | ||
fb60a39a SL |
115 | if (initized) |
116 | return; | |
117 | if (rflag) | |
118 | mode = 0; | |
f7e35322 | 119 | initized = 1; |
fb60a39a | 120 | if ((floppydes = open(flopname, mode)) < 0) { |
f7e35322 BJ |
121 | printf("Floppy open failed\n"); |
122 | exit(1); | |
123 | } | |
124 | } | |
fb60a39a SL |
125 | |
126 | /* | |
127 | * Logical to physical adress translation | |
128 | */ | |
129 | long | |
130 | trans(logical) | |
131 | register int logical; | |
f7e35322 | 132 | { |
f7e35322 BJ |
133 | register int sector, bytes, track; |
134 | ||
135 | logical += 26 * 128; | |
136 | bytes = (logical & 127); | |
137 | logical >>= 7; | |
138 | sector = logical % 26; | |
fb60a39a SL |
139 | if (sector >= 13) |
140 | sector = sector*2 +1; | |
f7e35322 BJ |
141 | else |
142 | sector *= 2; | |
143 | sector += 26 + ((track = (logical / 26)) - 1) * 6; | |
144 | sector %= 26; | |
fb60a39a | 145 | return ((((track *26) + sector) << 7) + bytes); |
f7e35322 | 146 | } |
fb60a39a SL |
147 | |
148 | lread(startad, count, obuff) | |
149 | register startad, count; | |
150 | register char *obuff; | |
f7e35322 BJ |
151 | { |
152 | long trans(); | |
153 | extern floppydes; | |
fb60a39a | 154 | |
f7e35322 | 155 | rt_init(); |
fb60a39a SL |
156 | while ((count -= 128) >= 0) { |
157 | lseek(floppydes, trans(startad), 0); | |
158 | read(floppydes, obuff, 128); | |
159 | obuff += 128; | |
160 | startad += 128; | |
161 | } | |
f7e35322 | 162 | } |
fb60a39a SL |
163 | |
164 | lwrite(startad, count, obuff) | |
165 | register startad, count; | |
166 | register char *obuff; | |
f7e35322 BJ |
167 | { |
168 | long trans(); | |
169 | extern floppydes; | |
fb60a39a | 170 | |
f7e35322 | 171 | rt_init(); |
fb60a39a SL |
172 | while ((count -= 128) >= 0) { |
173 | lseek(floppydes, trans(startad), 0); | |
174 | write(floppydes, obuff, 128); | |
175 | obuff += 128; | |
176 | startad += 128; | |
177 | } | |
f7e35322 | 178 | } |