| 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 |
|
| 37 |
#include "intprops.h" |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
char * |
| 44 |
openat_proc_name (char buf[OPENAT_BUFFER_SIZE], int fd, char const *file) |
| 45 |
{ |
| 46 |
char *result = buf; |
| 47 |
int dirlen; |
| 48 |
|
| 49 |
|
| 50 |
if (!*file) |
| 51 |
{ |
| 52 |
buf[0] = '\0'; |
| 53 |
return buf; |
| 54 |
} |
| 55 |
|
| 56 |
#ifndef __KLIBC__ |
| 57 |
# define PROC_SELF_FD_FORMAT "/proc/self/fd/%d/" |
| 58 |
{ |
| 59 |
enum { |
| 60 |
PROC_SELF_FD_DIR_SIZE_BOUND |
| 61 |
= (sizeof PROC_SELF_FD_FORMAT - (sizeof "%d" - 1) |
| 62 |
+ INT_STRLEN_BOUND (int)) |
| 63 |
}; |
| 64 |
|
| 65 |
static int proc_status = 0; |
| 66 |
if (! proc_status) |
| 67 |
{ |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
int proc_self_fd = open ("/proc/self/fd", |
| 77 |
O_SEARCH | O_DIRECTORY | O_NOCTTY | O_NONBLOCK); |
| 78 |
if (proc_self_fd < 0) |
| 79 |
proc_status = -1; |
| 80 |
else |
| 81 |
{ |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
char dotdot_buf[PROC_SELF_FD_DIR_SIZE_BOUND + sizeof "../fd" - 1]; |
| 88 |
sprintf (dotdot_buf, PROC_SELF_FD_FORMAT "../fd", proc_self_fd); |
| 89 |
proc_status = access (dotdot_buf, F_OK) ? -1 : 1; |
| 90 |
close (proc_self_fd); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
if (proc_status < 0) |
| 95 |
return NULL; |
| 96 |
else |
| 97 |
{ |
| 98 |
size_t bufsize = PROC_SELF_FD_DIR_SIZE_BOUND + strlen (file); |
| 99 |
if (OPENAT_BUFFER_SIZE < bufsize) |
| 100 |
{ |
| 101 |
result = malloc (bufsize); |
| 102 |
if (! result) |
| 103 |
return NULL; |
| 104 |
} |
| 105 |
|
| 106 |
dirlen = sprintf (result, PROC_SELF_FD_FORMAT, fd); |
| 107 |
} |
| 108 |
} |
| 109 |
#else |
| 110 |
|
| 111 |
{ |
| 112 |
char dir[_MAX_PATH]; |
| 113 |
size_t bufsize; |
| 114 |
|
| 115 |
if (__libc_Back_ioFHToPath (fd, dir, sizeof dir)) |
| 116 |
return NULL; |
| 117 |
|
| 118 |
dirlen = strlen (dir); |
| 119 |
bufsize = dirlen + 1 + strlen (file) + 1; |
| 120 |
if (OPENAT_BUFFER_SIZE < bufsize) |
| 121 |
{ |
| 122 |
result = malloc (bufsize); |
| 123 |
if (! result) |
| 124 |
return NULL; |
| 125 |
} |
| 126 |
|
| 127 |
strcpy (result, dir); |
| 128 |
result[dirlen++] = '/'; |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
strcpy (result + dirlen, file); |
| 133 |
return result; |
| 134 |
} |