summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2016-12-17 09:06:15 +0200
committerSergey Poznyakoff <gray@gnu.org.ua>2016-12-17 11:30:45 +0200
commit7324f9691545126806bbe219607248c7ad854852 (patch)
treeb03008efade4cbd6ff1f7f49fc467d6140ee7da5 /examples
parente87f373e24cd64b77e9b66348fc8aa0c53de5d52 (diff)
downloadmailutils-7324f9691545126806bbe219607248c7ad854852.tar.gz
mailutils-7324f9691545126806bbe219607248c7ad854852.tar.bz2
Improve rename/copy/reomove API.
* examples/rename.c: Remove. * examples/fcopy.c: New file. * examples/fremove.c: New file. * examples/frename.c: New file. * examples/Makefile.am: Update. * include/mailutils/util.h (mu_rename_file): Add flags. (mu_remove_file): New function. (MU_COPY_OVERWRITE): New flag. * libmailutils/base/renamefile.c: New file. * libmailutils/base/Makefile.am: Add newe file. * libmailutils/base/copyfile.c: Fix error handling. * libmailutils/base/renamefile.c (mu_rename_file): Refuse to proceed if the destination file exists and MU_COPY_OVERWRITE flag is not set * libmailutils/diag/errors (MU_ERR_REMOVE_SOURCE) (MU_ERR_RESTORE_META): New errors * imap4d/rename.c (imap4d_rename): Use mu_rename_file * mh/forw.c: Likewise. * mh/mh_whatnow.c: Likewise. * mh/mhn.c: Likewise. * mh/send.c: Likewise. * include/mailutils/cstr.h (mu_str_count): New proto. * include/mailutils/util.h (mu_file_name_is_safe): New proto. * libmailutils/string/safefilename.c: New file. * libmailutils/string/strcount.c: New file. * libmailutils/string/Makefile.am: Update.
Diffstat (limited to 'examples')
-rw-r--r--examples/.gitignore3
-rw-r--r--examples/Makefile.am4
-rw-r--r--examples/fcopy.c63
-rw-r--r--examples/fremove.c49
-rw-r--r--examples/frename.c59
-rw-r--r--examples/rename.c60
6 files changed, 177 insertions, 61 deletions
diff --git a/examples/.gitignore b/examples/.gitignore
index e75f2264a..d4792a31f 100644
--- a/examples/.gitignore
+++ b/examples/.gitignore
@@ -5,6 +5,9 @@ base64
decode2047
echosrv
encode2047
+fcopy
+fremove
+frename
header
http
iconv
diff --git a/examples/Makefile.am b/examples/Makefile.am
index ca5e0947a..fa63b6624 100644
--- a/examples/Makefile.am
+++ b/examples/Makefile.am
@@ -30,6 +30,9 @@ noinst_PROGRAMS = \
addr\
base64\
echosrv\
+ fcopy\
+ fremove\
+ frename\
header\
http\
iconv\
@@ -44,7 +47,6 @@ noinst_PROGRAMS = \
murun\
musocio\
$(NNTPCLIENT)\
- rename\
sa\
sfrom
diff --git a/examples/fcopy.c b/examples/fcopy.c
new file mode 100644
index 000000000..e4aa86852
--- /dev/null
+++ b/examples/fcopy.c
@@ -0,0 +1,63 @@
+#include <mailutils/mailutils.h>
+
+int owner_option;
+int mode_option;
+int force_option;
+int deref_option;
+
+static struct mu_option copy_options[] = {
+ { "owner", 'u', NULL, MU_OPTION_DEFAULT,
+ "copy ownership",
+ mu_c_bool, &owner_option },
+ { "mode", 'm', NULL, MU_OPTION_DEFAULT,
+ "copy mode",
+ mu_c_bool, &mode_option },
+ { "force", 'f', NULL, MU_OPTION_DEFAULT,
+ "force overwriting the destination file if it exists",
+ mu_c_bool, &force_option },
+ { "overwrite", 0, NULL, MU_OPTION_ALIAS },
+ { "dereference", 'h', NULL, MU_OPTION_DEFAULT,
+ "dereference symbolic links",
+ mu_c_bool, &deref_option },
+ MU_OPTION_END
+}, *options[] = { copy_options, NULL };
+
+struct mu_cli_setup cli = {
+ options,
+ NULL,
+ "copy file",
+ "SRC DST"
+};
+
+static char *capa[] = {
+ "debug",
+ NULL
+};
+
+int
+main (int argc, char **argv)
+{
+ int rc;
+ int flags;
+
+ mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
+
+ if (argc != 2)
+ {
+ mu_error ("wrong number of arguments");
+ return 1;
+ }
+
+ flags = (owner_option ? MU_COPY_OWNER : 0)
+ | (mode_option ? MU_COPY_MODE : 0)
+ | (force_option ? MU_COPY_OVERWRITE : 0)
+ | (deref_option ? MU_COPY_DEREF : 0);
+ rc = mu_copy_file (argv[0], argv[1], flags);
+
+ if (rc)
+ mu_diag_funcall (MU_DIAG_ERROR, "mu_copy_file", NULL, rc);
+
+ return !!rc;
+}
+
+
diff --git a/examples/fremove.c b/examples/fremove.c
new file mode 100644
index 000000000..e7c2d9118
--- /dev/null
+++ b/examples/fremove.c
@@ -0,0 +1,49 @@
+#include <mailutils/mailutils.h>
+
+int owner_option;
+int mode_option;
+int force_option;
+
+static struct mu_option *options[] = { NULL };
+
+struct mu_cli_setup cli = {
+ options,
+ NULL,
+ "delete file",
+ "FILE"
+};
+
+static char *capa[] = {
+ "debug",
+ NULL
+};
+
+int
+main (int argc, char **argv)
+{
+ int rc;
+
+ mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
+
+ if (argc != 1)
+ {
+ mu_error ("wrong number of arguments");
+ return 1;
+ }
+
+ if (!mu_file_name_is_safe (argv[0])
+ || (argv[0][0] == '/' && mu_str_count (argv[0], '/') < 2))
+ {
+ mu_error ("unsafe file name");
+ return 1;
+ }
+
+ rc = mu_remove_file (argv[0]);
+
+ if (rc)
+ mu_diag_funcall (MU_DIAG_ERROR, "mu_remove_file", NULL, rc);
+
+ return !!rc;
+}
+
+
diff --git a/examples/frename.c b/examples/frename.c
new file mode 100644
index 000000000..1bf6556f4
--- /dev/null
+++ b/examples/frename.c
@@ -0,0 +1,59 @@
+#include <mailutils/mailutils.h>
+
+int force_option;
+
+static struct mu_option rename_options[] = {
+ { "force", 'f', NULL, MU_OPTION_DEFAULT,
+ "force overwriting the destination file if it exists",
+ mu_c_bool, &force_option },
+ { "overwrite", 0, NULL, MU_OPTION_ALIAS },
+ MU_OPTION_END
+}, *options[] = { rename_options, NULL };
+
+struct mu_cli_setup cli = {
+ options,
+ NULL,
+ "rename file",
+ "SRC DST"
+};
+
+static char *capa[] = {
+ "debug",
+ NULL
+};
+
+int
+main (int argc, char **argv)
+{
+ int rc;
+
+ mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
+
+ if (argc != 2)
+ {
+ mu_error ("wrong number of arguments");
+ return 1;
+ }
+
+ if (!mu_file_name_is_safe (argv[0])
+ || (argv[0][0] == '/' && mu_str_count (argv[0], '/') < 2))
+ {
+ mu_error ("%s: unsafe file name", argv[0]);
+ return 1;
+ }
+ if (!mu_file_name_is_safe (argv[1])
+ || (argv[1][0] == '/' && mu_str_count (argv[1], '/') < 2))
+ {
+ mu_error ("%sunsafe file name", argv[0]);
+ return 1;
+ }
+
+ rc = mu_rename_file (argv[0], argv[1], force_option ? MU_COPY_OVERWRITE : 0);
+
+ if (rc)
+ mu_diag_funcall (MU_DIAG_ERROR, "mu_rename_file", NULL, rc);
+
+ return !!rc;
+}
+
+
diff --git a/examples/rename.c b/examples/rename.c
deleted file mode 100644
index f12264ac7..000000000
--- a/examples/rename.c
+++ /dev/null
@@ -1,60 +0,0 @@
-#include <mailutils/mailutils.h>
-
-int copy_option;
-int owner_option;
-int mode_option;
-
-static struct mu_option rename_options[] = {
- { "copy", 'c', NULL, MU_OPTION_DEFAULT,
- "copy the file",
- mu_c_bool, &copy_option },
- { "owner", 'u', NULL, MU_OPTION_DEFAULT,
- "copy ownership",
- mu_c_bool, &owner_option },
- { "mode", 'm', NULL, MU_OPTION_DEFAULT,
- "copy mode",
- mu_c_bool, &mode_option },
- MU_OPTION_END
-}, *options[] = { rename_options, NULL };
-
-struct mu_cli_setup cli = {
- options,
- NULL,
- "copy or rename file",
- "SRC DST"
-};
-
-static char *capa[] = {
- "debug",
- NULL
-};
-
-int
-main (int argc, char **argv)
-{
- int rc;
-
- mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
-
- if (argc != 2)
- {
- mu_error ("wrong number of arguments");
- return 1;
- }
-
- if (copy_option)
- {
- int flags = (owner_option ? MU_COPY_OWNER : 0)
- | (mode_option ? MU_COPY_MODE : 0);
- rc = mu_copy_file (argv[0], argv[1], flags);
- }
- else
- rc = mu_rename_file (argv[0], argv[1]);
-
- if (rc)
- mu_diag_funcall (MU_DIAG_ERROR, "mu_rename_file", NULL, rc);
-
- return !!rc;
-}
-
-

Return to:

Send suggestions and report system problems to the System administrator.