+ int nat_flags;
+ int fd;
+
+ if (inf_child_fileio_open_flags_to_host (flags, &nat_flags) == -1)
+ {
+ *target_errno = FILEIO_EINVAL;
+ return -1;
+ }
+
+ /* We do not need to convert MODE, since the fileio protocol uses
+ the standard values. */
+ fd = gdb_open_cloexec (filename, nat_flags, mode);
+ if (fd == -1)
+ *target_errno = inf_child_errno_to_fileio_error (errno);
+
+ return fd;
+}
+
+/* Write up to LEN bytes from WRITE_BUF to FD on the target.
+ Return the number of bytes written, or -1 if an error occurs
+ (and set *TARGET_ERRNO). */
+static int
+inf_child_fileio_pwrite (struct target_ops *self,
+ int fd, const gdb_byte *write_buf, int len,
+ ULONGEST offset, int *target_errno)
+{
+ int ret;
+
+#ifdef HAVE_PWRITE
+ ret = pwrite (fd, write_buf, len, (long) offset);
+#else
+ ret = -1;
+#endif
+ /* If we have no pwrite or it failed for this file, use lseek/write. */
+ if (ret == -1)
+ {
+ ret = lseek (fd, (long) offset, SEEK_SET);
+ if (ret != -1)
+ ret = write (fd, write_buf, len);
+ }
+
+ if (ret == -1)
+ *target_errno = inf_child_errno_to_fileio_error (errno);
+
+ return ret;
+}
+
+/* Read up to LEN bytes FD on the target into READ_BUF.
+ Return the number of bytes read, or -1 if an error occurs
+ (and set *TARGET_ERRNO). */
+static int
+inf_child_fileio_pread (struct target_ops *self,
+ int fd, gdb_byte *read_buf, int len,
+ ULONGEST offset, int *target_errno)
+{
+ int ret;
+
+#ifdef HAVE_PREAD
+ ret = pread (fd, read_buf, len, (long) offset);
+#else
+ ret = -1;
+#endif
+ /* If we have no pread or it failed for this file, use lseek/read. */
+ if (ret == -1)
+ {
+ ret = lseek (fd, (long) offset, SEEK_SET);
+ if (ret != -1)
+ ret = read (fd, read_buf, len);
+ }
+
+ if (ret == -1)
+ *target_errno = inf_child_errno_to_fileio_error (errno);
+
+ return ret;