www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1993/04/18/18:25:31

Date: Fri, 16 Apr 93 22:13:40 +1000
From: graeme AT labtam DOT labtam DOT oz DOT au (Graeme Gill)
To: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Bugs in open(), close(), truncate()


	I am trying to get a utility going under djgpp 1.09,
and have discovered a problem with truncating files.
The following program fails to truncate a file :

------------------------ cut here -------------------------
#include <fcntl.h>
#include <stdio.h>

main(argc,argv)
int argc;
char *argv[];
	{
	int file;
	int len;

	if (argc != 3)
		{
		fprintf(stderr,"%s: filename length\n",argv[0]);
		exit(-1);
		}
	
	len = atoi(argv[2]);
	printf("Attempting to truncate file '%s' to length %d\n",argv[1],len);

	truncate(argv[1], len);
	}
------------------------ cut here -------------------------

This seems to work OK on a SUN workstation. It doesn't work
even when the open() lseek() write() and close() equivalent
is used. I suspect there is a problem with write() or close().

In addition, it appears that truncate() and ftruncate() do
not return proper status. 

According to my BSD manual:

	truncate()
	A value of 0 is returned if the call succeeds. If the call fails a
	-1 is returned, and the global variable errno specifies the error.

If we examine truncate.c, we see that:
lseek return value is not checked, write status
is not checked, close status is not checked,
and no value is returned, except for an open error.

truncate(const char *fn, unsigned long where)
{
  int fd = open(fn, O_WRONLY);
  if (fd < 0)
    return -1;
  lseek(fd, where, 0);
  write(fd, 0, 0);
  close(fd);
}

ftruncate() also make use of lseek() and
write() without checking return values, or
returning error indication itself.

	I'd offer to investigate these problems
and send some patches, but no-one has been able to
tell me exactly how I can re-build the gcc libraries
without a working MSDOS version of gmake.

	Graeme Gill
	graeme AT labtam DOT oz DOT au




- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019