1 #include <mailutils/mailutils.h>
2
3 int force_option;
4
5 static struct mu_option rename_options[] = {
6 { "force", 'f', NULL, MU_OPTION_DEFAULT,
7 "force overwriting the destination file if it exists",
8 mu_c_bool, &force_option },
9 { "overwrite", 0, NULL, MU_OPTION_ALIAS },
10 MU_OPTION_END
11 }, *options[] = { rename_options, NULL };
12
13 struct mu_cli_setup cli = {
14 options,
15 NULL,
16 "rename file",
17 "SRC DST"
18 };
19
20 static char *capa[] = {
21 "debug",
22 NULL
23 };
24
25 int
main(int argc,char ** argv)26 main (int argc, char **argv)
27 {
28 int rc;
29
30 mu_cli (argc, argv, &cli, capa, NULL, &argc, &argv);
31
32 if (argc != 2)
33 {
34 mu_error ("wrong number of arguments");
35 return 1;
36 }
37
38 if (!mu_file_name_is_safe (argv[0])
39 || (argv[0][0] == '/' && mu_str_count (argv[0], "/", NULL) < 2))
40 {
41 mu_error ("%s: unsafe file name", argv[0]);
42 return 1;
43 }
44 if (!mu_file_name_is_safe (argv[1])
45 || (argv[1][0] == '/' && mu_str_count (argv[1], "/", NULL) < 2))
46 {
47 mu_error ("%sunsafe file name", argv[0]);
48 return 1;
49 }
50
51 rc = mu_rename_file (argv[0], argv[1], force_option ? MU_COPY_OVERWRITE : 0);
52
53 if (rc)
54 mu_diag_funcall (MU_DIAG_ERROR, "mu_rename_file", NULL, rc);
55
56 return !!rc;
57 }
58
59
60