Version 1.8.0.1
[socat.git] / xio-file.c
blobc19be4b9dbeca892f8356d05f06d767eb9367aa2
1 /* source: xio-file.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 open type */
7 #include "xiosysincludes.h"
8 #include "xioopen.h"
10 #include "xio-named.h"
11 #include "xio-file.h"
14 static int xioopen_open(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, const struct addrdesc *addrdesc);
17 #if WITH_OPEN
19 /****** OPEN addresses ******/
20 const struct optdesc opt_o_rdonly = { "o-rdonly", "rdonly", OPT_O_RDONLY, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG_PATTERN, O_RDONLY, O_ACCMODE };
21 const struct optdesc opt_o_wronly = { "o-wronly", "wronly", OPT_O_WRONLY, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG_PATTERN, O_WRONLY, O_ACCMODE };
22 const struct optdesc opt_o_rdwr = { "o-rdwr", "rdwr", OPT_O_RDWR, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG_PATTERN, O_RDWR, O_ACCMODE };
23 const struct optdesc opt_o_create = { "o-create", "creat", OPT_O_CREATE, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_CREAT };
24 const struct optdesc opt_o_excl = { "o-excl", "excl", OPT_O_EXCL, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_EXCL };
25 const struct optdesc opt_o_noctty = { "o-noctty", "noctty", OPT_O_NOCTTY, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_NOCTTY };
26 #ifdef O_SYNC
27 const struct optdesc opt_o_sync = { "o-sync", "sync", OPT_O_SYNC, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_SYNC };
28 #endif
29 #ifdef O_NOFOLLOW
30 const struct optdesc opt_o_nofollow = { "o-nofollow", "nofollow",OPT_O_NOFOLLOW, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_NOFOLLOW };
31 #endif
32 #ifdef O_DIRECTORY
33 const struct optdesc opt_o_directory = { "o-directory", "directory",OPT_O_DIRECTORY, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_DIRECTORY };
34 #endif
35 #ifdef O_LARGEFILE
36 const struct optdesc opt_o_largefile = { "o-largefile", "largefile",OPT_O_LARGEFILE, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_LARGEFILE };
37 #endif
38 #ifdef O_NSHARE
39 const struct optdesc opt_o_nshare = { "o-nshare", "nshare", OPT_O_NSHARE, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_NSHARE };
40 #endif
41 #ifdef O_RSHARE
42 const struct optdesc opt_o_rshare = { "o-rshare", "rshare", OPT_O_RSHARE, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_RSHARE };
43 #endif
44 #ifdef O_DEFER
45 const struct optdesc opt_o_defer = { "o-defer", "defer", OPT_O_DEFER, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_DEFER };
46 #endif
47 #ifdef O_DIRECT
48 const struct optdesc opt_o_direct = { "o-direct", "direct", OPT_O_DIRECT, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_DIRECT };
49 #endif
50 #ifdef O_DSYNC
51 const struct optdesc opt_o_dsync = { "o-dsync", "dsync", OPT_O_DSYNC, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_DSYNC };
52 #endif
53 #ifdef O_RSYNC
54 const struct optdesc opt_o_rsync = { "o-rsync", "rsync", OPT_O_RSYNC, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_RSYNC };
55 #endif
56 #ifdef O_DELAY
57 const struct optdesc opt_o_delay = { "o-delay", "delay", OPT_O_DELAY, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_DELAY };
58 #endif
59 #ifdef O_PRIV
60 const struct optdesc opt_o_priv = { "o-priv", "priv", OPT_O_PRIV, GROUP_OPEN, PH_OPEN, TYPE_BOOL, OFUNC_FLAG, O_PRIV };
61 #endif
62 const struct optdesc opt_o_trunc = { "o-trunc", "trunc", OPT_O_TRUNC, GROUP_OPEN, PH_LATE, TYPE_BOOL, OFUNC_FLAG, O_TRUNC };
64 #endif /* WITH_OPEN */
67 #if _WITH_FILE /*! inconsistent name FILE vs. OPEN */
69 const struct addrdesc xioaddr_open = { "OPEN", 3, xioopen_open, GROUP_FD|GROUP_FIFO|GROUP_CHR|GROUP_BLK|GROUP_REG|GROUP_NAMED|GROUP_OPEN|GROUP_FILE|GROUP_TERMIOS, 0, 0, 0 HELP(":<filename>") };
71 /* open for writing:
72 if the filesystem entry already exists, the data is appended
73 if it does not exist, a file is created and the data is appended
75 static int xioopen_open(
76 int argc,
77 const char *argv[],
78 struct opt *opts,
79 int xioflags,
80 xiofile_t *xfd,
81 const struct addrdesc *addrdesc)
83 const char *filename = argv[1];
84 int rw = (xioflags & XIO_ACCMODE);
85 struct single *sfd = &xfd->stream;
86 bool exists;
87 bool opt_unlink_close = false;
88 int result;
90 /* remove old file, or set user/permissions on old file; parse options */
91 if ((result =
92 _xioopen_named_early(argc, argv, xfd, addrdesc->groups, &exists, opts,
93 addrdesc->syntax))
94 < 0) {
95 return result;
98 retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close);
99 if (opt_unlink_close) {
100 if ((sfd->unlink_close = strdup(filename)) == NULL) {
101 Error1("strdup(\"%s\"): out of memory", filename);
103 sfd->opt_unlink_close = true;
106 Notice3("opening %s \"%s\" for %s",
107 filetypenames[(result&S_IFMT)>>12], filename, ddirection[rw]);
108 if ((result = _xioopen_open(filename, rw, opts)) < 0)
109 return result;
110 sfd->fd = result;
112 #if WITH_TERMIOS
113 if (Isatty(sfd->fd)) {
114 if (Tcgetattr(sfd->fd, &sfd->savetty) < 0) {
115 Warn2("cannot query current terminal settings on fd %d: %s",
116 sfd->fd, strerror(errno));
117 } else {
118 sfd->ttyvalid = true;
121 #endif /* WITH_TERMIOS */
123 applyopts_named(filename, opts, PH_FD);
124 applyopts(sfd, -1, opts, PH_FD);
125 applyopts_cloexec(sfd->fd, opts);
127 applyopts_fchown(sfd->fd, opts);
129 if ((result = _xio_openlate(sfd, opts)) < 0)
130 return result;
132 return 0;
135 #endif /* _WITH_FILE */