coreutils/lib/memcasecmp.c

61 lines
1.7 KiB
C
Raw Normal View History

1996-04-25 12:33:03 +08:00
/* Case-insensitive buffer comparator.
Copyright (C) 1996, 1997, 2000, 2003 Free Software Foundation, Inc.
1996-04-25 12:33:03 +08:00
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
1996-04-25 12:33:03 +08:00
/* Written by Jim Meyering. */
1996-04-25 12:33:03 +08:00
#if HAVE_CONFIG_H
1996-04-10 11:29:05 +08:00
# include <config.h>
#endif
#include <ctype.h>
2003-08-18 17:44:49 +08:00
#if defined STDC_HEADERS || (!defined isascii && !defined HAVE_ISASCII)
# define IN_CTYPE_DOMAIN(Char) 1
#else
2003-08-18 17:44:49 +08:00
# define IN_CTYPE_DOMAIN(Char) isascii (Char)
#endif
#define ISLOWER(Char) (IN_CTYPE_DOMAIN (Char) && islower (Char))
1996-04-10 11:29:05 +08:00
#if _LIBC || STDC_HEADERS
# define TOUPPER(Char) toupper (Char)
1996-04-10 11:29:05 +08:00
#else
# define TOUPPER(Char) (ISLOWER (Char) ? toupper (Char) : (Char))
1996-04-10 11:29:05 +08:00
#endif
#include "memcasecmp.h"
1997-10-27 12:59:25 +08:00
/* Like memcmp, but ignore differences in case.
Convert to upper case (not lower) before comparing so that
join -i works with sort -f. */
1996-04-10 11:29:05 +08:00
int
memcasecmp (const void *vs1, const void *vs2, size_t n)
1996-04-10 11:29:05 +08:00
{
2003-08-18 17:44:49 +08:00
size_t i;
char const *s1 = vs1;
char const *s2 = vs2;
1996-04-10 11:29:05 +08:00
for (i = 0; i < n; i++)
{
2003-08-18 17:44:49 +08:00
unsigned char u1 = s1[i];
unsigned char u2 = s2[i];
int diff = TOUPPER (u1) - TOUPPER (u2);
if (diff)
return diff;
1996-04-10 11:29:05 +08:00
}
return 0;
}