]> Dogcows Code - chaz/tar/blob - src/rmt.c
Initial revision
[chaz/tar] / src / rmt.c
1 /*
2 * Copyright (c) 1983 Regents of the University of California.
3 * All rights reserved.
4 *
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.
16 */
17
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
21 All rights reserved.\n";
22 #endif /* not lint */
23
24 #ifndef lint
25 static char sccsid[] = "@(#)rmt.c 5.4 (Berkeley) 6/29/88";
26 #endif /* not lint */
27
28 /* JF added #ifdef about SO_RCVBUF in attempt to make this run on more
29 machines. Maybe it'll work */
30 /*
31 * rmt
32 */
33 #include <stdio.h>
34 #include <sgtty.h>
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <sys/mtio.h>
38 #include <errno.h>
39
40 #if defined (i386) && defined (AIX)
41 #include <fcntl.h>
42 #endif
43
44 int tape = -1;
45
46 char *record;
47 int maxrecsize = -1;
48 char *checkbuf();
49
50 #define SSIZE 64
51 char device[SSIZE];
52 char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
53
54 extern errno;
55 extern char *sys_errlist[];
56 char resp[BUFSIZ];
57
58 long lseek();
59
60 FILE *debug;
61 #define DEBUG(f) if (debug) fprintf(debug, f)
62 #define DEBUG1(f,a) if (debug) fprintf(debug, f, a)
63 #define DEBUG2(f,a1,a2) if (debug) fprintf(debug, f, a1, a2)
64
65 main(argc, argv)
66 int argc;
67 char **argv;
68 {
69 int rval;
70 char c;
71 int n, i, cc;
72
73 argc--, argv++;
74 if (argc > 0) {
75 debug = fopen(*argv, "w");
76 if (debug == 0)
77 exit(1);
78 (void) setbuf(debug, (char *)0);
79 }
80 top:
81 errno = 0;
82 rval = 0;
83 if (read(0, &c, 1) != 1)
84 exit(0);
85 switch (c) {
86
87 case 'O':
88 if (tape >= 0)
89 (void) close(tape);
90 getstring(device); getstring(mode);
91 DEBUG2("rmtd: O %s %s\n", device, mode);
92 #if defined (i386) && defined (AIX)
93 /* This is alleged to fix a byte ordering problem. */
94 /* I'm quite suspicious if it's right. -- mib */
95 {
96 int oflag = atoi (mode);
97 int nflag = 0;
98 if ((oflag & 3) == 0)
99 nflag |= O_RDONLY;
100 if (oflag & 1)
101 nflag |= O_WRONLY;
102 if (oflag & 2)
103 nflag |= O_RDWR;
104 if (oflag & 0x0008)
105 nflag |= O_APPEND;
106 if (oflag & 0x0200)
107 nflag |= O_CREAT;
108 if (oflag & 0x0400)
109 nflag |= O_TRUNC;
110 if (oflag & 0x0800)
111 nflag |= O_EXCL;
112 tape = open (device, nflag, 0666);
113 }
114 #else
115 tape = open(device, atoi(mode),0666);
116 #endif
117 if (tape < 0)
118 goto ioerror;
119 goto respond;
120
121 case 'C':
122 DEBUG("rmtd: C\n");
123 getstring(device); /* discard */
124 if (close(tape) < 0)
125 goto ioerror;
126 tape = -1;
127 goto respond;
128
129 case 'L':
130 getstring(count); getstring(pos);
131 DEBUG2("rmtd: L %s %s\n", count, pos);
132 rval = lseek(tape, (long) atoi(count), atoi(pos));
133 if (rval < 0)
134 goto ioerror;
135 goto respond;
136
137 case 'W':
138 getstring(count);
139 n = atoi(count);
140 DEBUG1("rmtd: W %s\n", count);
141 record = checkbuf(record, n);
142 for (i = 0; i < n; i += cc) {
143 cc = read(0, &record[i], n - i);
144 if (cc <= 0) {
145 DEBUG("rmtd: premature eof\n");
146 exit(2);
147 }
148 }
149 rval = write(tape, record, n);
150 if (rval < 0)
151 goto ioerror;
152 goto respond;
153
154 case 'R':
155 getstring(count);
156 DEBUG1("rmtd: R %s\n", count);
157 n = atoi(count);
158 record = checkbuf(record, n);
159 rval = read(tape, record, n);
160 if (rval < 0)
161 goto ioerror;
162 (void) sprintf(resp, "A%d\n", rval);
163 (void) write(1, resp, strlen(resp));
164 (void) write(1, record, rval);
165 goto top;
166
167 case 'I':
168 getstring(op); getstring(count);
169 DEBUG2("rmtd: I %s %s\n", op, count);
170 { struct mtop mtop;
171 mtop.mt_op = atoi(op);
172 mtop.mt_count = atoi(count);
173 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
174 goto ioerror;
175 rval = mtop.mt_count;
176 }
177 goto respond;
178
179 case 'S': /* status */
180 DEBUG("rmtd: S\n");
181 { struct mtget mtget;
182 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
183 goto ioerror;
184 rval = sizeof (mtget);
185 (void) sprintf(resp, "A%d\n", rval);
186 (void) write(1, resp, strlen(resp));
187 (void) write(1, (char *)&mtget, sizeof (mtget));
188 goto top;
189 }
190
191 default:
192 DEBUG1("rmtd: garbage command %c\n", c);
193 exit(3);
194 }
195 respond:
196 DEBUG1("rmtd: A %d\n", rval);
197 (void) sprintf(resp, "A%d\n", rval);
198 (void) write(1, resp, strlen(resp));
199 goto top;
200 ioerror:
201 error(errno);
202 goto top;
203 }
204
205 getstring(bp)
206 char *bp;
207 {
208 int i;
209 char *cp = bp;
210
211 for (i = 0; i < SSIZE; i++) {
212 if (read(0, cp+i, 1) != 1)
213 exit(0);
214 if (cp[i] == '\n')
215 break;
216 }
217 cp[i] = '\0';
218 }
219
220 char *
221 checkbuf(record, size)
222 char *record;
223 int size;
224 {
225 extern char *malloc();
226
227 if (size <= maxrecsize)
228 return (record);
229 if (record != 0)
230 free(record);
231 record = malloc(size);
232 if (record == 0) {
233 DEBUG("rmtd: cannot allocate buffer space\n");
234 exit(4);
235 }
236 maxrecsize = size;
237 #ifdef SO_RCVBUF
238 while (size > 1024 &&
239 setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
240 size -= 1024;
241 #else
242 size= 1+((size-1)%1024);
243 #endif
244 return (record);
245 }
246
247 error(num)
248 int num;
249 {
250
251 DEBUG2("rmtd: E %d (%s)\n", num, sys_errlist[num]);
252 (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
253 (void) write(1, resp, strlen (resp));
254 }
This page took 0.045452 seconds and 5 git commands to generate.