* Move (optional) wildcard expansion to split()
authorGé van Geldorp <ge@gse.nl>
Fri, 17 Jan 2003 00:31:33 +0000 (00:31 +0000)
committerGé van Geldorp <ge@gse.nl>
Fri, 17 Jan 2003 00:31:33 +0000 (00:31 +0000)
* Only expand arguments actually containing wildcard characters
  (to prevent expanding directory names to all files within that directory)

svn path=/trunk/; revision=4018

16 files changed:
rosapps/cmd/attrib.c
rosapps/cmd/chcp.c
rosapps/cmd/choice.c
rosapps/cmd/cmd.h
rosapps/cmd/copy.c
rosapps/cmd/date.c
rosapps/cmd/del.c
rosapps/cmd/free.c
rosapps/cmd/internal.c
rosapps/cmd/label.c
rosapps/cmd/misc.c
rosapps/cmd/move.c
rosapps/cmd/ren.c
rosapps/cmd/time.c
rosapps/cmd/timer.c
rosapps/cmd/type.c

index 805c5fc..a4435aa 100644 (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++)
index 7f95235..48d541b 100644 (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 ();
index 59e8ea9..97f70f2 100644 (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)
index 426c0a2..c119365 100644 (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);
index 3e6ea7a..b1596f5 100644 (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)
        {
index ec65045..bcee0d6 100644 (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++)
index 02a6c29..6ef495e 100644 (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)
        {
index 364bdc8..934e776 100644 (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]);
index b5cf6e9..6f74ab5 100644 (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 */
index 6c06538..b7062f0 100644 (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)
        {
index bd827ac..f0889ec 100644 (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 (") */
index 685a13b..fd3744f 100644 (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 */
index d2af84e..ef80657 100644 (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)
     {
index e6ddc3a..eb2fc08 100644 (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++)
index f6e6300..2df9e40 100644 (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++)
index 2424bf1..8ed6a50 100644 (file)
 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);
 
-               do
+               if(hFile == INVALID_HANDLE_VALUE)
                {
-                       hFile = CreateFile(FindData.cFileName,
-                               GENERIC_READ,
-                               FILE_SHARE_READ,NULL,
-                               OPEN_EXISTING,
-                               FILE_ATTRIBUTE_NORMAL,NULL);
-
-                       if(hFile == INVALID_HANDLE_VALUE)
-                       {
-                               ConErrPrintf("File not found - %s\n",FindData.cFileName);
-                               continue;
-                       }
+                       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
-                       {
-                               bRet = ReadFile(hFile,buff,sizeof(buff),&dwRead,NULL);
+               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);
-
-                       CloseHandle(hFile);
+               } while(dwRead>0 && bRet);
 
-               }
-               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;