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