date and time created 82/04/02 10:29:00 by wnj
[unix-history] / usr / src / usr.sbin / rmt / rmt.c
CommitLineData
ca880174
BJ
1#ifndef lint
2static char sccsid[] = "@(#)rmt.c 4.1 82/04/02";
3#endif
4
5/*
6 * rmt
7 */
8#include <stdio.h>
9#include <sgtty.h>
10#include <sys/types.h>
11#include <sys/mtio.h>
12#include <errno.h>
13
14int tape = -1;
15
16#define MAXRECSIZ (60*1024)
17char record[MAXRECSIZ];
18
19#define SSIZE 64
20char device[SSIZE];
21char count[SSIZE], mode[SSIZE], pos[SSIZE], op[SSIZE];
22
23extern errno;
24char *sys_errlist[];
25char resp[BUFSIZ];
26
27char *sprintf();
28long lseek();
29
30FILE *debug;
31
32main(argc, argv)
33 int argc;
34 char **argv;
35{
36 long rval;
37 char c;
38 int n, i, cc;
39
40 argc--, argv++;
41 if (argc > 0) {
42 debug = fopen(*argv, "w");
43 if (debug == 0)
44 exit(1);
45 (void) setbuf(debug, (char *)0);
46 }
47top:
48 errno = 0;
49 rval = 0;
50 if (read(0, &c, 1) != 1)
51 exit(0);
52 switch (c) {
53
54 case 'O':
55 if (tape >= 0)
56 (void) close(tape);
57 gets(device); gets(mode);
58if (debug) fprintf(debug, "rmtd: O %s %s\n", device, mode);
59 tape = open(device, atoi(mode));
60 if (tape < 0)
61 goto ioerror;
62 break;
63
64 case 'C':
65if (debug) fprintf(debug, "rmtd: C\n");
66 gets(device); /* discard */
67 if (close(tape) < 0)
68 goto ioerror;
69 tape = -1;
70 break;
71
72 case 'L':
73 gets(count); gets(pos);
74if (debug) fprintf(debug, "rmtd: L %s %s\n", count, pos);
75 rval = lseek(tape, (long) atoi(count), atoi(pos));
76 if (rval < 0)
77 goto ioerror;
78 break;
79
80 case 'W':
81 gets(count);
82 n = atoi(count);
83if (debug) fprintf(debug, "rmtd: W %s\n", count);
84 for (i = 0; i < n; i += cc) {
85 cc = read(0, &record[i], n - i);
86 if (cc <= 0) {
87if (debug) fprintf(debug, "rmtd: premature eof\n");
88 exit(1);
89 }
90 }
91 rval = write(tape, record, n);
92 if (rval < 0)
93 goto ioerror;
94 break;
95
96 case 'R':
97 gets(count);
98if (debug) fprintf(debug, "rmtd: R %s\n", count);
99 n = atoi(count);
100 if (n > sizeof (record))
101 n = sizeof (record);
102 rval = read(tape, record, n);
103 if (rval < 0)
104 goto ioerror;
105 (void) write(1, record, n);
106 break;
107
108 case 'I':
109 gets(op); gets(count);
110if (debug) fprintf(debug, "rmtd: I %s %s\n", op, count);
111 { struct mtop mtop;
112 mtop.mt_op = atoi(op);
113 mtop.mt_count = atoi(count);
114 if (ioctl(tape, MTIOCTOP, (char *)&mtop) < 0)
115 goto ioerror;
116 rval = mtop.mt_count;
117 }
118 break;
119
120 case 'S': /* status */
121if (debug) fprintf(debug, "rmtd: S\n");
122 { struct mtget mtget;
123 if (ioctl(tape, MTIOCGET, (char *)&mtget) < 0)
124 goto ioerror;
125 rval = sizeof (mtget);
126 (void) write(1, (char *)&mtget, sizeof (mtget));
127 break;
128 }
129
130 default:
131if (debug) fprintf(debug, "rmtd: garbage command %c\n", c);
132 exit(1);
133 }
134 (void) sprintf(resp, "A%d\n", rval);
135 (void) write(1, resp, strlen(resp));
136 goto top;
137ioerror:
138 error(errno);
139 goto top;
140}
141
142gets(bp)
143 char *bp;
144{
145 int i;
146 char *cp = bp;
147
148 for (i = 0; i < SSIZE; i++) {
149 if (read(0, cp+i, 1) != 1)
150 exit(0);
151 if (cp[i] == '\n')
152 break;
153 }
154 cp[i] = '\0';
155}
156
157error(num)
158 int num;
159{
160
161 (void) sprintf(resp, "E%d\n%s\n", num, sys_errlist[num]);
162 (void) write(1, resp, strlen (resp));
163}