gdb/gdbserver/
[deliverable/binutils-gdb.git] / gdb / gdbserver / hostio.c
index 5160ba9194eb45f8d8fc88a5daa96b5f22c57131..99014bcdeb250e90ea5a7a2e2c02612bab5abedd 100644 (file)
@@ -1,6 +1,5 @@
 /* Host file transfer support for gdbserver.
-   Copyright (C) 2006
-   Free Software Foundation, Inc.
+   Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
 
    Contributed by CodeSourcery.
 
@@ -8,7 +7,7 @@
 
    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
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "server.h"
 #include "gdb/fileio.h"
 
-#include <errno.h>
 #include <fcntl.h>
 #include <limits.h>
 #include <unistd.h>
@@ -118,7 +114,7 @@ require_data (char *p, int p_len, char **data, int *data_len)
 {
   int input_index, output_index, escaped;
 
-  *data = malloc (p_len);
+  *data = xmalloc (p_len);
 
   output_index = 0;
   escaped = 0;
@@ -177,69 +173,18 @@ require_valid_fd (int fd)
   return -1;
 }
 
-static int
-errno_to_fileio_errno (int error)
-{
-  switch (error)
-    {
-    case EPERM:
-      return FILEIO_EPERM;
-    case ENOENT:
-      return FILEIO_ENOENT;
-    case EINTR:
-      return FILEIO_EINTR;
-    case EIO:
-      return FILEIO_EIO;
-    case EBADF:
-      return FILEIO_EBADF;
-    case EACCES:
-      return FILEIO_EACCES;
-    case EFAULT:
-      return FILEIO_EFAULT;
-    case EBUSY:
-      return FILEIO_EBUSY;
-    case EEXIST:
-      return FILEIO_EEXIST;
-    case ENODEV:
-      return FILEIO_ENODEV;
-    case ENOTDIR:
-      return FILEIO_ENOTDIR;
-    case EISDIR:
-      return FILEIO_EISDIR;
-    case EINVAL:
-      return FILEIO_EINVAL;
-    case ENFILE:
-      return FILEIO_ENFILE;
-    case EMFILE:
-      return FILEIO_EMFILE;
-    case EFBIG:
-      return FILEIO_EFBIG;
-    case ENOSPC:
-      return FILEIO_ENOSPC;
-    case ESPIPE:
-      return FILEIO_ESPIPE;
-    case EROFS:
-      return FILEIO_EROFS;
-    case ENOSYS:
-      return FILEIO_ENOSYS;
-    case ENAMETOOLONG:
-      return FILEIO_ENAMETOOLONG;
-    }
-  return FILEIO_EUNKNOWN;
-}
-
+/* Fill in own_buf with the last hostio error packet, however it
+   suitable for the target.  */
 static void
-hostio_error (char *own_buf, int error)
+hostio_error (char *own_buf)
 {
-  int fileio_error = errno_to_fileio_errno (error);
-
-  sprintf (own_buf, "F-1,%x", fileio_error);
+  the_target->hostio_last_error (own_buf);
 }
 
 static void
 hostio_packet_error (char *own_buf)
 {
-  hostio_error (own_buf, EINVAL);
+  sprintf (own_buf, "F-1,%x", FILEIO_EINVAL);
 }
 
 static void
@@ -343,12 +288,12 @@ handle_open (char *own_buf)
 
   if (fd == -1)
     {
-      hostio_error (own_buf, errno);
+      hostio_error (own_buf);
       return;
     }
 
   /* Record the new file descriptor.  */
-  new_fd = malloc (sizeof (struct fd_list));
+  new_fd = xmalloc (sizeof (struct fd_list));
   new_fd->fd = fd;
   new_fd->next = open_fds;
   open_fds = new_fd;
@@ -376,12 +321,18 @@ handle_pread (char *own_buf, int *new_packet_len)
       return;
     }
 
-  data = malloc (len);
+  data = xmalloc (len);
+#ifdef HAVE_PREAD
   ret = pread (fd, data, len, offset);
+#else
+  ret = lseek (fd, offset, SEEK_SET);
+  if (ret != -1)
+    ret = read (fd, data, len);
+#endif
 
   if (ret == -1)
     {
-      hostio_error (own_buf, errno);
+      hostio_error (own_buf);
       free (data);
       return;
     }
@@ -419,11 +370,17 @@ handle_pwrite (char *own_buf, int packet_len)
       return;
     }
 
+#ifdef HAVE_PWRITE
   ret = pwrite (fd, data, len, offset);
+#else
+  ret = lseek (fd, offset, SEEK_SET);
+  if (ret != -1)
+    ret = write (fd, data, len);
+#endif
 
   if (ret == -1)
     {
-      hostio_error (own_buf, errno);
+      hostio_error (own_buf);
       free (data);
       return;
     }
@@ -453,7 +410,7 @@ handle_close (char *own_buf)
 
   if (ret == -1)
     {
-      hostio_error (own_buf, errno);
+      hostio_error (own_buf);
       return;
     }
 
@@ -488,7 +445,7 @@ handle_unlink (char *own_buf)
 
   if (ret == -1)
     {
-      hostio_error (own_buf, errno);
+      hostio_error (own_buf);
       return;
     }
 
This page took 0.026816 seconds and 4 git commands to generate.