Sync 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 "alloca.h"
23 #include "intprops.h"
24
25 /* Set PROC_FD_FILENAME to the expansion of "/proc/self/fd/%d/%s" in
26    alloca'd memory, using FD and FILE, respectively for %d and %s. */
27 #define BUILD_PROC_NAME(Proc_fd_filename, Fd, File)                     \
28   do                                                                    \
29     {                                                                   \
30       size_t filelen = strlen (File);                                   \
31       static const char procfd[] = "/proc/self/fd/%d/%s";               \
32       /* Buffer for the file name we are going to use.  It consists of  \
33          - the string /proc/self/fd/                                    \
34          - the file descriptor number                                   \
35          - the file name provided.                                      \
36          The final NUL is included in the sizeof.                       \
37          Subtract 4 to account for %d and %s.  */                       \
38       size_t buflen = sizeof (procfd) - 4 + INT_STRLEN_BOUND (Fd) + filelen; \
39       (Proc_fd_filename) = alloca (buflen);                             \
40       snprintf ((Proc_fd_filename), buflen, procfd, (Fd), (File));      \
41     }                                                                   \
42   while (0)
43
44 /* Trying to access a BUILD_PROC_NAME file will fail on systems without
45    /proc support, and even on systems *with* ProcFS support.  Return
46    nonzero if the failure may be legitimate, e.g., because /proc is not
47    readable, or the particular .../fd/N directory is not present.  */
48 #define EXPECTED_ERRNO(Errno)                   \
49   ((Errno) == ENOTDIR || (Errno) == ENOENT      \
50    || (Errno) == EPERM || (Errno) == EACCES     \
51    || (Errno) == ENOSYS /* Solaris 8 */         \
52    || (Errno) == EOPNOTSUPP /* FreeBSD */)