| 1 | /* |
| 2 | * Copyright (c) 1989 The Regents of the University of California. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * 3. All advertising materials mentioning features or use of this software |
| 14 | * must display the following acknowledgement: |
| 15 | * This product includes software developed by the University of |
| 16 | * California, Berkeley and its contributors. |
| 17 | * 4. Neither the name of the University nor the names of its contributors |
| 18 | * may be used to endorse or promote products derived from this software |
| 19 | * without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 31 | * SUCH DAMAGE. |
| 32 | */ |
| 33 | |
| 34 | #ifndef lint |
| 35 | char copyright[] = |
| 36 | "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ |
| 37 | All rights reserved.\n"; |
| 38 | #endif /* not lint */ |
| 39 | |
| 40 | #ifndef lint |
| 41 | static char sccsid[] = "@(#)flcopy.c 5.4 (Berkeley) 1/20/91"; |
| 42 | #endif /* not lint */ |
| 43 | |
| 44 | #include <sys/file.h> |
| 45 | #include <stdio.h> |
| 46 | #include "pathnames.h" |
| 47 | |
| 48 | int floppydes; |
| 49 | char *flopname = _PATH_FLOPPY; |
| 50 | long dsize = 77 * 26 * 128; |
| 51 | int hflag; |
| 52 | int rflag; |
| 53 | |
| 54 | main(argc, argv) |
| 55 | register char **argv; |
| 56 | { |
| 57 | extern char *optarg; |
| 58 | extern int optind; |
| 59 | static char buff[512]; |
| 60 | register long count; |
| 61 | register startad = -26 * 128; |
| 62 | register int n, file; |
| 63 | register char *cp; |
| 64 | int ch; |
| 65 | |
| 66 | while ((ch = getopt(argc, argv, "f:hrt:")) != EOF) |
| 67 | switch(ch) { |
| 68 | case 'f': |
| 69 | flopname = optarg; |
| 70 | break; |
| 71 | case 'h': |
| 72 | hflag = 1; |
| 73 | printf("Halftime!\n"); |
| 74 | if ((file = open("floppy", 0)) < 0) { |
| 75 | printf("can't open \"floppy\"\n"); |
| 76 | exit(1); |
| 77 | } |
| 78 | break; |
| 79 | case 'r': |
| 80 | rflag = 1; |
| 81 | break; |
| 82 | case 't': |
| 83 | dsize = atoi(optarg); |
| 84 | if (dsize <= 0 || dsize > 77) { |
| 85 | (void)fprintf(stderr, |
| 86 | "flcopy: bad number of tracks (0 - 77).\n"); |
| 87 | exit(2); |
| 88 | } |
| 89 | dsize *= 26 * 128; |
| 90 | break; |
| 91 | case '?': |
| 92 | default: |
| 93 | usage(); |
| 94 | } |
| 95 | argc -= optind; |
| 96 | argv += optind; |
| 97 | |
| 98 | if (!hflag) { |
| 99 | file = open("floppy", O_RDWR|O_CREAT|O_TRUNC, 0666); |
| 100 | if (file < 0) { |
| 101 | printf("can't open \"floppy\"\n"); |
| 102 | exit(1); |
| 103 | } |
| 104 | for (count = dsize; count > 0 ; count -= 512) { |
| 105 | n = count > 512 ? 512 : count; |
| 106 | lread(startad, n, buff); |
| 107 | write(file, buff, n); |
| 108 | startad += 512; |
| 109 | } |
| 110 | } |
| 111 | if (rflag) |
| 112 | exit(0); |
| 113 | printf("Change Floppy, Hit return when done.\n"); |
| 114 | gets(buff); |
| 115 | lseek(file, 0, 0); |
| 116 | count = dsize; |
| 117 | startad = -26 * 128; |
| 118 | for ( ; count > 0 ; count -= 512) { |
| 119 | n = count > 512 ? 512 : count; |
| 120 | read(file, buff, n); |
| 121 | lwrite(startad, n, buff); |
| 122 | startad += 512; |
| 123 | } |
| 124 | exit(0); |
| 125 | } |
| 126 | |
| 127 | rt_init() |
| 128 | { |
| 129 | static initized = 0; |
| 130 | int mode = 2; |
| 131 | |
| 132 | if (initized) |
| 133 | return; |
| 134 | if (rflag) |
| 135 | mode = 0; |
| 136 | initized = 1; |
| 137 | if ((floppydes = open(flopname, mode)) < 0) { |
| 138 | printf("Floppy open failed\n"); |
| 139 | exit(1); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Logical to physical adress translation |
| 145 | */ |
| 146 | long |
| 147 | trans(logical) |
| 148 | register int logical; |
| 149 | { |
| 150 | register int sector, bytes, track; |
| 151 | |
| 152 | logical += 26 * 128; |
| 153 | bytes = (logical & 127); |
| 154 | logical >>= 7; |
| 155 | sector = logical % 26; |
| 156 | if (sector >= 13) |
| 157 | sector = sector*2 +1; |
| 158 | else |
| 159 | sector *= 2; |
| 160 | sector += 26 + ((track = (logical / 26)) - 1) * 6; |
| 161 | sector %= 26; |
| 162 | return ((((track *26) + sector) << 7) + bytes); |
| 163 | } |
| 164 | |
| 165 | lread(startad, count, obuff) |
| 166 | register startad, count; |
| 167 | register char *obuff; |
| 168 | { |
| 169 | long trans(); |
| 170 | extern floppydes; |
| 171 | |
| 172 | rt_init(); |
| 173 | while ((count -= 128) >= 0) { |
| 174 | lseek(floppydes, trans(startad), 0); |
| 175 | read(floppydes, obuff, 128); |
| 176 | obuff += 128; |
| 177 | startad += 128; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | lwrite(startad, count, obuff) |
| 182 | register startad, count; |
| 183 | register char *obuff; |
| 184 | { |
| 185 | long trans(); |
| 186 | extern floppydes; |
| 187 | |
| 188 | rt_init(); |
| 189 | while ((count -= 128) >= 0) { |
| 190 | lseek(floppydes, trans(startad), 0); |
| 191 | write(floppydes, obuff, 128); |
| 192 | obuff += 128; |
| 193 | startad += 128; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | usage() |
| 198 | { |
| 199 | (void)fprintf(stderr, "usage: flcopy [-hr] [-f file] [-t ntracks]\n"); |
| 200 | exit(1); |
| 201 | } |