00001 /* 00002 * Copyright 2005, 2006 PathScale, Inc. All Rights Reserved. 00003 */ 00004 00005 /* 00006 00007 Copyright (C) 2000, 2001, Silicon Graphics, Inc. All Rights Reserved. 00008 00009 This program is free software; you can redistribute it and/or modify it 00010 under the terms of version 2.1 of the GNU Lesser General Public License 00011 as published by the Free Software Foundation. 00012 00013 This program is distributed in the hope that it would be useful, but 00014 WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00016 00017 Further, this software is distributed without any warranty that it is 00018 free of the rightful claim of any third person regarding infringement 00019 or the like. Any license provided herein, whether implied or 00020 otherwise, applies only to this software file. Patent licenses, if 00021 any, provided herein do not apply to combinations of this program with 00022 other software, or any other product whatsoever. 00023 00024 You should have received a copy of the GNU Lesser General Public 00025 License along with this program; if not, write the Free Software 00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston MA 02111-1307, 00027 USA. 00028 00029 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00030 Mountain View, CA 94043, or: 00031 00032 http://www.sgi.com 00033 00034 For further information regarding this notice, see: 00035 00036 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00037 00038 */ 00039 00040 00041 00042 #pragma ident "@(#) libf/fio/unittrunc.c 92.1 06/18/99 18:38:26" 00043 00044 #include <errno.h> 00045 #include <liberrno.h> 00046 #include <foreign.h> 00047 #include <sys/types.h> 00048 #include <sys/stat.h> 00049 #include <unistd.h> 00050 #include "fio.h" 00051 #if defined(__mips) || (defined(_LITTLE_ENDIAN) && defined(__sv2)) 00052 typedef long long _ftelltype; 00053 #define LIBFSEEK fseek64 00054 #define LIBFTELL ftell64 00055 #define LIBFTRUNC ftruncate64 00056 #else 00057 #ifdef KEY /* Bug 1678 */ 00058 /* Need 64 bit position to support long files */ 00059 typedef off_t _ftelltype; 00060 #define LIBFSEEK fseeko 00061 #define LIBFTELL ftello 00062 #define LIBFTRUNC ftruncate /* Vanilla Linux ftruncate already uses off_t */ 00063 #else /* KEY Bug 1678 */ 00064 typedef long _ftelltype; 00065 #define LIBFSEEK fseek 00066 #define LIBFTELL ftell 00067 #define LIBFTRUNC ftruncate 00068 #endif /* KEY Bug 1678 */ 00069 #endif 00070 /* 00071 * _unit_trunc() 00072 * 00073 * Truncate a file at its current position. 00074 * 00075 * Return value 00076 * 00077 * 0 for OK 00078 * error code if an error is encountered 00079 */ 00080 int 00081 _unit_trunc(unit *cup) 00082 { 00083 _ftelltype flength; 00084 FILE *iop; 00085 00086 if (cup->useq == 0) /* If direct access file */ 00087 return(0); /* Don't truncate direct access files */ 00088 00089 switch(cup->ufs) { 00090 00091 case FS_TEXT: 00092 case STD: 00093 00094 /* 00095 * Truncate the file with trunc(2) if the file is a a regular 00096 * file. The trunc(2) system call is not allowed on terminal 00097 * files, FIFO piped files, "/dev/null", or sockets. 00098 */ 00099 if (cup->useek) { 00100 iop = cup->ufp.std; 00101 00102 /* 00103 * If stream is in O_APPEND mode (as when 'a.out>>ofil') 00104 * we call fseek to current position. Turns out that on 00105 * SysV systems, ftell does not flush the buffer. Thus 00106 * the file offset does not get bumped to EOF as the 00107 * result of the flush. 00108 */ 00109 if (LIBFSEEK(iop, 0, SEEK_CUR) != 0) 00110 return(errno); 00111 00112 flength = LIBFTELL(iop); /* get current position */ 00113 00114 /* position fd to the location of the truncation */ 00115 if (LIBFSEEK(iop, flength, SEEK_SET) != 0) 00116 return(errno); 00117 #ifdef _UNICOS 00118 if (trunc(fileno(iop)) == -1L) 00119 #else 00120 if (LIBFTRUNC(fileno(iop), flength) == -1) 00121 #endif 00122 return(errno); 00123 #ifdef KEY /* Bug 5386 */ 00124 /* Until Fedora Core 3, this code never encountered 00125 * a Unix system that required an "fflush" here. But 00126 * without it, the FC3 stdio code doesn't notice that 00127 * we've bypassed stdio and truncated the file, and 00128 * to be honest, it seems like blind dumb luck that 00129 * other systems haven't encountered a problem here. 00130 */ 00131 fflush(iop); 00132 #endif /* KEY Bug 5386 */ 00133 } 00134 break; 00135 00136 case FS_FDC: 00137 if (XRCALL(cup->ufp.fdc, weodrtn)cup->ufp.fdc, 00138 &cup->uffsw) < 0) 00139 return(cup->uffsw.sw_error); 00140 break; 00141 00142 default: 00143 return(FEINTFST); 00144 } 00145 00146 return(0); 00147 }
1.5.6