diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/.gitignore | 3 | ||||
-rw-r--r-- | examples/Makefile.am | 4 | ||||
-rw-r--r-- | examples/fcopy.c | 63 | ||||
-rw-r--r-- | examples/fremove.c | 49 | ||||
-rw-r--r-- | examples/frename.c | 59 | ||||
-rw-r--r-- | examples/rename.c | 60 |
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, ©_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; -} - - |