Merge from vendor branch NTPD:
[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  * $FreeBSD: src/usr.sbin/wormcontrol/wormcontrol.c,v 1.11 1999/10/01 14:12:46 peter Exp $
36  * $DragonFly: src/usr.sbin/wormcontrol/Attic/wormcontrol.c,v 1.2 2003/06/17 04:30:04 dillon Exp $
37  */
38
39 #include <err.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <sysexits.h>
45 #include <unistd.h>
46
47 #include <sys/ioctl.h>
48 #include <sys/wormio.h>
49
50 static void
51 usage()
52 {
53         fprintf(stderr,"usage: wormcontrol [-f device] command [args]\n");
54         exit(EX_USAGE);
55 }
56
57 #define eq(a, b) (strcmp(a, b) == 0)
58
59 int
60 main(int argc, char **argv)
61 {
62         int fd, c, i;
63         int errs = 0;
64         const char *devname = "/dev/rworm0";
65
66         while ((c = getopt(argc, argv, "f:")) != -1)
67                 switch(c) {
68                 case 'f':
69                         devname = optarg;
70                         break;
71
72                 case '?':
73                 default:
74                         errs++;
75                 }
76         
77         argc -= optind;
78         argv += optind;
79
80         if (errs || argc < 1)
81                 usage();
82
83         if ((fd = open(devname, O_RDONLY | O_NONBLOCK, 0)) == -1)
84                 err(EX_NOINPUT, "open(%s)", devname);
85
86         if (eq(argv[0], "prepdisk")) {
87                 struct wormio_prepare_disk d;
88                 d.dummy = 0;
89                 d.speed = -1;
90                 for (i = 1; i < argc; i++) {
91                         if (eq(argv[i], "dummy"))
92                                 d.dummy = 1;
93                         else if (eq(argv[i], "single"))
94                                 d.speed = 1;
95                         else if (eq(argv[i], "double"))
96                                 d.speed = 2;
97                         else if (eq(argv[i], "quad"))
98                                 d.speed = 4;
99                         else if (eq(argv[i], "max"))
100                                 d.speed = 99;
101                         else
102                                 errx(EX_USAGE,
103                                      "wrong param for \"prepdisk\": %s",
104                                      argv[i]);
105                 }
106                 if (d.speed == -1)
107                         errx(EX_USAGE, "missing speed parameter");
108                 if (ioctl(fd, WORMIOCPREPDISK, &d) == -1)
109                         err(EX_IOERR, "ioctl(WORMIOCPREPDISK)");
110         }
111         else if (eq(argv[0], "track")) {
112                 struct wormio_prepare_track t;
113                 bzero(&t, sizeof (t));
114                 t.audio = -1;
115                 t.preemp = 0;
116                 for (i = 1; i < argc; i++) {
117                         if (eq(argv[i], "audio"))
118                                 t.audio = 1;
119                         else if (eq(argv[i], "data")) {
120                                 t.audio = 0;
121                                 t.track_type = BLOCK_MODE_1;
122                         } else if (eq(argv[i], "preemp"))
123                                 t.preemp = 1;
124                         else
125                                 errx(EX_USAGE,
126                                      "wrong param for \"track\": %s",
127                                      argv[i]);
128                 }
129                 if (t.audio == -1)
130                         errx(EX_USAGE, "missing track type parameter");
131                 if (t.audio == 0 && t.preemp == 1)
132                         errx(EX_USAGE, "\"preemp\" attempted on data track");
133                 if (ioctl(fd, WORMIOCPREPTRACK, &t) == -1)
134                         err(EX_IOERR, "ioctl(WORMIOCPREPTRACK)");
135         }
136         else if (eq(argv[0], "fixate")) {
137                 struct wormio_fixation f;
138                 f.toc_type = -1;
139                 f.onp = 0;
140                 for (i = 1; i < argc; i++) {
141                         if (eq(argv[i], "onp"))
142                                 f.onp = 1;
143                         else if (argv[i][0] >= '0' && argv[i][0] <= '4' &&
144                                  argv[i][1] == '\0')
145                                 f.toc_type = argv[i][0] - '0';
146                         else
147                                 errx(EX_USAGE,
148                                      "wrong param for \"fixate\": %s",
149                                      argv[i]);
150                 }
151                 if (f.toc_type == -1)
152                         errx(EX_USAGE, "missing TOC type parameter");
153                 if (ioctl(fd, WORMIOCFIXATION, &f) == -1)
154                         err(EX_IOERR, "ioctl(WORMIOFIXATION)");
155         }
156         else if (eq(argv[0], "blank")) {
157                 if (ioctl(fd, CDRIOCBLANK) == -1)
158                         err(EX_IOERR, "ioctl(CDRIOCBLANK)");
159         }
160         else if (eq(argv[0], "nextwriteable")) {
161                 int addr;
162                 if (ioctl(fd, CDRIOCNEXTWRITEABLEADDR, &addr) == -1)
163                         err(EX_IOERR, "ioctl(CDRIOCNEXTWRITEABLEADDR)");
164                 printf("%d\n", addr);
165         }
166         else
167                 errx(EX_USAGE, "unknown command: %s", argv[0]);
168
169         return EX_OK;
170 }