]> Dogcows Code - chaz/tar/blobdiff - src/rtapelib.c
Merge recent gnulib changes, and remove some lint.
[chaz/tar] / src / rtapelib.c
index 2116cb655295df789716c4249fdd9a4609d56f07..38036aee92b58482b42bbd3fabdf3bd54e02ff1e 100644 (file)
@@ -1,5 +1,7 @@
 /* Functions for communicating with a remote tape drive.
-   Copyright 1988, 1992, 1994, 1996, 1997, 1999 Free Software Foundation, Inc.
+
+   Copyright 1988, 1992, 1994, 1996, 1997, 1999, 2000, 2001, 2004 Free
+   Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -31,9 +33,9 @@
    code, courtesy of Dan Kegel.  */
 
 #include "system.h"
-
-#include "basename.h"
-#include "safe-read.h"
+#include "common.h"
+#include <safe-read.h>
+#include <full-write.h>
 
 /* Try hard to get EOPNOTSUPP defined.  486/ISC has it in net/errno.h,
    3B2/SVR3 has it in sys/inet.h.  Otherwise, like on MSDOS, use EINVAL.  */
@@ -58,9 +60,6 @@
 
 #include "rmt.h"
 
-/* FIXME: Just to shut up -Wall.  */
-int rexec ();
-
 /* Exit status if exec errors.  */
 #define EXIT_ON_EXEC_ERROR 128
 
@@ -93,10 +92,7 @@ static int to_remote[MAXUNIT][2] = {{-1, -1}, {-1, -1}, {-1, -1}, {-1, -1}};
 char *rmt_path__;
 \f
 
-/*----------------------------------------------------------------------.
-| Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE.  |
-`----------------------------------------------------------------------*/
-
+/* Close remote tape connection HANDLE, and reset errno to ERRNO_VALUE.  */
 static void
 _rmt_shutdown (int handle, int errno_value)
 {
@@ -104,33 +100,27 @@ _rmt_shutdown (int handle, int errno_value)
   close (WRITE_SIDE (handle));
   READ_SIDE (handle) = -1;
   WRITE_SIDE (handle) = -1;
-  errno = errno_value;         /* FIXME: errno should be read-only */
+  errno = errno_value;
 }
 
-/*-------------------------------------------------------------------------.
-| Attempt to perform the remote tape command specified in BUFFER on remote |
-| tape connection HANDLE.  Return 0 if successful, -1 on error.                   |
-`-------------------------------------------------------------------------*/
-
+/* Attempt to perform the remote tape command specified in BUFFER on
+   remote tape connection HANDLE.  Return 0 if successful, -1 on
+   error.  */
 static int
 do_command (int handle, const char *buffer)
 {
-  size_t length;
-  RETSIGTYPE (*pipe_handler) ();
-
   /* Save the current pipe handler and try to make the request.  */
 
-  pipe_handler = signal (SIGPIPE, SIG_IGN);
-  length = strlen (buffer);
-  if (full_write (WRITE_SIDE (handle), buffer, length) == length)
-    {
-      signal (SIGPIPE, pipe_handler);
-      return 0;
-    }
+  size_t length = strlen (buffer);
+  RETSIGTYPE (*pipe_handler) () = signal (SIGPIPE, SIG_IGN);
+  ssize_t written = full_write (WRITE_SIDE (handle), buffer, length);
+  signal (SIGPIPE, pipe_handler);
+
+  if (written == length)
+    return 0;
 
   /* Something went wrong.  Close down and go home.  */
 
-  signal (SIGPIPE, pipe_handler);
   _rmt_shutdown (handle, EIO);
   return -1;
 }
@@ -173,8 +163,6 @@ get_status_string (int handle, char *command_buffer)
 
   if (*cursor == 'E' || *cursor == 'F')
     {
-      errno = atoi (cursor + 1); /* FIXME: errno should be read-only */
-
       /* Skip the error message line.  */
 
       /* FIXME: there is better to do than merely ignoring error messages
@@ -188,6 +176,8 @@ get_status_string (int handle, char *command_buffer)
            break;
       }
 
+      errno = atoi (cursor + 1);
+
       if (*cursor == 'F')
        _rmt_shutdown (handle, errno);
 
@@ -207,17 +197,21 @@ get_status_string (int handle, char *command_buffer)
   return cursor + 1;
 }
 
-/*----------------------------------------------------------------------.
-| Read and return the status from remote tape connection HANDLE.  If an |
-| error occurred, return -1 and set errno.                             |
-`----------------------------------------------------------------------*/
-
-static long
+/* Read and return the status from remote tape connection HANDLE.  If
+   an error occurred, return -1 and set errno.  */
+static long int
 get_status (int handle)
 {
   char command_buffer[COMMAND_BUFFER_SIZE];
   const char *status = get_status_string (handle, command_buffer);
-  return status ? atol (status) : -1L;
+  if (status)
+    {
+      long int result = atol (status);
+      if (0 <= result)
+       return result;
+      errno = EIO;
+    }
+  return -1;
 }
 
 static off_t
@@ -239,10 +233,10 @@ get_status_off (int handle)
 
       for (;  *status == ' ' || *status == '\t';  status++)
        continue;
-      
+
       negative = *status == '-';
       status += negative || *status == '+';
-      
+
       for (;;)
        {
          int digit = *status++ - '0';
@@ -264,18 +258,15 @@ get_status_off (int handle)
 
 #if WITH_REXEC
 
-/*-------------------------------------------------------------------------.
-| Execute /etc/rmt as user USER on remote system HOST using rexec.  Return |
-| a file descriptor of a bidirectional socket for stdin and stdout.  If           |
-| USER is zero, use the current username.                                 |
-|                                                                         |
-| By default, this code is not used, since it requires that the user have  |
-| a .netrc file in his/her home directory, or that the application        |
-| designer be willing to have rexec prompt for login and password info.           |
-| This may be unacceptable, and .rhosts files for use with rsh are much           |
-| more common on BSD systems.                                             |
-`-------------------------------------------------------------------------*/
+/* Execute /etc/rmt as user USER on remote system HOST using rexec.
+   Return a file descriptor of a bidirectional socket for stdin and
+   stdout.  If USER is zero, use the current username.
 
+   By default, this code is not used, since it requires that the user
+   have a .netrc file in his/her home directory, or that the
+   application designer be willing to have rexec prompt for login and
+   password info.  This may be unacceptable, and .rhosts files for use
+   with rsh are much more common on BSD systems.  */
 static int
 _rmt_rexec (char *host, char *user)
 {
@@ -328,7 +319,9 @@ encode_oflag (char *buf, int oflag)
     default: abort ();
     }
 
+#ifdef O_APPEND
   if (oflag & O_APPEND) strcat (buf, "|O_APPEND");
+#endif
   if (oflag & O_CREAT) strcat (buf, "|O_CREAT");
 #ifdef O_DSYNC
   if (oflag & O_DSYNC) strcat (buf, "|O_DSYNC");
@@ -352,13 +345,11 @@ encode_oflag (char *buf, int oflag)
   if (oflag & O_TRUNC) strcat (buf, "|O_TRUNC");
 }
 
-/*------------------------------------------------------------------------.
-| Open a file (a magnetic tape device?) on the system specified in PATH,  |
-| as the given user.  PATH has the form `[USER@]HOST:FILE'.  OPEN_MODE is |
-| O_RDONLY, O_WRONLY, etc.  If successful, return the remote pipe number  |
-| plus BIAS.  REMOTE_SHELL may be overridden.  On error, return -1.      |
-`------------------------------------------------------------------------*/
-
+/* Open a file (a magnetic tape device?) on the system specified in
+   PATH, as the given user.  PATH has the form `[USER@]HOST:FILE'.
+   OPEN_MODE is O_RDONLY, O_WRONLY, etc.  If successful, return the
+   remote pipe number plus BIAS.  REMOTE_SHELL may be overridden.  On
+   error, return -1.  */
 int
 rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
 {
@@ -379,7 +370,7 @@ rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
 
   if (remote_pipe_number == MAXUNIT)
     {
-      errno = EMFILE;          /* FIXME: errno should be read-only */
+      errno = EMFILE;
       return -1;
     }
 
@@ -498,10 +489,7 @@ rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
        close (from_remote[remote_pipe_number][PREAD]);
        close (from_remote[remote_pipe_number][PWRITE]);
 
-#if !MSDOS
-       setuid (getuid ());
-       setgid (getgid ());
-#endif
+       sys_reset_uid_gid ();
 
        if (remote_user)
          execl (remote_shell, remote_shell_basename, remote_host,
@@ -547,15 +535,12 @@ rmt_open__ (const char *path, int open_mode, int bias, const char *remote_shell)
   return remote_pipe_number + bias;
 }
 
-/*----------------------------------------------------------------.
-| Close remote tape connection HANDLE and shut down.  Return 0 if |
-| successful, -1 on error.                                       |
-`----------------------------------------------------------------*/
-
+/* Close remote tape connection HANDLE and shut down.  Return 0 if
+   successful, -1 on error.  */
 int
 rmt_close__ (int handle)
 {
-  int status;
+  long int status;
 
   if (do_command (handle, "C\n") == -1)
     return -1;
@@ -565,70 +550,68 @@ rmt_close__ (int handle)
   return status;
 }
 
-/*-------------------------------------------------------------------------.
-| Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE.  |
-| Return the number of bytes read on success, -1 on error.                |
-`-------------------------------------------------------------------------*/
-
-ssize_t
+/* Read up to LENGTH bytes into BUFFER from remote tape connection HANDLE.
+   Return the number of bytes read on success, SAFE_READ_ERROR on error.  */
+size_t
 rmt_read__ (int handle, char *buffer, size_t length)
 {
   char command_buffer[COMMAND_BUFFER_SIZE];
-  ssize_t status, rlen;
+  size_t status;
+  size_t rlen;
   size_t counter;
 
   sprintf (command_buffer, "R%lu\n", (unsigned long) length);
   if (do_command (handle, command_buffer) == -1
-      || (status = get_status (handle)) == -1)
-    return -1;
+      || (status = get_status (handle)) == SAFE_READ_ERROR)
+    return SAFE_READ_ERROR;
 
   for (counter = 0; counter < status; counter += rlen, buffer += rlen)
     {
       rlen = safe_read (READ_SIDE (handle), buffer, status - counter);
-      if (rlen <= 0)
+      if (rlen == SAFE_READ_ERROR || rlen == 0)
        {
          _rmt_shutdown (handle, EIO);
-         return -1;
+         return SAFE_READ_ERROR;
        }
     }
 
   return status;
 }
 
-/*-------------------------------------------------------------------------.
-| Write LENGTH bytes from BUFFER to remote tape connection HANDLE.  Return |
-| the number of bytes written on success, -1 on error.                    |
-`-------------------------------------------------------------------------*/
-
-ssize_t
+/* Write LENGTH bytes from BUFFER to remote tape connection HANDLE.
+   Return the number of bytes written.  */
+size_t
 rmt_write__ (int handle, char *buffer, size_t length)
 {
   char command_buffer[COMMAND_BUFFER_SIZE];
   RETSIGTYPE (*pipe_handler) ();
+  size_t written;
 
   sprintf (command_buffer, "W%lu\n", (unsigned long) length);
   if (do_command (handle, command_buffer) == -1)
-    return -1;
+    return 0;
 
   pipe_handler = signal (SIGPIPE, SIG_IGN);
-  if (full_write (WRITE_SIDE (handle), buffer, length) == length)
+  written = full_write (WRITE_SIDE (handle), buffer, length);
+  signal (SIGPIPE, pipe_handler);
+  if (written == length)
     {
-      signal (SIGPIPE, pipe_handler);
-      return get_status (handle);
+      long int r = get_status (handle);
+      if (r < 0)
+       return 0;
+      if (r == length)
+       return length;
+      written = r;
     }
 
   /* Write error.  */
 
-  signal (SIGPIPE, pipe_handler);
   _rmt_shutdown (handle, EIO);
-  return -1;
+  return written;
 }
 
-/*------------------------------------------------------------------------.
-| Perform an imitation lseek operation on remote tape connection HANDLE.  |
-| Return the new file offset if successful, -1 if on error.              |
-`------------------------------------------------------------------------*/
-
+/* Perform an imitation lseek operation on remote tape connection
+   HANDLE.  Return the new file offset if successful, -1 if on error.  */
 off_t
 rmt_lseek__ (int handle, off_t offset, int whence)
 {
@@ -637,6 +620,7 @@ rmt_lseek__ (int handle, off_t offset, int whence)
   uintmax_t u = offset < 0 ? - (uintmax_t) offset : (uintmax_t) offset;
   char *p = operand_buffer + sizeof operand_buffer;
 
+  *--p = 0;
   do
     *--p = '0' + (int) (u % 10);
   while ((u /= 10) != 0);
@@ -659,18 +643,15 @@ rmt_lseek__ (int handle, off_t offset, int whence)
   return get_status_off (handle);
 }
 
-/*-----------------------------------------------------------------------.
-| Perform a raw tape operation on remote tape connection HANDLE.  Return |
-| the results of the ioctl, or -1 on error.                             |
-`-----------------------------------------------------------------------*/
-
+/* Perform a raw tape operation on remote tape connection HANDLE.
+   Return the results of the ioctl, or -1 on error.  */
 int
 rmt_ioctl__ (int handle, int operation, char *argument)
 {
   switch (operation)
     {
     default:
-      errno = EOPNOTSUPP;      /* FIXME: errno should be read-only */
+      errno = EOPNOTSUPP;
       return -1;
 
 #ifdef MTIOCTOP
@@ -682,7 +663,8 @@ rmt_ioctl__ (int handle, int operation, char *argument)
                       ? - (uintmax_t) ((struct mtop *) argument)->mt_count
                       : (uintmax_t) ((struct mtop *) argument)->mt_count);
        char *p = operand_buffer + sizeof operand_buffer;
-       
+
+        *--p = 0;
        do
          *--p = '0' + (int) (u % 10);
        while ((u /= 10) != 0);
@@ -704,7 +686,7 @@ rmt_ioctl__ (int handle, int operation, char *argument)
     case MTIOCGET:
       {
        ssize_t status;
-       ssize_t counter;
+       size_t counter;
 
        /* Grab the status and read it directly into the structure.  This
           assumes that the status buffer is not padded and that 2 shorts
@@ -719,7 +701,7 @@ rmt_ioctl__ (int handle, int operation, char *argument)
        for (; status > 0; status -= counter, argument += counter)
          {
            counter = safe_read (READ_SIDE (handle), argument, status);
-           if (counter <= 0)
+           if (counter == SAFE_READ_ERROR || counter == 0)
              {
                _rmt_shutdown (handle, EIO);
                return -1;
This page took 0.036666 seconds and 4 git commands to generate.