Commit 2ee90b9b 2ee90b9b7bfa8631f752a1b8cf56f7b189087df7 by Sergey Poznyakoff

Added to the repository by gnulib-sync

1 parent 1a2c62bf
1 /* intprops.h -- properties of integer types
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19 /* Written by Paul Eggert. */
20
21 #include <limits.h>
22
23 /* The extra casts in the following macros work around compiler bugs,
24 e.g., in Cray C 5.0.3.0. */
25
26 /* True if the arithmetic type T is an integer type. bool counts as
27 an integer. */
28 #define TYPE_IS_INTEGER(t) ((t) 1.5 == 1)
29
30 /* True if negative values of the signed integer type T use two's
31 complement, ones' complement, or signed magnitude representation,
32 respectively. Much GNU code assumes two's complement, but some
33 people like to be portable to all possible C hosts. */
34 #define TYPE_TWOS_COMPLEMENT(t) ((t) ~ (t) 0 == (t) -1)
35 #define TYPE_ONES_COMPLEMENT(t) ((t) ~ (t) 0 == 0)
36 #define TYPE_SIGNED_MAGNITUDE(t) ((t) ~ (t) 0 < (t) -1)
37
38 /* True if the arithmetic type T is signed. */
39 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
40
41 /* The maximum and minimum values for the integer type T. These
42 macros have undefined behavior if T is signed and has padding bits.
43 If this is a problem for you, please let us know how to fix it for
44 your host. */
45 #define TYPE_MINIMUM(t) \
46 ((t) (! TYPE_SIGNED (t) \
47 ? (t) 0 \
48 : TYPE_SIGNED_MAGNITUDE (t) \
49 ? ~ (t) 0 \
50 : ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1)))
51 #define TYPE_MAXIMUM(t) \
52 ((t) (! TYPE_SIGNED (t) \
53 ? (t) -1 \
54 : ~ (~ (t) 0 << (sizeof (t) * CHAR_BIT - 1))))
55
56 /* Bound on length of the string representing an integer type or expression T.
57 Subtract 1 for the sign bit if t is signed; log10 (2.0) < 146/485;
58 add 1 for integer division truncation; add 1 more for a minus sign
59 if needed. */
60 #define INT_STRLEN_BOUND(t) \
61 ((sizeof (t) * CHAR_BIT - 1) * 146 / 485 + 2)
62
63 /* Bound on buffer size needed to represent an integer type or expression T,
64 including the terminating null. */
65 #define INT_BUFSIZE_BOUND(t) (INT_STRLEN_BOUND (t) + 1)
1 /* Report a memory allocation failure and exit.
2
3 Copyright (C) 1997, 1998, 1999, 2000, 2002, 2003, 2004 Free
4 Software Foundation, Inc.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19
20 #if HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "xalloc.h"
25
26 #include <stdlib.h>
27
28 #include "error.h"
29 #include "exitfail.h"
30
31 #include "gettext.h"
32 #define _(msgid) gettext (msgid)
33 #define N_(msgid) msgid
34
35 void
36 xalloc_die (void)
37 {
38 error (exit_failure, 0, "%s", _("memory exhausted"));
39
40 /* The `noreturn' cannot be given to error, since it may return if
41 its first argument is 0. To help compilers understand the
42 xalloc_die does not return, call abort. Also, the abort is a
43 safety feature if exit_failure is 0 (which shouldn't happen). */
44 abort ();
45 }
1 # getdelim.m4 serial 1
2
3 dnl Copyright (C) 2005 Free Software dnl Foundation, Inc.
4 dnl
5 dnl This file is free software; the Free Software Foundation
6 dnl gives unlimited permission to copy and/or distribute it,
7 dnl with or without modifications, as long as this notice is preserved.
8
9 AC_PREREQ(2.52)
10
11 AC_DEFUN([gl_FUNC_GETDELIM],
12 [
13 MU_LIBSOURCES([getdelim.c, getdelim.h])
14
15 dnl Persuade glibc <stdio.h> to declare getdelim().
16 AC_REQUIRE([AC_GNU_SOURCE])
17
18 MU_REPLACE_FUNCS(getdelim)
19 AC_CHECK_DECLS_ONCE(getdelim)
20
21 if test $ac_cv_func_getdelim = no; then
22 gl_PREREQ_GETDELIM
23 fi
24 ])
25
26 # Prerequisites of lib/getdelim.c.
27 AC_DEFUN([gl_PREREQ_GETDELIM],
28 [
29 AC_CHECK_FUNCS([flockfile funlockfile])
30 ])
1 # mbchar.m4 serial 1
2 dnl Copyright (C) 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 dnl autoconf tests required for use of mbchar.m4
8 dnl From Bruno Haible.
9
10 AC_DEFUN([gl_MBCHAR],
11 [
12 AC_REQUIRE([AC_GNU_SOURCE])
13 :
14 ])
1 # memchr.m4 serial 4
2 dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_MEMCHR],
8 [
9 MU_REPLACE_FUNCS(memchr)
10 if test $ac_cv_func_memchr = no; then
11 gl_PREREQ_MEMCHR
12 fi
13 ])
14
15 # Prerequisites of lib/memchr.c.
16 AC_DEFUN([gl_PREREQ_MEMCHR], [
17 AC_CHECK_HEADERS(bp-sym.h)
18 ])
1 # minmax.m4 serial 1
2 dnl Copyright (C) 2005 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_MINMAX],
8 [
9 AC_REQUIRE([gl_PREREQ_MINMAX])
10 ])
11
12 # Prerequisites of lib/minmax.h.
13 AC_DEFUN([gl_PREREQ_MINMAX],
14 [
15 gl_MINMAX_IN_HEADER([limits.h])
16 gl_MINMAX_IN_HEADER([sys/param.h])
17 ])
18
19 dnl gl_MINMAX_IN_HEADER(HEADER)
20 AC_DEFUN([gl_MINMAX_IN_HEADER],
21 [
22 define([header],[translit([$1],[./-],
23 [___])])
24 define([HEADER],[translit([$1],[abcdefghijklmnopqrstuvwxyz./-],
25 [ABCDEFGHIJKLMNOPQRSTUVWXYZ___])])
26 AC_CACHE_CHECK([whether <$1> defines MIN and MAX],
27 [gl_cv_minmax_in_]header,
28 [AC_TRY_COMPILE([#include <$1>
29 int x = MIN (42, 17);], [],
30 [gl_cv_minmax_in_]header[=yes],
31 [gl_cv_minmax_in_]header[=no])])
32 if test $gl_cv_minmax_in_[]header = yes; then
33 AC_DEFINE([HAVE_MINMAX_IN_]HEADER, 1,
34 [Define to 1 if <$1> defines the MIN and MAX macros.])
35 fi
36 undefine([HEADER])
37 undefine([header])
38 ])
1 /* getdelim.c --- Implementation of replacement getdelim function.
2 Copyright (C) 1994, 1996, 1997, 1998, 2001, 2003, 2005 Free
3 Software Foundation, Inc.
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or (at
8 your option) any later version.
9
10 This program is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 02110-1301, USA. */
19
20 /* Ported from glibc by Simon Josefsson. */
21
22 #if HAVE_CONFIG_H
23 # include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <errno.h>
28
29 #include "getdelim.h"
30
31 #if !HAVE_FLOCKFILE
32 # undef flockfile
33 # define flockfile(x) ((void) 0)
34 #endif
35 #if !HAVE_FUNLOCKFILE
36 # undef funlockfile
37 # define funlockfile(x) ((void) 0)
38 #endif
39
40 /* Read up to (and including) a DELIMITER from FP into *LINEPTR (and
41 NUL-terminate it). *LINEPTR is a pointer returned from malloc (or
42 NULL), pointing to *N characters of space. It is realloc'ed as
43 necessary. Returns the number of characters read (not including
44 the null terminator), or -1 on error or EOF. */
45
46 ssize_t
47 getdelim (char **lineptr, size_t *n, int delimiter, FILE *fp)
48 {
49 int result;
50 ssize_t cur_len = 0;
51 ssize_t len;
52
53 if (lineptr == NULL || n == NULL || fp == NULL)
54 {
55 errno = EINVAL;
56 return -1;
57 }
58
59 flockfile (fp);
60
61 if (*lineptr == NULL || *n == 0)
62 {
63 *n = 120;
64 *lineptr = (char *) malloc (*n);
65 if (*lineptr == NULL)
66 {
67 result = -1;
68 goto unlock_return;
69 }
70 }
71
72 for (;;)
73 {
74 char *t;
75 int i;
76
77 i = getc (fp);
78 if (i == EOF)
79 break;
80
81 /* Make enough space for len+1 (for final NUL) bytes. */
82 if (cur_len + 1 >= *n)
83 {
84 size_t needed = 2 * (cur_len + 1) + 1; /* Be generous. */
85 char *new_lineptr;
86
87 if (needed < cur_len)
88 {
89 result = -1;
90 goto unlock_return;
91 }
92
93 new_lineptr = (char *) realloc (*lineptr, needed);
94 if (new_lineptr == NULL)
95 {
96 result = -1;
97 goto unlock_return;
98 }
99
100 *lineptr = new_lineptr;
101 *n = needed;
102 }
103
104 (*lineptr)[cur_len] = i;
105 cur_len++;
106
107 if (i == delimiter)
108 break;
109 }
110 (*lineptr)[cur_len] = '\0';
111 result = cur_len;
112
113 unlock_return:
114 funlockfile (fp);
115 return result;
116 }
1 /* getdelim.h --- Prototype for replacement getdelim function.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2, or (at
7 your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA. */
18
19 /* Written by Simon Josefsson. */
20
21 /* Get size_t, FILE, ssize_t. And getdelim, if available. */
22 # include <stddef.h>
23 # include <stdio.h>
24 # include <sys/types.h>
25
26 #if !HAVE_DECL_GETDELIM
27 ssize_t getdelim (char **lineptr, size_t *n, int delimiter, FILE *stream);
28 #endif /* !HAVE_GETDELIM */
1 /* Copyright (C) 2001 Free Software Foundation, Inc.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2, or (at your option)
6 any later version.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software Foundation,
15 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
16
17
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <limits.h>
23
24 #include "mbchar.h"
25
26 #if IS_BASIC_ASCII
27
28 /* Bit table of characters in the ISO C "basic character set". */
29 unsigned int is_basic_table [UCHAR_MAX / 32 + 1] =
30 {
31 0x00001a00, /* '\t' '\v' '\f' */
32 0xffffffef, /* ' '...'#' '%'...'?' */
33 0xfffffffe, /* 'A'...'Z' '[' '\\' ']' '^' '_' */
34 0x7ffffffe /* 'a'...'z' '{' '|' '}' '~' */
35 /* The remaining bits are 0. */
36 };
37
38 #endif /* IS_BASIC_ASCII */
1 /* Copyright (C) 1991, 1993, 1996, 1997, 1999, 2000, 2003, 2004 Free
2 Software Foundation, Inc.
3
4 Based on strlen implementation by Torbjorn Granlund (tege@sics.se),
5 with help from Dan Sahlin (dan@sics.se) and
6 commentary by Jim Blandy (jimb@ai.mit.edu);
7 adaptation to memchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
8 and implemented by Roland McGrath (roland@ai.mit.edu).
9
10 NOTE: The canonical source of this file is maintained with the GNU C Library.
11 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
12
13 This program is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by the
15 Free Software Foundation; either version 2, or (at your option) any
16 later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 USA. */
27
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #include <string.h>
33
34 #include <stddef.h>
35
36 #if defined _LIBC
37 # include <memcopy.h>
38 #else
39 # define reg_char char
40 #endif
41
42 #include <limits.h>
43
44 #if HAVE_BP_SYM_H || defined _LIBC
45 # include <bp-sym.h>
46 #else
47 # define BP_SYM(sym) sym
48 #endif
49
50 #undef memchr
51 #undef __memchr
52
53 /* Search no more than N bytes of S for C. */
54 void *
55 __memchr (void const *s, int c_in, size_t n)
56 {
57 const unsigned char *char_ptr;
58 const unsigned long int *longword_ptr;
59 unsigned long int longword, magic_bits, charmask;
60 unsigned reg_char c;
61 int i;
62
63 c = (unsigned char) c_in;
64
65 /* Handle the first few characters by reading one character at a time.
66 Do this until CHAR_PTR is aligned on a longword boundary. */
67 for (char_ptr = (const unsigned char *) s;
68 n > 0 && (size_t) char_ptr % sizeof longword != 0;
69 --n, ++char_ptr)
70 if (*char_ptr == c)
71 return (void *) char_ptr;
72
73 /* All these elucidatory comments refer to 4-byte longwords,
74 but the theory applies equally well to any size longwords. */
75
76 longword_ptr = (const unsigned long int *) char_ptr;
77
78 /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
79 the "holes." Note that there is a hole just to the left of
80 each byte, with an extra at the end:
81
82 bits: 01111110 11111110 11111110 11111111
83 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
84
85 The 1-bits make sure that carries propagate to the next 0-bit.
86 The 0-bits provide holes for carries to fall into. */
87
88 /* Set MAGIC_BITS to be this pattern of 1 and 0 bits.
89 Set CHARMASK to be a longword, each of whose bytes is C. */
90
91 magic_bits = 0xfefefefe;
92 charmask = c | (c << 8);
93 charmask |= charmask << 16;
94 #if 0xffffffffU < ULONG_MAX
95 magic_bits |= magic_bits << 32;
96 charmask |= charmask << 32;
97 if (8 < sizeof longword)
98 for (i = 64; i < sizeof longword * 8; i *= 2)
99 {
100 magic_bits |= magic_bits << i;
101 charmask |= charmask << i;
102 }
103 #endif
104 magic_bits = (ULONG_MAX >> 1) & (magic_bits | 1);
105
106 /* Instead of the traditional loop which tests each character,
107 we will test a longword at a time. The tricky part is testing
108 if *any of the four* bytes in the longword in question are zero. */
109 while (n >= sizeof longword)
110 {
111 /* We tentatively exit the loop if adding MAGIC_BITS to
112 LONGWORD fails to change any of the hole bits of LONGWORD.
113
114 1) Is this safe? Will it catch all the zero bytes?
115 Suppose there is a byte with all zeros. Any carry bits
116 propagating from its left will fall into the hole at its
117 least significant bit and stop. Since there will be no
118 carry from its most significant bit, the LSB of the
119 byte to the left will be unchanged, and the zero will be
120 detected.
121
122 2) Is this worthwhile? Will it ignore everything except
123 zero bytes? Suppose every byte of LONGWORD has a bit set
124 somewhere. There will be a carry into bit 8. If bit 8
125 is set, this will carry into bit 16. If bit 8 is clear,
126 one of bits 9-15 must be set, so there will be a carry
127 into bit 16. Similarly, there will be a carry into bit
128 24. If one of bits 24-30 is set, there will be a carry
129 into bit 31, so all of the hole bits will be changed.
130
131 The one misfire occurs when bits 24-30 are clear and bit
132 31 is set; in this case, the hole at bit 31 is not
133 changed. If we had access to the processor carry flag,
134 we could close this loophole by putting the fourth hole
135 at bit 32!
136
137 So it ignores everything except 128's, when they're aligned
138 properly.
139
140 3) But wait! Aren't we looking for C, not zero?
141 Good point. So what we do is XOR LONGWORD with a longword,
142 each of whose bytes is C. This turns each byte that is C
143 into a zero. */
144
145 longword = *longword_ptr++ ^ charmask;
146
147 /* Add MAGIC_BITS to LONGWORD. */
148 if ((((longword + magic_bits)
149
150 /* Set those bits that were unchanged by the addition. */
151 ^ ~longword)
152
153 /* Look at only the hole bits. If any of the hole bits
154 are unchanged, most likely one of the bytes was a
155 zero. */
156 & ~magic_bits) != 0)
157 {
158 /* Which of the bytes was C? If none of them were, it was
159 a misfire; continue the search. */
160
161 const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
162
163 if (cp[0] == c)
164 return (void *) cp;
165 if (cp[1] == c)
166 return (void *) &cp[1];
167 if (cp[2] == c)
168 return (void *) &cp[2];
169 if (cp[3] == c)
170 return (void *) &cp[3];
171 if (4 < sizeof longword && cp[4] == c)
172 return (void *) &cp[4];
173 if (5 < sizeof longword && cp[5] == c)
174 return (void *) &cp[5];
175 if (6 < sizeof longword && cp[6] == c)
176 return (void *) &cp[6];
177 if (7 < sizeof longword && cp[7] == c)
178 return (void *) &cp[7];
179 if (8 < sizeof longword)
180 for (i = 8; i < sizeof longword; i++)
181 if (cp[i] == c)
182 return (void *) &cp[i];
183 }
184
185 n -= sizeof longword;
186 }
187
188 char_ptr = (const unsigned char *) longword_ptr;
189
190 while (n-- > 0)
191 {
192 if (*char_ptr == c)
193 return (void *) char_ptr;
194 else
195 ++char_ptr;
196 }
197
198 return 0;
199 }
200 #ifdef weak_alias
201 weak_alias (__memchr, BP_SYM (memchr))
202 #endif
This diff could not be displayed because it is too large.
This diff could not be displayed because it is too large.
1 /* size_max.h -- declare SIZE_MAX through system headers
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19 #ifndef GNULIB_SIZE_MAX_H
20 #define GNULIB_SIZE_MAX_H
21
22 # include <limits.h>
23 # if HAVE_STDINT_H
24 # include <stdint.h>
25 # endif
26
27 #endif /* GNULIB_SIZE_MAX_H */
1 /* Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
2 Written by Bruno Haible <haible@clisp.cons.org>, 2001.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
17
18 #ifndef _STDBOOL_H
19 #define _STDBOOL_H
20
21 /* ISO C 99 <stdbool.h> for platforms that lack it. */
22
23 /* Usage suggestions:
24
25 Programs that use <stdbool.h> should be aware of some limitations
26 and standards compliance issues.
27
28 Standards compliance:
29
30 - <stdbool.h> must be #included before 'bool', 'false', 'true'
31 can be used.
32
33 - You cannot assume that sizeof (bool) == 1.
34
35 - Programs should not undefine the macros bool, true, and false,
36 as C99 lists that as an "obsolescent feature".
37
38 Limitations of this substitute, when used in a C89 environment:
39
40 - <stdbool.h> must be #included before the '_Bool' type can be used.
41
42 - You cannot assume that _Bool is a typedef; it might be a macro.
43
44 - In C99, casts and automatic conversions to '_Bool' or 'bool' are
45 performed in such a way that every nonzero value gets converted
46 to 'true', and zero gets converted to 'false'. This doesn't work
47 with this substitute. With this substitute, only the values 0 and 1
48 give the expected result when converted to _Bool' or 'bool'.
49
50 Also, it is suggested that programs use 'bool' rather than '_Bool';
51 this isn't required, but 'bool' is more common. */
52
53
54 /* 7.16. Boolean type and values */
55
56 /* BeOS <sys/socket.h> already #defines false 0, true 1. We use the same
57 definitions below, but temporarily we have to #undef them. */
58 #ifdef __BEOS__
59 # include <OS.h> /* defines bool but not _Bool */
60 # undef false
61 # undef true
62 #endif
63
64 /* For the sake of symbolic names in gdb, we define true and false as
65 enum constants, not only as macros.
66 It is tempting to write
67 typedef enum { false = 0, true = 1 } _Bool;
68 so that gdb prints values of type 'bool' symbolically. But if we do
69 this, values of type '_Bool' may promote to 'int' or 'unsigned int'
70 (see ISO C 99 6.7.2.2.(4)); however, '_Bool' must promote to 'int'
71 (see ISO C 99 6.3.1.1.(2)). So we add a negative value to the
72 enum; this ensures that '_Bool' promotes to 'int'. */
73 #if !(defined __cplusplus || defined __BEOS__)
74 # if !@HAVE__BOOL@
75 # if defined __SUNPRO_C && (__SUNPRO_C < 0x550 || __STDC__ == 1)
76 /* Avoid stupid "warning: _Bool is a keyword in ISO C99". */
77 # define _Bool signed char
78 enum { false = 0, true = 1 };
79 # else
80 typedef enum { _Bool_must_promote_to_int = -1, false = 0, true = 1 } _Bool;
81 # endif
82 # endif
83 #else
84 typedef bool _Bool;
85 #endif
86 #define bool _Bool
87
88 /* The other macros must be usable in preprocessor directives. */
89 #define false 0
90 #define true 1
91 #define __bool_true_false_are_defined 1
92
93 #endif /* _STDBOOL_H */
1 /* Find the length of STRING, but scan at most MAXLEN characters.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3 Written by Simon Josefsson.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18
19 #ifndef STRNLEN_H
20 #define STRNLEN_H
21
22 /* Get strnlen declaration, if available. */
23 #include <string.h>
24
25 #if defined HAVE_DECL_STRNLEN && !HAVE_DECL_STRNLEN
26 /* Find the length (number of bytes) of STRING, but scan at most
27 MAXLEN bytes. If no '\0' terminator is found in that many bytes,
28 return MAXLEN. */
29 extern size_t strnlen(const char *string, size_t maxlen);
30 #endif
31
32 #endif /* STRNLEN_H */
1 /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program 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.
13
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA. */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 /* Specification. */
24 #include "strnlen1.h"
25
26 #include <string.h>
27
28 /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
29 If no '\0' terminator is found in that many characters, return MAXLEN. */
30 /* This is the same as strnlen (string, maxlen - 1) + 1. */
31 size_t
32 strnlen1 (const char *string, size_t maxlen)
33 {
34 const char *end = memchr (string, '\0', maxlen);
35 if (end != NULL)
36 return end - string + 1;
37 else
38 return maxlen;
39 }
1 /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
2 Copyright (C) 2005 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published
6 by the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program 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.
13
14 You should have received a copy of the GNU Library General Public
15 License along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA. */
18
19 #ifndef _STRNLEN1_H
20 #define _STRNLEN1_H
21
22 #include <stddef.h>
23
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29
30 /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
31 If no '\0' terminator is found in that many characters, return MAXLEN. */
32 /* This is the same as strnlen (string, maxlen - 1) + 1. */
33 extern size_t strnlen1 (const char *string, size_t maxlen);
34
35
36 #ifdef __cplusplus
37 }
38 #endif
39
40
41 #endif /* _STRNLEN1_H */