[PATCH] Fix glob() function

Jordi Sanfeliu <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
Hello,

While porting 'mandoc' [1] to my hobbyOS FiwixOS, I've discovered that the
glob() function in Newlib always returns zero (success) even when the
pathname was not found.

I was comparing the file 'libc/posix/glob.c' with the one from Apple [2] 
and then I applied the following patch:

diff --git a/newlib/libc/posix/glob.c b/newlib/libc/posix/glob.c
index 5e6c2fcba..0347979de 100644
--- a/newlib/libc/posix/glob.c
+++ b/newlib/libc/posix/glob.c
@@ -502,11 +502,14 @@ glob0(pattern, pglob, limit)
   	 * 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));
+	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(3);	/* GLOB_NOMATCH */
+	}
   	else if (!(pglob->gl_flags & GLOB_NOSORT))
   		qsort(pglob->gl_pathv + pglob->gl_offs + oldpathc,
   		    pglob->gl_pathc - oldpathc, sizeof(char *), compare);



The following is a simple test program:

# cat glob.c
#include <stdio.h>
#include <glob.h>

int main(void)
{
 	int globres;
 	glob_t globinfo;
 	char *ok1 = "/usr/share/man/man1/ls.[01-9]*";
 	char *ko1 = "/usr/share/man/man1/lsx.[01-9]*";
 	char *ok2 = "glob.c";
 	char *ko2 = "glob.x";

 	globres = glob(ok1, 0, NULL, &globinfo);
 	printf("globres = %d (%s)\n", globres, ok1);
 	globres = glob(ko1, 0, NULL, &globinfo);
 	printf("globres = %d (%s)\n", globres, ko1);

 	globres = glob(ok2, 0, NULL, &globinfo);
 	printf("globres = %d (%s)\n", globres, ok2);
 	globres = glob(ko2, 0, NULL, &globinfo);
 	printf("globres = %d (%s)\n", globres, ko2);

 	return 0;
}


The results before applying the patch were:

# ./glob
globres = 0 (/usr/share/man/man1/ls.[01-9]*)
globres = 0 (/usr/share/man/man1/lsx.[01-9]*)
globres = 0 (glob.c)
globres = 0 (glob.x)


The results after applying the patch are:
# ./glob
globres = 0 (/usr/share/man/man1/ls.[01-9]*)
globres = 3 (/usr/share/man/man1/lsx.[01-9]*)
globres = 0 (glob.c)
globres = 3 (glob.x)


Thanks.

[1] https://mandoc.bsd.lv/
[2] 
https://opensource.apple.com/source/Libinfo/Libinfo-129/util.subproj/glob.c

--
Jordi Sanfeliu
FIBRANET Network Services Provider
https://www.fibranet.cat
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.