home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / std / c / 2626 < prev    next >
Encoding:
Text File  |  1992-09-14  |  2.3 KB  |  53 lines

  1. Newsgroups: comp.std.c
  2. Path: sparky!uunet!decwrl!pa.dec.com!nntpd2.cxo.dec.com!nntpd.lkg.dec.com!jit345.bad.jit.dec.com!diamond
  3. From: diamond@jit345.bad.jit.dec.com (Norman Diamond)
  4. Subject: Re: POSIX version of chsize/ftruncate?
  5. Message-ID: <1992Sep15.041437.27427@nntpd.lkg.dec.com>
  6. Sender: usenet@nntpd.lkg.dec.com (USENET News System)
  7. Reply-To: diamond@jit.dec.com (Norman Diamond)
  8. Organization: Digital Equipment Corporation Japan , Tokyo
  9. References: <BuL6E4.2wq@csn.org>
  10. Date: Tue, 15 Sep 1992 04:14:37 GMT
  11. Lines: 40
  12.  
  13. In article <BuL6E4.2wq@csn.org> c4craig@teal.csn.org (Craig Anderson) writes:
  14. >How should I port a DOS call to chsize() to STD C / POSIX?
  15. >In SunOS and BSD/386 I find:
  16. >int ftruncate(int fd, off_t length)
  17. >  truncate a file to a specified length
  18.  
  19. I hope that the POSIX standard has ftruncate(), but let's hope this will
  20. be answered by someone who knows.
  21.  
  22. If you want to be portable to most ANSI C implementations (including those
  23. which are not POSIX conforming) then the answer is ugly.  Use fopen() to
  24. create a new file with some name that you think will be acceptable (e.g.
  25. will be located on the same physical device and partition and probably
  26. directory as the old file), copy the contents of the old file to the new
  27. one, stop where you want it truncated, fclose() the new file, remove() the
  28. old one, and rename() the new one.  There is no guarantee that this will
  29. work (disk space might be exceeded or the implementation can return failure
  30. status for any reasons it wishes) but this is as close as you can get.
  31. This will probably lose the file permissions and various other OS-dependent
  32. stuff that were set on the old file.
  33.  
  34. I would define a few flags at compile time and do stuff like
  35. #if have_unix_fds && have_ftruncate
  36.   status = ftruncate(fileno(my_file), where);
  37. #else
  38.   all that garbage
  39. #endif
  40.  
  41. >In DOS Turbo C++ I find:
  42. >int chsize(int handle,k long size)
  43. >  Changes the file size.
  44.  
  45. If you only want to port to POSIX conforming systems, you'll have UNIX-style
  46. file descriptor numbers (which are probably the same as handles) and, I hope,
  47. ftruncate.  If you want to port to all ANSI C implementations, you'll have
  48. to change all of your handles to use stdio-style files.
  49. --
  50. Norman Diamond       diamond@jit081.enet.dec.com
  51. If this were the company's opinion, I wouldn't be allowed to post it.
  52. "Yeah -- bad wiring.  That was probably it.  Very bad."
  53.