Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / fdcontrol / fdcontrol.c
1 /*
2  * Copyright (C) 1994 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 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD: src/usr.sbin/fdcontrol/fdcontrol.c,v 1.6 1999/08/28 01:16:13 peter Exp $";
31 #endif /* not lint */
32
33 #include <err.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <machine/ioctl_fd.h>
38 #include <sys/file.h>
39
40 int
41 getnumber(void)
42 {
43   int i;
44   char b[80];
45
46   fgets(b, 80, stdin);
47   if(b[0] == '\n') return -1;
48
49   sscanf(b, " %i", &i);
50   return i;
51 }
52
53 void
54 usage(void)
55 {
56   fprintf(stderr, "usage: fdcontrol [-d 0|1] | [-s] device-node\n");
57   exit(2);
58 }
59
60
61 #define ask(name, fmt) \
62 printf(#name "? [" fmt "]: ", ft.name); fflush(stdout);   \
63 if((i = getnumber()) != -1) ft.name = i
64
65 int
66 main(int argc, char **argv)
67 {
68   struct fd_type ft;
69   int fd, i;
70   int debug = -1, settype = 1;
71
72   while((i = getopt(argc, argv, "d:s")) != -1)
73     switch(i)
74       {
75       case 'd':
76         debug = atoi(optarg);
77         settype = 0;
78         break;
79
80       case 's':
81         debug = -1;
82         settype = 1;
83         break;
84
85       case '?':
86       default:
87         usage();
88       }
89
90   argc -= optind;
91   argv += optind;
92
93   if(argc != 1)
94     usage();
95
96   if((fd = open(argv[0], 0)) < 0)
97     {
98       warn("open(floppy)");
99       return 1;
100     }
101
102   if(debug != -1)
103     {
104       if(ioctl(fd, FD_DEBUG, &debug) < 0)
105         {
106           warn("ioctl(FD_DEBUG)");
107           return 1;
108         }
109       return 0;
110     }
111
112   if(settype)
113     {
114       if(ioctl(fd, FD_GTYPE, &ft) < 0)
115         {
116           warn("ioctl(FD_GTYPE)");
117           return 1;
118         }
119
120       ask(sectrac, "%d");
121       ask(secsize, "%d");
122       ask(datalen, "0x%x");
123       ask(gap, "0x%x");
124       ask(tracks, "%d");
125       ask(size, "%d");
126       ask(steptrac, "%d");
127       ask(trans, "%d");
128       ask(heads, "%d");
129       ask(f_gap, "0x%x");
130       ask(f_inter, "%d");
131
132       if(ioctl(fd, FD_STYPE, &ft) < 0)
133         {
134           warn("ioctl(FD_STYPE)");
135           return 1;
136         }
137       return 0;
138     }
139
140   return 0;
141 }