- errno = 0;
- rval = 0;
- if (read(0, &c, 1) != 1)
- exit(0);
- switch (c) {
-
- case 'O':
- if (tape >= 0)
- (void) close(tape);
- getstring(device); getstring(mode);
- DEBUG2("rmtd: O %s %s\n", device, mode);
-#if defined (i386) && defined (AIX)
- /* This is alleged to fix a byte ordering problem. */
- /* I'm quite suspicious if it's right. -- mib */
- {
- int oflag = atoi (mode);
- int nflag = 0;
- if ((oflag & 3) == 0)
- nflag |= O_RDONLY;
- if (oflag & 1)
- nflag |= O_WRONLY;
- if (oflag & 2)
- nflag |= O_RDWR;
- if (oflag & 0x0008)
- nflag |= O_APPEND;
- if (oflag & 0x0200)
- nflag |= O_CREAT;
- if (oflag & 0x0400)
- nflag |= O_TRUNC;
- if (oflag & 0x0800)
- nflag |= O_EXCL;
- tape = open (device, nflag, 0666);
- }
-#else
- tape = open(device, atoi(mode),0666);
-#endif
- if (tape < 0)
- goto ioerror;
- goto respond;
-
- case 'C':
- DEBUG("rmtd: C\n");
- getstring(device); /* discard */
- if (close(tape) < 0)
- goto ioerror;
- tape = -1;
- goto respond;
-
- case 'L':
- getstring(count); getstring(pos);
- DEBUG2("rmtd: L %s %s\n", count, pos);
- rval = lseek(tape, (long) atoi(count), atoi(pos));
- if (rval < 0)
- goto ioerror;
- goto respond;
-
- case 'W':
- getstring(count);
- n = atoi(count);
- DEBUG1("rmtd: W %s\n", count);
- record = checkbuf(record, n);
- for (i = 0; i < n; i += cc) {
- cc = read(0, &record[i], n - i);
- if (cc <= 0) {
- DEBUG("rmtd: premature eof\n");
- exit(2);
- }
- }
- rval = write(tape, record, n);
- if (rval < 0)
- goto ioerror;
- goto respond;
-
- case 'R':
- getstring(count);
- DEBUG1("rmtd: R %s\n", count);
- n = atoi(count);
- record = checkbuf(record, n);
- rval = read(tape, record, n);
- if (rval < 0)
- goto ioerror;
- (void) sprintf(resp, "A%d\n", rval);
- (void) write(1, resp, strlen(resp));
- (void) write(1, record, rval);
- goto top;
-
- case 'I':
- getstring(op); getstring(count);
- DEBUG2("rmtd: I %s %s\n", op, count);
+ errno = 0;
+ status = 0;
+ if (safe_read (STDIN_FILENO, &command, 1) != 1)
+ return EXIT_SUCCESS;
+
+ switch (command)
+ {
+ /* FIXME: Maybe 'H' and 'V' for --help and --version output? */
+
+ case 'O':
+ {
+ char device_string[STRING_SIZE];
+ char oflag_string[STRING_SIZE];
+
+ get_string (device_string);
+ get_string (oflag_string);
+ DEBUG2 ("rmtd: O %s %s\n", device_string, oflag_string);
+
+ if (tape >= 0)
+ close (tape);
+
+ tape = open (device_string, decode_oflag (oflag_string), MODE_RW);
+ if (tape < 0)
+ goto ioerror;
+ goto respond;
+ }
+
+ case 'C':
+ {
+ char device_string[STRING_SIZE];
+
+ get_string (device_string); /* discard */
+ DEBUG ("rmtd: C\n");
+
+ if (close (tape) < 0)
+ goto ioerror;
+ tape = -1;
+ goto respond;
+ }
+
+ case 'L':
+ {
+ char count_string[STRING_SIZE];
+ char position_string[STRING_SIZE];
+ off_t count = 0;
+ int negative;
+ int whence;
+ char *p;
+
+ get_string (count_string);
+ get_string (position_string);
+ DEBUG2 ("rmtd: L %s %s\n", count_string, position_string);
+
+ /* Parse count_string, taking care to check for overflow.
+ We can't use standard functions,
+ since off_t might be longer than long. */
+
+ for (p = count_string; *p == ' ' || *p == '\t'; p++)
+ continue;
+
+ negative = *p == '-';
+ p += negative || *p == '+';
+
+ for (;;)
+ {
+ int digit = *p++ - '0';
+ if (9 < (unsigned) digit)
+ break;
+ else
+ {
+ off_t c10 = 10 * count;
+ off_t nc = negative ? c10 - digit : c10 + digit;
+ if (c10 / 10 != count || (negative ? c10 < nc : nc < c10))
+ {
+ report_error_message (N_("Seek offset out of range"));
+ return EXIT_FAILURE;
+ }
+ count = nc;
+ }
+ }
+
+ switch (atoi (position_string))
+ {
+ case 0: whence = SEEK_SET; break;
+ case 1: whence = SEEK_CUR; break;
+ case 2: whence = SEEK_END; break;
+ default:
+ report_error_message (N_("Seek direction out of range"));
+ return EXIT_FAILURE;
+ }
+ count = lseek (tape, count, whence);
+ if (count < 0)
+ goto ioerror;
+
+ /* Convert count back to string for reply.
+ We can't use sprintf, since off_t might be longer than long. */
+ p = count_string + sizeof count_string;
+ *--p = '\0';
+ do
+ *--p = '0' + (int) (count % 10);
+ while ((count /= 10) != 0);
+
+ DEBUG1 ("rmtd: A %s\n", p);
+
+ sprintf (reply_buffer, "A%s\n", p);
+ full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
+ goto top;
+ }
+
+ case 'W':
+ {
+ char count_string[STRING_SIZE];
+ size_t size;
+ size_t counter;
+
+ get_string (count_string);
+ size = atol (count_string);
+ DEBUG1 ("rmtd: W %s\n", count_string);
+
+ prepare_input_buffer (STDIN_FILENO, size);
+ for (counter = 0; counter < size; counter += status)
+ {
+ status = safe_read (STDIN_FILENO, &record_buffer[counter],
+ size - counter);
+ if (status == SAFE_READ_ERROR || status == 0)
+ {
+ DEBUG (_("rmtd: Premature eof\n"));
+
+ report_error_message (N_("Premature end of file"));
+ return EXIT_FAILURE; /* exit status used to be 2 */
+ }
+ }
+ status = full_write (tape, record_buffer, size);
+ if (status != size)
+ goto ioerror;
+ goto respond;
+ }
+
+ case 'R':
+ {
+ char count_string[STRING_SIZE];
+ size_t size;
+
+ get_string (count_string);
+ DEBUG1 ("rmtd: R %s\n", count_string);
+
+ size = atol (count_string);
+ prepare_input_buffer (-1, size);
+ status = safe_read (tape, record_buffer, size);
+ if (status == SAFE_READ_ERROR)
+ goto ioerror;
+ sprintf (reply_buffer, "A%lu\n", (unsigned long int) status);
+ full_write (STDOUT_FILENO, reply_buffer, strlen (reply_buffer));
+ full_write (STDOUT_FILENO, record_buffer, status);
+ goto top;
+ }
+
+ case 'I':
+ {
+ char operation_string[STRING_SIZE];
+ char count_string[STRING_SIZE];
+
+ get_string (operation_string);
+ get_string (count_string);
+ DEBUG2 ("rmtd: I %s %s\n", operation_string, count_string);
+