]> git.gag.com Git - debian/tar/blob - src/rmt.c
*** empty log message ***
[debian/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 /*
25  * rmt
26  */
27 #include <stdio.h>
28 #include <sgtty.h>
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>
33 #else
34 #include <sys/mtio.h>
35 #endif
36 #include <errno.h>
37
38 #if defined (_I386) && defined (_AIX)
39 #include <fcntl.h>
40 #endif
41
42 #ifdef HAVE_UNISTD_H
43 #include <unistd.h>
44 #else
45 long    lseek();
46 #endif
47
48 #ifdef STDC_HEADERS
49 #include <string.h>
50 #include <stdlib.h>
51 #else
52 extern char *malloc();
53 #endif
54
55 int     tape = -1;
56
57 char    *record;
58 int     maxrecsize = -1;
59 char    *checkbuf();
60 void    getstring();
61 void    error();
62
63 #define SSIZE   64
64 char    device[SSIZE];
65 char    count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
66
67 extern  errno;
68 extern char     *sys_errlist[];
69 char    resp[BUFSIZ];
70
71 FILE    *debug;
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)
75
76 int
77 main(argc, argv)
78         int argc;
79         char **argv;
80 {
81         int rval;
82         char c;
83         int n, i, cc;
84
85         argc--, argv++;
86         if (argc > 0) {
87                 debug = fopen(*argv, "w");
88                 if (debug == 0)
89                         exit(1);
90                 (void) setbuf(debug, (char *)0);
91         }
92 top:
93         errno = 0;
94         rval = 0;
95         if (read(0, &c, 1) != 1)
96                 exit(0);
97         switch (c) {
98
99         case 'O':
100                 if (tape >= 0)
101                         (void) close(tape);
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 */
107                 {
108                   int oflag = atoi (mode);
109                   int nflag = 0;
110                   if ((oflag & 3) == 0)
111                     nflag |= O_RDONLY;
112                   if (oflag & 1)
113                     nflag |= O_WRONLY;
114                   if (oflag & 2)
115                     nflag |= O_RDWR;
116                   if (oflag & 0x0008)
117                     nflag |= O_APPEND;
118                   if (oflag & 0x0200)
119                     nflag |= O_CREAT;
120                   if (oflag & 0x0400)
121                     nflag |= O_TRUNC;
122                   if (oflag & 0x0800)
123                     nflag |= O_EXCL;
124                   tape = open (device, nflag, 0666);
125                 }
126 #else           
127                 tape = open(device, atoi(mode),0666);
128 #endif
129                 if (tape < 0)
130                         goto ioerror;
131                 goto respond;
132
133         case 'C':
134                 DEBUG("rmtd: C\n");
135                 getstring(device);              /* discard */
136                 if (close(tape) < 0)
137                         goto ioerror;
138                 tape = -1;
139                 goto respond;
140
141         case 'L':
142                 getstring(count); getstring(pos);
143                 DEBUG2("rmtd: L %s %s\n", count, pos);
144                 rval = lseek(tape, (long) atoi(count), atoi(pos));
145                 if (rval < 0)
146                         goto ioerror;
147                 goto respond;
148
149         case 'W':
150                 getstring(count);
151                 n = atoi(count);
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);
156                         if (cc <= 0) {
157                                 DEBUG("rmtd: premature eof\n");
158                                 exit(2);
159                         }
160                 }
161                 rval = write(tape, record, n);
162                 if (rval < 0)
163                         goto ioerror;
164                 goto respond;
165
166         case 'R':
167                 getstring(count);
168                 DEBUG1("rmtd: R %s\n", count);
169                 n = atoi(count);
170                 record = checkbuf(record, n);
171                 rval = read(tape, record, n);
172                 if (rval < 0)
173                         goto ioerror;
174                 (void) sprintf(resp, "A%d\n", rval);
175                 (void) write(1, resp, strlen(resp));
176                 (void) write(1, record, rval);
177                 goto top;
178
179         case 'I':
180                 getstring(op); getstring(count);
181                 DEBUG2("rmtd: I %s %s\n", op, count);
182 #ifdef MTIOCTOP
183                 { struct mtop mtop;
184                   mtop.mt_op = atoi(op);
185                   mtop.mt_count = atoi(count);
186                   if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
187                         goto ioerror;
188                   rval = mtop.mt_count;
189                 }
190 #endif
191                 goto respond;
192
193         case 'S':               /* status */
194                 DEBUG("rmtd: S\n");
195                 {
196 #ifdef MTIOCGET
197                   struct mtget mtget;
198                   if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
199                         goto ioerror;
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));
204 #endif
205                   goto top;
206                 }
207
208         default:
209                 DEBUG1("rmtd: garbage command %c\n", c);
210                 exit(3);
211         }
212 respond:
213         DEBUG1("rmtd: A %d\n", rval);
214         (void) sprintf(resp, "A%d\n", rval);
215         (void) write(1, resp, strlen(resp));
216         goto top;
217 ioerror:
218         error(errno);
219         goto top;
220 }
221
222 void
223 getstring(bp)
224         char *bp;
225 {
226         int i;
227         char *cp = bp;
228
229         for (i = 0; i < SSIZE; i++) {
230                 if (read(0, cp+i, 1) != 1)
231                         exit(0);
232                 if (cp[i] == '\n')
233                         break;
234         }
235         cp[i] = '\0';
236 }
237
238 char *
239 checkbuf(record, size)
240         char *record;
241         int size;
242 {
243         if (size <= maxrecsize)
244                 return (record);
245         if (record != 0)
246                 free(record);
247         record = malloc(size);
248         if (record == 0) {
249                 DEBUG("rmtd: cannot allocate buffer space\n");
250                 exit(4);
251         }
252         maxrecsize = size;
253 #ifdef SO_RCVBUF
254         while (size > 1024 &&
255                setsockopt(0, SOL_SOCKET, SO_RCVBUF, (char *)&size, sizeof (size)) < 0)
256                 size -= 1024;
257 #else
258         size= 1+((size-1)%1024);
259 #endif
260         return (record);
261 }
262
263 void
264 error(num)
265         int num;
266 {
267
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));
271 }