Home | History | Annotate | Line # | Download | only in rmt
rmt.c revision 1.4
      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, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #ifndef lint
     35 char copyright[] =
     36 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
     37  All rights reserved.\n";
     38 #endif /* not lint */
     39 
     40 #ifndef lint
     41 /*static char sccsid[] = "from: @(#)rmt.c	5.6 (Berkeley) 6/1/90";*/
     42 static char rcsid[] = "$Id: rmt.c,v 1.4 1994/03/30 02:33:23 cgd Exp $";
     43 #endif /* not lint */
     44 
     45 /*
     46  * rmt
     47  */
     48 #include <stdio.h>
     49 #include <sgtty.h>
     50 #include <sys/types.h>
     51 #include <sys/socket.h>
     52 #include <sys/mtio.h>
     53 #include <errno.h>
     54 #include <string.h>
     55 
     56 int	tape = -1;
     57 
     58 char	*record;
     59 int	maxrecsize = -1;
     60 char	*checkbuf();
     61 
     62 #define	SSIZE	64
     63 char	device[SSIZE];
     64 char	count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
     65 
     66 char	resp[BUFSIZ];
     67 
     68 FILE	*debug;
     69 #define	DEBUG(f)	if (debug) fprintf(debug, f)
     70 #define	DEBUG1(f,a)	if (debug) fprintf(debug, f, a)
     71 #define	DEBUG2(f,a1,a2)	if (debug) fprintf(debug, f, a1, a2)
     72 
     73 main(argc, argv)
     74 	int argc;
     75 	char **argv;
     76 {
     77 	int rval;
     78 	char c;
     79 	int n, i, cc;
     80 
     81 	argc--, argv++;
     82 	if (argc > 0) {
     83 		debug = fopen(*argv, "w");
     84 		if (debug == 0)
     85 			exit(1);
     86 		(void) setbuf(debug, (char *)0);
     87 	}
     88 top:
     89 	errno = 0;
     90 	rval = 0;
     91 	if (read(0, &c, 1) != 1)
     92 		exit(0);
     93 	switch (c) {
     94 
     95 	case 'O':
     96 		if (tape >= 0)
     97 			(void) close(tape);
     98 		getstring(device); getstring(mode);
     99 		DEBUG2("rmtd: O %s %s\n", device, mode);
    100 		tape = open(device, atoi(mode));
    101 		if (tape < 0)
    102 			goto ioerror;
    103 		goto respond;
    104 
    105 	case 'C':
    106 		DEBUG("rmtd: C\n");
    107 		getstring(device);		/* discard */
    108 		if (close(tape) < 0)
    109 			goto ioerror;
    110 		tape = -1;
    111 		goto respond;
    112 
    113 	case 'L':
    114 		getstring(count); getstring(pos);
    115 		DEBUG2("rmtd: L %s %s\n", count, pos);
    116 		rval = lseek(tape, atoi(count), atoi(pos));
    117 		if (rval < 0)
    118 			goto ioerror;
    119 		goto respond;
    120 
    121 	case 'W':
    122 		getstring(count);
    123 		n = atoi(count);
    124 		DEBUG1("rmtd: W %s\n", count);
    125 		record = checkbuf(record, n);
    126 		for (i = 0; i < n; i += cc) {
    127 			cc = read(0, &record[i], n - i);
    128 			if (cc <= 0) {
    129 				DEBUG("rmtd: premature eof\n");
    130 				exit(2);
    131 			}
    132 		}
    133 		rval = write(tape, record, n);
    134 		if (rval < 0)
    135 			goto ioerror;
    136 		goto respond;
    137 
    138 	case 'R':
    139 		getstring(count);
    140 		DEBUG1("rmtd: R %s\n", count);
    141 		n = atoi(count);
    142 		record = checkbuf(record, n);
    143 		rval = read(tape, record, n);
    144 		if (rval < 0)
    145 			goto ioerror;
    146 		(void) sprintf(resp, "A%d\n", rval);
    147 		(void) write(1, resp, strlen(resp));
    148 		(void) write(1, record, rval);
    149 		goto top;
    150 
    151 	case 'I':
    152 		getstring(op); getstring(count);
    153 		DEBUG2("rmtd: I %s %s\n", op, count);
    154 		{ struct mtop mtop;
    155 		  mtop.mt_op = atoi(op);
    156 		  mtop.mt_count = atoi(count);
    157 		  if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
    158 			goto ioerror;
    159 		  rval = mtop.mt_count;
    160 		}
    161 		goto respond;
    162 
    163 	case 'S':		/* status */
    164 		DEBUG("rmtd: S\n");
    165 		{ struct mtget mtget;
    166 		  if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
    167 			goto ioerror;
    168 		  rval = sizeof (mtget);
    169 		  (void) sprintf(resp, "A%d\n", rval);
    170 		  (void) write(1, resp, strlen(resp));
    171 		  (void) write(1, (char *)&mtget, sizeof (mtget));
    172 		  goto top;
    173 		}
    174 
    175 	default:
    176 		DEBUG1("rmtd: garbage command %c\n", c);
    177 		exit(3);
    178 	}
    179 respond:
    180 	DEBUG1("rmtd: A %d\n", rval);
    181 	(void) sprintf(resp, "A%d\n", rval);
    182 	(void) write(1, resp, strlen(resp));
    183 	goto top;
    184 ioerror:
    185 	error(errno);
    186 	goto top;
    187 }
    188 
    189 getstring(bp)
    190 	char *bp;
    191 {
    192 	int i;
    193 	char *cp = bp;
    194 
    195 	for (i = 0; i < SSIZE; i++) {
    196 		if (read(0, cp+i, 1) != 1)
    197 			exit(0);
    198 		if (cp[i] == '\n')
    199 			break;
    200 	}
    201 	cp[i] = '\0';
    202 }
    203 
    204 char *
    205 checkbuf(record, size)
    206 	char *record;
    207 	int size;
    208 {
    209 	extern char *malloc();
    210 
    211 	if (size <= maxrecsize)
    212 		return (record);
    213 	if (record != 0)
    214 		free(record);
    215 	record = malloc(size);
    216 	if (record == 0) {
    217 		DEBUG("rmtd: cannot allocate buffer space\n");
    218 		exit(4);
    219 	}
    220 	maxrecsize = size;
    221 	while (size > 1024 &&
    222 	       setsockopt(0, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size)) < 0)
    223 		size -= 1024;
    224 	return (record);
    225 }
    226 
    227 error(num)
    228 	int num;
    229 {
    230 
    231 	DEBUG2("rmtd: E %d (%s)\n", num, strerror(num));
    232 	(void) sprintf(resp, "E%d\n%s\n", num, strerror(num));
    233 	(void) write(1, resp, strlen(resp));
    234 }
    235