-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdup2.c
50 lines (38 loc) · 915 Bytes
/
dup2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/*
* Copyright 2023-2024 Gaël PORTAY
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <unistd.h>
#include "iamroot.h"
static int (*sym)(int, int);
hidden int next_dup2(int oldfd, int newfd)
{
if (!sym)
sym = dlsym(RTLD_NEXT, "dup2");
if (!sym)
return __dl_set_errno_and_perror(ENOSYS, -1);
return sym(oldfd, newfd);
}
int dup2(int oldfd, int newfd)
{
const int errno_save = errno;
char buf[PATH_MAX];
ssize_t siz;
int ret;
siz = fpath(oldfd, buf, sizeof(buf));
if (siz == -1 && __strncpy(buf, "(null)"))
errno = errno_save;
ret = next_dup2(oldfd, newfd);
if (ret >= 0 && __setfd(ret, __fpath(oldfd)))
errno = errno_save;
__debug("%s(oldfd: %i <-> '%s', newfd: %i <-> '%s') -> %i\n",
__func__, oldfd, buf, newfd, __fpath2(newfd), ret);
return ret;
}