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 debug = -1, 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, mode; 76 77 while((i = getopt(argc, argv, "d:Ff:s:v")) != -1) 78 switch(i) { 79 case 'd': 80 if (strcmp(optarg, "0") == 0) 81 debug = 0; 82 else if (strcmp(optarg, "1") == 0) 83 debug = 1; 84 else 85 usage(); 86 show = 0; 87 break; 88 89 case 'F': 90 showfmt = 1; 91 show = 0; 92 break; 93 94 case 'f': 95 if (getnum(optarg, &format)) { 96 fprintf(stderr, 97 "Bad argument %s to -f option; must be numeric\n", 98 optarg); 99 usage(); 100 } 101 show = 0; 102 break; 103 104 case 's': 105 fmtstring = optarg; 106 show = 0; 107 break; 108 109 case 'v': 110 verbose++; 111 break; 112 113 default: 114 usage(); 115 } 116 117 argc -= optind; 118 argv += optind; 119 120 if(argc != 1) 121 usage(); 122 123 if (show || showfmt) 124 mode = O_RDONLY | O_NONBLOCK; 125 else 126 mode = O_RDWR; 127 128 if((fd = open(argv[0], mode)) < 0) 129 err(EX_UNAVAILABLE, "open(%s)", argv[0]); 130 131 if (ioctl(fd, FD_GDTYPE, &type) == -1) 132 err(EX_OSERR, "ioctl(FD_GDTYPE)"); 133 if (ioctl(fd, FD_GTYPE, &ft) == -1) 134 err(EX_OSERR, "ioctl(FD_GTYPE)"); 135 136 if (show) { 137 showdev(type, argv[0]); 138 return (0); 139 } 140 141 if (format) { 142 getname(type, &name, &descr); 143 fdtp = get_fmt(format, type); 144 if (fdtp == 0) 145 errx(EX_USAGE, 146 "unknown format %d KB for drive type %s", 147 format, name); 148 ft = *fdtp; 149 } 150 151 if (fmtstring) { 152 parse_fmt(fmtstring, type, ft, &newft); 153 ft = newft; 154 } 155 156 if (showfmt) { 157 if (verbose) 158 printf("%s: %d KB media type, fmt = ", 159 argv[0], ft.size / 2); 160 print_fmt(ft); 161 return (0); 162 } 163 164 if (format || fmtstring) { 165 if (ioctl(fd, FD_STYPE, &ft) == -1) 166 err(EX_OSERR, "ioctl(FD_STYPE)"); 167 return (0); 168 } 169 170 if (debug != -1) { 171 if (ioctl(fd, FD_DEBUG, &debug) == -1) 172 err(EX_OSERR, "ioctl(FD_DEBUG)"); 173 return (0); 174 } 175 176 return 0; 177 } 178