x86_64: Fix a warning.
[dragonfly.git] / usr.sbin / rmt / rmt.c
blobc049729cb7dc57f6ba29c5e1a2857c78444b429c
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)rmt.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.sbin/rmt/rmt.c,v 1.7 2000/02/12 01:14:33 mjacob Exp $
36 * $DragonFly: src/usr.sbin/rmt/rmt.c,v 1.5 2005/12/05 02:40:28 swildner Exp $
40 * rmt
42 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <sys/mtio.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <sgtty.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <unistd.h>
53 int tape = -1;
55 char *record;
56 int maxrecsize = -1;
58 #define SSIZE 64
59 char device[SSIZE];
60 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
62 char resp[BUFSIZ];
64 FILE *debug;
65 #define DEBUG(f) if (debug) fprintf(debug, f)
66 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
67 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
69 char *checkbuf(char *, int);
70 void error(int);
71 void getstring(char *);
73 int
74 main(int argc, char **argv)
76 int rval;
77 char c;
78 int n, i, cc;
80 argc--, argv++;
81 if (argc > 0) {
82 debug = fopen(*argv, "w");
83 if (debug == 0)
84 exit(1);
85 setbuf(debug, NULL);
87 top:
88 errno = 0;
89 rval = 0;
90 if (read(0, &c, 1) != 1)
91 exit(0);
92 switch (c) {
94 case 'O':
95 if (tape >= 0)
96 close(tape);
97 getstring(device);
98 getstring(mode);
99 DEBUG2("rmtd: O %s %s\n", device, mode);
101 * XXX the rmt protocol does not provide a means to
102 * specify the permission bits; allow rw for everyone,
103 * as modified by the users umask
105 tape = open(device, atoi(mode), 0666);
106 if (tape < 0)
107 goto ioerror;
108 goto respond;
110 case 'C':
111 DEBUG("rmtd: C\n");
112 getstring(device); /* discard */
113 if (close(tape) < 0)
114 goto ioerror;
115 tape = -1;
116 goto respond;
118 case 'L':
119 getstring(count);
120 getstring(pos);
121 DEBUG2("rmtd: L %s %s\n", count, pos);
122 rval = lseek(tape, (off_t)atol(count), atoi(pos));
123 if (rval < 0)
124 goto ioerror;
125 goto respond;
127 case 'W':
128 getstring(count);
129 n = atoi(count);
130 DEBUG1("rmtd: W %s\n", count);
131 record = checkbuf(record, n);
132 for (i = 0; i < n; i += cc) {
133 cc = read(0, &record[i], n - i);
134 if (cc <= 0) {
135 DEBUG("rmtd: premature eof\n");
136 exit(2);
139 rval = write(tape, record, n);
140 if (rval < 0)
141 goto ioerror;
142 goto respond;
144 case 'R':
145 getstring(count);
146 DEBUG1("rmtd: R %s\n", count);
147 n = atoi(count);
148 record = checkbuf(record, n);
149 rval = read(tape, record, n);
150 if (rval < 0)
151 goto ioerror;
152 sprintf(resp, "A%d\n", rval);
153 write(1, resp, strlen(resp));
154 write(1, record, rval);
155 goto top;
157 case 'I':
158 getstring(op);
159 getstring(count);
160 DEBUG2("rmtd: I %s %s\n", op, count);
161 { struct mtop mtop;
162 mtop.mt_op = atoi(op);
163 mtop.mt_count = atoi(count);
164 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
165 goto ioerror;
166 rval = mtop.mt_count;
168 goto respond;
170 case 'S': /* status */
171 DEBUG("rmtd: S\n");
172 { struct mtget mtget;
173 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
174 goto ioerror;
175 rval = sizeof (mtget);
176 if (rval > 24) /* original mtget structure size */
177 rval = 24;
178 sprintf(resp, "A%d\n", rval);
179 write(1, resp, strlen(resp));
180 write(1, (char *)&mtget, rval);
181 goto top;
184 case 'V': /* version */
185 getstring(op);
186 DEBUG1("rmtd: V %s\n", op);
187 rval = 2;
188 goto respond;
190 default:
191 DEBUG1("rmtd: garbage command %c\n", c);
192 exit(3);
194 respond:
195 DEBUG1("rmtd: A %d\n", rval);
196 sprintf(resp, "A%d\n", rval);
197 write(1, resp, strlen(resp));
198 goto top;
199 ioerror:
200 error(errno);
201 goto top;
204 void
205 getstring(char *bp)
207 int i;
208 char *cp = bp;
210 for (i = 0; i < SSIZE; i++) {
211 if (read(0, cp+i, 1) != 1)
212 exit(0);
213 if (cp[i] == '\n')
214 break;
216 cp[i] = '\0';
219 char *
220 checkbuf(char *record, int size)
223 if (size <= maxrecsize)
224 return (record);
225 if (record != 0)
226 free(record);
227 record = malloc(size);
228 if (record == 0) {
229 DEBUG("rmtd: cannot allocate buffer space\n");
230 exit(4);
232 maxrecsize = size;
233 while (size > 1024 &&
234 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
235 size -= 1024;
236 return (record);
239 void
240 error(int num)
243 DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
244 snprintf(resp, sizeof(resp), "E%d\n%s\n", num, strerror(num));
245 write(1, resp, strlen(resp));