| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
|
| 19 |
|
| 20 |
#include <unistd.h> |
| 21 |
|
| 22 |
#include <dirent.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <stdbool.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string.h> |
| 28 |
#include <sys/types.h> |
| 29 |
#include <sys/stat.h> |
| 30 |
|
| 31 |
#include "assure.h" |
| 32 |
#include "filename.h" |
| 33 |
#include "filenamecat.h" |
| 34 |
|
| 35 |
#ifndef REPLACE_OPEN_DIRECTORY |
| 36 |
# define REPLACE_OPEN_DIRECTORY 0 |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
typedef struct |
| 50 |
{ |
| 51 |
char *name; |
| 52 |
|
| 53 |
} dir_info_t; |
| 54 |
static dir_info_t *dirs; |
| 55 |
static size_t dirs_allocated; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
static bool |
| 61 |
ensure_dirs_slot (size_t fd) |
| 62 |
{ |
| 63 |
if (fd < dirs_allocated) |
| 64 |
free (dirs[fd].name); |
| 65 |
else |
| 66 |
{ |
| 67 |
size_t new_allocated; |
| 68 |
dir_info_t *new_dirs; |
| 69 |
|
| 70 |
new_allocated = 2 * dirs_allocated + 1; |
| 71 |
if (new_allocated <= fd) |
| 72 |
new_allocated = fd + 1; |
| 73 |
new_dirs = |
| 74 |
(dirs != NULL |
| 75 |
? (dir_info_t *) realloc (dirs, new_allocated * sizeof *dirs) |
| 76 |
: (dir_info_t *) malloc (new_allocated * sizeof *dirs)); |
| 77 |
if (new_dirs == NULL) |
| 78 |
return false; |
| 79 |
memset (new_dirs + dirs_allocated, 0, |
| 80 |
(new_allocated - dirs_allocated) * sizeof *dirs); |
| 81 |
dirs = new_dirs; |
| 82 |
dirs_allocated = new_allocated; |
| 83 |
} |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
static char * |
| 89 |
get_name (char const *dir) |
| 90 |
{ |
| 91 |
char *cwd; |
| 92 |
char *result; |
| 93 |
int saved_errno; |
| 94 |
|
| 95 |
if (IS_ABSOLUTE_FILE_NAME (dir)) |
| 96 |
return strdup (dir); |
| 97 |
|
| 98 |
|
| 99 |
cwd = getcwd (NULL, 0); |
| 100 |
if (!cwd || (dir[0] == '.' && dir[1] == '\0')) |
| 101 |
return cwd; |
| 102 |
|
| 103 |
result = mfile_name_concat (cwd, dir, NULL); |
| 104 |
saved_errno = errno; |
| 105 |
free (cwd); |
| 106 |
errno = saved_errno; |
| 107 |
return result; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
void |
| 116 |
_gl_unregister_fd (int fd) |
| 117 |
{ |
| 118 |
if (fd >= 0 && fd < dirs_allocated) |
| 119 |
{ |
| 120 |
free (dirs[fd].name); |
| 121 |
dirs[fd].name = NULL; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
int |
| 131 |
_gl_register_fd (int fd, const char *filename) |
| 132 |
{ |
| 133 |
struct stat statbuf; |
| 134 |
|
| 135 |
assure (0 <= fd); |
| 136 |
if (REPLACE_OPEN_DIRECTORY |
| 137 |
|| (fstat (fd, &statbuf) == 0 && S_ISDIR (statbuf.st_mode))) |
| 138 |
{ |
| 139 |
if (!ensure_dirs_slot (fd) |
| 140 |
|| (dirs[fd].name = get_name (filename)) == NULL) |
| 141 |
{ |
| 142 |
int saved_errno = errno; |
| 143 |
close (fd); |
| 144 |
errno = saved_errno; |
| 145 |
return -1; |
| 146 |
} |
| 147 |
} |
| 148 |
return fd; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
int |
| 157 |
_gl_register_dup (int oldfd, int newfd) |
| 158 |
{ |
| 159 |
assure (0 <= oldfd && 0 <= newfd && oldfd != newfd); |
| 160 |
if (oldfd < dirs_allocated && dirs[oldfd].name) |
| 161 |
{ |
| 162 |
|
| 163 |
if (!ensure_dirs_slot (newfd) |
| 164 |
|| (dirs[newfd].name = strdup (dirs[oldfd].name)) == NULL) |
| 165 |
{ |
| 166 |
int saved_errno = errno; |
| 167 |
close (newfd); |
| 168 |
errno = saved_errno; |
| 169 |
newfd = -1; |
| 170 |
} |
| 171 |
} |
| 172 |
else if (newfd < dirs_allocated) |
| 173 |
{ |
| 174 |
|
| 175 |
free (dirs[newfd].name); |
| 176 |
dirs[newfd].name = NULL; |
| 177 |
} |
| 178 |
return newfd; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
const char * |
| 184 |
_gl_directory_name (int fd) |
| 185 |
{ |
| 186 |
if (0 <= fd && fd < dirs_allocated && dirs[fd].name != NULL) |
| 187 |
return dirs[fd].name; |
| 188 |
|
| 189 |
|
| 190 |
if (0 <= fd) |
| 191 |
{ |
| 192 |
if (dup2 (fd, fd) == fd) |
| 193 |
errno = ENOTDIR; |
| 194 |
} |
| 195 |
else |
| 196 |
errno = EBADF; |
| 197 |
return NULL; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
int |
| 204 |
fchdir (int fd) |
| 205 |
{ |
| 206 |
const char *name = _gl_directory_name (fd); |
| 207 |
return name ? chdir (name) : -1; |
| 208 |
} |