delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2001/02/14/14:40:56

From: "Mark E." <snowball3 AT bigfoot DOT com>
To: djgpp-workers AT delorie DOT com
Date: Wed, 14 Feb 2001 14:40:46 -0500
MIME-Version: 1.0
Subject: O_TEMPORARY
Message-ID: <3A8A98EE.30071.A8D978@localhost>
X-mailer: Pegasus Mail for Win32 (v3.12c)
Reply-To: djgpp-workers AT delorie DOT com

Hi guys,
Below is a first try at a patch to support O_TEMPORARY currently supported by 
Cygwin and Mingw32. A file opened with O_TEMPORARY will be deleted after the 
last descriptor referring to it has been closed.

*** /djgpp/include/fcntl.h.orig	Wed Feb 14 14:30:06 2001
--- /djgpp/include/fcntl.h	Sat Feb 10 11:24:04 2001
*************** int	fcntl(int _fildes, int _cmd, ...);
*** 77,82 ****
--- 77,84 ----
  #define O_NOLINK        0x4000
  #define O_NOFOLLOW      0x8000
  
+ #define O_TEMPORARY	0x10000 /* Delete file after closing.  */
+ 
  #define SH_COMPAT	0x0000
  #define SH_DENYRW	0x0010
  #define SH_DENYWR	0x0020
*** /dev/null	Wed Feb 14 14:33:09 2001
--- delonclo.c	Wed Feb 14 13:34:16 2001
***************
*** 0 ****
--- 1,52 ----
+ #include <stdlib.h>
+ #include <limits.h>
+ #include <strings.h>
+ #include <sys/stat.h>
+ #include <libc/delonclo.h>
+ 
+ o_temporary_file_rec **__o_temporary_files = NULL;
+ 
+ void
+ _set_file_delete_on_close_attr(int fd, const char *file)
+ {
+   if (file)
+   {
+     char buffer[PATH_MAX + 1];
+     int len;
+     if (__o_temporary_files == NULL)
+     {
+       size_t size = 255 * sizeof(o_temporary_file_rec *);
+       __o_temporary_files = malloc(size);
+       memset(__o_temporary_files, 0, size);
+     }
+     _fixpath(file, buffer);
+     len = strlen(buffer);
+     __o_temporary_files[fd] = (o_temporary_file_rec *)malloc(1 + len + 1);
+     __o_temporary_files[fd]->ref_count = 1;
+     strcpy(__o_temporary_files[fd]->filename, buffer);
+   }
+   else
+   {
+     if (__o_temporary_files[fd] == NULL)
+       return;
+     if (--(__o_temporary_files[fd]->ref_count) == 0)
+     {
+       remove(__o_temporary_files[fd]->filename);
+       free(__o_temporary_files[fd]);
+     }
+     __o_temporary_files[fd] = NULL;
+   }
+ }
+ 
+ void
+ _dup_file_delete_on_close_attr(int from, int to)
+ {
+   if (__o_temporary_files[to])
+     _set_file_delete_on_close_attr(to, NULL);
+   if (__o_temporary_files[from])
+   {
+     __o_temporary_files[to] = __o_temporary_files[from];
+     ++(__o_temporary_files[to]->ref_count);
+   }
+ }
+ 
*** open.c.orig	Mon Aug 28 10:22:34 2000
--- open.c	Wed Feb 14 13:30:56 2001
***************
*** 18,23 ****
--- 18,24 ----
  #include <io.h>
  
  #include <libc/dosio.h>
+ #include <libc/delonclo.h>
  
  /* Extra share flags that can be indicated by the user */
  int __djgpp_share_flags;
*************** open(const char* filename, int oflag, ..
*** 152,157 ****
--- 153,161 ----
    if(oflag & O_APPEND)
      lseek(fd, 0, SEEK_END);
  
+   if (oflag & O_TEMPORARY)
+     _set_file_delete_on_close_attr(fd, real_name);
+     
    return fd;
  }
  
*** _close.c.orig	Thu Jan  1 19:12:50 1998
--- _close.c	Wed Feb 14 13:28:20 2001
***************
*** 1,3 ****
--- 1,4 ----
+ /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1998 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  #include <unistd.h>
***************
*** 7,18 ****
--- 8,22 ----
  #include <io.h>
  #include <sys/fsext.h>
  
+ #include <libc/farptrgs.h>
  #include <libc/dosio.h>
+ #include <libc/delonclo.h>
  
  int
  _close(int handle)
  {
    __dpmi_regs r;
+   unsigned char ref_count;
  
    __FSEXT_Function *func = __FSEXT_get_function(handle);
    if (func)
*************** _close(int handle)
*** 37,41 ****
--- 41,50 ----
      errno = EBADF;
      return -1;
    }
+ 
+   if (__o_temporary_files && __o_temporary_files[handle])
+     _set_file_delete_on_close_attr(handle, NULL);
+ 
    return 0;
  }
+ 
*** dup.c.orig	Sun Feb 26 19:43:08 1995
--- dup.c	Wed Feb 14 13:29:20 2001
***************
*** 1,3 ****
--- 1,4 ----
+ /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  #include <libc/stubs.h>
  #include <unistd.h>
***************
*** 5,10 ****
--- 6,12 ----
  #include <errno.h>
  #include <io.h>
  #include <libc/dosio.h>
+ #include <libc/delonclo.h>
  
  int
  dup(int fd)
*************** dup(int fd)
*** 19,23 ****
--- 21,29 ----
      return -1;
    }
    setmode(r.x.ax, __file_handle_modes[fd]);
+ 
+   if (__o_temporary_files && __o_temporary_files[fd])
+     _dup_file_delete_on_close_attr(fd, r.x.ax);
+ 
    return r.x.ax;
  }
*** dup2.c.orig	Sun Sep 29 06:20:56 1996
--- dup2.c	Wed Feb 14 13:30:38 2001
***************
*** 1,3 ****
--- 1,4 ----
+ /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  #include <libc/stubs.h>
***************
*** 7,12 ****
--- 8,14 ----
  #include <errno.h>
  #include <io.h>
  #include <libc/dosio.h>
+ #include <libc/delonclo.h>
  
  int
  dup2(int fd, int newfd)
*************** dup2(int fd, int newfd)
*** 25,29 ****
--- 27,35 ----
      return -1;
    }
    setmode(newfd, __file_handle_modes[fd]);
+ 
+   if (__o_temporary_files)
+     _dup_file_delete_on_close_attr(fd, newfd);
+ 
    return newfd;
  }
*** /dev/null	Wed Feb 14 14:33:09 2001
--- /djgpp/include/libc/delonclo.h	Wed Feb 14 13:28:00 2001
***************
*** 0 ****
--- 1,34 ----
+ /* Copyright (C) 2001 DJ Delorie, see COPYING.DJ for details */
+ #ifndef __dj_include_libc_delonclo_h__
+ #define __dj_include_libc_delonclo_h__
+ 
+ #ifdef __cplusplus
+ extern "C" {
+ #endif
+ 
+ #ifndef __dj_ENFORCE_ANSI_FREESTANDING
+ 
+ #ifndef __STRICT_ANSI__
+ 
+ #ifndef _POSIX_SOURCE
+ 
+ typedef struct
+ {
+   unsigned char ref_count __attribute__((packed));
+   char filename[0] __attribute__((packed));
+ } o_temporary_file_rec;
+ 
+ void _set_file_del_on_close_attr (int _fd, const char * _file);
+ void _dup_file_del_on_close_attr (int _from, int _to);
+ 
+ extern o_temporary_file_rec ** __o_temporary_files;
+ 
+ #endif /* !_POSIX_SOURCE */
+ #endif /* !__STRICT_ANSI__ */
+ #endif /* !__dj_ENFORCE_ANSI_FREESTANDING */
+ 
+ #ifdef __cplusplus
+ }
+ #endif
+ 
+ #endif /* __dj_include_libc_delonclo_h__  */

- Raw text -


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