fdcontrol.c revision 114601
14007Sjoerg/*
287992Sjoerg * Copyright (C) 1994, 2001 by Joerg Wunsch, Dresden
34007Sjoerg * All rights reserved.
44007Sjoerg *
54007Sjoerg * Redistribution and use in source and binary forms, with or without
64007Sjoerg * modification, are permitted provided that the following conditions
74007Sjoerg * are met:
84007Sjoerg * 1. Redistributions of source code must retain the above copyright
94007Sjoerg *    notice, this list of conditions and the following disclaimer.
104007Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
114007Sjoerg *    notice, this list of conditions and the following disclaimer in the
124007Sjoerg *    documentation and/or other materials provided with the distribution.
134007Sjoerg *
144007Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY
154007Sjoerg * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
164007Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
174007Sjoerg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE
184007Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
194007Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
204007Sjoerg * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
214007Sjoerg * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
224007Sjoerg * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
234007Sjoerg * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
244007Sjoerg * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
254007Sjoerg * DAMAGE.
264007Sjoerg */
274007Sjoerg
28114601Sobrien#include <sys/cdefs.h>
29114601Sobrien__FBSDID("$FreeBSD: head/usr.sbin/fdcontrol/fdcontrol.c 114601 2003-05-03 21:06:42Z obrien $");
3029530Scharnier
3178855Sdd#include <sys/fdcio.h>
3278855Sdd#include <sys/file.h>
3378855Sdd
3429530Scharnier#include <err.h>
354007Sjoerg#include <stdio.h>
364007Sjoerg#include <stdlib.h>
3787992Sjoerg#include <string.h>
3887992Sjoerg#include <sysexits.h>
3923716Speter#include <unistd.h>
404007Sjoerg
4187992Sjoerg#include "fdutil.h"
4278855Sdd
438857Srgrimes
4487992Sjoergstatic	int debug = -1, format, verbose, show = 1, showfmt;
4587992Sjoergstatic	char *fmtstring;
464007Sjoerg
4787992Sjoergstatic void showdev(enum fd_drivetype, const char *);
4887992Sjoergstatic void usage(void);
494007Sjoerg
5078855Sddstatic void
514007Sjoergusage(void)
524007Sjoerg{
5387992Sjoerg	errx(EX_USAGE,
5487992Sjoerg	     "usage: fdcontrol [-F] [-d dbg] [-f fmt] [-s fmtstr] [-v] device");
554007Sjoerg}
564007Sjoerg
5787992Sjoergvoid
5887992Sjoergshowdev(enum fd_drivetype type, const char *fname)
5987992Sjoerg{
6087992Sjoerg	const char *name, *descr;
614007Sjoerg
6287992Sjoerg	getname(type, &name, &descr);
6387992Sjoerg	if (verbose)
6487992Sjoerg		printf("%s: %s drive (%s)\n", fname, name, descr);
6587992Sjoerg	else
6687992Sjoerg		printf("%s\n", name);
6787992Sjoerg}
684007Sjoerg
694007Sjoergint
704007Sjoergmain(int argc, char **argv)
714007Sjoerg{
7287992Sjoerg	enum fd_drivetype type;
7387992Sjoerg	struct fd_type ft, newft, *fdtp;
7487992Sjoerg	const char *name, *descr;
7587992Sjoerg	int fd, i, mode;
764007Sjoerg
7787992Sjoerg	while((i = getopt(argc, argv, "d:Ff:s:v")) != -1)
7887992Sjoerg		switch(i) {
7987992Sjoerg		case 'd':
8087992Sjoerg			if (strcmp(optarg, "0") == 0)
8187992Sjoerg				debug = 0;
8287992Sjoerg			else if (strcmp(optarg, "1") == 0)
8387992Sjoerg				debug = 1;
8487992Sjoerg			else
8587992Sjoerg				usage();
8687992Sjoerg			show = 0;
8787992Sjoerg			break;
888857Srgrimes
8987992Sjoerg		case 'F':
9087992Sjoerg			showfmt = 1;
9187992Sjoerg			show = 0;
9287992Sjoerg			break;
938857Srgrimes
9487992Sjoerg		case 'f':
9587992Sjoerg			if (getnum(optarg, &format)) {
9687992Sjoerg				fprintf(stderr,
9787992Sjoerg			"Bad argument %s to -f option; must be numeric\n",
9887992Sjoerg					optarg);
9987992Sjoerg				usage();
10087992Sjoerg			}
10187992Sjoerg			show = 0;
10287992Sjoerg			break;
1038857Srgrimes
10487992Sjoerg		case 's':
10587992Sjoerg			fmtstring = optarg;
10687992Sjoerg			show = 0;
10787992Sjoerg			break;
1084007Sjoerg
10987992Sjoerg		case 'v':
11087992Sjoerg			verbose++;
11187992Sjoerg			break;
1128857Srgrimes
11387992Sjoerg		default:
11487992Sjoerg			usage();
11587992Sjoerg		}
1164007Sjoerg
11787992Sjoerg	argc -= optind;
11887992Sjoerg	argv += optind;
11987992Sjoerg
12087992Sjoerg	if(argc != 1)
12187992Sjoerg		usage();
12287992Sjoerg
12387992Sjoerg	if (show || showfmt)
12487992Sjoerg		mode = O_RDONLY | O_NONBLOCK;
12587992Sjoerg	else
12687992Sjoerg		mode = O_RDWR;
12787992Sjoerg
12887992Sjoerg	if((fd = open(argv[0], mode)) < 0)
12987992Sjoerg		err(EX_UNAVAILABLE, "open(%s)", argv[0]);
13087992Sjoerg
13187992Sjoerg	if (ioctl(fd, FD_GDTYPE, &type) == -1)
13287992Sjoerg		err(EX_OSERR, "ioctl(FD_GDTYPE)");
13387992Sjoerg	if (ioctl(fd, FD_GTYPE, &ft) == -1)
13487992Sjoerg		err(EX_OSERR, "ioctl(FD_GTYPE)");
13587992Sjoerg
13687992Sjoerg	if (show) {
13787992Sjoerg		showdev(type, argv[0]);
13887992Sjoerg		return (0);
1394007Sjoerg	}
1408857Srgrimes
14187992Sjoerg	if (format) {
14287992Sjoerg		getname(type, &name, &descr);
14387992Sjoerg		fdtp = get_fmt(format, type);
14487992Sjoerg		if (fdtp == 0)
14587992Sjoerg			errx(EX_USAGE,
14687992Sjoerg			    "unknown format %d KB for drive type %s",
14787992Sjoerg			    format, name);
14887992Sjoerg		ft = *fdtp;
1494007Sjoerg	}
1504007Sjoerg
15187992Sjoerg	if (fmtstring) {
15287992Sjoerg		parse_fmt(fmtstring, type, ft, &newft);
15387992Sjoerg		ft = newft;
15487992Sjoerg	}
1554007Sjoerg
15687992Sjoerg	if (showfmt) {
15787992Sjoerg		if (verbose)
15887992Sjoerg			printf("%s: %d KB media type, fmt = ",
15987992Sjoerg			       argv[0], ft.size / 2);
16087992Sjoerg		print_fmt(ft);
16187992Sjoerg		return (0);
1624007Sjoerg	}
1638857Srgrimes
16487992Sjoerg	if (format || fmtstring) {
16587992Sjoerg		if (ioctl(fd, FD_STYPE, &ft) == -1)
16687992Sjoerg			err(EX_OSERR, "ioctl(FD_STYPE)");
16787992Sjoerg		return (0);
16887992Sjoerg	}
16987992Sjoerg
17087992Sjoerg	if (debug != -1) {
17187992Sjoerg		if (ioctl(fd, FD_DEBUG, &debug) == -1)
17287992Sjoerg			err(EX_OSERR, "ioctl(FD_DEBUG)");
17387992Sjoerg		return (0);
17487992Sjoerg	}
17587992Sjoerg
17687992Sjoerg	return 0;
1774007Sjoerg}
178