/[cvs]/ccvs/lib/openat.c
ViewVC logotype

Diff of /ccvs/lib/openat.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by dprice, Mon Sep 19 21:26:13 2005 UTC revision 1.5 by dprice, Tue Oct 4 02:34:54 2005 UTC
# Line 29  Line 29 
29  #include <errno.h>  #include <errno.h>
30  #include <fcntl.h>  #include <fcntl.h>
31    
32  #include "error.h"  #include "dirname.h" /* solely for definition of IS_ABSOLUTE_FILE_NAME */
 #include "exitfail.h"  
33  #include "save-cwd.h"  #include "save-cwd.h"
34    
35  #include "gettext.h"  #include "gettext.h"
# Line 64  rpl_openat (int fd, char const *file, in Line 63  rpl_openat (int fd, char const *file, in
63        va_end (arg);        va_end (arg);
64      }      }
65    
66    if (fd == AT_FDCWD || *file == '/')    if (fd == AT_FDCWD || IS_ABSOLUTE_FILE_NAME (file))
67      return open (file, flags, mode);      return open (file, flags, mode);
68    
69    if (save_cwd (&saved_cwd) != 0)    if (save_cwd (&saved_cwd) != 0)
70      error (exit_failure, errno,      openat_save_fail (errno);
            _("openat: unable to record current working directory"));  
71    
72    if (fchdir (fd) != 0)    if (fchdir (fd) != 0)
73      {      {
# Line 83  rpl_openat (int fd, char const *file, in Line 81  rpl_openat (int fd, char const *file, in
81    saved_errno = errno;    saved_errno = errno;
82    
83    if (restore_cwd (&saved_cwd) != 0)    if (restore_cwd (&saved_cwd) != 0)
84      error (exit_failure, errno,      openat_restore_fail (errno);
            _("openat: unable to restore working directory"));  
85    
86    free_cwd (&saved_cwd);    free_cwd (&saved_cwd);
87    
# Line 92  rpl_openat (int fd, char const *file, in Line 89  rpl_openat (int fd, char const *file, in
89    return new_fd;    return new_fd;
90  }  }
91    
92    #if !HAVE_FDOPENDIR
93    
94  /* Replacement for Solaris' function by the same name.  /* Replacement for Solaris' function by the same name.
95     <http://www.google.com/search?q=fdopendir+site:docs.sun.com>     <http://www.google.com/search?q=fdopendir+site:docs.sun.com>
96     Simulate it by doing save_cwd/fchdir/opendir(".")/restore_cwd.     Simulate it by doing save_cwd/fchdir/opendir(".")/restore_cwd.
97     If either the save_cwd or the restore_cwd fails (relatively unlikely,     If either the save_cwd or the restore_cwd fails (relatively unlikely,
98     and usually indicative of a problem that deserves close attention),     and usually indicative of a problem that deserves close attention),
99     then give a diagnostic and exit nonzero.     then give a diagnostic and exit nonzero.
100     Otherwise, this function works just like Solaris' fdopendir.  */     Otherwise, this function works just like Solaris' fdopendir.
101    
102       W A R N I N G:
103       Unlike the other fd-related functions here, this one
104       effectively consumes its FD parameter.  The caller should not
105       close or otherwise manipulate FD if this function returns successfully.  */
106  DIR *  DIR *
107  fdopendir (int fd)  fdopendir (int fd)
108  {  {
# Line 106  fdopendir (int fd) Line 110  fdopendir (int fd)
110    int saved_errno;    int saved_errno;
111    DIR *dir;    DIR *dir;
112    
   if (fd == AT_FDCWD)  
     return opendir (".");  
   
113    if (save_cwd (&saved_cwd) != 0)    if (save_cwd (&saved_cwd) != 0)
114      error (exit_failure, errno,      openat_save_fail (errno);
            _("fdopendir: unable to record current working directory"));  
115    
116    if (fchdir (fd) != 0)    if (fchdir (fd) != 0)
117      {      {
# Line 125  fdopendir (int fd) Line 125  fdopendir (int fd)
125    saved_errno = errno;    saved_errno = errno;
126    
127    if (restore_cwd (&saved_cwd) != 0)    if (restore_cwd (&saved_cwd) != 0)
128      error (exit_failure, errno,      openat_restore_fail (errno);
            _("fdopendir: unable to restore working directory"));  
129    
130    free_cwd (&saved_cwd);    free_cwd (&saved_cwd);
131      if (dir)
132        close (fd);
133    
134    errno = saved_errno;    errno = saved_errno;
135    return dir;    return dir;
136  }  }
137    
138    #endif
139    
140  /* Replacement for Solaris' function by the same name.  /* Replacement for Solaris' function by the same name.
141     <http://www.google.com/search?q=fstatat+site:docs.sun.com>     <http://www.google.com/search?q=fstatat+site:docs.sun.com>
142     Simulate it by doing save_cwd/fchdir/(stat|lstat)/restore_cwd.     Simulate it by doing save_cwd/fchdir/(stat|lstat)/restore_cwd.
# Line 154  fstatat (int fd, char const *file, struc Line 157  fstatat (int fd, char const *file, struc
157              : stat (file, st));              : stat (file, st));
158    
159    if (save_cwd (&saved_cwd) != 0)    if (save_cwd (&saved_cwd) != 0)
160      error (exit_failure, errno,      openat_save_fail (errno);
            _("fstatat: unable to record current working directory"));  
161    
162    if (fchdir (fd) != 0)    if (fchdir (fd) != 0)
163      {      {
# Line 171  fstatat (int fd, char const *file, struc Line 173  fstatat (int fd, char const *file, struc
173    saved_errno = errno;    saved_errno = errno;
174    
175    if (restore_cwd (&saved_cwd) != 0)    if (restore_cwd (&saved_cwd) != 0)
176      error (exit_failure, errno,      openat_restore_fail (errno);
177             _("fstatat: unable to restore working directory"));  
178      free_cwd (&saved_cwd);
179    
180      errno = saved_errno;
181      return err;
182    }
183    
184    /* Replacement for Solaris' function by the same name.
185       <http://www.google.com/search?q=unlinkat+site:docs.sun.com>
186       Simulate it by doing save_cwd/fchdir/(unlink|rmdir)/restore_cwd.
187       If either the save_cwd or the restore_cwd fails (relatively unlikely,
188       and usually indicative of a problem that deserves close attention),
189       then give a diagnostic and exit nonzero.
190       Otherwise, this function works just like Solaris' unlinkat.  */
191    int
192    unlinkat (int fd, char const *file, int flag)
193    {
194      struct saved_cwd saved_cwd;
195      int saved_errno;
196      int err;
197    
198      if (fd == AT_FDCWD)
199        return (flag == AT_REMOVEDIR ? rmdir (file) : unlink (file));
200    
201      if (save_cwd (&saved_cwd) != 0)
202        openat_save_fail (errno);
203    
204      if (fchdir (fd) != 0)
205        {
206          saved_errno = errno;
207          free_cwd (&saved_cwd);
208          errno = saved_errno;
209          return -1;
210        }
211    
212      err = (flag == AT_REMOVEDIR ? rmdir (file) : unlink (file));
213      saved_errno = errno;
214    
215      if (restore_cwd (&saved_cwd) != 0)
216        openat_restore_fail (errno);
217    
218    free_cwd (&saved_cwd);    free_cwd (&saved_cwd);
219    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.5

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26