Added configure option --enable-openssl-base
[socat.git] / xio-creat.c
blobed98f0c59588a4dbd0660bc2de45e0b4409218f0
1 /* source: xio-creat.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 create type */
7 #include "xiosysincludes.h"
9 #if WITH_CREAT
11 #include "xioopen.h"
12 #include "xio-named.h"
13 #include "xio-creat.h"
16 static int xioopen_creat(int arg, const char *argv[], struct opt *opts, int rw, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3);
19 /*! within stream model, this is a write-only address - use 2 instead of 3 */
20 const struct addrdesc addr_creat = { "create", 3, xioopen_creat, GROUP_FD|GROUP_NAMED|GROUP_FILE, 0, 0, 0 HELP(":<filename>") };
23 /* retrieve the mode option and perform the creat() call.
24 returns the file descriptor or a negative value. */
25 static int _xioopen_creat(const char *path, int rw, struct opt *opts) {
26 mode_t mode = 0666;
27 int fd;
29 retropt_modet(opts, OPT_PERM, &mode);
31 if ((fd = Creat(path, mode)) < 0) {
32 Error3("creat(\"%s\", 0%03o): %s",
33 path, mode, strerror(errno));
34 return STAT_RETRYLATER;
36 return fd;
40 static int xioopen_creat(int argc, const char *argv[], struct opt *opts, int xioflags, xiofile_t *fd, unsigned groups, int dummy1, int dummy2, int dummy3) {
41 const char *filename = argv[1];
42 int rw = (xioflags&XIO_ACCMODE);
43 bool exists;
44 bool opt_unlink_close = false;
45 int result;
47 /* remove old file, or set user/permissions on old file; parse options */
48 if ((result = _xioopen_named_early(argc, argv, fd, groups, &exists, opts)) < 0) {
49 return result;
52 retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close);
53 if (opt_unlink_close) {
54 if ((fd->stream.unlink_close = strdup(filename)) == NULL) {
55 Error1("strdup(\"%s\"): out of memory", filename);
57 fd->stream.opt_unlink_close = true;
60 Notice2("creating regular file \"%s\" for %s", filename, ddirection[rw]);
61 if ((result = _xioopen_creat(filename, rw, opts)) < 0)
62 return result;
63 fd->stream.fd = result;
65 applyopts_named(filename, opts, PH_PASTOPEN);
66 if ((result = applyopts2(fd->stream.fd, opts, PH_PASTOPEN, PH_LATE2)) < 0)
67 return result;
69 applyopts_cloexec(fd->stream.fd, opts);
71 applyopts_fchown(fd->stream.fd, opts);
73 if ((result = _xio_openlate(&fd->stream, opts)) < 0)
74 return result;
76 return 0;
79 #endif /* WITH_CREAT */