Update from coreutils.
[gnulib.git] / lib / openat-priv.h
1 /* macros used by openat-like functions
2    Copyright (C) 2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* written by Jim Meyering */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <errno.h>
23 #include "alloca.h"
24 #include "intprops.h"
25
26 /* Set PROC_FD_FILENAME to the expansion of "/proc/self/fd/%d/%s" in
27    alloca'd memory, using FD and FILE, respectively for %d and %s. */
28 #define BUILD_PROC_NAME(Proc_fd_filename, Fd, File)                     \
29   do                                                                    \
30     {                                                                   \
31       size_t filelen = strlen (File);                                   \
32       static const char procfd[] = "/proc/self/fd/%d/%s";               \
33       /* Buffer for the file name we are going to use.  It consists of  \
34          - the string /proc/self/fd/                                    \
35          - the file descriptor number                                   \
36          - the file name provided.                                      \
37          The final NUL is included in the sizeof.                       \
38          Subtract 4 to account for %d and %s.  */                       \
39       size_t buflen = sizeof (procfd) - 4 + INT_STRLEN_BOUND (Fd) + filelen; \
40       (Proc_fd_filename) = alloca (buflen);                             \
41       snprintf ((Proc_fd_filename), buflen, procfd, (Fd), (File));      \
42     }                                                                   \
43   while (0)
44
45 /* Some systems don't have ENOSYS.  */
46 #ifndef ENOSYS
47 # ifdef ENOTSUP
48 #  define ENOSYS ENOTSUP
49 # else
50 /* Some systems don't have ENOTSUP either.  */
51 #  define ENOSYS EINVAL
52 # endif
53 #endif
54
55 /* Trying to access a BUILD_PROC_NAME file will fail on systems without
56    /proc support, and even on systems *with* ProcFS support.  Return
57    nonzero if the failure may be legitimate, e.g., because /proc is not
58    readable, or the particular .../fd/N directory is not present.  */
59 #define EXPECTED_ERRNO(Errno)                   \
60   ((Errno) == ENOTDIR || (Errno) == ENOENT      \
61    || (Errno) == EPERM || (Errno) == EACCES     \
62    || (Errno) == ENOSYS /* Solaris 8 */         \
63    || (Errno) == EOPNOTSUPP /* FreeBSD */)