Back to index

glibc  2.9
pread.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997, 1998, 2000, 2002, 2003, 2004
00002    Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <assert.h>
00022 #include <errno.h>
00023 #ifndef NO_SGIDEFS_H
00024 #include <sgidefs.h>
00025 #endif
00026 #include <unistd.h>
00027 #include <endian.h>
00028 
00029 #include <sysdep-cancel.h>
00030 #include <sys/syscall.h>
00031 #include <bp-checks.h>
00032 
00033 #include <kernel-features.h>
00034 
00035 #ifdef __NR_pread64             /* Newer kernels renamed but it's the same.  */
00036 # ifdef __NR_pread
00037 #  error "__NR_pread and __NR_pread64 both defined???"
00038 # endif
00039 # define __NR_pread __NR_pread64
00040 #endif
00041 
00042 #if defined __NR_pread || __ASSUME_PREAD_SYSCALL > 0
00043 
00044 # if __ASSUME_PREAD_SYSCALL == 0
00045 static ssize_t __emulate_pread (int fd, void *buf, size_t count,
00046                             off_t offset) internal_function;
00047 # endif
00048 
00049 ssize_t
00050 __libc_pread (fd, buf, count, offset)
00051      int fd;
00052      void *buf;
00053      size_t count;
00054      off_t offset;
00055 {
00056   ssize_t result;
00057 
00058 #if _MIPS_SIM != _ABI64
00059   assert (sizeof (offset) == 4);
00060 #endif
00061 
00062   if (SINGLE_THREAD_P)
00063     {
00064       /* First try the syscall.  */
00065 #if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
00066       result = INLINE_SYSCALL (pread, 4, fd, CHECK_N (buf, count), count,
00067                             offset);
00068 #else
00069       result = INLINE_SYSCALL (pread, 6, fd, CHECK_N (buf, count), count, 0,
00070                             __LONG_LONG_PAIR (offset >> 31, offset));
00071 #endif
00072 # if __ASSUME_PREAD_SYSCALL == 0
00073       if (result == -1 && errno == ENOSYS)
00074         /* No system call available.  Use the emulation.  */
00075         result = __emulate_pread (fd, buf, count, offset);
00076 # endif
00077       return result;
00078     }
00079 
00080   int oldtype = LIBC_CANCEL_ASYNC ();
00081 
00082   /* First try the syscall.  */
00083 #if _MIPS_SIM == _ABIN32 || _MIPS_SIM == _ABI64
00084   result = INLINE_SYSCALL (pread, 4, fd, CHECK_N (buf, count), count, offset);
00085 #else
00086   result = INLINE_SYSCALL (pread, 6, fd, CHECK_N (buf, count), count, 0,
00087                         __LONG_LONG_PAIR (offset >> 31, offset));
00088 #endif
00089 # if __ASSUME_PREAD_SYSCALL == 0
00090   if (result == -1 && errno == ENOSYS)
00091     /* No system call available.  Use the emulation.  */
00092     result = __emulate_pread (fd, buf, count, offset);
00093 # endif
00094 
00095   LIBC_CANCEL_RESET (oldtype);
00096 
00097   return result;
00098 }
00099 
00100 strong_alias (__libc_pread, __pread)
00101 weak_alias (__libc_pread, pread)
00102 
00103 # define __libc_pread(fd, buf, count, offset) \
00104      static internal_function __emulate_pread (fd, buf, count, offset)
00105 #endif
00106 
00107 #if __ASSUME_PREAD_SYSCALL == 0
00108 # include <sysdeps/posix/pread.c>
00109 #endif