BSD 的函數 ftruncate() 可以設定檔案的長度。但是並不是每一種版本的動作 都一樣。其他 UNIX 的變種似乎也都支援其他版本的截斷功能。
支援 ftruncate 函數的系統多半可歸類為以下三種:
BSD 4.2 - Ultrix, SGI, LynxOS -無法使用截斷功能來增加檔案長度 -執行截斷動作不會移動檔案指標
BSD 4.3 - SunOS, Solaris, OSF/1, HP/UX, Amiga -可以用截斷功能來增加檔案長度 -執行截斷動作不會移動檔案指標
Cray - UniCOS 7, UniCOS 8 -無法使用截斷功能來增加檔案長度 -執行截斷動作會移動檔案指標
其他系統則可能在以下四個地方與眾不同:
F_CHSIZE - 只在 SCO 上 -有些系統定義了 F_CHSIZE 但並沒有真的支援此功能 -動作類似 BSD 4.3
F_FREESP - 只在 Interative Unix 上 -有些系統(如 Interactive Unix)定義了 F_FREESP 但並沒有真的支援此 功能 -動作類似 BSD 4.3
chsize() - QNX and SCO -有些系統(如 Interactive Unix)有 chsize() 函數但並沒有真的支援 此功能 -動作類似 BSD 4.3
「空空如也」-目前找不到這種系統 -也許會有系統完全不支援 truncate功能
FAQ 維護者的註解:以下是我在幾年前從網路抓到的程式,原作者已不可考, 不過 S.Spencer Sun <spencer@ncd.com> 也貢獻了一份 F_FREESP的功能。
functions for each non-native ftruncate follow
/* ftruncate emulations that work on some System V's.
This file is in the public domain. */
#include
#include
#ifdef F_CHSIZE
int
ftruncate (fd, length)
int fd;
off_t length;
{
return fcntl (fd, F_CHSIZE, length);
}
#else
#ifdef F_FREESP
/* The following function was written by
kucharsk@Solbourne.com (William Kucharski) */
#include
#include
#include
int
ftruncate (fd, length)
int fd;
off_t length;
{
struct flock fl;
struct stat filebuf;
if (fstat (fd, &filebuf) < 0)
return -1;
if (filebuf.st_size < length)
{
/* Extend file length. */
if (lseek (fd, (length - 1), SEEK_SET) < 0)
return -1;
/* Write a "0" byte. */
if (write (fd, "", 1) != 1)
return -1;
}
else
{
/* Truncate length. */
fl.l_whence = 0;
fl.l_len = 0;
fl.l_start = length;
fl.l_type = F_WRLCK; /* Write lock on file space. */
/* This relies on the UNDOCUMENTED F_FREESP argument to
fcntl, which truncates the file so that it ends at the
position indicated by fl.l_start.
Will minor miracles never cease? */
if (fcntl (fd, F_FREESP, &fl) < 0)
return -1;
}
return 0;
}
#else
int
ftruncate (fd, length)
int fd;
off_t length;
{
return chsize (fd, length);
}
#endif
#endif