update for HEAD-2003091401
[reactos.git] / subsys / system / cmd / copy.c
index a77f516..4ca2bb7 100644 (file)
@@ -31,6 +31,7 @@
 #include <string.h>
 #include <stdlib.h>
 #include <ctype.h>
+#include <tchar.h>
 
 #include "cmd.h"
 #include "batch.h"
@@ -54,10 +55,10 @@ typedef struct tagFILES
 
 
 static BOOL DoSwitches (LPTSTR, LPDWORD);
-static BOOL AddFile (LPFILES, char *, int *, int *, LPDWORD);
-static BOOL AddFiles (LPFILES, char *, int *, int *, int *, LPDWORD);
+static BOOL AddFile (LPFILES, TCHAR *, int *, int *, LPDWORD);
+static BOOL AddFiles (LPFILES, TCHAR *, int *, int *, int *, LPDWORD);
 static BOOL GetDestination (LPFILES, LPFILES);
-static INT  ParseCommand (LPFILES, int, char **, LPDWORD);
+static INT  ParseCommand (LPFILES, int, TCHAR **, LPDWORD);
 static VOID DeleteFileList (LPFILES);
 static INT  Overwrite (LPTSTR);
 
@@ -83,7 +84,7 @@ DoSwitches (LPTSTR arg, LPDWORD lpdwFlags)
        }
        else if (_tcslen (arg) > 2)
        {
-               error_too_many_parameters ("");
+               error_too_many_parameters (_T(""));
                return FALSE;
        }
 
@@ -117,11 +118,11 @@ DoSwitches (LPTSTR arg, LPDWORD lpdwFlags)
 
 
 static BOOL
-AddFile (LPFILES f, char *arg, int *source, int *dest, LPDWORD flags)
+AddFile (LPFILES f, TCHAR *arg, int *source, int *dest, LPDWORD flags)
 {
        if (*dest)
        {
-               error_too_many_parameters ("");
+               error_too_many_parameters (_T(""));
                return FALSE;
        }
        if (*source)
@@ -149,16 +150,16 @@ AddFile (LPFILES f, char *arg, int *source, int *dest, LPDWORD flags)
 
 
 static BOOL
-AddFiles (LPFILES f, char *arg, int *source, int *dest,
+AddFiles (LPFILES f, TCHAR *arg, int *source, int *dest,
                  int *count, LPDWORD flags)
 {
-       char t[128];
+       TCHAR t[128];
        int j;
        int k;
 
        if (*dest)
        {
-               error_too_many_parameters ("");
+               error_too_many_parameters (_T(""));
                return FALSE;
        }
 
@@ -237,13 +238,13 @@ GetDestination (LPFILES f, LPFILES dest)
 
 
 static INT
-ParseCommand (LPFILES f, int argc, char **arg, LPDWORD lpdwFlags)
+ParseCommand (LPFILES f, int argc, TCHAR **arg, LPDWORD lpdwFlags)
 {
        INT i;
        INT dest;
        INT source;
        INT count;
-
+       TCHAR temp[128];
        dest = 0;
        source = 0;
        count = 0;
@@ -261,14 +262,24 @@ ParseCommand (LPFILES f, int argc, char **arg, LPDWORD lpdwFlags)
                                source = 0;
                        else if (!_tcschr(arg[i], _T('+')) && source)
                        {
-                               if (!AddFile(f, arg[i], &source, &dest, lpdwFlags))
+
+//                             Make sure we have a clean workable path
+                       
+                               GetFullPathName( arg[i], 128, (LPTSTR) &temp, NULL);
+//                             printf("A Input %s, Output %s\n", arg[i], temp);
+
+                               if (!AddFile(f, (TCHAR *) &temp, &source, &dest, lpdwFlags))
                                        return -1;
                                f = f->next;
                                count++;
                        }
                        else
                        {
-                               if (!AddFiles(f, arg[i], &source, &dest, &count, lpdwFlags))
+
+                               GetFullPathName( arg[i], 128, (LPTSTR) &temp, NULL);
+//                             printf("B Input %s, Output %s\n", arg[i], temp);
+                               
+                               if (!AddFiles(f, (TCHAR *) &temp, &source, &dest, &count, lpdwFlags))
                                        return -1;
                                while (f->next != NULL)
                                        f = f->next;
@@ -277,8 +288,8 @@ ParseCommand (LPFILES f, int argc, char **arg, LPDWORD lpdwFlags)
        }
 
 #ifdef _DEBUG
-       DebugPrintf ("ParseCommand: flags has %s\n",
-                                *lpdwFlags & ASCII ? "ASCII" : "BINARY");
+       DebugPrintf (_T("ParseCommand: flags has %s\n"),
+                                *lpdwFlags & ASCII ? _T("ASCII") : _T("BINARY"));
 #endif
        return count;
 }
@@ -337,7 +348,7 @@ int copy (LPTSTR source, LPTSTR dest, int append, LPDWORD lpdwFlags)
        BOOL   bEof = FALSE;
 
 #ifdef _DEBUG
-       DebugPrintf ("checking mode\n");
+       DebugPrintf (_T("checking mode\n"));
 #endif
 
        dwAttrib = GetFileAttributes (source);
@@ -485,17 +496,16 @@ int copy (LPTSTR source, LPTSTR dest, int append, LPDWORD lpdwFlags)
 
 
 static INT
-SetupCopy (LPFILES sources, char **p, BOOL bMultiple,
-           char *drive_d, char *dir_d, char *file_d,
-           char *ext_d, int *append, LPDWORD lpdwFlags)
+SetupCopy (LPFILES sources, TCHAR **p, BOOL bMultiple,
+           TCHAR *drive_d, TCHAR *dir_d, TCHAR *file_d,
+           TCHAR *ext_d, int *append, LPDWORD lpdwFlags)
 {
        WIN32_FIND_DATA find;
-
-       char drive_s[_MAX_DRIVE];
-       CHAR dir_s[_MAX_DIR];
-       char file_s[_MAX_FNAME];
-       char ext_s[_MAX_EXT];
-       char from_merge[_MAX_PATH];
+       TCHAR drive_s[_MAX_DRIVE];
+       TCHAR dir_s[_MAX_DIR];
+       TCHAR file_s[_MAX_FNAME];
+       TCHAR ext_s[_MAX_EXT];
+       TCHAR from_merge[_MAX_PATH];
 
        LPTSTR real_source;
        LPTSTR real_dest;
@@ -504,13 +514,14 @@ SetupCopy (LPFILES sources, char **p, BOOL bMultiple,
        BOOL bAll = FALSE;
        BOOL bDone;
        HANDLE hFind;
+       TCHAR temp[128];
 
 #ifdef _DEBUG
        DebugPrintf (_T("SetupCopy\n"));
 #endif
 
-       real_source = (LPTSTR)malloc (MAX_PATH);
-       real_dest = (LPTSTR)malloc (MAX_PATH);
+       real_source = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
+       real_dest = (LPTSTR)malloc (MAX_PATH * sizeof(TCHAR));
 
        if (!real_source || !real_dest)
        {
@@ -524,8 +535,14 @@ SetupCopy (LPFILES sources, char **p, BOOL bMultiple,
 
        while (sources->next != NULL)
        {
-               _splitpath (sources->szFile, drive_s, dir_s, file_s, ext_s);
-               hFind = FindFirstFile (sources->szFile, &find);
+
+/*             Force a clean full path
+*/
+               GetFullPathName( sources->szFile, 128, (LPTSTR) &temp, NULL);
+
+               _tsplitpath (sources->szFile, drive_s, dir_s, file_s, ext_s);
+
+               hFind = FindFirstFile ((TCHAR*)&temp, &find);
                if (hFind == INVALID_HANDLE_VALUE)
                {
                        error_file_not_found();
@@ -540,12 +557,18 @@ SetupCopy (LPFILES sources, char **p, BOOL bMultiple,
                        if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
                                goto next;
 
-                       _makepath(from_merge, drive_d, dir_d, file_d, ext_d);
+                       _tmakepath(from_merge, drive_d, dir_d, file_d, ext_d);
+
                        if (from_merge[_tcslen(from_merge) - 1] == _T('\\'))
                                from_merge[_tcslen(from_merge) - 1] = 0;
 
+//                     printf("Merge %s, filename %s\n", from_merge, find.cFileName);
+
                        if (IsDirectory (from_merge))
                        {
+
+//                     printf("Merge DIR\n");
+                       
                                bMultiple = FALSE;
                                _tcscat (from_merge, _T("\\"));
                                _tcscat (from_merge, find.cFileName);
@@ -554,13 +577,13 @@ SetupCopy (LPFILES sources, char **p, BOOL bMultiple,
                                bMultiple = TRUE;
 
                        _tcscpy (real_dest, from_merge);
-                       _makepath (real_source, drive_s, dir_s, find.cFileName, NULL);
+                       _tmakepath (real_source, drive_s, dir_s, find.cFileName, NULL);
 
 #ifdef _DEBUG
-                       DebugPrintf (_T("copying %s -> %s (%sappending%s)\n"),
+                       DebugPrintf(_T("copying %S -> %S (%Sappending%S)\n"),
                                                 real_source, real_dest,
-                                                *append ? "" : "not ",
-                                                sources->dwFlag & ASCII ? ", ASCII" : ", BINARY");
+                                                *append ? _T("") : _T("not "),
+                                                sources->dwFlag & ASCII ? _T(", ASCII") : _T(", BINARY"));
 #endif
 
                        if (IsValidFileName (real_dest) && !bAll)
@@ -606,11 +629,11 @@ SetupCopy (LPFILES sources, char **p, BOOL bMultiple,
 
 INT cmd_copy (LPTSTR first, LPTSTR rest)
 {
-       char **p;
-       char drive_d[_MAX_DRIVE];
-       char dir_d[_MAX_DIR];
-       char file_d[_MAX_FNAME];
-       char ext_d[_MAX_EXT];
+       TCHAR **p;
+       TCHAR drive_d[_MAX_DRIVE];
+       TCHAR dir_d[_MAX_DIR];
+       TCHAR file_d[_MAX_FNAME];
+       TCHAR ext_d[_MAX_EXT];
 
        int argc;
        int append;
@@ -682,9 +705,10 @@ INT cmd_copy (LPTSTR first, LPTSTR rest)
        bDestFound = GetDestination (sources, &dest);
        if (bDestFound)
        {
-               _splitpath (dest.szFile, drive_d, dir_d, file_d, ext_d);
+               _tsplitpath (dest.szFile, drive_d, dir_d, file_d, ext_d);
                if (IsDirectory (dest.szFile))
                {
+//             printf("A szFile= %s, Dir = %s, File = %s, Ext = %s\n", dest.szFile, dir_d, file_d, ext_d);
                        _tcscat (dir_d, file_d);
                        _tcscat (dir_d, ext_d);
                        file_d[0] = _T('\0');
@@ -697,7 +721,7 @@ INT cmd_copy (LPTSTR first, LPTSTR rest)
        else
                bWildcards = FALSE;
 
-       if (strchr(rest, '+'))
+       if (_tcschr(rest, _T('+')))
                bMultiple = TRUE;
        else
                bMultiple = FALSE;
@@ -707,6 +731,9 @@ INT cmd_copy (LPTSTR first, LPTSTR rest)
 
        if (bDestFound && !bWildcards)
        {
+
+//             _tcscpy(sources->szFile, dest.szFile);
+
                copied = SetupCopy (sources, p, bMultiple, drive_d, dir_d, file_d, ext_d, &append, &dwFlags);
        }
        else if (bDestFound && bWildcards)
@@ -718,21 +745,25 @@ INT cmd_copy (LPTSTR first, LPTSTR rest)
        }
        else if (!bDestFound && !bMultiple)
        {
-               _splitpath (sources->szFile, drive_d, dir_d, file_d, ext_d);
+               _tsplitpath (sources->szFile, drive_d, dir_d, file_d, ext_d);
                if (IsDirectory (sources->szFile))
                {
+//             printf("B File = %s, Ext = %s\n", file_d, ext_d);
+
                        _tcscat (dir_d, file_d);
                        _tcscat (dir_d, ext_d);
                        file_d[0] = _T('\0');
                        ext_d[0] = _T('\0');
                }
-               copied = SetupCopy (sources, p, FALSE, "", "", file_d, ext_d, &append, &dwFlags);
+               copied = SetupCopy (sources, p, FALSE, _T(""), _T(""), file_d, ext_d, &append, &dwFlags);
        }
        else
        {
-               _splitpath(sources->szFile, drive_d, dir_d, file_d, ext_d);
+               _tsplitpath(sources->szFile, drive_d, dir_d, file_d, ext_d);
                if (IsDirectory (sources->szFile))
                {
+//             printf("C File = %s, Ext = %s\n", file_d, ext_d);
+
                        _tcscat (dir_d, file_d);
                        _tcscat (dir_d, ext_d);
                        file_d[0] = _T('\0');
@@ -745,7 +776,7 @@ INT cmd_copy (LPTSTR first, LPTSTR rest)
        }
 
        DeleteFileList (sources);
-       freep (p);
+       freep ((VOID*)p);
        ConOutPrintf (_T("        %d file(s) copied\n"), copied);
 
        return 1;