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