]>
git.saurik.com Git - apple/file_cmds.git/blob - rmt/rmt.c
1 /* $NetBSD: rmt.c,v 1.9 1997/10/17 13:03:25 lukem Exp $ */
4 * Copyright (c) 1983, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
38 __COPYRIGHT("@(#) Copyright (c) 1983, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
44 static char sccsid
[] = "@(#)rmt.c 8.1 (Berkeley) 6/6/93";
46 __RCSID("$NetBSD: rmt.c,v 1.9 1997/10/17 13:03:25 lukem Exp $");
53 #include <sys/types.h>
54 #include <sys/ioctl.h>
56 #include <sys/socket.h>
73 char count
[SSIZE
], mode
[SSIZE
], pos
[SSIZE
], op
[SSIZE
];
78 #define DEBUG(f) if (debug) fprintf(debug, f)
79 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
80 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
82 char *checkbuf
__P((char *, int));
83 void error
__P((int));
84 int main
__P((int, char **));
85 void getstring
__P((char *));
98 debug
= fopen(*argv
, "w");
101 (void)setbuf(debug
, (char *)0);
106 if (read(STDIN_FILENO
, &c
, 1) != 1)
115 DEBUG2("rmtd: O %s %s\n", device
, mode
);
116 tape
= open(device
, atoi(mode
),
117 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
|S_IROTH
|S_IWOTH
);
124 getstring(device
); /* discard */
133 DEBUG2("rmtd: L %s %s\n", count
, pos
);
134 rval
= lseek(tape
, (off_t
)strtoq(count
, NULL
, 10), atoi(pos
));
142 DEBUG1("rmtd: W %s\n", count
);
143 record
= checkbuf(record
, n
);
144 for (i
= 0; i
< n
; i
+= cc
) {
145 cc
= read(STDIN_FILENO
, &record
[i
], n
- i
);
147 DEBUG("rmtd: premature eof\n");
151 rval
= write(tape
, record
, n
);
158 DEBUG1("rmtd: R %s\n", count
);
160 record
= checkbuf(record
, n
);
161 rval
= read(tape
, record
, n
);
164 (void)sprintf(resp
, "A%d\n", rval
);
165 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));
166 (void)write(STDOUT_FILENO
, record
, rval
);
172 DEBUG2("rmtd: I %s %s\n", op
, count
);
176 mtop
.mt_op
= atoi(op
);
177 mtop
.mt_count
= atoi(count
);
178 if (ioctl(tape
, MTIOCTOP
, (char *)&mtop
) < 0)
180 rval
= mtop
.mt_count
;
184 case 'S': /* status */
189 if (ioctl(tape
, MTIOCGET
, (char *)&mtget
) < 0)
191 rval
= sizeof (mtget
);
192 (void)sprintf(resp
, "A%d\n", rval
);
193 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));
194 (void)write(STDOUT_FILENO
, (char *)&mtget
,
200 DEBUG1("rmtd: garbage command %c\n", c
);
204 DEBUG1("rmtd: A %d\n", rval
);
205 (void)sprintf(resp
, "A%d\n", rval
);
206 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));
220 for (i
= 0; i
< SSIZE
- 1; i
++) {
221 if (read(STDIN_FILENO
, cp
+i
, 1) != 1)
230 checkbuf(record
, size
)
235 if (size
<= maxrecsize
)
239 record
= malloc(size
);
241 DEBUG("rmtd: cannot allocate buffer space\n");
245 while (size
> 1024 &&
246 setsockopt(0, SOL_SOCKET
, SO_RCVBUF
, &size
, sizeof (size
)) < 0)
256 DEBUG2("rmtd: E %d (%s)\n", num
, strerror(num
));
257 (void)sprintf(resp
, "E%d\n%s\n", num
, strerror(num
));
258 (void)write(STDOUT_FILENO
, resp
, strlen(resp
));