diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2013-07-01 17:38:06 +0400 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2013-07-10 16:00:55 +0400 |
commit | d1e644e59ee5eb679a7118b35a055e1249d250e2 (patch) | |
tree | dd2af38e1db50e2c31553352ae379a85792209b3 /gnulib/lib/strcasecmp.c | |
parent | 5e147cf09fe97d9488571e222f54e6ed5b7846ba (diff) | |
download | dctrl-tools-d1e644e59ee5eb679a7118b35a055e1249d250e2.tar.gz |
Added gnulib (argp)
Diffstat (limited to 'gnulib/lib/strcasecmp.c')
-rw-r--r-- | gnulib/lib/strcasecmp.c | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/gnulib/lib/strcasecmp.c b/gnulib/lib/strcasecmp.c new file mode 100644 index 0000000..77ca288 --- /dev/null +++ b/gnulib/lib/strcasecmp.c @@ -0,0 +1,62 @@ +/* Case-insensitive string comparison function. + Copyright (C) 1998-1999, 2005-2007, 2009-2012 Free Software Foundation, Inc. + + 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 3, 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, see <http://www.gnu.org/licenses/>. */ + +#include <config.h> + +/* Specification. */ +#include <string.h> + +#include <ctype.h> +#include <limits.h> + +#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch)) + +/* Compare strings S1 and S2, ignoring case, returning less than, equal to or + greater than zero if S1 is lexicographically less than, equal to or greater + than S2. + Note: This function does not work with multibyte strings! */ + +int +strcasecmp (const char *s1, const char *s2) +{ + const unsigned char *p1 = (const unsigned char *) s1; + const unsigned char *p2 = (const unsigned char *) s2; + unsigned char c1, c2; + + if (p1 == p2) + return 0; + + do + { + c1 = TOLOWER (*p1); + c2 = TOLOWER (*p2); + + if (c1 == '\0') + break; + + ++p1; + ++p2; + } + while (c1 == c2); + + if (UCHAR_MAX <= INT_MAX) + return c1 - c2; + else + /* On machines where 'char' and 'int' are types of the same size, the + difference of two 'unsigned char' values - including the sign bit - + doesn't fit in an 'int'. */ + return (c1 > c2 ? 1 : c1 < c2 ? -1 : 0); +} |