[PATCH v5 3/6] robocopy: Add source / destination / file argument parser

Florian Eder others.meder at gmail.com
Mon Sep 20 13:51:23 CDT 2021


Parses (relative / absolute) path arguments as source, destination
and files to include

Signed-off-by: Florian Eder <others.meder at gmail.com>
---
v5: Identical to patch sent in v4
---
 programs/robocopy/Makefile.in |  1 +
 programs/robocopy/main.c      | 61 +++++++++++++++++++++++++++++++++++
 programs/robocopy/robocopy.h  | 33 +++++++++++++++++++
 3 files changed, 95 insertions(+)
 create mode 100644 programs/robocopy/robocopy.h

diff --git a/programs/robocopy/Makefile.in b/programs/robocopy/Makefile.in
index 5a48be3725b..0f4f5c76119 100644
--- a/programs/robocopy/Makefile.in
+++ b/programs/robocopy/Makefile.in
@@ -1,4 +1,5 @@
 MODULE    = robocopy.exe
+IMPORTS   = kernelbase
 
 EXTRADLLFLAGS = -mconsole -municode -mno-cygwin
 
diff --git a/programs/robocopy/main.c b/programs/robocopy/main.c
index bbda15f573a..e1800fbd74e 100644
--- a/programs/robocopy/main.c
+++ b/programs/robocopy/main.c
@@ -21,9 +21,70 @@ WINE_DEFAULT_DEBUG_CHANNEL(robocopy);
 
 #define WIN32_LEAN_AND_MEAN
 #include <windows.h>
+#include <stdlib.h>
+#include <pathcch.h>
+#include "robocopy.h"
+
+struct robocopy_options options;
+
+static WCHAR *get_absolute_path(const WCHAR *path)
+{
+    DWORD size;
+    WCHAR *absolute_path;
+
+    /* allocate absolute path + potential backslash + null WCHAR */
+    size = GetFullPathNameW(path, 0, NULL, NULL) + 2;
+    absolute_path = calloc(size, sizeof(WCHAR));
+    GetFullPathNameW(path, size, absolute_path, NULL);
+    PathCchAddBackslashEx(absolute_path, size, NULL, NULL);
+    return absolute_path;
+}
+
+static void parse_arguments(int argc, WCHAR *argv[])
+{
+    int i;
+
+    memset(&options, 0, sizeof(options));
+    options.files = calloc(1, offsetof(struct path_array, array[argc]));
+
+    for (i = 1; i < argc; i++)
+    {
+        /*
+         * Robocopy switches contain one (and only one) backslash at the start
+         * /xf => valid flag
+         * /r:1 => valid flag
+         * /r:1aö => valid flag
+         * /r:1aö/ => not a valid flag, is interpreted as a filename
+         */
+        if ((argv[i][0] == L'/') && (wcschr(argv[i] + 1, L'/') == NULL))
+            WINE_FIXME("encountered an unknown robocopy flag: %s\n", debugstr_w(argv[i]));
+        else
+        {
+            /*
+            *(Probably) not a flag, we can parse it as the source / the destination / a filename
+            * Priority: Source > Destination > (more than one) File
+            */
+            if (!options.source)
+            {
+                options.source = get_absolute_path(argv[i]);
+            }
+            else if (!options.destination)
+            {
+                options.destination = get_absolute_path(argv[i]);
+            }
+            else
+            {
+                options.files->array[options.files->size] = wcsdup(argv[i]);
+                options.files->size++;
+            }
+        }
+    }
+}
 
 int __cdecl wmain(int argc, WCHAR *argv[])
 {
+    parse_arguments(argc, argv);
+
     WINE_FIXME("robocopy stub");
     return 1;
 }
diff --git a/programs/robocopy/robocopy.h b/programs/robocopy/robocopy.h
new file mode 100644
index 00000000000..f62eb92c663
--- /dev/null
+++ b/programs/robocopy/robocopy.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright 2021 Florian Eder
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+
+struct path_array
+{
+    UINT size;
+    WCHAR *array[1];
+};
+
+struct robocopy_options
+{
+    WCHAR *destination;
+    WCHAR *source;
+    struct path_array *files;
+};
-- 
2.32.0




More information about the wine-devel mailing list