Article View: comp.std.c
Article #1201Re: POSIX version of chsize/ftruncate?
From: Norman Diamond
Date: Tue, 15 Sep 1992 04:14
Date: Tue, 15 Sep 1992 04:14
40 lines
2434 bytes
2434 bytes
In article <BuL6E4.2wq@csn.org> c4craig@teal.csn.org (Craig Anderson) writes: >How should I port a DOS call to chsize() to STD C / POSIX? >In SunOS and BSD/386 I find: >int ftruncate(int fd, off_t length) > truncate a file to a specified length I hope that the POSIX standard has ftruncate(), but let's hope this will be answered by someone who knows. If you want to be portable to most ANSI C implementations (including those which are not POSIX conforming) then the answer is ugly. Use fopen() to create a new file with some name that you think will be acceptable (e.g. will be located on the same physical device and partition and probably directory as the old file), copy the contents of the old file to the new one, stop where you want it truncated, fclose() the new file, remove() the old one, and rename() the new one. There is no guarantee that this will work (disk space might be exceeded or the implementation can return failure status for any reasons it wishes) but this is as close as you can get. This will probably lose the file permissions and various other OS-dependent stuff that were set on the old file. I would define a few flags at compile time and do stuff like #if have_unix_fds && have_ftruncate status = ftruncate(fileno(my_file), where); #else all that garbage #endif >In DOS Turbo C++ I find: >int chsize(int handle,k long size) > Changes the file size. If you only want to port to POSIX conforming systems, you'll have UNIX-style file descriptor numbers (which are probably the same as handles) and, I hope, ftruncate. If you want to port to all ANSI C implementations, you'll have to change all of your handles to use stdio-style files. -- Norman Diamond diamond@jit081.enet.dec.com If this were the company's opinion, I wouldn't be allowed to post it. "Yeah -- bad wiring. That was probably it. Very bad."
Technical Details
Message-ID:
<1992Sep15.041437.27427@nntpd.lkg.dec.com>Path:
arc1-ca.pugleaf.net!sparky!uunet!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamondReferences:
<BuL6E4.2wq@csn.org>Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c Path: arc1-ca.pugleaf.net!sparky!uunet!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond From: diamond@jit345.bad.jit.dec.com (Norman Diamond) Subject: Re: POSIX version of chsize/ftruncate? Message-ID: <1992Sep15.041437.27427@nntpd.lkg.dec.com> Sender: usenet@nntpd.lkg.dec.com (USENET News System) Reply-To: diamond@jit.dec.com (Norman Diamond) Organization: Digital Equipment Corporation Japan , Tokyo References: <BuL6E4.2wq@csn.org> Date: Tue, 15 Sep 1992 04:14:37 GMT Lines: 40