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