Various cmd fixes by Carlo Bramini (carlo DOT bramix AT libero DOT it)

See issue #2232 for more details.

svn path=/trunk/; revision=28022
This commit is contained in:
Colin Finck 2007-07-29 19:53:17 +00:00
parent 8fd67d96e0
commit ddfbe083e9
3 changed files with 29 additions and 47 deletions

View file

@ -66,7 +66,6 @@ copy (TCHAR source[MAX_PATH],
DWORD dwAttrib; DWORD dwAttrib;
DWORD dwRead; DWORD dwRead;
DWORD dwWritten; DWORD dwWritten;
DWORD i;
BOOL bEof = FALSE; BOOL bEof = FALSE;
TCHAR TrueDest[MAX_PATH]; TCHAR TrueDest[MAX_PATH];
TCHAR TempSrc[MAX_PATH]; TCHAR TempSrc[MAX_PATH];
@ -168,7 +167,6 @@ copy (TCHAR source[MAX_PATH],
} }
if (!IsExistingFile (dest)) if (!IsExistingFile (dest))
{ {
#ifdef _DEBUG #ifdef _DEBUG
@ -231,7 +229,9 @@ copy (TCHAR source[MAX_PATH],
nErrorLevel = 1; nErrorLevel = 1;
return 0; return 0;
} }
buffer = (LPBYTE)malloc (BUFF_SIZE);
/* A page-aligned buffer usually give more speed */
buffer = (LPBYTE)VirtualAlloc(NULL, BUFF_SIZE, MEM_COMMIT, PAGE_READWRITE);
if (buffer == NULL) if (buffer == NULL)
{ {
CloseHandle (hFileDest); CloseHandle (hFileDest);
@ -247,16 +247,13 @@ copy (TCHAR source[MAX_PATH],
ReadFile (hFileSrc, buffer, BUFF_SIZE, &dwRead, NULL); ReadFile (hFileSrc, buffer, BUFF_SIZE, &dwRead, NULL);
if (lpdwFlags & COPY_ASCII) if (lpdwFlags & COPY_ASCII)
{ {
for (i = 0; i < dwRead; i++) LPBYTE pEof = memchr(buffer, 0x1A, dwRead);
if (pEof != NULL)
{ {
/* we're dealing with ASCII files! */ bEof = TRUE;
if (((LPSTR)buffer)[i] == 0x1A) dwRead = pEof-buffer+1;
{ break;
bEof = TRUE;
break;
}
} }
dwRead = i;
} }
if (dwRead == 0) if (dwRead == 0)
@ -274,25 +271,24 @@ copy (TCHAR source[MAX_PATH],
return 0; return 0;
} }
} }
while (dwRead && !bEof); while (!bEof);
#ifdef _DEBUG #ifdef _DEBUG
DebugPrintf (_T("setting time\n")); DebugPrintf (_T("setting time\n"));
#endif #endif
SetFileTime (hFileDest, &srctime, NULL, NULL); SetFileTime (hFileDest, &srctime, NULL, NULL);
if (lpdwFlags & COPY_ASCII) if ((lpdwFlags & COPY_ASCII) && !bEof)
{ {
/* we're dealing with ASCII files! */ /* we're dealing with ASCII files! */
((LPSTR)buffer)[0] = 0x1A; buffer[0] = 0x1A;
((LPSTR)buffer)[1] = '\0';
#ifdef _DEBUG #ifdef _DEBUG
DebugPrintf (_T("appending ^Z\n")); DebugPrintf (_T("appending ^Z\n"));
#endif #endif
WriteFile (hFileDest, buffer, sizeof(CHAR), &dwWritten, NULL); WriteFile (hFileDest, buffer, sizeof(CHAR), &dwWritten, NULL);
} }
free (buffer); VirtualFree (buffer, 0, MEM_RELEASE);
CloseHandle (hFileDest); CloseHandle (hFileDest);
CloseHandle (hFileSrc); CloseHandle (hFileSrc);
@ -318,8 +314,6 @@ copy (TCHAR source[MAX_PATH],
if(lpdwFlags & COPY_DECRYPT) if(lpdwFlags & COPY_DECRYPT)
DeleteFile(TempSrc); DeleteFile(TempSrc);
return 1; return 1;
} }
@ -877,7 +871,7 @@ INT cmd_copy (LPTSTR cmd, LPTSTR param)
LoadString(CMD_ModuleHandle, STRING_COPY_FILE, szMsg, RC_STRING_MAX_SIZE); LoadString(CMD_ModuleHandle, STRING_COPY_FILE, szMsg, RC_STRING_MAX_SIZE);
ConOutPrintf(szMsg, nFiles); ConOutPrintf(szMsg, nFiles);
CloseHandle(hFile); FindClose(hFile);
if (arg!=NULL) if (arg!=NULL)
free(arg); free(arg);

View file

@ -473,7 +473,8 @@ VOID FindPrefixAndSuffix(LPTSTR strIN, LPTSTR szPrefix, LPTSTR szSuffix)
_tcscpy(szSuffix,&strIN[SBreak]); _tcscpy(szSuffix,&strIN[SBreak]);
strIN[PBreak] = _T('\0'); strIN[PBreak] = _T('\0');
_tcscpy(szPrefix,strIN); _tcscpy(szPrefix,strIN);
if(szPrefix[_tcslen(szPrefix) - 2] == _T('\"')) if (szPrefix[_tcslen(szPrefix) - 2] == _T('\"') &&
szPrefix[_tcslen(szPrefix) - 1] != _T(' '))
{ {
/* need to remove the " right before a \ at the end to /* need to remove the " right before a \ at the end to
allow the next stuff to stay inside one set of quotes allow the next stuff to stay inside one set of quotes
@ -604,21 +605,17 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, UINT cusor)
} }
/* search for the files it might be */ /* search for the files it might be */
hFile = FindFirstFile (szSearchPath, &file); hFile = FindFirstFile (szSearchPath, &file);
if(hFile == INVALID_HANDLE_VALUE)
{
/* Assemble the orginal string and return */
_tcscpy(strOut,szOrginal);
return;
}
/* aseemble a list of all files names */ /* aseemble a list of all files names */
do do
{ {
if(hFile == INVALID_HANDLE_VALUE) if(!_tcscmp (file.cFileName, _T(".")) ||
{
/* Assemble the orginal string and return */
_tcscpy(strOut,szOrginal);
CloseHandle(hFile);
if(FileList != NULL)
free(FileList);
return;
}
if(!_tcscmp (file.cFileName, _T(".")) ||
!_tcscmp (file.cFileName, _T(".."))) !_tcscmp (file.cFileName, _T("..")))
continue; continue;
@ -631,36 +628,28 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, UINT cusor)
} }
/* Add the file to the list of files */ /* Add the file to the list of files */
if(FileList == NULL) FileList = realloc(FileList, ++FileListSize * sizeof(FileName));
{
FileListSize = 1;
FileList = malloc(FileListSize * sizeof(FileName));
}
else
{
FileListSize++;
FileList = realloc(FileList, FileListSize * sizeof(FileName));
}
if(FileList == NULL) if(FileList == NULL)
{ {
/* Assemble the orginal string and return */ /* Assemble the orginal string and return */
_tcscpy(strOut,szOrginal); _tcscpy(strOut,szOrginal);
CloseHandle(hFile); FindClose(hFile);
ConOutFormatMessage (GetLastError()); ConOutFormatMessage (GetLastError());
return; return;
} }
/* Copies the file name into the struct */ /* Copies the file name into the struct */
_tcscpy(FileList[FileListSize-1].Name,file.cFileName); _tcscpy(FileList[FileListSize-1].Name,file.cFileName);
}while(FindNextFile(hFile,&file)); } while(FindNextFile(hFile,&file));
FindClose(hFile);
/* Check the size of the list to see if we /* Check the size of the list to see if we
found any matches */ found any matches */
if(FileListSize == 0) if(FileListSize == 0)
{ {
_tcscpy(strOut,szOrginal); _tcscpy(strOut,szOrginal);
CloseHandle(hFile);
if(FileList != NULL) if(FileList != NULL)
free(FileList); free(FileList);
return; return;
@ -751,7 +740,6 @@ VOID CompleteFilename (LPTSTR strIN, BOOL bNext, LPTSTR strOut, UINT cusor)
_tcscpy(LastReturned,strOut); _tcscpy(LastReturned,strOut);
EndLength = _tcslen(strOut); EndLength = _tcslen(strOut);
DiffLength = EndLength - StartLength; DiffLength = EndLength - StartLength;
CloseHandle(hFile);
if(FileList != NULL) if(FileList != NULL)
free(FileList); free(FileList);

View file

@ -142,7 +142,7 @@ VOID GetPathCase( TCHAR * Path, TCHAR * OutPath)
_tcscat(TempPath, _T("\\")); _tcscat(TempPath, _T("\\"));
_tcscat(OutPath, FindFileData.cFileName); _tcscat(OutPath, FindFileData.cFileName);
_tcscat(OutPath, _T("\\")); _tcscat(OutPath, _T("\\"));
CloseHandle(hFind); FindClose(hFind);
} }
} }
} }