* Move (optional) wildcard expansion to split()

* Only expand arguments actually containing wildcard characters
  (to prevent expanding directory names to all files within that directory)

svn path=/trunk/; revision=4018
This commit is contained in:
Gé van Geldorp 2003-01-17 00:31:33 +00:00
parent 1bfce04639
commit 3ade7829d0
16 changed files with 120 additions and 97 deletions

View file

@ -224,7 +224,7 @@ INT CommandAttrib (LPTSTR cmd, LPTSTR param)
}
/* build parameter array */
arg = split (param, &argc);
arg = split (param, &argc, FALSE);
/* check for options */
for (i = 0; i < argc; i++)

View file

@ -53,7 +53,7 @@ INT CommandChcp (LPTSTR cmd, LPTSTR param)
}
/* get parameters */
arg = split (param, &args);
arg = split (param, &args, FALSE);
/* save old code page */
uOldCodePage = GetConsoleCP ();

View file

@ -160,7 +160,7 @@ CommandChoice (LPTSTR cmd, LPTSTR param)
}
/* build parameter array */
arg = split (param, &argc);
arg = split (param, &argc, FALSE);
/* evaluate arguments */
if (argc > 0)

View file

@ -1,4 +1,4 @@
/* $Id: cmd.h,v 1.21 2001/02/03 10:37:51 ekohl Exp $
/* $Id: cmd.h,v 1.22 2003/01/17 00:31:32 gvg Exp $
*
* CMD.H - header file for the modules in CMD.EXE
*
@ -303,7 +303,7 @@ INT CommandMemory (LPTSTR, LPTSTR);
/* Prototypes for MISC.C */
TCHAR cgetchar (VOID);
BOOL CheckCtrlBreak (INT);
LPTSTR *split (LPTSTR, LPINT);
LPTSTR *split (LPTSTR, LPINT, BOOL);
VOID freep (LPTSTR *);
LPTSTR stpcpy (LPTSTR, LPTSTR);
BOOL IsValidPathName (LPCTSTR);

View file

@ -1,4 +1,4 @@
/* $Id: copy.c,v 1.7 2000/04/08 14:50:47 ekohl Exp $
/* $Id: copy.c,v 1.8 2003/01/17 00:31:32 gvg Exp $
*
* COPY.C -- copy internal command.
*
@ -647,7 +647,7 @@ INT cmd_copy (LPTSTR first, LPTSTR rest)
return 1;
}
p = split (rest, &argc);
p = split (rest, &argc, FALSE);
if (argc == 0)
{

View file

@ -202,7 +202,7 @@ INT cmd_date (LPTSTR cmd, LPTSTR param)
}
/* build parameter array */
arg = split (param, &argc);
arg = split (param, &argc, FALSE);
/* check for options */
for (i = 0; i < argc; i++)

View file

@ -111,7 +111,7 @@ INT CommandDelete (LPTSTR cmd, LPTSTR param)
return 0;
}
arg = split (param, &args);
arg = split (param, &args, FALSE);
if (args > 0)
{

View file

@ -150,7 +150,7 @@ INT CommandFree (LPTSTR cmd, LPTSTR param)
else
szParam = param;
arg = split (szParam, &argc);
arg = split (szParam, &argc, FALSE);
for (i = 0; i < argc; i++)
PrintDiskInfo (arg[i]);

View file

@ -314,7 +314,7 @@ INT cmd_mkdir (LPTSTR cmd, LPTSTR param)
}
else
{
p = split (param, &argc);
p = split (param, &argc, FALSE);
if (argc > 1)
{
/*JPP 20-Jul-1998 use standard error message */
@ -387,7 +387,7 @@ INT cmd_rmdir (LPTSTR cmd, LPTSTR param)
}
else
{
p = split (param, &argc);
p = split (param, &argc, FALSE);
if (argc > 1)
{
/*JPP 20-Jul-1998 use standard error message */

View file

@ -47,7 +47,7 @@ INT cmd_label (LPTSTR cmd, LPTSTR param)
}
/* get parameters */
arg = split (param, &args);
arg = split (param, &args, FALSE);
if (args > 2)
{

View file

@ -35,7 +35,6 @@
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
//#include <stdarg.h>
#include <stdio.h>
#include <tchar.h>
@ -122,16 +121,65 @@ BOOL CheckCtrlBreak (INT mode)
return TRUE;
}
/* add new entry for new argument */
static BOOL add_entry (LPINT ac, LPTSTR **arg, LPTSTR entry)
{
LPTSTR q;
LPTSTR *oldarg;
q = malloc ((_tcslen(entry) + 1) * sizeof (TCHAR));
if (NULL == q)
{
return FALSE;
}
_tcscpy (q, entry);
oldarg = *arg;
*arg = realloc (oldarg, (*ac + 2) * sizeof (LPTSTR));
if (NULL == *arg)
{
*arg = oldarg;
return FALSE;
}
/* save new entry */
(*arg)[*ac] = q;
(*arg)[++(*ac)] = NULL;
return TRUE;
}
static BOOL expand (LPINT ac, LPTSTR **arg, LPTSTR pattern)
{
HANDLE hFind;
WIN32_FIND_DATA FindData;
BOOL ok;
hFind = FindFirstFile (pattern, &FindData);
if (INVALID_HANDLE_VALUE != hFind)
{
do
{
ok = add_entry(ac, arg, FindData.cFileName);
} while (FindNextFile (hFind, &FindData) && ok);
FindClose (hFind);
}
else
{
ok = add_entry(ac, arg, pattern);
}
return ok;
}
/*
* split - splits a line up into separate arguments, deliminators
* are spaces and slashes ('/').
*/
LPTSTR *split (LPTSTR s, LPINT args)
LPTSTR *split (LPTSTR s, LPINT args, BOOL expand_wildcards)
{
LPTSTR *arg;
LPTSTR *p;
LPTSTR start;
LPTSTR q;
INT ac;
@ -178,24 +226,33 @@ LPTSTR *split (LPTSTR s, LPINT args)
/* a word was found */
if (s != start)
{
/* add new entry for new argument */
arg = realloc (p = arg, (ac + 2) * sizeof (LPTSTR));
if (!arg)
{
freep (p);
return NULL;
}
/* create new entry */
q = arg[ac] = malloc (((len = s - start) + 1) * sizeof (TCHAR));
arg[++ac] = NULL;
q = malloc (((len = s - start) + 1) * sizeof (TCHAR));
if (!q)
{
freep (arg);
return NULL;
}
memcpy (q, start, len * sizeof (TCHAR));
q[len] = _T('\0');
if (expand_wildcards && _T('/') != *start &&
(NULL != _tcschr(q, _T('*')) || NULL != _tcschr(q, _T('?'))))
{
if (! expand(&ac, &arg, q))
{
free (q);
freep (arg);
return NULL;
}
}
else
{
if (! add_entry(&ac, &arg, q))
{
free (q);
freep (arg);
return NULL;
}
}
free (q);
}
/* adjust string pointer if quoted (") */

View file

@ -110,7 +110,7 @@ INT cmd_move (LPTSTR cmd, LPTSTR param)
return 0;
}
arg = split (param, &argc);
arg = split (param, &argc, FALSE);
nFiles = argc;
/* read options */

View file

@ -84,7 +84,7 @@ INT cmd_rename (LPTSTR cmd, LPTSTR param)
}
/* split the argument list */
arg = split(param, &args);
arg = split(param, &args, FALSE);
if (args < 2)
{

View file

@ -155,7 +155,7 @@ INT cmd_time (LPTSTR cmd, LPTSTR param)
}
/* build parameter array */
arg = split (param, &argc);
arg = split (param, &argc, FALSE);
/* check for options */
for (i = 0; i < argc; i++)

View file

@ -132,7 +132,7 @@ INT CommandTimer (LPTSTR cmd, LPTSTR param)
}
p = split (param,&argc);
p = split (param, &argc, FALSE);
//read options
for (i = 0; i < argc; i++)

View file

@ -37,18 +37,16 @@
INT cmd_type (LPTSTR cmd, LPTSTR param)
{
TCHAR buff[256];
HANDLE hFile, hConsoleOut, hFind;
HANDLE hFile, hConsoleOut;
DWORD dwRead;
DWORD dwWritten;
BOOL bRet;
INT argc,i;
LPTSTR *argv;
WIN32_FIND_DATA FindData;
LPTSTR errmsg;
hConsoleOut=GetStdHandle (STD_OUTPUT_HANDLE);
if (!_tcsncmp (param, _T("/?"), 2))
{
ConOutPuts (_T("Displays the contents of text files.\n\n"
@ -62,81 +60,49 @@ INT cmd_type (LPTSTR cmd, LPTSTR param)
return 1;
}
argv = split (param, &argc);
argv = split (param, &argc, TRUE);
for (i = 0; i < argc; i++)
{
hFind=FindFirstFile(argv[i],&FindData);
if (hFind==INVALID_HANDLE_VALUE)
if (_T('/') == argv[i][0])
{
ConErrPrintf("File not found - %s\n",argv[i]);
ConErrPrintf("Invalid option \"%s\"\n", argv[i] + 1);
continue;
}
hFile = CreateFile(argv[i],
GENERIC_READ,
FILE_SHARE_READ,NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &errmsg,
0,
NULL);
ConErrPrintf ("%s - %s", argv[i], errmsg);
LocalFree (errmsg);
continue;
}
do
{
hFile = CreateFile(FindData.cFileName,
GENERIC_READ,
FILE_SHARE_READ,NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
bRet = ReadFile(hFile,buff,sizeof(buff),&dwRead,NULL);
if(hFile == INVALID_HANDLE_VALUE)
{
ConErrPrintf("File not found - %s\n",FindData.cFileName);
continue;
}
do
{
bRet = ReadFile(hFile,buff,sizeof(buff),&dwRead,NULL);
if (dwRead>0 && bRet)
WriteFile(hConsoleOut,buff,dwRead,&dwWritten,NULL);
if (dwRead>0 && bRet)
WriteFile(hConsoleOut,buff,dwRead,&dwWritten,NULL);
} while(dwRead>0 && bRet);
} while(dwRead>0 && bRet);
CloseHandle(hFile);
}
while(FindNextFile(hFind,&FindData));
FindClose(hFind);
CloseHandle(hFile);
}
/*
if (args > 1)
{
error_too_many_parameters (_T("\b \b"));
freep (arg);
return 1;
}
hFile = CreateFile (arg[0], GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN,
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
error_sfile_not_found (param);
freep (arg);
return 1;
}
do
{
bResult = ReadFile (hFile, szBuffer, sizeof(szBuffer),
&dwBytesRead, NULL);
if (dwBytesRead)
WriteFile (GetStdHandle (STD_OUTPUT_HANDLE), szBuffer, dwBytesRead,
&dwBytesWritten, NULL);
}
while (bResult && dwBytesRead > 0);
CloseHandle (hFile);
*/
freep (argv);
return 0;