Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / wormcontrol / wormcontrol.c
1 /* 
2  * Copyright (C) 1996
3  *   interface business GmbH
4  *   Tolkewitzer Strasse 49
5  *   D-01277 Dresden
6  *   F.R. Germany
7  *
8  * All rights reserved.
9  *
10  * Written by Joerg Wunsch <joerg_wunsch@interface-business.de>
11  *
12  * 
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
28  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
29  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
33  * DAMAGE.
34  */
35
36 #ifndef lint
37 static const char rcsid[] =
38   "$FreeBSD: src/usr.sbin/wormcontrol/wormcontrol.c,v 1.11 1999/10/01 14:12:46 peter Exp $";
39 #endif /* not lint */
40
41 #include <err.h>
42 #include <fcntl.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <sysexits.h>
47 #include <unistd.h>
48
49 #include <sys/ioctl.h>
50 #include <sys/wormio.h>
51
52 static void
53 usage()
54 {
55         fprintf(stderr,"usage: wormcontrol [-f device] command [args]\n");
56         exit(EX_USAGE);
57 }
58
59 #define eq(a, b) (strcmp(a, b) == 0)
60
61 int
62 main(int argc, char **argv)
63 {
64         int fd, c, i;
65         int errs = 0;
66         const char *devname = "/dev/rworm0";
67
68         while ((c = getopt(argc, argv, "f:")) != -1)
69                 switch(c) {
70                 case 'f':
71                         devname = optarg;
72                         break;
73
74                 case '?':
75                 default:
76                         errs++;
77                 }
78         
79         argc -= optind;
80         argv += optind;
81
82         if (errs || argc < 1)
83                 usage();
84
85         if ((fd = open(devname, O_RDONLY | O_NONBLOCK, 0)) == -1)
86                 err(EX_NOINPUT, "open(%s)", devname);
87
88         if (eq(argv[0], "prepdisk")) {
89                 struct wormio_prepare_disk d;
90                 d.dummy = 0;
91                 d.speed = -1;
92                 for (i = 1; i < argc; i++) {
93                         if (eq(argv[i], "dummy"))
94                                 d.dummy = 1;
95                         else if (eq(argv[i], "single"))
96                                 d.speed = 1;
97                         else if (eq(argv[i], "double"))
98                                 d.speed = 2;
99                         else if (eq(argv[i], "quad"))
100                                 d.speed = 4;
101                         else if (eq(argv[i], "max"))
102                                 d.speed = 99;
103                         else
104                                 errx(EX_USAGE,
105                                      "wrong param for \"prepdisk\": %s",
106                                      argv[i]);
107                 }
108                 if (d.speed == -1)
109                         errx(EX_USAGE, "missing speed parameter");
110                 if (ioctl(fd, WORMIOCPREPDISK, &d) == -1)
111                         err(EX_IOERR, "ioctl(WORMIOCPREPDISK)");
112         }
113         else if (eq(argv[0], "track")) {
114                 struct wormio_prepare_track t;
115                 bzero(&t, sizeof (t));
116                 t.audio = -1;
117                 t.preemp = 0;
118                 for (i = 1; i < argc; i++) {
119                         if (eq(argv[i], "audio"))
120                                 t.audio = 1;
121                         else if (eq(argv[i], "data")) {
122                                 t.audio = 0;
123                                 t.track_type = BLOCK_MODE_1;
124                         } else if (eq(argv[i], "preemp"))
125                                 t.preemp = 1;
126                         else
127                                 errx(EX_USAGE,
128                                      "wrong param for \"track\": %s",
129                                      argv[i]);
130                 }
131                 if (t.audio == -1)
132                         errx(EX_USAGE, "missing track type parameter");
133                 if (t.audio == 0 && t.preemp == 1)
134                         errx(EX_USAGE, "\"preemp\" attempted on data track");
135                 if (ioctl(fd, WORMIOCPREPTRACK, &t) == -1)
136                         err(EX_IOERR, "ioctl(WORMIOCPREPTRACK)");
137         }
138         else if (eq(argv[0], "fixate")) {
139                 struct wormio_fixation f;
140                 f.toc_type = -1;
141                 f.onp = 0;
142                 for (i = 1; i < argc; i++) {
143                         if (eq(argv[i], "onp"))
144                                 f.onp = 1;
145                         else if (argv[i][0] >= '0' && argv[i][0] <= '4' &&
146                                  argv[i][1] == '\0')
147                                 f.toc_type = argv[i][0] - '0';
148                         else
149                                 errx(EX_USAGE,
150                                      "wrong param for \"fixate\": %s",
151                                      argv[i]);
152                 }
153                 if (f.toc_type == -1)
154                         errx(EX_USAGE, "missing TOC type parameter");
155                 if (ioctl(fd, WORMIOCFIXATION, &f) == -1)
156                         err(EX_IOERR, "ioctl(WORMIOFIXATION)");
157         }
158         else if (eq(argv[0], "blank")) {
159                 if (ioctl(fd, CDRIOCBLANK) == -1)
160                         err(EX_IOERR, "ioctl(CDRIOCBLANK)");
161         }
162         else if (eq(argv[0], "nextwriteable")) {
163                 int addr;
164                 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) == -1)
165                         err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
166                 printf("%d\n", addr);
167         }
168         else
169                 errx(EX_USAGE, "unknown command: %s", argv[0]);
170
171         return EX_OK;
172 }