From: "Mark E." To: djgpp-workers AT delorie DOT com Date: Tue, 13 Mar 2001 00:03:13 -0500 MIME-Version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Subject: exit handler for fd_props.c Message-ID: <3AAD63C1.14365.38DA4A@localhost> X-mailer: Pegasus Mail for Win32 (v3.12c) Reply-To: djgpp-workers AT delorie DOT com This patch adds an exit handler for those fds that have a fd_properties struct associated with it. Problems or objections? Index: ansi/stdlib/exit.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/ansi/stdlib/exit.c,v retrieving revision 1.5 diff -c -p -r1.5 exit.c *** exit.c 1999/06/03 17:27:33 1.5 --- exit.c 2001/03/13 04:56:40 *************** void (*__stdio_cleanup_hook)(void); *** 18,23 **** --- 18,26 ---- This does not force them to be linked in. */ void (*__FSEXT_exit_hook)(void) = NULL; + /* A hook to close those file descriptors with properties. */ + void (*__fd_properties_cleanup_hook)(void) = NULL; + typedef void (*FUNC)(void); extern FUNC djgpp_first_dtor[] __asm__("djgpp_first_dtor"); extern FUNC djgpp_last_dtor[] __asm__("djgpp_last_dtor"); *************** exit(int status) *** 51,56 **** --- 54,62 ---- /* Do this after the stdio cleanup to let it close off the fopen'd files */ if (__FSEXT_exit_hook) __FSEXT_exit_hook(); + + if (__fd_properties_cleanup_hook) + __fd_properties_cleanup_hook(); /* in case the program set it this way */ setmode(0, O_TEXT); Index: dos/io/fd_props.c =================================================================== RCS file: /cvs/djgpp/djgpp/src/libc/dos/io/fd_props.c,v retrieving revision 1.1 diff -c -p -r1.1 fd_props.c *** fd_props.c 2001/03/07 05:34:26 1.1 --- fd_props.c 2001/03/13 04:56:57 *************** *** 7,15 **** #include #include #include ! #include static void open_fd(fd_properties *fd, int open_flags); static void close_fd(fd_properties *fd); static fd_properties * find_eq_filename(const fd_properties *fd); --- 7,17 ---- #include #include #include ! #include #include + extern void (*__fd_properties_cleanup_hook); + static void open_fd(fd_properties *fd, int open_flags); static void close_fd(fd_properties *fd); static fd_properties * find_eq_filename(const fd_properties *fd); *************** static fd_properties * alloc_fd_properti *** 17,22 **** --- 19,25 ---- static void free_fd_properties(fd_properties *); static void insert_list(fd_properties **, fd_properties *); static void remove_list(fd_properties **, fd_properties *); + static void cleanup(void); /* Array of pointers to fd_properties objects associated with a file descriptor. */ *************** __set_fd_properties(int fd, const char * *** 72,77 **** --- 75,81 ---- if (__fd_properties == NULL) return -1; memset(__fd_properties, 0, size); + __fd_properties_cleanup_hook = cleanup; } /* This function may be called twice for the same fd, *************** remove_list(fd_properties **head_ptr, fd *** 210,212 **** --- 214,251 ---- *head_ptr = head; } + + static void + cleanup(void) + { + int fd; + fd_properties *ptr, *cur; + + if (__fd_properties == NULL) + return; + + if (active_fds) + { + fd = 0; + while (fd < 255) + { + if (__fd_properties[fd]) + close(fd); + ++fd; + } + } + + ptr = cached_fds; + while (ptr) + { + cur = ptr; + ptr = ptr->next; + free(cur); + } + free(__fd_properties); + __fd_properties = NULL; + active_fds = NULL; + cached_fds = NULL; + __cleanup_fd_properties = NULL; + } +