Back to index

glibc  2.9
ftruncate64.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997-2002,2005,2006 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <sys/types.h>
00020 #include <errno.h>
00021 #include <unistd.h>
00022 
00023 #include <sysdep.h>
00024 #include <sys/syscall.h>
00025 
00026 #include <kernel-features.h>
00027 
00028 #ifdef __NR_ftruncate64
00029 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00030 /* The variable is shared between all wrappers around *truncate64 calls.  */
00031 extern int __have_no_truncate64;
00032 #endif
00033 
00034 
00035 /* Truncate the file FD refers to to LENGTH bytes.  */
00036 int
00037 __ftruncate64 (fd, length)
00038      int fd;
00039      off64_t length;
00040 {
00041 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00042   if (! __have_no_truncate64)
00043 #endif
00044     {
00045 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00046       int saved_errno = errno;
00047 #endif
00048       /* On PPC32 64bit values are aligned in odd/even register pairs.  */
00049       int result = INLINE_SYSCALL (ftruncate64, 4, fd, 0,
00050                                (long) (length >> 32),
00051                                (long) length);
00052 
00053 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00054       if (result != -1 || errno != ENOSYS)
00055 #endif
00056        return result;
00057 
00058 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00059       __set_errno (saved_errno);
00060       __have_no_truncate64 = 1;
00061 #endif
00062     }
00063 
00064 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00065   if ((off_t) length != length)
00066     {
00067       __set_errno (EINVAL);
00068       return -1;
00069     }
00070   return __ftruncate (fd, (off_t) length);
00071 #endif
00072 }
00073 weak_alias (__ftruncate64, ftruncate64)
00074 
00075 #else
00076 /* Use the generic implementation.  */
00077 # include <misc/ftruncate64.c>
00078 #endif