[CentOS] compile question on CentOS 7

Fred Smith fredex at fcshome.stoneham.ma.us
Mon Mar 18 02:05:59 UTC 2019


On Sun, Mar 17, 2019 at 09:23:23PM -0400, Jerry Geis wrote:
> I am trying to use the fnmatch() function in a C program and pass as the
> 3rd arg (options) of case insensitive search...
> 
> When I use the value FNM_CASEFOLD it does not work...
> 
> I'm confused on what to send to fnmatch() to do a case insensitive search.
> 
> Any pointers ? Thanks.

Depends on what you mean by "doesn't work". If you mean that you
get a compile-time error:

cc -o foo foo.c
foo.c: In function \u2018main\u2019:
foo.c:20:35: error: \u2018FNM_CASEFOLD\u2019 undeclared (first use in this function)
  ret = fnmatch (argv[1], argv[2], FNM_CASEFOLD);
                                   ^
foo.c:20:35: note: each undeclared identifier is reported only once for each function it appears in

that's what I get too. The man page doesn't say anything about special
requirements to use that function, but if you go look at fnmatch.h, 
you'll find:

#if !defined _POSIX_C_SOURCE || _POSIX_C_SOURCE < 2 || defined _GNU_SOURCE
# define FNM_FILE_NAME   FNM_PATHNAME   /* Preferred GNU name.  */
# define FNM_LEADING_DIR (1 << 3)       /* Ignore `/...' after a match.  */
# define FNM_CASEFOLD    (1 << 4)       /* Compare without regard to case.  */
# define FNM_EXTMATCH    (1 << 5)       /* Use ksh-like extended matching. */
#endif

which indicates that those values aren't available UNLESS your program
has defines matching the first line shown. I haven't bothered to check
for the value of _POSIX_C_SOURCE in the absence of commandline options
that change the GCC default behavior, but just defining _GNU_SOURCE
is enough to make it compile:

[fredex at fcshome tmp]$ cc -o foo foo.c
[fredex at fcshome tmp]$ 

foo.c:

	#include <stdio.h>
	#include <stdlib.h>

	#define _GNU_SOURCE
	#include <fnmatch.h>

	int main (int argc, char ** argv)
		{
		int ret = 0;

		if (argc != 3)
			{
			printf ("Oops. please specify two strings to compare\n");
			exit (0);
			}

		ret = fnmatch (argv[1], argv[2], FNM_CASEFOLD);
		printf ("fnmatch %s, %s returned %d\n", argv[1], argv[2], ret);

		return 0;
		}

and running it results in:

$ ./foo abc abc
fnmatch abc, abc returned 0

$ ./foo abc aBc
fnmatch abc, aBc returned 0

$ ./foo abc aBcz
fnmatch abc, aBcz returned 1

-- 
-------------------------------------------------------------------------------
 .----    Fred Smith   /              
( /__  ,__.   __   __ /  __   : /     
 /    /  /   /__) /  /  /__) .+'           Home: fredex at fcshome.stoneham.ma.us 
/    /  (__ (___ (__(_ (___ / :__                                 781-438-5471 
-------------------------------- Jude 1:24,25 ---------------------------------


More information about the CentOS mailing list