int main (void) { char *linebuf = NULL; size_t linebuflen = 0; int ntests = 0; int nfailed = 0; int nskipped = 0; char *escinput = NULL; size_t escinputlen = 0; char *escpattern = NULL; size_t escpatternlen = 0; int nr = 0; /* Read lines from stdin with the following format: locale input-string match-string flags result where `result' is either 0 or 1. If the first character of a string is '"' we read until the next '"' and handled escaped '"'. */ while (! feof (stdin)) { ssize_t n = getline (&linebuf, &linebuflen, stdin); char *cp; const char *locale; const char *input; const char *pattern; const char *result_str; int result; const char *flags; int flags_val; int fnmres; char numbuf[24]; if (n == -1) break; if (n == 0) /* Maybe an empty line. */ continue; /* Skip over all leading white spaces. */ cp = linebuf; locale = next_input (&cp, 1, 0); if (locale == NULL) continue; input = next_input (&cp, 0, 0); if (input == NULL) continue; pattern = next_input (&cp, 0, 0); if (pattern == NULL) continue; result_str = next_input (&cp, 0, 0); if (result_str == NULL) continue; if (strcmp (result_str, "0") == 0) result = 0; else if (strcasecmp (result_str, "NOMATCH") == 0) result = FNM_NOMATCH; else { char *endp; result = strtol (result_str, &endp, 0); if (*endp != '\0') continue; } flags = next_input (&cp, 0, 1); if (flags == NULL) /* We allow the flags missing. */ flags = ""; /* Convert the text describing the flags in a numeric value. */ flags_val = convert_flags (flags); if (flags_val == -1) /* Something went wrong. */ continue; /* Now run the actual test. */ ++ntests; #ifdef __UCLIBC_HAS_XLOCALE__ if (setlocale (LC_COLLATE, locale) == NULL || setlocale (LC_CTYPE, locale) == NULL) { puts ("*** Cannot set locale"); ++nfailed; continue; } #else /* skip non-ascii strings */ if (!str_isalpha(pattern) || !str_isalpha(input)) { ++nskipped; printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP multibyte test (requires locale support)\n", ++nr, pattern, input); continue; } /* skip collating symbols */ if (str_has_funk(pattern, '.') || str_has_funk(input, '.')) { ++nskipped; printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP collating symbol test (requires locale support)\n", ++nr, pattern, input); continue; } /* skip equivalence class expressions */ if (str_has_funk(pattern, '=') || str_has_funk(input, '=')) { ++nskipped; printf("%3d: fnmatch (\"%s\", \"%s\"): SKIP equivalence class test (requires locale support)\n", ++nr, pattern, input); continue; } #endif fnmres = fnmatch (pattern, input, flags_val); printf ("%3d: fnmatch (\"%s\", \"%s\", %s) = %s%c", ++nr, escape (pattern, &escpatternlen, &escpattern), escape (input, &escinputlen, &escinput), flag_output (flags_val), (fnmres == 0 ? "0" : (fnmres == FNM_NOMATCH ? "FNM_NOMATCH" : (sprintf (numbuf, "%d", fnmres), numbuf))), (fnmres != 0) != (result != 0) ? ' ' : '\n'); if ((fnmres != 0) != (result != 0)) { printf ("(FAIL, expected %s) ***\n", result == 0 ? "0" : (result == FNM_NOMATCH ? "FNM_NOMATCH" : (sprintf (numbuf, "%d", result), numbuf))); ++nfailed; } } printf ("=====================\n%3d tests, %3d failed, %3d skipped\n", ntests, nfailed, nskipped); free (escpattern); free (escinput); free (linebuf); return nfailed != 0; }
_export bool str_isalnum(char ch) { return str_isalpha(ch) || str_isdigit(ch); }