Version 1.8.0.0
[socat.git] / xio-creat.c
blobc2f09d7dd873e381521d429e94784e12995f0c07
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, const struct addrdesc *addrdesc);
19 /*! within stream model, this is a write-only address - use 2 instead of 3 */
20 const struct addrdesc xioaddr_creat = { "CREATE", 1+XIO_WRONLY, 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(
41 int argc,
42 const char *argv[],
43 struct opt *opts,
44 int xioflags,
45 xiofile_t *xxfd,
46 const struct addrdesc *addrdesc)
48 struct single *sfd = &xxfd->stream;
49 const char *filename = argv[1];
50 int rw = (xioflags&XIO_ACCMODE);
51 bool exists;
52 bool opt_unlink_close = false;
53 int result;
55 /* remove old file, or set user/permissions on old file; parse options */
56 if ((result =
57 _xioopen_named_early(argc, argv, xxfd, addrdesc->groups, &exists, opts,
58 addrdesc->syntax))
59 < 0) {
60 return result;
63 retropt_bool(opts, OPT_UNLINK_CLOSE, &opt_unlink_close);
64 if (opt_unlink_close) {
65 if ((sfd->unlink_close = strdup(filename)) == NULL) {
66 Error1("strdup(\"%s\"): out of memory", filename);
68 sfd->opt_unlink_close = true;
71 Notice2("creating regular file \"%s\" for %s", filename, ddirection[rw]);
72 if ((result = _xioopen_creat(filename, rw, opts)) < 0)
73 return result;
74 sfd->fd = result;
76 applyopts_named(filename, opts, PH_PASTOPEN);
77 if ((result = applyopts2(sfd, -1, opts, PH_PASTOPEN, PH_LATE2)) < 0)
78 return result;
80 applyopts_cloexec(sfd->fd, opts);
82 applyopts_fchown(sfd->fd, opts);
84 if ((result = _xio_openlate(sfd, opts)) < 0)
85 return result;
87 return 0;
90 #endif /* WITH_CREAT */