Mail Archives: djgpp/2009/05/30/17:00:17

X-Authentication-Warning: mail set sender to djgpp-bounces using -f
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Using rename() with files still opened.
Date: Sat, 30 May 2009 13:51:40 -0700 (PDT)
Lines: 94
Message-ID: <>
Mime-Version: 1.0
X-Trace: 1243716700 5485 (30 May 2009 20:51:40 GMT)
X-Complaints-To: groups-abuse AT google DOT com
NNTP-Posting-Date: Sat, 30 May 2009 20:51:40 +0000 (UTC)
Complaints-To: groups-abuse AT google DOT com
Injection-Info:; posting-host=;
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET
CLR 2.0.50727),gzip(gfe),gzip(gfe)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Please note the following code snippet:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

int main(void)
  FILE *source_file, *target_file;
  char source_file_name[FILENAME_MAX], target_file_name[FILENAME_MAX];
  int fd, status;

  sprintf(target_file_name, "./targetXXXXXX");
  fd = mkstemp(target_file_name);
  target_file = fdopen (fd, "w");
  fprintf(target_file, "target file = \"%s\"", target_file_name);

  target_file = fopen(target_file_name, "rt");

  sprintf(source_file_name, "./sourceXXXXXX");
  fd = mkstemp(source_file_name);
  source_file = fdopen (fd, "w");
  fprintf(source_file, "source file = \"%s\"", source_file_name);

  errno = 0;
  status = rename(source_file_name, target_file_name);
  printf("rename status=%d   errno=%d\n", status, errno);

  return 0;

The goal is to rename the source file into the target file.  With
rename() and _rename() functions this fails because the target file is
still open.  Of course, if the line:
  target_file = fopen(target_file_name, "rt");
is removed then the codes works flawlessly.  An inspection of
shows that function 0x7156 cannot rename the still opened target file.
After failing _rename tries a second time but before it tries to
the target file but this also fails to due to the same reasson, so the
whole function does not work very well in this particular case when
target file has not been closed before the function call has been
On my linux box the code snippet works no matter if the target file to
replaced by the source file has been closed or not.  Now the question
arises if this is a bug or a feature?
Please note that the issue is not trivial.  I have noticed this while
was trying to port GNU sed 4.2.  This sed version and also the
one has a new option "-i" called something like "in place replacement"
or something similar.  That means that it will be possible to start
like this:
  sed -i s/foobar/raboof/ 1.txt
and this will perform the ordered subtitution but instead of writting
to stdout it will replace the source file (1.txt) with the result of
sed program.  Of course, this is done by storing the output of the sed
program in some temporary file and then renaming/replacing the input
with the tempory file by renaming it to the input file name.  For some
reason I have still not fully understood, sed does not fclose the
file before trying to replace it with the output file.  As explained
before, this causes no difficulty on linux but fails misserably when
is compiled with DJGPP.
The question arises if _rename() shall be modified in such a way that
the file corresponding to the new file name is fclose/closed before
trying to replace it with the old file.  Because in case of success
file corrresponding to the new file name *before* calling _rename()
be deleted and replaced anyway, I think that closing it without
the function that called _rename() will not harm.
Some comments on this issue would be appreciated.

Juan M. Guerrero

- Raw text -

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