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";
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #ifdef HAVE_SYS_GENTAPE_H /* e.g., ISC UNIX */
32 #include <sys/gentape.h>
38 #if defined (_I386) && defined (_AIX)
52 extern char *malloc();
65 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
68 extern char *sys_errlist[];
72 #define DEBUG(f) if (debug) fprintf(debug, f)
73 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
74 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
87 debug = fopen(*argv, "w");
90 (void) setbuf(debug, (char *)0);
95 if (read(0, &c, 1) != 1)
102 getstring(device); getstring(mode);
103 DEBUG2("rmtd: O %s %s\n", device, mode);
104 #if defined (i386) && defined (AIX)
105 /* This is alleged to fix a byte ordering problem. */
106 /* I'm quite suspicious if it's right. -- mib */
108 int oflag = atoi (mode);
110 if ((oflag & 3) == 0)
124 tape = open (device, nflag, 0666);
127 tape = open(device, atoi(mode),0666);
135 getstring(device); /* discard */
142 getstring(count); getstring(pos);
143 DEBUG2("rmtd: L %s %s\n", count, pos);
144 rval = lseek(tape, (long) atoi(count), atoi(pos));
152 DEBUG1("rmtd: W %s\n", count);
153 record = checkbuf(record, n);
154 for (i = 0; i < n; i += cc) {
155 cc = read(0, &record[i], n - i);
157 DEBUG("rmtd: premature eof\n");
161 rval = write(tape, record, n);
168 DEBUG1("rmtd: R %s\n", count);
170 record = checkbuf(record, n);
171 rval = read(tape, record, n);
174 (void) sprintf(resp, "A%d\n", rval);
175 (void) write(1, resp, strlen(resp));
176 (void) write(1, record, rval);
180 getstring(op); getstring(count);
181 DEBUG2("rmtd: I %s %s\n", op, count);
184 mtop.mt_op = atoi(op);
185 mtop.mt_count = atoi(count);
186 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
188 rval = mtop.mt_count;
193 case 'S': /* status */
198 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
200 rval = sizeof (mtget);
201 (void) sprintf(resp, "A%d\n", rval);
202 (void) write(1, resp, strlen(resp));
203 (void) write(1, (char *)&mtget, sizeof (mtget));
209 DEBUG1("rmtd: garbage command %c\n", c);
213 DEBUG1("rmtd: A %d\n", rval);
214 (void) sprintf(resp, "A%d\n", rval);
215 (void) write(1, resp, strlen(resp));
229 for (i = 0; i < SSIZE; i++) {
230 if (read(0, cp+i, 1) != 1)
239 checkbuf(record, size)
243 if (size <= maxrecsize)
247 record = malloc(size);
249 DEBUG("rmtd: cannot allocate buffer space\n");
254 while (size > 1024 &&
255 setsockopt(0, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof (size)) < 0)
258 size= 1+((size-1)%1024);
268 DEBUG2("rmtd: E %d (%s)\n", num, sys_errlist[num]);
269 (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
270 (void) write(1, resp, strlen (resp));