2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
21 All rights reserved.\n";
25 static char sccsid[] = "@(#)rmt.c 5.4 (Berkeley) 6/29/88";
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #ifdef GENTAPE /* e.g. ISC UNIX */
36 #include <sys/gentape.h>
42 #if defined (i386) && defined (AIX)
54 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
57 extern char *sys_errlist[];
63 #define DEBUG(f) if (debug) fprintf(debug, f)
64 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
65 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
77 debug = fopen(*argv, "w");
80 (void) setbuf(debug, (char *)0);
85 if (read(0, &c, 1) != 1)
92 getstring(device); getstring(mode);
93 DEBUG2("rmtd: O %s %s\n", device, mode);
94 #if defined (i386) && defined (AIX)
95 /* This is alleged to fix a byte ordering problem. */
96 /* I'm quite suspicious if it's right. -- mib */
98 int oflag = atoi (mode);
100 if ((oflag & 3) == 0)
114 tape = open (device, nflag, 0666);
117 tape = open(device, atoi(mode),0666);
125 getstring(device); /* discard */
132 getstring(count); getstring(pos);
133 DEBUG2("rmtd: L %s %s\n", count, pos);
134 rval = lseek(tape, (long) atoi(count), 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(0, &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(1, resp, strlen(resp));
166 (void) write(1, record, rval);
170 getstring(op); getstring(count);
171 DEBUG2("rmtd: I %s %s\n", op, count);
174 mtop.mt_op = atoi(op);
175 mtop.mt_count = atoi(count);
176 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
178 rval = mtop.mt_count;
183 case 'S': /* status */
188 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
190 rval = sizeof (mtget);
191 (void) sprintf(resp, "A%d\n", rval);
192 (void) write(1, resp, strlen(resp));
193 (void) write(1, (char *)&mtget, sizeof (mtget));
199 DEBUG1("rmtd: garbage command %c\n", c);
203 DEBUG1("rmtd: A %d\n", rval);
204 (void) sprintf(resp, "A%d\n", rval);
205 (void) write(1, resp, strlen(resp));
218 for (i = 0; i < SSIZE; i++) {
219 if (read(0, cp+i, 1) != 1)
228 checkbuf(record, size)
232 extern char *malloc();
234 if (size <= maxrecsize)
238 record = malloc(size);
240 DEBUG("rmtd: cannot allocate buffer space\n");
245 while (size > 1024 &&
246 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
249 size= 1+((size-1)%1024);
258 DEBUG2("rmtd: E %d (%s)\n", num, sys_errlist[num]);
259 (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
260 (void) write(1, resp, strlen (resp));