rmt.c 5.4 KB

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