* modules/chdir-long, modules/openat: New files.
[gnulib.git] / lib / openat.c
1 /* provide a replacement openat function
2    Copyright (C) 2004 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* written by Jim Meyering */
19
20 #include <config.h>
21
22 #include "openat.h"
23
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <fcntl.h>
29
30 #include "error.h"
31 #include "exitfail.h"
32 #include "save-cwd.h"
33
34 #include "gettext.h"
35 #define _(msgid) gettext (msgid)
36
37 /* Replacement for Solaris' openat function.
38    <http://www.google.com/search?q=openat+site:docs.sun.com>
39    Simulate it by doing save_cwd/fchdir/open/restore_cwd.
40    If either the save_cwd or the restore_cwd fails (relatively unlikely,
41    and usually indicative of a problem that deserves close attention),
42    then give a diagnostic and exit nonzero.
43    Otherwise, upon failure, set errno and return -1, as openat does.
44    Upon successful completion, return a file descriptor.  */
45 int
46 rpl_openat (int fd, char const *filename, int flags, ...)
47 {
48   struct saved_cwd saved_cwd;
49   int saved_errno;
50   int new_fd;
51   mode_t mode = 0;
52
53   if (flags & O_CREAT)
54     {
55       va_list arg;
56       va_start (arg, flags);
57
58       /* Assume that mode_t is passed compatibly with mode_t's type
59          after argument promotion.  */
60       mode = va_arg (arg, mode_t);
61
62       va_end (arg);
63     }
64
65   if (fd == AT_FDCWD || *filename == '/')
66     return open (filename, flags, mode);
67
68   if (save_cwd (&saved_cwd) != 0)
69     error (exit_failure, errno,
70            _("openat: unable to record current working directory"));
71
72   if (fchdir (fd) != 0)
73     {
74       saved_errno = errno;
75       free_cwd (&saved_cwd);
76       errno = saved_errno;
77       return -1;
78     }
79
80   new_fd = open (filename, flags, mode);
81   saved_errno = errno;
82
83   if (restore_cwd (&saved_cwd) != 0)
84     error (exit_failure, errno,
85            _("openat: unable to restore working directory"));
86
87   free_cwd (&saved_cwd);
88
89   errno = saved_errno;
90   return new_fd;
91 }