xref: /freebsd/usr.sbin/fdcontrol/fdcontrol.c (revision b52b9d56d4e96089873a75f9e29062eec19fabba)
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 #ifndef lint
29 static const char rcsid[] =
30   "$FreeBSD$";
31 #endif /* not lint */
32 
33 #include <sys/fdcio.h>
34 #include <sys/file.h>
35 
36 #include <err.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <sysexits.h>
41 #include <unistd.h>
42 
43 #include "fdutil.h"
44 
45 
46 static	int debug = -1, format, verbose, show = 1, showfmt;
47 static	char *fmtstring;
48 
49 static void showdev(enum fd_drivetype, const char *);
50 static void usage(void);
51 
52 static void
53 usage(void)
54 {
55 	errx(EX_USAGE,
56 	     "usage: fdcontrol [-F] [-d dbg] [-f fmt] [-s fmtstr] [-v] device");
57 }
58 
59 void
60 showdev(enum fd_drivetype type, const char *fname)
61 {
62 	const char *name, *descr;
63 
64 	getname(type, &name, &descr);
65 	if (verbose)
66 		printf("%s: %s drive (%s)\n", fname, name, descr);
67 	else
68 		printf("%s\n", name);
69 }
70 
71 int
72 main(int argc, char **argv)
73 {
74 	enum fd_drivetype type;
75 	struct fd_type ft, newft, *fdtp;
76 	const char *name, *descr;
77 	int fd, i, mode;
78 
79 	while((i = getopt(argc, argv, "d:Ff:s:v")) != -1)
80 		switch(i) {
81 		case 'd':
82 			if (strcmp(optarg, "0") == 0)
83 				debug = 0;
84 			else if (strcmp(optarg, "1") == 0)
85 				debug = 1;
86 			else
87 				usage();
88 			show = 0;
89 			break;
90 
91 		case 'F':
92 			showfmt = 1;
93 			show = 0;
94 			break;
95 
96 		case 'f':
97 			if (getnum(optarg, &format)) {
98 				fprintf(stderr,
99 			"Bad argument %s to -f option; must be numeric\n",
100 					optarg);
101 				usage();
102 			}
103 			show = 0;
104 			break;
105 
106 		case 's':
107 			fmtstring = optarg;
108 			show = 0;
109 			break;
110 
111 		case 'v':
112 			verbose++;
113 			break;
114 
115 		default:
116 			usage();
117 		}
118 
119 	argc -= optind;
120 	argv += optind;
121 
122 	if(argc != 1)
123 		usage();
124 
125 	if (show || showfmt)
126 		mode = O_RDONLY | O_NONBLOCK;
127 	else
128 		mode = O_RDWR;
129 
130 	if((fd = open(argv[0], mode)) < 0)
131 		err(EX_UNAVAILABLE, "open(%s)", argv[0]);
132 
133 	if (ioctl(fd, FD_GDTYPE, &type) == -1)
134 		err(EX_OSERR, "ioctl(FD_GDTYPE)");
135 	if (ioctl(fd, FD_GTYPE, &ft) == -1)
136 		err(EX_OSERR, "ioctl(FD_GTYPE)");
137 
138 	if (show) {
139 		showdev(type, argv[0]);
140 		return (0);
141 	}
142 
143 	if (format) {
144 		getname(type, &name, &descr);
145 		fdtp = get_fmt(format, type);
146 		if (fdtp == 0)
147 			errx(EX_USAGE,
148 			    "unknown format %d KB for drive type %s",
149 			    format, name);
150 		ft = *fdtp;
151 	}
152 
153 	if (fmtstring) {
154 		parse_fmt(fmtstring, type, ft, &newft);
155 		ft = newft;
156 	}
157 
158 	if (showfmt) {
159 		if (verbose)
160 			printf("%s: %d KB media type, fmt = ",
161 			       argv[0], ft.size / 2);
162 		print_fmt(ft);
163 		return (0);
164 	}
165 
166 	if (format || fmtstring) {
167 		if (ioctl(fd, FD_STYPE, &ft) == -1)
168 			err(EX_OSERR, "ioctl(FD_STYPE)");
169 		return (0);
170 	}
171 
172 	if (debug != -1) {
173 		if (ioctl(fd, FD_DEBUG, &debug) == -1)
174 			err(EX_OSERR, "ioctl(FD_DEBUG)");
175 		return (0);
176 	}
177 
178 	return 0;
179 }
180