Mercurial > hg > octave-lyh > gnulib-hg
changeset 10606:442a5ff4bcc1
Reduce code duplication.
author | Paolo Bonzini <bonzini@gnu.org> |
---|---|
date | Thu, 09 Oct 2008 13:31:16 +0200 |
parents | e47d71e06ebd |
children | e8a140ab4e53 |
files | ChangeLog lib/fchdir.c |
diffstat | 2 files changed, 46 insertions(+), 34 deletions(-) [+] |
line wrap: on
line diff
--- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2008-10-09 Paolo Bonzini <bonzini@gnu.org> + Bruno Haible <bruno@clisp.org> + + * lib/fchdir.c (_gl_unregister_fd): New functions, extracted from + rpl_close. + (_gl_register_fd): New function, extracted from rpl_open. + (rpl_close, rpl_closedir): Use _gl_unregister_fd. + (rpl_open, rpl_opendir): Use _gl_register_fd. + 2008-10-09 Paolo Bonzini <bonzini@gnu.org> Fix organization of 'open' replacement.
--- a/lib/fchdir.c +++ b/lib/fchdir.c @@ -1,5 +1,5 @@ /* fchdir replacement. - Copyright (C) 2006, 2007 Free Software Foundation, Inc. + Copyright (C) 2006-2008 Free Software Foundation, Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -75,6 +75,36 @@ } } +/* Hook into the gnulib replacements for open() and close() to keep track + of the open file descriptors. */ + +void +_gl_unregister_fd (int fd) +{ + if (fd >= 0 && fd < dirs_allocated) + { + if (dirs[fd].name != NULL) + free (dirs[fd].name); + dirs[fd].name = NULL; + dirs[fd].saved_errno = ENOTDIR; + } +} + +void +_gl_register_fd (int fd, const char *filename) +{ + struct stat statbuf; + + ensure_dirs_slot (fd); + if (fd < dirs_allocated + && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode)) + { + dirs[fd].name = canonicalize_file_name (filename); + if (dirs[fd].name == NULL) + dirs[fd].saved_errno = errno; + } +} + /* Override open() and close(), to keep track of the open file descriptors. */ int @@ -83,13 +113,8 @@ { int retval = close (fd); - if (retval >= 0 && fd >= 0 && fd < dirs_allocated) - { - if (dirs[fd].name != NULL) - free (dirs[fd].name); - dirs[fd].name = NULL; - dirs[fd].saved_errno = ENOTDIR; - } + if (retval >= 0) + _gl_unregister_fd (fd); return retval; } @@ -122,16 +147,7 @@ #endif fd = open (filename, flags, mode); if (fd >= 0) - { - ensure_dirs_slot (fd); - if (fd < dirs_allocated - && fstat (fd, &statbuf) >= 0 && S_ISDIR (statbuf.st_mode)) - { - dirs[fd].name = canonicalize_file_name (filename); - if (dirs[fd].name == NULL) - dirs[fd].saved_errno = errno; - } - } + _gl_register_fd (fd, filename); return fd; } @@ -145,13 +161,8 @@ int fd = dirfd (dp); int retval = closedir (dp); - if (retval >= 0 && fd >= 0 && fd < dirs_allocated) - { - if (dirs[fd].name != NULL) - free (dirs[fd].name); - dirs[fd].name = NULL; - dirs[fd].saved_errno = ENOTDIR; - } + if (retval >= 0) + _gl_unregister_fd (fd); return retval; } @@ -166,15 +177,7 @@ { int fd = dirfd (dp); if (fd >= 0) - { - ensure_dirs_slot (fd); - if (fd < dirs_allocated) - { - dirs[fd].name = canonicalize_file_name (filename); - if (dirs[fd].name == NULL) - dirs[fd].saved_errno = errno; - } - } + _gl_register_fd (fd, filename); } return dp; }