Mon Mar 18 22:54:32 1996 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[glibc.git] / wcsmbs / mbsncmp.c
blob43fb527afbdf531a3eb63b704d253f538719c4e4
1 /* Copyright (C) 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <mbstr.h>
20 #include <stdlib.h>
22 #define __need_wchar_t
23 /* FIXME: should be defined in stddef.h.
24 !!! #define __need_uwchar_t */
25 typedef unsigned int uwchar_t;
26 #include <stddef.h>
29 /* Compare N characters of MBS1 and MBS2. */
30 int
31 mbsncmp (mbs1, mbs2, n)
32 const char *mbs1;
33 const char *mbs2;
34 size_t n;
36 size_t len = 0;
37 int clen1 = 0;
38 int clen2 = 0;
39 uwchar_t c1;
40 uwchar_t c2;
42 if (n == 0)
43 return 0;
45 /* Reset multibyte characters to their initial state. */
46 (void) mblen ((char *) NULL, 0);
50 clen1 = mbtowc ((wchar_t *) &c1, mbs1, MB_CUR_MAX);
51 clen2 = mbtowc ((wchar_t *) &c2, mbs2, MB_CUR_MAX);
53 if (clen1 == 0)
54 return clen2 == 0 ? 0 : -1;
55 if (clen2 == 0)
56 return 1;
57 if (clen1 < 0 || clen2 < 0)
58 /* FIXME: an illegal character appears. What to do? */
59 return c1 - c2;
61 mbs1 += clen1;
62 mbs2 += clen2;
64 while (c1 == c2 && ++len < n);
66 return len < n ? c1 - c2 : 0;