new pathnames (getty, utmp, and wtmp)
[unix-history] / usr / src / old / flcopy / flcopy.c
CommitLineData
04972d3c 1/*
7abf8d65
KB
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
32ce521f 5 * %sccs.include.redist.c%
04972d3c
KM
6 */
7
8#ifndef lint
9char copyright[] =
7abf8d65 10"@(#) Copyright (c) 1989 The Regents of the University of California.\n\
04972d3c 11 All rights reserved.\n";
7abf8d65 12#endif /* not lint */
04972d3c 13
9397a91f 14#ifndef lint
32ce521f 15static char sccsid[] = "@(#)flcopy.c 5.3 (Berkeley) %G%";
7abf8d65 16#endif /* not lint */
9397a91f
SL
17
18#include <sys/file.h>
7abf8d65 19#include "pathnames.h"
f7e35322 20
fb60a39a 21int floppydes;
7abf8d65 22char *flopname = _PATH_FLOPPY;
fb60a39a
SL
23long dsize = 77 * 26 * 128;
24int hflag;
25int rflag;
26
27main(argc, argv)
28 register char **argv;
f7e35322
BJ
29{
30 static char buff[512];
58bb1161
BJ
31 register long count;
32 register startad = -26 * 128;
fb60a39a
SL
33 register int n, file;
34 register char *cp;
35
36 while ((cp = *++argv), --argc > 0) {
9397a91f 37 while (*cp) {
fb60a39a
SL
38 switch(*cp++) {
39
9397a91f
SL
40 case '-':
41 continue;
42
fb60a39a
SL
43 case 'h':
44 hflag++;
45 printf("Halftime!\n");
9397a91f
SL
46 if ((file = open("floppy", 0)) < 0) {
47 printf("can't open \"floppy\"\n");
48 exit(1);
49 }
fb60a39a 50 continue;
f7e35322 51
9397a91f
SL
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
fb60a39a
SL
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 }
9397a91f
SL
80 break;
81 }
f7e35322 82 }
fb60a39a 83 if (!hflag) {
9397a91f 84 file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666);
fb60a39a
SL
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 }
f7e35322 95 }
fb60a39a
SL
96 if (rflag)
97 exit(0);
f7e35322
BJ
98 printf("Change Floppy, Hit return when done.\n");
99 gets(buff);
fb60a39a
SL
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);
f7e35322
BJ
107 startad += 512;
108 }
a7cb025c 109 exit(0);
f7e35322 110}
fb60a39a 111
f7e35322
BJ
112rt_init()
113{
114 static initized = 0;
115 int mode = 2;
116
fb60a39a
SL
117 if (initized)
118 return;
119 if (rflag)
120 mode = 0;
f7e35322 121 initized = 1;
fb60a39a 122 if ((floppydes = open(flopname, mode)) < 0) {
f7e35322
BJ
123 printf("Floppy open failed\n");
124 exit(1);
125 }
126}
fb60a39a
SL
127
128/*
129 * Logical to physical adress translation
130 */
131long
132trans(logical)
133 register int logical;
f7e35322 134{
f7e35322
BJ
135 register int sector, bytes, track;
136
137 logical += 26 * 128;
138 bytes = (logical & 127);
139 logical >>= 7;
140 sector = logical % 26;
fb60a39a
SL
141 if (sector >= 13)
142 sector = sector*2 +1;
f7e35322
BJ
143 else
144 sector *= 2;
145 sector += 26 + ((track = (logical / 26)) - 1) * 6;
146 sector %= 26;
fb60a39a 147 return ((((track *26) + sector) << 7) + bytes);
f7e35322 148}
fb60a39a
SL
149
150lread(startad, count, obuff)
151 register startad, count;
152 register char *obuff;
f7e35322
BJ
153{
154 long trans();
155 extern floppydes;
fb60a39a 156
f7e35322 157 rt_init();
fb60a39a
SL
158 while ((count -= 128) >= 0) {
159 lseek(floppydes, trans(startad), 0);
160 read(floppydes, obuff, 128);
161 obuff += 128;
162 startad += 128;
163 }
f7e35322 164}
fb60a39a
SL
165
166lwrite(startad, count, obuff)
167 register startad, count;
168 register char *obuff;
f7e35322
BJ
169{
170 long trans();
171 extern floppydes;
fb60a39a 172
f7e35322 173 rt_init();
fb60a39a
SL
174 while ((count -= 128) >= 0) {
175 lseek(floppydes, trans(startad), 0);
176 write(floppydes, obuff, 128);
177 obuff += 128;
178 startad += 128;
179 }
f7e35322 180}