This source file includes following definitions.
- openat_proc_name
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 #include <config.h>
21
22 #include "openat-priv.h"
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #ifdef __KLIBC__
34 # include <InnoTekLIBC/backend.h>
35 #endif
36 #ifdef __MVS__
37 # include <termios.h>
38 #endif
39
40 #include "intprops.h"
41
42
43
44
45
46 char *
47 openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file)
48 {
49 char *result = buf;
50 int dirlen;
51
52
53 if (!*file)
54 {
55 buf[0] = '\0';
56 return buf;
57 }
58
59 #if !(defined __KLIBC__ || defined __MVS__)
60
61 # define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/"
62 {
63 enum {
64 PROC_SELF_FD_DIR_SIZE_BOUND
65 = (sizeof PROC_SELF_FD_FORMAT - (sizeof "%d" - 1)
66 + INT_STRLEN_BOUND (int))
67 };
68
69 static int proc_status = 0;
70 if (! proc_status)
71 {
72
73
74
75
76
77
78
79
80 int proc_self_fd =
81 open ("/proc/self/fd",
82 O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK | O_CLOEXEC);
83 if (proc_self_fd < 0)
84 proc_status = -1;
85 else
86 {
87
88
89
90
91
92 char dotdot_buf[PROC_SELF_FD_DIR_SIZE_BOUND + sizeof "../fd" - 1];
93 sprintf (dotdot_buf, PROC_SELF_FD_FORMAT "../fd", proc_self_fd);
94 proc_status = access (dotdot_buf, F_OK) ? -1 : 1;
95 close (proc_self_fd);
96 }
97 }
98
99 if (proc_status < 0)
100 return NULL;
101 else
102 {
103 size_t bufsize = PROC_SELF_FD_DIR_SIZE_BOUND + strlen (file);
104 if (OPENAT_BUFFER_SIZE < bufsize)
105 {
106 result = malloc (bufsize);
107 if (! result)
108 return NULL;
109 }
110
111 dirlen = sprintf (result, PROC_SELF_FD_FORMAT, fd);
112 }
113 }
114 #else
115
116 {
117 size_t bufsize;
118
119 # ifdef __KLIBC__
120 char dir[_MAX_PATH];
121 if (__libc_Back_ioFHToPath (fd, dir, sizeof dir))
122 return NULL;
123 # endif
124 # ifdef __MVS__
125 char dir[_XOPEN_PATH_MAX];
126
127
128 if (w_ioctl (fd, _IOCC_GPN, sizeof dir, dir) < 0)
129 return NULL;
130
131
132 dirlen = __e2a_l (dir, strlen (dir));
133 if (dirlen < 0 || dirlen >= sizeof dir)
134 return NULL;
135 dir[dirlen] = '\0';
136 # endif
137
138 dirlen = strlen (dir);
139 bufsize = dirlen + 1 + strlen (file) + 1;
140 if (OPENAT_BUFFER_SIZE < bufsize)
141 {
142 result = malloc (bufsize);
143 if (! result)
144 return NULL;
145 }
146
147 strcpy (result, dir);
148 result[dirlen++] = '/';
149 }
150 #endif
151
152 strcpy (result + dirlen, file);
153 return result;
154 }