Back to index

glibc  2.9
truncate64.c
Go to the documentation of this file.
00001 /* Copyright (C) 1997-2000,2003,2004,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 <endian.h>
00022 #include <errno.h>
00023 #include <unistd.h>
00024 
00025 #include <sysdep.h>
00026 #include <sys/syscall.h>
00027 #include <bp-checks.h>
00028 
00029 #include <kernel-features.h>
00030 
00031 #ifdef __NR_truncate64
00032 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00033 /* The variable is shared between all wrappers around *truncate64 calls.  */
00034 int __have_no_truncate64;
00035 #endif
00036 
00037 /* Truncate the file FD refers to to LENGTH bytes.  */
00038 int
00039 truncate64 (const char *path, off64_t length)
00040 {
00041 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00042   if (! __have_no_truncate64)
00043 #endif
00044     {
00045       unsigned int low = length & 0xffffffff;
00046       unsigned int high = length >> 32;
00047 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00048       int saved_errno = errno;
00049 #endif
00050       int result = INLINE_SYSCALL (truncate64, 3, CHECK_STRING (path),
00051                                __LONG_LONG_PAIR (high, low));
00052 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00053       if (result != -1 || errno != ENOSYS)
00054 #endif
00055        return result;
00056 
00057 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00058       __set_errno (saved_errno);
00059       __have_no_truncate64 = 1;
00060 #endif
00061     }
00062 
00063 #ifndef __ASSUME_TRUNCATE64_SYSCALL
00064   if ((off_t) length != length)
00065     {
00066       __set_errno (EINVAL);
00067       return -1;
00068     }
00069   return __truncate (path, (off_t) length);
00070 #endif
00071 }
00072 
00073 #else
00074 /* Use the generic implementation.  */
00075 # include <misc/truncate64.c>
00076 #endif