/* pathexp.c -- The shell interface to the globbing library. */
-/* Copyright (C) 1995 Free Software Foundation, Inc.
+/* Copyright (C) 1995-2002 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING. If not, write to the Free Software
- Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+ Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
#include "config.h"
#include "pathexp.h"
#include "flags.h"
-#include <glob/fnmatch.h>
-#include <glob/glob.h>
+#include "shmbutil.h"
+
+#include <glob/strmatch.h>
+
+static int glob_name_is_acceptable __P((const char *));
+static void ignore_globbed_names __P((char **, sh_ignore_func_t *));
+
+#if defined (USE_POSIX_GLOB_LIBRARY)
+# include <glob.h>
+typedef int posix_glob_errfunc_t __P((const char *, int));
+#else
+# include <glob/glob.h>
+#endif
/* Control whether * matches .files in globbing. */
int glob_dot_filenames;
register char *string;
{
register int c;
+ char *send;
int open;
+ DECLARE_MBSTATE;
+
open = 0;
+ send = string + strlen (string);
+
while (c = *string++)
{
switch (c)
if (*string++ == '\0')
return (0);
}
+
+ /* Advance one fewer byte than an entire multibyte character to
+ account for the auto-increment in the loop above. */
+#ifdef HANDLE_MULTIBYTE
+ string--;
+ ADVANCE_CHAR_P (string, send - string);
+ string++;
+#else
+ ADVANCE_CHAR_P (string, send - string);
+#endif
}
return (0);
}
to match a filename should be performed. */
char *
quote_string_for_globbing (pathname, qflags)
- char *pathname;
+ const char *pathname;
int qflags;
{
char *temp;
register int i, j;
- temp = xmalloc (strlen (pathname) + 1);
+ temp = (char *)xmalloc (strlen (pathname) + 1);
if ((qflags & QGLOB_CVTNULL) && QUOTED_NULL (pathname))
{
for (i = j = 0; pathname[i]; i++)
{
if (pathname[i] == CTLESC)
- {
- if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
- continue;
+ {
+ if ((qflags & QGLOB_FILENAME) && pathname[i+1] == '/')
+ continue;
temp[j++] = '\\';
- }
- else
- temp[j++] = pathname[i];
+ i++;
+ if (pathname[i] == '\0')
+ break;
+ }
+ temp[j++] = pathname[i];
}
temp[j] = '\0';
quote_globbing_chars (string)
char *string;
{
- char *temp, *s, *t;
+ size_t slen;
+ char *temp, *s, *t, *send;
+ DECLARE_MBSTATE;
- temp = xmalloc (strlen (string) * 2 + 1);
+ slen = strlen (string);
+ send = string + slen;
+
+ temp = (char *)xmalloc (slen * 2 + 1);
for (t = temp, s = string; *s; )
{
switch (*s)
- {
- case '*':
- case '[':
- case ']':
- case '?':
- case '\\':
- *t++ = '\\';
- break;
- case '+':
- case '@':
- case '!':
+ {
+ case '*':
+ case '[':
+ case ']':
+ case '?':
+ case '\\':
+ *t++ = '\\';
+ break;
+ case '+':
+ case '@':
+ case '!':
if (s[1] == '(') /*(*/
*t++ = '\\';
break;
- }
- *t++ = *s++;
+ }
+
+ /* Copy a single (possibly multibyte) character from s to t,
+ incrementing both. */
+ COPY_CHAR_P (t, s, send);
}
*t = '\0';
return temp;
/* Call the glob library to do globbing on PATHNAME. */
char **
shell_glob_filename (pathname)
- char *pathname;
+ const char *pathname;
{
#if defined (USE_POSIX_GLOB_LIBRARY)
register int i;
- char *temp, **return_value;
+ char *temp, **results;
glob_t filenames;
int glob_flags;
filenames.gl_offs = 0;
+# if defined (GLOB_PERIOD)
glob_flags = glob_dot_filenames ? GLOB_PERIOD : 0;
+# else
+ glob_flags = 0;
+# endif /* !GLOB_PERIOD */
+
glob_flags |= (GLOB_ERR | GLOB_DOOFFS);
- i = glob (temp, glob_flags, (Function *)NULL, &filenames);
+ i = glob (temp, glob_flags, (posix_glob_errfunc_t *)NULL, &filenames);
free (temp);
- if (i == GLOB_NOSPACE || i == GLOB_ABEND)
+ if (i == GLOB_NOSPACE || i == GLOB_ABORTED)
return ((char **)NULL);
-
- if (i == GLOB_NOMATCH)
+ else if (i == GLOB_NOMATCH)
+ filenames.gl_pathv = (char **)NULL;
+ else if (i != 0) /* other error codes not in POSIX.2 */
filenames.gl_pathv = (char **)NULL;
- return (filenames.gl_pathv);
+ results = filenames.gl_pathv;
+
+ if (results && ((GLOB_FAILED (results)) == 0))
+ {
+ if (should_ignore_glob_matches ())
+ ignore_glob_matches (results);
+ if (results && results[0])
+ strvec_sort (results);
+ else
+ {
+ FREE (results);
+ results = (char **)NULL;
+ }
+ }
+
+ return (results);
#else /* !USE_POSIX_GLOB_LIBRARY */
noglob_dot_filenames = glob_dot_filenames == 0;
temp = quote_string_for_globbing (pathname, QGLOB_FILENAME);
-
- results = glob_filename (temp);
+ results = glob_filename (temp, 0);
free (temp);
if (results && ((GLOB_FAILED (results)) == 0))
if (should_ignore_glob_matches ())
ignore_glob_matches (results);
if (results && results[0])
- sort_char_array (results);
+ strvec_sort (results);
else
{
FREE (results);
(struct ign *)0,
0,
(char *)0,
- (Function *)0,
+ (sh_iv_item_func_t *)0,
};
/* Set up to ignore some glob matches because the value of GLOBIGNORE
/* Return 0 if NAME matches a pattern in the globignore.ignores list. */
static int
glob_name_is_acceptable (name)
- char *name;
+ const char *name;
{
struct ign *p;
int flags;
flags = FNM_PATHNAME | FNMATCH_EXTFLAG;
for (p = globignore.ignores; p->val; p++)
{
- if (fnmatch (p->val, name, flags) != FNM_NOMATCH)
- return (0);
+ if (strmatch (p->val, (char *)name, flags) != FNM_NOMATCH)
+ return (0);
}
return (1);
}
static void
ignore_globbed_names (names, name_func)
char **names;
- Function *name_func;
+ sh_ignore_func_t *name_func;
{
char **newnames;
int n, i;
for (i = 0; names[i]; i++)
;
- newnames = (char **)xmalloc ((i + 1) * sizeof (char *));
+ newnames = strvec_create (i + 1);
for (n = i = 0; names[i]; i++)
{
if ((*name_func) (names[i]))
- newnames[n++] = names[i];
+ newnames[n++] = names[i];
else
free (names[i]);
}
ivp->ignores[numitems].len = strlen (colon_bit);
ivp->ignores[numitems].flags = 0;
if (ivp->item_func)
- (*ivp->item_func) (&ivp->ignores[numitems]);
+ (*ivp->item_func) (&ivp->ignores[numitems]);
numitems++;
}
ivp->ignores[numitems].val = (char *)NULL;