Gmatch did not trim the characters inside a range.
[unix-history] / usr / src / bin / csh / glob.c
index 0028772..51214df 100644 (file)
@@ -6,12 +6,23 @@
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)glob.c     5.16 (Berkeley) %G%";
+static char sccsid[] = "@(#)glob.c     5.21 (Berkeley) %G%";
 #endif /* not lint */
 
 #endif /* not lint */
 
+#include <sys/param.h>
+#include <glob.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#if __STDC__
+# include <stdarg.h>
+#else
+# include <varargs.h>
+#endif
+
 #include "csh.h"
 #include "extern.h"
 #include "csh.h"
 #include "extern.h"
-#include <glob.h>
 
 static int noglob, nonomatch;
 static int pargsiz, gargsiz;
 
 static int noglob, nonomatch;
 static int pargsiz, gargsiz;
@@ -31,9 +42,9 @@ static int pargsiz, gargsiz;
 #define RBRK ']'
 #define EOS '\0'
 
 #define RBRK ']'
 #define EOS '\0'
 
-Char  **gargv = (Char **) 0;
+Char  **gargv = NULL;
 long    gargc = 0;
 long    gargc = 0;
-Char  **pargv = (Char **) 0;
+Char  **pargv = NULL;
 long    pargc = 0;
 
 /*
 long    pargc = 0;
 
 /*
@@ -45,13 +56,13 @@ long    pargc = 0;
  * handled in glob() which is part of the 4.4BSD libc.
  *
  */
  * handled in glob() which is part of the 4.4BSD libc.
  *
  */
-static Char *globtilde();
-static Char **libglob();
-static Char **globexpand();
-static int globbrace();
-static void pword();
-static void psave();
-static void backeval();
+static Char    *globtilde __P((Char **, Char *));
+static Char    **libglob __P((Char **));
+static Char    **globexpand __P((Char **));
+static int     globbrace __P((Char *, Char *, Char ***));
+static void    pword __P((void));
+static void    psave __P((int));
+static void    backeval __P((Char *, bool));
 
 
 static Char *
 
 
 static Char *
@@ -93,7 +104,7 @@ globbrace(s, p, bl)
     int     size = GLOBSPACE;
 
     nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
     int     size = GLOBSPACE;
 
     nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
-    *vl = (Char *) 0;
+    *vl = NULL;
 
     len = 0;
     /* copy part up to the brace */
 
     len = 0;
     /* copy part up to the brace */
@@ -130,7 +141,7 @@ globbrace(s, p, bl)
            for (++pm; *pm != RBRK && *pm != EOS; pm++)
                continue;
            if (*pm == EOS) {
            for (++pm; *pm != RBRK && *pm != EOS; pm++)
                continue;
            if (*pm == EOS) {
-               *vl = (Char *) 0;
+               *vl = NULL;
                blkfree(nv);
                return (-RBRK);
            }
                blkfree(nv);
                return (-RBRK);
            }
@@ -166,7 +177,7 @@ globbrace(s, p, bl)
            }
            break;
        }
            }
            break;
        }
-    *vl = (Char *) 0;
+    *vl = NULL;
     *bl = nv;
     return (len);
 }
     *bl = nv;
     return (len);
 }
@@ -181,7 +192,7 @@ globexpand(v)
 
 
     nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
 
 
     nv = vl = (Char **) xmalloc((size_t) sizeof(Char *) * size);
-    *vl = (Char *) 0;
+    *vl = NULL;
 
     /*
      * Step 1: expand backquotes.
 
     /*
      * Step 1: expand backquotes.
@@ -201,7 +212,7 @@ globexpand(v)
                }
            }
            xfree((ptr_t) pargv);
                }
            }
            xfree((ptr_t) pargv);
-           pargv = (Char **) 0;
+           pargv = NULL;
        }
        else {
            *vl++ = Strsave(s);
        }
        else {
            *vl++ = Strsave(s);
@@ -213,7 +224,7 @@ globexpand(v)
            }
        }
     }
            }
        }
     }
-    *vl = (Char *) 0;
+    *vl = NULL;
 
     if (noglob)
        return (nv);
 
     if (noglob)
        return (nv);
@@ -341,7 +352,7 @@ libglob(vl)
        if (!nonomatch && (globv.gl_matchc == 0) &&
            (globv.gl_flags & GLOB_MAGCHAR)) {
            globfree(&globv);
        if (!nonomatch && (globv.gl_matchc == 0) &&
            (globv.gl_flags & GLOB_MAGCHAR)) {
            globfree(&globv);
-           return ((Char **) 0);
+           return (NULL);
        }
        gflgs |= GLOB_APPEND;
     }
        }
        gflgs |= GLOB_APPEND;
     }
@@ -373,7 +384,11 @@ globone(str, action)
         */
        vo = globexpand(v);
        if (noglob || (gflag & G_GLOB) == 0) {
         */
        vo = globexpand(v);
        if (noglob || (gflag & G_GLOB) == 0) {
-           if (vo[1] != (Char *) 0)
+           if (vo[0] == NULL) {
+               xfree((ptr_t) vo);
+               return (Strsave(STRNULL));
+           }
+           if (vo[1] != NULL)
                return (handleone(str, vo, action));
            else {
                str = strip(vo[0]);
                return (handleone(str, vo, action));
            else {
                str = strip(vo[0]);
@@ -390,17 +405,21 @@ globone(str, action)
     vl = libglob(vo);
     if (gflag & G_CSH)
        blkfree(vo);
     vl = libglob(vo);
     if (gflag & G_CSH)
        blkfree(vo);
-    if (vl == (Char **) 0) {
+    if (vl == NULL) {
        setname(short2str(str));
        stderror(ERR_NAME | ERR_NOMATCH);
     }
        setname(short2str(str));
        stderror(ERR_NAME | ERR_NOMATCH);
     }
-    else if (vl[1])
+    if (vl[0] == NULL) {
+       xfree((ptr_t) vl);
+       return (Strsave(STRNULL));
+    }
+    if (vl[1] != NULL)
        return (handleone(str, vl, action));
     else {
        str = strip(*vl);
        xfree((ptr_t) vl);
        return (handleone(str, vl, action));
     else {
        str = strip(*vl);
        xfree((ptr_t) vl);
+       return (str);
     }
     }
-    return (str);
 }
 
 Char  **
 }
 
 Char  **
@@ -609,7 +628,7 @@ backeval(cp, literal)
     xfree((ptr_t) cp);
     (void) close(pvec[1]);
     c = 0;
     xfree((ptr_t) cp);
     (void) close(pvec[1]);
     c = 0;
-    ip = (Char *) 0;
+    ip = NULL;
     do {
        int     cnt = 0;
 
     do {
        int     cnt = 0;
 
@@ -664,7 +683,7 @@ backeval(cp, literal)
 
 static void
 psave(c)
 
 static void
 psave(c)
-    Char    c;
+    int    c;
 {
     if (--pnleft <= 0)
        stderror(ERR_WTOOLONG);
 {
     if (--pnleft <= 0)
        stderror(ERR_WTOOLONG);
@@ -721,9 +740,9 @@ Gmatch(string, pattern)
                        return (0);
                if (match)
                    continue;
                        return (0);
                if (match)
                    continue;
-               if (rangec == '-') {
-                   match = (stringc <= *pattern &&
-                            *(pattern - 2) <= stringc);
+               if (rangec == '-' && *(pattern - 2) != '[' && *pattern != ']') {
+                   match = (stringc <= (*pattern & TRIM) &&
+                            (*(pattern - 2) & TRIM) <= stringc);
                    pattern++;
                }
                else
                    pattern++;
                }
                else