Check which getprotobynumber_r() variant to use
[socat.git] / xio-fdnum.c
blob15564ecf3de8851c36dfa1326af6d777e58d764c
1 /* source: xio-fdnum.c */
2 /* Copyright Gerhard Rieger and contributors (see file CHANGES) */
3 /* Published under the GNU General Public License V.2, see file COPYING */
5 /* this file contains the source for opening addresses of fdnum type */
7 #include "xiosysincludes.h"
8 #include "xioopen.h"
10 #include "xio-fdnum.h"
13 #if WITH_FDNUM
15 static int xioopen_fdnum(int argc, const char *argv[], struct opt *opts, int rw, xiofile_t *xfd, unsigned groups, int dummy1, int dummy2, int dummy3);
18 const struct addrdesc addr_fd = { "fd", 3, xioopen_fdnum, GROUP_FD|GROUP_FIFO|GROUP_CHR|GROUP_BLK|GROUP_FILE|GROUP_SOCKET|GROUP_TERMIOS|GROUP_SOCK_UNIX|GROUP_SOCK_IP|GROUP_IPAPP, 0, 0, 0 HELP(":<num>") };
21 /* use some file descriptor and apply the options. Set the FD_CLOEXEC flag. */
22 static int xioopen_fdnum(int argc, const char *argv[], struct opt *opts,
23 int xioflags, xiofile_t *xfd, unsigned groups,
24 int dummy1, int dummy2, int dummy3) {
25 char *a1;
26 int rw = (xioflags&XIO_ACCMODE);
27 int numfd;
28 int result;
30 if (argc != 2) {
31 Error3("%s:%s: wrong number of parameters (%d instead of 1)", argv[0], argv[1], argc-1);
34 numfd = strtoul(argv[1], &a1, 0);
35 if (*a1 != '\0') {
36 Error1("error in FD number \"%s\"", argv[1]);
38 /* we dont want to see these fds in child processes */
39 if (Fcntl_l(numfd, F_SETFD, FD_CLOEXEC) < 0) {
40 Warn2("fcntl(%d, F_SETFD, FD_CLOEXEC): %s", numfd, strerror(errno));
42 Notice2("using file descriptor %d for %s", numfd, ddirection[rw]);
43 if ((result = xioopen_fd(opts, rw, &xfd->stream, numfd, dummy2, dummy3)) < 0) {
44 return result;
46 return 0;
49 #endif /* WITH_FDNUM */
51 #if WITH_FD
53 /* retrieve and apply options to a standard file descriptor.
54 Do not set FD_CLOEXEC flag. */
55 int xioopen_fd(struct opt *opts, int rw, xiosingle_t *xfd, int numfd, int dummy2, int dummy3) {
57 xfd->fd = numfd;
58 xfd->howtoend = END_NONE;
60 #if WITH_TERMIOS
61 if (Isatty(xfd->fd)) {
62 if (Tcgetattr(xfd->fd, &xfd->savetty) < 0) {
63 Warn2("cannot query current terminal settings on fd %d: %s",
64 xfd->fd, strerror(errno));
65 } else {
66 xfd->ttyvalid = true;
69 #endif /* WITH_TERMIOS */
70 if (applyopts_single(xfd, opts, PH_INIT) < 0) return -1;
71 applyopts(-1, opts, PH_INIT);
73 applyopts2(xfd->fd, opts, PH_INIT, PH_FD);
75 return _xio_openlate(xfd, opts);
78 #endif /* WITH_FD */