From 3ade7829d06320f6a1aaed296ed71c693885293f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Fri, 17 Jan 2003 00:31:33 +0000 Subject: [PATCH] * 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 --- rosapps/cmd/attrib.c | 2 +- rosapps/cmd/chcp.c | 2 +- rosapps/cmd/choice.c | 2 +- rosapps/cmd/cmd.h | 4 +- rosapps/cmd/copy.c | 4 +- rosapps/cmd/date.c | 2 +- rosapps/cmd/del.c | 2 +- rosapps/cmd/free.c | 2 +- rosapps/cmd/internal.c | 4 +- rosapps/cmd/label.c | 2 +- rosapps/cmd/misc.c | 87 +++++++++++++++++++++++++++++++------- rosapps/cmd/move.c | 2 +- rosapps/cmd/ren.c | 2 +- rosapps/cmd/time.c | 2 +- rosapps/cmd/timer.c | 2 +- rosapps/cmd/type.c | 96 ++++++++++++++---------------------------- 16 files changed, 120 insertions(+), 97 deletions(-) diff --git a/rosapps/cmd/attrib.c b/rosapps/cmd/attrib.c index 805c5fca49e..a4435aa0991 100644 --- a/rosapps/cmd/attrib.c +++ b/rosapps/cmd/attrib.c @@ -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++) diff --git a/rosapps/cmd/chcp.c b/rosapps/cmd/chcp.c index 7f95235ec30..48d541bc6c8 100644 --- a/rosapps/cmd/chcp.c +++ b/rosapps/cmd/chcp.c @@ -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 (); diff --git a/rosapps/cmd/choice.c b/rosapps/cmd/choice.c index 59e8ea92b7c..97f70f2d3f7 100644 --- a/rosapps/cmd/choice.c +++ b/rosapps/cmd/choice.c @@ -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) diff --git a/rosapps/cmd/cmd.h b/rosapps/cmd/cmd.h index 426c0a20fcd..c1193654c2a 100644 --- a/rosapps/cmd/cmd.h +++ b/rosapps/cmd/cmd.h @@ -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); diff --git a/rosapps/cmd/copy.c b/rosapps/cmd/copy.c index 3e6ea7a2d37..b1596f57dec 100644 --- a/rosapps/cmd/copy.c +++ b/rosapps/cmd/copy.c @@ -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) { diff --git a/rosapps/cmd/date.c b/rosapps/cmd/date.c index ec65045a9fd..bcee0d63e18 100644 --- a/rosapps/cmd/date.c +++ b/rosapps/cmd/date.c @@ -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++) diff --git a/rosapps/cmd/del.c b/rosapps/cmd/del.c index 02a6c29972c..6ef495e9c2e 100644 --- a/rosapps/cmd/del.c +++ b/rosapps/cmd/del.c @@ -111,7 +111,7 @@ INT CommandDelete (LPTSTR cmd, LPTSTR param) return 0; } - arg = split (param, &args); + arg = split (param, &args, FALSE); if (args > 0) { diff --git a/rosapps/cmd/free.c b/rosapps/cmd/free.c index 364bdc8158b..934e776879c 100644 --- a/rosapps/cmd/free.c +++ b/rosapps/cmd/free.c @@ -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]); diff --git a/rosapps/cmd/internal.c b/rosapps/cmd/internal.c index b5cf6e9b39e..6f74ab50434 100644 --- a/rosapps/cmd/internal.c +++ b/rosapps/cmd/internal.c @@ -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 */ diff --git a/rosapps/cmd/label.c b/rosapps/cmd/label.c index 6c065389192..b7062f002a3 100644 --- a/rosapps/cmd/label.c +++ b/rosapps/cmd/label.c @@ -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) { diff --git a/rosapps/cmd/misc.c b/rosapps/cmd/misc.c index bd827ac956f..f0889ec0dca 100644 --- a/rosapps/cmd/misc.c +++ b/rosapps/cmd/misc.c @@ -35,7 +35,6 @@ #include #include #include -//#include #include #include @@ -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 (") */ diff --git a/rosapps/cmd/move.c b/rosapps/cmd/move.c index 685a13bd520..fd3744f674e 100644 --- a/rosapps/cmd/move.c +++ b/rosapps/cmd/move.c @@ -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 */ diff --git a/rosapps/cmd/ren.c b/rosapps/cmd/ren.c index d2af84eb24a..ef8065721b2 100644 --- a/rosapps/cmd/ren.c +++ b/rosapps/cmd/ren.c @@ -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) { diff --git a/rosapps/cmd/time.c b/rosapps/cmd/time.c index e6ddc3a0884..eb2fc08dbec 100644 --- a/rosapps/cmd/time.c +++ b/rosapps/cmd/time.c @@ -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++) diff --git a/rosapps/cmd/timer.c b/rosapps/cmd/timer.c index f6e6300535f..2df9e408a5c 100644 --- a/rosapps/cmd/timer.c +++ b/rosapps/cmd/timer.c @@ -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++) diff --git a/rosapps/cmd/type.c b/rosapps/cmd/type.c index 2424bf12c9d..8ed6a508bdc 100644 --- a/rosapps/cmd/type.c +++ b/rosapps/cmd/type.c @@ -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;