Date: Sat, 06 Jul 2002 05:38:33 -0400 (EDT) From: Mike Heffner <mikeh@freebsd.org> To: standards@freebsd.org Subject: Update of glob(3) Message-ID: <XFMail.20020706053833.mikeh@freebsd.org>
next in thread | raw e-mail | index | archive | help
This message is in MIME format --_=XFMail.1.5.2.FreeBSD:20020706053833:97334=_ Content-Type: text/plain; charset=us-ascii Hi all, Below is a patch to update glob(3) to add the newer options in 1003.1-2001 and correct some mis-conforming behaviors. Specifically: - add GLOB_NOMATCH return value and use it when we don't get a match - rename GLOB_ABEND to GLOB_ABORTED and use it instead of returning 1 in some places - add GLOB_NOESCAPE flag Reviews are welcome, I'll probably commit it in a week or so. Thanks, Mike -- Mike Heffner <mheffner@[acm.]vt.edu> <mikeh@FreeBSD.org> Index: include/glob.h =================================================================== RCS file: /home/ncvs/src/include/glob.h,v retrieving revision 1.6 diff -u -r1.6 glob.h --- include/glob.h 23 Mar 2002 17:24:53 -0000 1.6 +++ include/glob.h 6 Jul 2002 09:14:19 -0000 @@ -70,7 +70,15 @@ #define GLOB_MARK 0x0008 /* Append / to matching directories. */ #define GLOB_NOCHECK 0x0010 /* Return pattern itself if nothing matches. */ #define GLOB_NOSORT 0x0020 /* Don't sort. */ +#define GLOB_NOESCAPE 0x2000 /* Disable backslash escaping. */ +/* Error values returned by glob(3) */ +#define GLOB_NOSPACE (-1) /* Malloc call failed. */ +#define GLOB_ABORTED (-2) /* Unignored error. */ +#define GLOB_NOMATCH (-3) /* No match and GLOB_NOCHECK was not set. */ +#define GLOB_NOSYS (-4) /* Obsolete: for source comptability only. */ + +#ifndef _POSIX_SOURCE #define GLOB_ALTDIRFUNC 0x0040 /* Use alternately specified directory funcs. */ #define GLOB_BRACE 0x0080 /* Expand braces ala csh. */ #define GLOB_MAGCHAR 0x0100 /* Pattern had globbing characters. */ @@ -79,11 +87,10 @@ #define GLOB_TILDE 0x0800 /* Expand tilde names from the passwd file. */ #define GLOB_LIMIT 0x1000 /* limit number of returned paths */ -/* backwards compatibility, this is the old name for this option */ +/* source compatibility, these are the old names */ #define GLOB_MAXPATH GLOB_LIMIT - -#define GLOB_NOSPACE (-1) /* Malloc call failed. */ -#define GLOB_ABEND (-2) /* Unignored error. */ +#define GLOB_ABEND GLOB_ABORTED +#endif __BEGIN_DECLS int glob(const char *, int, int (*)(const char *, int), glob_t *); Index: lib/libc/gen/glob.3 =================================================================== RCS file: /home/ncvs/src/lib/libc/gen/glob.3,v retrieving revision 1.20 diff -u -r1.20 glob.3 --- lib/libc/gen/glob.3 1 Oct 2001 16:08:51 -0000 1.20 +++ lib/libc/gen/glob.3 6 Jul 2002 09:14:19 -0000 @@ -187,9 +187,15 @@ .Fa pattern , with the number of total pathnames is set to 1, and the number of matched pathnames set to 0. +The effect of backslash escaping is present in the pattern returned. +.It Dv GLOB_NOESCAPE +By default, a backslash +.Pq Ql \e +character is used to escape the following character in the pattern, +avoiding any special interpretation of the character. If -.Dv GLOB_QUOTE -is set, its effect is present in the pattern returned. +.Dv GLOB_NOESCAPE +is set, backslash escaping is disabled. .It Dv GLOB_NOSORT By default, the pathnames are sorted in ascending .Tn ASCII @@ -250,12 +256,6 @@ is provided to simplify implementing the historic .Xr csh 1 globbing behavior and should probably not be used anywhere else. -.It Dv GLOB_QUOTE -Use the backslash -.Pq Ql \e -character for quoting: every occurrence of -a backslash followed by a character in the pattern is replaced by that -character, avoiding any special interpretation of the character. .It Dv GLOB_TILDE Expand patterns that start with .Ql ~ @@ -304,7 +304,7 @@ returns non-zero, .Fn glob stops the scan and returns -.Dv GLOB_ABEND +.Dv GLOB_ABORTED after setting .Fa gl_pathc and @@ -386,12 +386,16 @@ was specified in the flags and .Fa pglob\->gl_matchc or more patterns were matched. -.It Dv GLOB_ABEND +.It Dv GLOB_ABORTED The scan was stopped because an error was encountered and either .Dv GLOB_ERR was set or .Fa \*(lp*errfunc\*(rp\*(lp\*(rp returned non-zero. +.It Dv GLOB_NOMATCH +The pattern did not match a pathname and +.Dv GLOB_NOCHECK +was not set. .El .Pp The arguments @@ -430,7 +434,6 @@ .Dv GLOB_LIMIT , .Dv GLOB_MAGCHAR , .Dv GLOB_NOMAGIC , -.Dv GLOB_QUOTE , and .Dv GLOB_TILDE , and the fields Index: lib/libc/gen/glob.c =================================================================== RCS file: /home/ncvs/src/lib/libc/gen/glob.c,v retrieving revision 1.19 diff -u -r1.19 glob.c --- lib/libc/gen/glob.c 1 Feb 2002 01:32:19 -0000 1.19 +++ lib/libc/gen/glob.c 6 Jul 2002 09:14:20 -0000 @@ -182,7 +182,10 @@ bufnext = patbuf; bufend = bufnext + MAXPATHLEN - 1; - if (flags & GLOB_QUOTE) { + if (flags & GLOB_NOESCAPE) + while (bufnext < bufend && (c = *patnext++) != EOS) + *bufnext++ = c; + else { /* Protect the quoted characters. */ while (bufnext < bufend && (c = *patnext++) != EOS) if (c == QUOTE) { @@ -195,9 +198,6 @@ else *bufnext++ = c; } - else - while (bufnext < bufend && (c = *patnext++) != EOS) - *bufnext++ = c; *bufnext = EOS; if (flags & GLOB_BRACE) @@ -415,8 +415,7 @@ * The main glob() routine: compiles the pattern (optionally processing * quotes), calls glob1() to do the real pattern matching, and finally * sorts the list (unless unsorted operation is requested). Returns 0 - * if things went well, nonzero if errors occurred. It is not an error - * to find no matches. + * if things went well, nonzero if errors occurred. */ static int glob0(pattern, pglob, limit) @@ -493,12 +492,15 @@ * and the pattern did not contain any magic characters * GLOB_NOMAGIC is there just for compatibility with csh. */ - if (pglob->gl_pathc == oldpathc && - ((pglob->gl_flags & GLOB_NOCHECK) || - ((pglob->gl_flags & GLOB_NOMAGIC) && - !(pglob->gl_flags & GLOB_MAGCHAR)))) - return(globextend(pattern, pglob, limit)); - else if (!(pglob->gl_flags & GLOB_NOSORT)) + if (pglob->gl_pathc == oldpathc) { + if (((pglob->gl_flags & GLOB_NOCHECK) || + ((pglob->gl_flags & GLOB_NOMAGIC) && + !(pglob->gl_flags & GLOB_MAGCHAR)))) + return(globextend(pattern, pglob, limit)); + else + return(GLOB_NOMATCH); + } + if (!(pglob->gl_flags & GLOB_NOSORT)) qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc, pglob->gl_pathc - oldpathc, sizeof(char *), compare); return(0); @@ -557,7 +559,7 @@ (g_stat(pathbuf, &sb, pglob) == 0) && S_ISDIR(sb.st_mode)))) { if (pathend + 1 > pathend_last) - return (1); + return (GLOB_ABORTED); *pathend++ = SEP; *pathend = EOS; } @@ -572,7 +574,7 @@ if (ismeta(*p)) anymeta = 1; if (q + 1 > pathend_last) - return (1); + return (GLOB_ABORTED); *q++ = *p++; } @@ -581,7 +583,7 @@ pattern = p; while (*pattern == SEP) { if (pathend + 1 > pathend_last) - return (1); + return (GLOB_ABORTED); *pathend++ = *pattern++; } } else /* Need expansion, recurse. */ @@ -611,7 +613,7 @@ struct dirent *(*readdirfunc)(); if (pathend > pathend_last) - return (1); + return (GLOB_ABORTED); *pathend = EOS; errno = 0; @@ -619,10 +621,10 @@ /* TODO: don't call for ENOENT or ENOTDIR? */ if (pglob->gl_errfunc) { if (g_Ctoc(pathbuf, buf, sizeof(buf))) - return (GLOB_ABEND); + return (GLOB_ABORTED); if (pglob->gl_errfunc(buf, errno) || pglob->gl_flags & GLOB_ERR) - return (GLOB_ABEND); + return (GLOB_ABORTED); } return(0); } --_=XFMail.1.5.2.FreeBSD:20020706053833:97334=_ Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.0.6 (FreeBSD) Comment: For info see http://www.gnupg.org iD8DBQE9JrqZFokZQs3sv5kRAsmtAJ4zd5qOFoL5iDanumNqDCnXLqaRKACeN/IZ 97jLLr2GHcP0MoRSq5dOz9Y= =wBN4 -----END PGP SIGNATURE----- --_=XFMail.1.5.2.FreeBSD:20020706053833:97334=_-- End of MIME message To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-standards" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?XFMail.20020706053833.mikeh>