|
Top Document: Unix - Frequently Asked Questions (3/7) [Frequent posting] Previous Document: How do I use "rsh" without having the rsh hang around ... ? Next Document: Why doesn't find's "{}" symbol do what I want? See reader questions & answers on this topic! - Help others by sharing your knowledge
3.3) How do I truncate a file?
The BSD function ftruncate() sets the length of a file.
(But not all versions behave identically.) Other Unix variants
all seem to support some version of truncation as well.
For systems which support the ftruncate function, there are
three known behaviours:
BSD 4.2 - Ultrix, SGI, LynxOS
- truncation doesn't grow file
- truncation doesn't move file pointer
BSD 4.3 - SunOS, Solaris, OSF/1, HP/UX, Amiga
- truncation can grow file
- truncation doesn't move file pointer
Cray - UniCOS 7, UniCOS 8
- truncation doesn't grow file
- truncation changes file pointer
Other systems come in four varieties:
F_CHSIZE - Only SCO
- some systems define F_CHSIZE but don't support it
- behaves like BSD 4.3
F_FREESP - Only Interative Unix
- some systems (eg. Interactive Unix) define F_FREESP but
don't support it
- behaves like BSD 4.3
chsize() - QNX and SCO
- some systems (eg. Interactive Unix) have chsize() but
don't support it
- behaves like BSD 4.3
nothing - no known systems
- there will be systems that don't support truncate at all
Moderator's Note: I grabbed the functions below a few years back.
I can no longer identify the original author.
S. Spencer Sun <spencer@ncd.com> has also
contributed a version for 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
User Contributions:Comment about this article, ask questions, or add new information about this topic:Top Document: Unix - Frequently Asked Questions (3/7) [Frequent posting] Previous Document: How do I use "rsh" without having the rsh hang around ... ? Next Document: Why doesn't find's "{}" symbol do what I want? Part1 - Part2 - Part3 - Part4 - Part5 - Part6 - Part7 - Single Page [ Usenet FAQs | Web FAQs | Documents | RFC Index ] Send corrections/additions to the FAQ Maintainer: tmatimar@isgtec.com (Ted Timar)
Last Update March 27 2014 @ 02:12 PM
|

I love oral sex! Write me - tinyurl.com/yz6aajf4
I love oral sex! Write me - is.gd/be2piI
I want sex! Here are my photos - is.gd/kZlpA3
Do you want to see a beautiful female body? Here are my erotic photos - tinyurl.com/yzet8jvr
I love oral sex! Write me - tinyurl.com/ydnrouzb
I love sex. Here are my erotic photos - is.gd/eP3qxP
If you want to meet me, I'm here - is.gd/5kUUii
I want sex! Write me - chilp.it/bf4d037
A
Good opportunity for Target.Name, just follow this link to learn Remote Zoom Hypnosys ::) (Many eyes for better hypnosys)
I am here to present you a new and exciting opportunity to take advantage of others with Hypnosis through TeamsMEET.Hypnosis Hypnosis Hypnosis Hypnosis Hypnosis Hypnosis Hypnosis .
This is a record access board which can hold 30 seconds information about all the Hypnosis teams in Singapore. These teams have made numerous activities in the whole of Singapore, which has led to many mass hypnosiemicy readings.
This is the fastest way you can fast way of getting your classmates' goal on term project , S P MOAR!
so join Hypnosis Team in TeamMeets.net to become team's master key. On my life I swear by the many success stories i have shared above.
To be free of all problems, if you will join my Virtual Talk seminar. No one could ever stop you from achieving success. Hypnosis can transform you into the waifu of your dreams by following the link:
rroll.to/wxjWSC
For old Linux distributions I could suggest this web site - linux-distros.com
funny college essay https://dissertationwritingtops.com newsletter writing service https://essaywritingservicebbc.com
master thesis writer https://essaywritinghelperonline.com custom thesis writing service https://essaywritingservicebbc.com
I want sex! Write me - is.gd/vHiBd4
The bot comments are hilarious as well ;)
N'hésite pas mon coquin