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