Back to index

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