Capture the dup/fdopen pattern
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Martin Schwenke <mschwenke@ddn.com>
size_t size = 0;
size_t chunk = 1024;
int err;
- int fd_dup;
if (maxsize == 0) {
maxsize = SIZE_MAX;
}
- fd_dup = dup(fd);
- if (fd_dup == -1) {
- return NULL;
- }
-
- file = fdopen(fd_dup, "r");
+ file = fdopen_keepfd(fd, "r");
if (file == NULL) {
- close(fd_dup);
return NULL;
}
return p;
}
+
+/*
+ * fopen a dup'ed fd. Prevent fclose to close the fd passed in.
+ *
+ * Don't use on fd's that have fcntl locks, on error it will close the
+ * dup'ed fd, thus killing your fcntl locks.
+ */
+FILE *fdopen_keepfd(int fd, const char *mode)
+{
+ FILE *f = NULL;
+ int dup_fd;
+
+ dup_fd = dup(fd);
+ if (dup_fd == -1) {
+ return NULL;
+ }
+
+ f = fdopen(dup_fd, mode);
+ if (f == NULL) {
+ int err = errno;
+ close(dup_fd);
+ errno = err;
+ return NULL;
+ }
+
+ return f;
+}
*/
char *file_ploadv(char * const argl[], size_t *size);
+FILE *fdopen_keepfd(int fd, const char *mode);
+
#endif