ash: if using libc glob(), skip it if no metachars are in word
authorDenys Vlasenko <vda.linux@googlemail.com>
Sun, 30 Oct 2016 17:41:01 +0000 (18:41 +0100)
committerDenys Vlasenko <vda.linux@googlemail.com>
Sun, 30 Oct 2016 17:41:01 +0000 (18:41 +0100)
This saves making tons of pointless stat() calls

function                                             old     new   delta
expandarg                                            888     921     +33

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
shell/ash.c

index d617168b9c26f2ab491d14ae8d236ff1fbbb6c40..ecd2146e462592329676c303def614fb01f4b90c 100644 (file)
@@ -7047,6 +7047,21 @@ expandmeta(struct strlist *str /*, int flag*/)
 
                if (fflag)
                        goto nometa;
+
+               /* Avoid glob() (and thus, stat() et al) for words like "echo" */
+               p = str->text;
+               while (*p) {
+                       if (*p == '*')
+                               goto need_glob;
+                       if (*p == '?')
+                               goto need_glob;
+                       if (*p == '[')
+                               goto need_glob;
+                       p++;
+               }
+               goto nometa;
+
+ need_glob:
                INT_OFF;
                p = preglob(str->text, RMESCAPE_ALLOC | RMESCAPE_HEAP);
 // GLOB_NOMAGIC (GNU): if no *?[ chars in pattern, return it even if no match