2 #include "FreeBSDGNUReplacements.h"
4 #if __FreeBSD_version < 800067
7 * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 strnlen(const char *s, size_t maxlen)
37 for (len = 0; len < maxlen; len++, s++) {
44 /* Copyright (C) 1991, 1992, 1995, 1996, 1997 Free Software Foundation, Inc.
45 This file is part of the GNU C Library.
47 The GNU C Library is free software; you can redistribute it and/or
48 modify it under the terms of the GNU Library General Public License as
49 published by the Free Software Foundation; either version 2 of the
50 License, or (at your option) any later version.
52 The GNU C Library is distributed in the hope that it will be useful,
53 but WITHOUT ANY WARRANTY; without even the implied warranty of
54 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
55 Library General Public License for more details.
57 You should have received a copy of the GNU Library General Public
58 License along with the GNU C Library; see the file COPYING.LIB. If not,
59 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
60 Boston, MA 02111-1307, USA.
68 /* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR
69 (and null-terminate it). *LINEPTR is a pointer returned from malloc (or
70 NULL), pointing to *N characters of space. It is realloc'd as
71 necessary. Returns the number of characters read (not including the
72 null terminator), or -1 on error or EOF. */
75 getdelim(char **lineptr, size_t *n, int terminator, FILE *stream)
80 if (stream == NULL || lineptr == NULL || n == NULL)
89 /* Make sure we have a line buffer to start with. */
90 if (*lineptr == NULL || *n < 2) /* !seen and no buf yet need 2 chars. */
95 line = (char *)realloc (*lineptr, MAX_CANON);
114 register int c = getc (stream);
117 else if ((*p++ = c) == terminator)
121 /* Need to enlarge the line buffer. */
124 line = (char *)realloc (line, size);
136 /* Return a partial line since we got an error in the middle. */
144 /* Compare strings while treating digits characters numerically.
145 Copyright (C) 1997, 2002, 2005 Free Software Foundation, Inc.
146 This file is part of the libiberty library.
147 Contributed by Jean-François Bignolles <bignolle@ecoledoc.ibp.fr>, 1997.
149 Libiberty is free software; you can redistribute it and/or
150 modify it under the terms of the GNU Lesser General Public
151 License as published by the Free Software Foundation; either
152 version 2.1 of the License, or (at your option) any later version.
154 Libiberty is distributed in the hope that it will be useful,
155 but WITHOUT ANY WARRANTY; without even the implied warranty of
156 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
157 Lesser General Public License for more details.
159 You should have received a copy of the GNU Lesser General Public
160 License along with the GNU C Library; if not, write to the Free
161 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
166 #define ISASCII(c) isascii(c)
167 #define ISDIGIT(c) (ISASCII (c) && isdigit (c))
168 /* states: S_N: normal, S_I: comparing integral part, S_F: comparing
169 fractional parts, S_Z: idem but with leading Zeroes only */
175 /* result_type: CMP: return diff; LEN: compare using len_diff/diff */
180 /* Compare S1 and S2 as strings holding indices/version numbers,
181 returning less than, equal to or greater than zero if S1 is less than,
182 equal to or greater than S2 (for more info, see the Glibc texinfo doc). */
185 strverscmp (const char *s1, const char *s2)
187 const unsigned char *p1 = (const unsigned char *) s1;
188 const unsigned char *p2 = (const unsigned char *) s2;
189 unsigned char c1, c2;
193 /* Symbol(s) 0 [1-9] others (padding)
194 Transition (10) 0 (01) d (00) x (11) - */
195 static const unsigned int next_state[] =
198 /* S_N */ S_N, S_I, S_Z, S_N,
199 /* S_I */ S_N, S_I, S_I, S_I,
200 /* S_F */ S_N, S_F, S_F, S_F,
201 /* S_Z */ S_N, S_F, S_Z, S_Z
204 static const int result_type[] =
206 /* state x/x x/d x/0 x/- d/x d/d d/0 d/-
207 0/x 0/d 0/0 0/- -/x -/d -/0 -/- */
209 /* S_N */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
210 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
211 /* S_I */ CMP, -1, -1, CMP, +1, LEN, LEN, CMP,
212 +1, LEN, LEN, CMP, CMP, CMP, CMP, CMP,
213 /* S_F */ CMP, CMP, CMP, CMP, CMP, LEN, CMP, CMP,
214 CMP, CMP, CMP, CMP, CMP, CMP, CMP, CMP,
215 /* S_Z */ CMP, +1, +1, CMP, -1, CMP, CMP, CMP,
224 /* Hint: '0' is a digit too. */
225 state = S_N | ((c1 == '0') + (ISDIGIT (c1) != 0));
227 while ((diff = c1 - c2) == 0 && c1 != '\0')
229 state = next_state[state];
232 state |= (c1 == '0') + (ISDIGIT (c1) != 0);
235 state = result_type[state << 2 | (((c2 == '0') + (ISDIGIT (c2) != 0)))];
243 while (ISDIGIT (*p1++))
244 if (!ISDIGIT (*p2++))
247 return ISDIGIT (*p2) ? -1 : diff;