]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - usr.sbin/fdcontrol/fdcontrol.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / usr.sbin / fdcontrol / fdcontrol.c
1 /*
2  * Copyright (C) 1994, 2001 by Joerg Wunsch, Dresden
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
20  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
21  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
22  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
24  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
25  * DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/fdcio.h>
32 #include <sys/file.h>
33
34 #include <err.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sysexits.h>
39 #include <unistd.h>
40
41 #include "fdutil.h"
42
43
44 static  int format, verbose, show = 1, showfmt;
45 static  char *fmtstring;
46
47 static void showdev(enum fd_drivetype, const char *);
48 static void usage(void);
49
50 static void
51 usage(void)
52 {
53         errx(EX_USAGE,
54              "usage: fdcontrol [-F] [-d dbg] [-f fmt] [-s fmtstr] [-v] device");
55 }
56
57 void
58 showdev(enum fd_drivetype type, const char *fname)
59 {
60         const char *name, *descr;
61
62         getname(type, &name, &descr);
63         if (verbose)
64                 printf("%s: %s drive (%s)\n", fname, name, descr);
65         else
66                 printf("%s\n", name);
67 }
68
69 int
70 main(int argc, char **argv)
71 {
72         enum fd_drivetype type;
73         struct fd_type ft, newft, *fdtp;
74         const char *name, *descr;
75         int fd, i, autofmt;
76
77         autofmt = 0;
78         while((i = getopt(argc, argv, "aFf:s:v")) != -1)
79                 switch(i) {
80
81                 case 'a':
82                         autofmt = 1;
83                 case 'F':
84                         showfmt = 1;
85                         show = 0;
86                         break;
87
88                 case 'f':
89                         if (!strcmp(optarg, "auto")) {
90                                 format = -1;
91                         } else if (getnum(optarg, &format)) {
92                                 fprintf(stderr,
93                         "Bad argument %s to -f option; must be numeric\n",
94                                         optarg);
95                                 usage();
96                         }
97                         show = 0;
98                         break;
99
100                 case 's':
101                         fmtstring = optarg;
102                         show = 0;
103                         break;
104
105                 case 'v':
106                         verbose++;
107                         break;
108
109                 default:
110                         usage();
111                 }
112
113         argc -= optind;
114         argv += optind;
115
116         if(argc != 1)
117                 usage();
118
119         if((fd = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
120                 err(EX_UNAVAILABLE, "open(%s)", argv[0]);
121
122         if (ioctl(fd, FD_GDTYPE, &type) == -1)
123                 err(EX_OSERR, "ioctl(FD_GDTYPE)");
124         if (ioctl(fd, FD_GTYPE, &ft) == -1)
125                 err(EX_OSERR, "ioctl(FD_GTYPE)");
126
127         if (show) {
128                 showdev(type, argv[0]);
129                 return (0);
130         }
131
132         if (autofmt) {
133                 memset(&newft, 0, sizeof newft);
134                 ft = newft;
135         }
136
137         if (format) {
138                 getname(type, &name, &descr);
139                 fdtp = get_fmt(format, type);
140                 if (fdtp == 0)
141                         errx(EX_USAGE,
142                             "unknown format %d KB for drive type %s",
143                             format, name);
144                 ft = *fdtp;
145         }
146
147         if (fmtstring) {
148                 parse_fmt(fmtstring, type, ft, &newft);
149                 ft = newft;
150         }
151
152         if (showfmt) {
153                 if (verbose) {
154                         const char *s;
155
156                         printf("%s: %d KB media type\n", argv[0],
157                             (128 << ft.secsize) * ft.size / 1024);
158                         printf("\tFormat:\t\t");
159                         print_fmt(ft);
160                         if (ft.datalen != 0xff &&
161                             ft.datalen != (128 << ft.secsize))
162                                 printf("\tData length:\t%d\n", ft.datalen);
163                         printf("\tSector size:\t%d\n", 128 << ft.secsize);
164                         printf("\tSectors/track:\t%d\n", ft.sectrac);
165                         printf("\tHeads/cylinder:\t%d\n", ft.heads);
166                         printf("\tCylinders/disk:\t%d\n", ft.tracks);
167                         switch (ft.trans) {
168                         case 0: printf("\tTransfer rate:\t500 kbps\n"); break;
169                         case 1: printf("\tTransfer rate:\t300 kbps\n"); break;
170                         case 2: printf("\tTransfer rate:\t250 kbps\n"); break;
171                         case 3: printf("\tTransfer rate:\t1 Mbps\n"); break;
172                         }
173                         printf("\tSector gap:\t%d\n", ft.gap);
174                         printf("\tFormat gap:\t%d\n", ft.f_gap);
175                         printf("\tInterleave:\t%d\n", ft.f_inter);
176                         printf("\tSide offset:\t%d\n", ft.offset_side2);
177                         printf("\tFlags\t\t<");
178                         s = "";
179                         if (ft.flags & FL_MFM) {
180                                 printf("%sMFM", s);
181                                 s = ",";
182                         }
183                         if (ft.flags & FL_2STEP) {
184                                 printf("%s2STEP", s);
185                                 s = ",";
186                         }
187                         if (ft.flags & FL_PERPND) {
188                                 printf("%sPERPENDICULAR", s);
189                                 s = ",";
190                         }
191                         if (ft.flags & FL_AUTO) {
192                                 printf("%sAUTO", s);
193                                 s = ",";
194                         }
195                         printf(">\n");
196                 } else {
197                         print_fmt(ft);
198                 }
199                 return (0);
200         }
201
202         if (format || fmtstring) {
203                 if (ioctl(fd, FD_STYPE, &ft) == -1)
204                         err(EX_OSERR, "ioctl(FD_STYPE)");
205                 return (0);
206         }
207
208         return 0;
209 }