Commit 2db5c711 2db5c7115e5b37734993603c0381aab7e62c4be9 by Sergey Poznyakoff

Updated by gnulib-sync

1 parent 96aac328
1 /* 1 /* Formatted output to strings.
2 Unix snprintf implementation. 2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Version 1.3 3 Written by Simon Josefsson.
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Library General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2, or (at your option)
8 (at your option) any later version. 8 any later version.
9 9
10 This program is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Library General Public License for more details. 13 GNU General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public License 15 You should have received a copy of the GNU General Public License along
16 along with this program; if not, write to the Free Software 16 with this program; if not, write to the Free Software Foundation,
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 18
19 Revision History: 19 #ifndef SNPRINTF_H
20 see header of snprintf.c. 20 #define SNPRINTF_H
21 21
22 format: 22 /* Get snprintf declaration, if available. */
23 int snprintf(holder, sizeof_holder, format, ...) 23 #include <stdio.h>
24 24
25 Return values: 25 #if defined HAVE_DECL_SNPRINTF && !HAVE_DECL_SNPRINTF
26 (sizeof_holder - 1) 26 int snprintf (char *str, size_t size, const char *format, ...);
27
28
29 THANKS(for the patches and ideas):
30 Miles Bader
31 Cyrille Rustom
32 Jacek Slabocewiz
33 Mike Parker(mouse)
34
35 Alain Magloire: alainm@rcsm.ee.mcgill.ca
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include <config.h>
40 #endif
41
42 #if __STDC__
43 #include <stdarg.h>
44 #else
45 #include <varargs.h>
46 #endif
47
48 #include <stdlib.h> /* for atoi() */
49 #include <ctype.h>
50
51
52 /*
53 * For the FLOATING POINT FORMAT :
54 * the challenge was finding a way to
55 * manipulate the Real numbers without having
56 * to resort to mathematical function(it
57 * would require to link with -lm) and not
58 * going down to the bit pattern(not portable)
59 *
60 * so a number, a real is:
61
62 real = integral + fraction
63
64 integral = ... + a(2)*10^2 + a(1)*10^1 + a(0)*10^0
65 fraction = b(1)*10^-1 + b(2)*10^-2 + ...
66
67 where:
68 0 <= a(i) => 9
69 0 <= b(i) => 9
70
71 from then it was simple math
72 */
73
74 /*
75 * size of the buffer for the integral part
76 * and the fraction part
77 */
78 #define MAX_INT 99 + 1 /* 1 for the null */
79 #define MAX_FRACT 29 + 1
80
81 /*
82 * If the compiler supports (long long)
83 */
84 #ifndef LONG_LONG
85 # define LONG_LONG long long
86 /*# define LONG_LONG int64_t*/
87 #endif
88
89 /*
90 * If the compiler supports (long double)
91 */
92 #ifndef LONG_DOUBLE
93 # define LONG_DOUBLE long double
94 /*# define LONG_DOUBLE double*/
95 #endif 27 #endif
96 28
97 /* 29 #endif /* SNPRINTF_H */
98 * numtoa() uses PRIVATE buffers to store the results,
99 * So this function is not reentrant
100 */
101 #define itoa(n) numtoa(n, 10, 0, (char **)0)
102 #define otoa(n) numtoa(n, 8, 0, (char **)0)
103 #define htoa(n) numtoa(n, 16, 0, (char **)0)
104 #define dtoa(n, p, f) numtoa(n, 10, p, f)
105
106 #define SWAP_INT(a,b) {int t; t = (a); (a) = (b); (b) = t;}
107
108 /* this struct holds everything we need */
109 struct DATA {
110 int length;
111 char *holder;
112 int counter;
113 #ifdef __STDC__
114 const char *pf;
115 #else
116 char *pf;
117 #endif
118 /* FLAGS */
119 int width, precision;
120 int justify; char pad;
121 int square, space, star_w, star_p, a_long, a_longlong;
122 };
123
124 #define PRIVATE static
125 #define PUBLIC
126 /* signature of the functions */
127 #ifdef __STDC__
128 /* the floating point stuff */
129 PRIVATE double pow_10(int);
130 PRIVATE int log_10(double);
131 PRIVATE double integral(double, double *);
132 PRIVATE char * numtoa(double, int, int, char **);
133
134 /* for the format */
135 PRIVATE void conv_flag(char *, struct DATA *);
136 PRIVATE void floating(struct DATA *, double);
137 PRIVATE void exponent(struct DATA *, double);
138 PRIVATE void decimal(struct DATA *, double);
139 PRIVATE void octal(struct DATA *, double);
140 PRIVATE void hexa(struct DATA *, double);
141 PRIVATE void strings(struct DATA *, char *);
142
143 #else
144 /* the floating point stuff */
145 PRIVATE double pow_10();
146 PRIVATE int log_10();
147 PRIVATE double integral();
148 PRIVATE char * numtoa();
149
150 /* for the format */
151 PRIVATE void conv_flag();
152 PRIVATE void floating();
153 PRIVATE void exponent();
154 PRIVATE void decimal();
155 PRIVATE void octal();
156 PRIVATE void hexa();
157 PRIVATE void strings();
158 #endif
159
160 /* those are defines specific to snprintf to hopefully
161 * make the code clearer :-)
162 */
163 #define RIGHT 1
164 #define LEFT 0
165 #define NOT_FOUND -1
166 #define FOUND 1
167 #define MAX_FIELD 15
168
169 /* the conversion flags */
170 #define isflag(c) ((c) == '#' || (c) == ' ' || \
171 (c) == '*' || (c) == '+' || \
172 (c) == '-' || (c) == '.' || \
173 isdigit(c))
174
175 /* round off to the precision */
176 #define ROUND(d, p) \
177 (d < 0.) ? \
178 d - pow_10(-(p)->precision) * 0.5 : \
179 d + pow_10(-(p)->precision) * 0.5
180
181 /* set default precision */
182 #define DEF_PREC(p) \
183 if ((p)->precision == NOT_FOUND) \
184 (p)->precision = 6
185
186 /* put a char */
187 #define PUT_CHAR(c, p) \
188 if ((p)->counter < (p)->length) { \
189 *(p)->holder++ = (c); \
190 (p)->counter++; \
191 }
192
193 #define PUT_PLUS(d, p) \
194 if ((d) > 0. && (p)->justify == RIGHT) \
195 PUT_CHAR('+', p)
196
197 #define PUT_SPACE(d, p) \
198 if ((p)->space == FOUND && (d) > 0.) \
199 PUT_CHAR(' ', p)
200
201 /* pad right */
202 #define PAD_RIGHT(p) \
203 if ((p)->width > 0 && (p)->justify != LEFT) \
204 for (; (p)->width > 0; (p)->width--) \
205 PUT_CHAR((p)->pad, p)
206
207 /* pad left */
208 #define PAD_LEFT(p) \
209 if ((p)->width > 0 && (p)->justify == LEFT) \
210 for (; (p)->width > 0; (p)->width--) \
211 PUT_CHAR((p)->pad, p)
212
213 /* if width and prec. in the args */
214 #define STAR_ARGS(p) \
215 if ((p)->star_w == FOUND) \
216 (p)->width = va_arg(args, int); \
217 if ((p)->star_p == FOUND) \
218 (p)->precision = va_arg(args, int)
......
1 /* Like vsprintf but provides a pointer to malloc'd storage, which must 1 /* Formatted output to strings.
2 be freed by the caller. 2 Copyright (C) 1999, 2002-2004 Free Software Foundation, Inc.
3 Copyright (C) 1994 Free Software Foundation, Inc.
4 3
5 This program is free software; you can redistribute it and/or modify 4 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 5 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) 6 the Free Software Foundation; either version 2, or (at your option)
8 any later version. 7 any later version.
9 8
10 This program is distributed in the hope that it will be useful, 9 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 12 GNU General Public License for more details.
14 13
15 You should have received a copy of the GNU General Public License 14 You should have received a copy of the GNU General Public License along
16 along with this program; if not, write to the Free Software 15 with this program; if not, write to the Free Software Foundation,
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 17
19 #ifdef HAVE_CONFIG_H 18 #ifdef HAVE_CONFIG_H
20 # include <config.h> 19 # include <config.h>
21 #endif 20 #endif
22 21
23 #include <stdio.h> 22 /* Specification. */
24 #include <string.h> 23 #include "vasprintf.h"
25 24
26 #if __STDC__ 25 #include <stdlib.h>
27 # include <stdarg.h>
28 #else
29 # include <varargs.h>
30 #endif
31
32 #ifdef TEST
33 int global_total_width;
34 #endif
35
36 unsigned long strtoul ();
37 char *malloc ();
38
39 static int
40 int_vasprintf (result, format, args)
41 char **result;
42 const char *format;
43 va_list *args;
44 {
45 const char *p = format;
46 /* Add one to make sure that it is never zero, which might cause malloc
47 to return NULL. */
48 int total_width = strlen (format) + 1;
49 va_list ap;
50
51 memcpy (&ap, args, sizeof (va_list));
52
53 while (*p != '\0')
54 {
55 if (*p++ == '%')
56 {
57 while (strchr ("-+ #0", *p))
58 ++p;
59 if (*p == '*')
60 {
61 ++p;
62 total_width += abs (va_arg (ap, int));
63 }
64 else
65 total_width += strtoul (p, &p, 10);
66 if (*p == '.')
67 {
68 ++p;
69 if (*p == '*')
70 {
71 ++p;
72 total_width += abs (va_arg (ap, int));
73 }
74 else
75 total_width += strtoul (p, &p, 10);
76 }
77 while (strchr ("hlL", *p))
78 ++p;
79 /* Should be big enough for any format specifier except %s. */
80 total_width += 30;
81 switch (*p)
82 {
83 case 'd':
84 case 'i':
85 case 'o':
86 case 'u':
87 case 'x':
88 case 'X':
89 case 'c':
90 (void) va_arg (ap, int);
91 break;
92 case 'f':
93 case 'e':
94 case 'E':
95 case 'g':
96 case 'G':
97 (void) va_arg (ap, double);
98 break;
99 case 's':
100 total_width += strlen (va_arg (ap, char *));
101 break;
102 case 'p':
103 case 'n':
104 (void) va_arg (ap, char *);
105 break;
106 }
107 }
108 }
109 #ifdef TEST
110 global_total_width = total_width;
111 #endif
112 *result = malloc (total_width);
113 if (*result != NULL)
114 return vsprintf (*result, format, *args);
115 else
116 return 0;
117 }
118
119 int
120 vasprintf (result, format, args)
121 char **result;
122 const char *format;
123 va_list args;
124 {
125 return int_vasprintf (result, format, &args);
126 }
127
128 int
129 asprintf
130 #if __STDC__
131 (char **result, const char *format, ...)
132 #else
133 (result, va_alist)
134 char **result;
135 va_dcl
136 #endif
137 {
138 va_list args;
139 int done;
140
141 #if __STDC__
142 va_start (args, format);
143 #else
144 char *format;
145 va_start (args);
146 format = va_arg (args, char *);
147 #endif
148 done = vasprintf (result, format, args);
149 va_end (args);
150 26
151 return done; 27 #include "vasnprintf.h"
152 }
153
154 #ifdef TEST
155 void
156 checkit
157 #if __STDC__
158 (const char* format, ...)
159 #else
160 (va_alist)
161 va_dcl
162 #endif
163 {
164 va_list args;
165 char *result;
166
167 #if __STDC__
168 va_start (args, format);
169 #else
170 char *format;
171 va_start (args);
172 format = va_arg (args, char *);
173 #endif
174 vasprintf (&result, format, args);
175 va_end (args);
176 if (strlen (result) < global_total_width)
177 printf ("PASS: ");
178 else
179 printf ("FAIL: ");
180 printf ("%d %s\n", global_total_width, result);
181 }
182 28
183 int 29 int
184 main () 30 vasprintf (char **resultp, const char *format, va_list args)
185 { 31 {
186 checkit ("%d", 0x12345678); 32 size_t length;
187 checkit ("%200d", 5); 33 char *result = vasnprintf (NULL, &length, format, args);
188 checkit ("%.300d", 6); 34 if (result == NULL)
189 checkit ("%100.150d", 7); 35 return -1;
190 checkit ("%s", "jjjjjjjjjiiiiiiiiiiiiiiioooooooooooooooooppppppppppppaa\n\ 36
191 777777777777777777333333333333366666666666622222222222777777777777733333"); 37 *resultp = result;
192 checkit ("%f%s%d%s", 1.0, "foo", 77, "asdjffffffffffffffiiiiiiiiiiixxxxx"); 38 /* Return the number of resulting bytes, excluding the trailing NUL.
39 If it wouldn't fit in an 'int', vasnprintf() would have returned NULL
40 and set errno to EOVERFLOW. */
41 return length;
193 } 42 }
194 #endif /* TEST */
......
1 /* xalloc.h -- malloc with out-of-memory checking 1 /* xalloc.h -- malloc with out-of-memory checking
2 Copyright (C) 1990-1998, 1999 Free Software Foundation, Inc. 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2003, 2004 Free Software Foundation, Inc.
3 5
4 This program is free software; you can redistribute it and/or modify 6 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 7 it under the terms of the GNU General Public License as published by
...@@ -18,14 +20,14 @@ ...@@ -18,14 +20,14 @@
18 #ifndef XALLOC_H_ 20 #ifndef XALLOC_H_
19 # define XALLOC_H_ 21 # define XALLOC_H_
20 22
21 # ifndef PARAMS 23 # include <stddef.h>
22 # if defined PROTOTYPES || (defined __STDC__ && __STDC__) 24
23 # define PARAMS(Args) Args 25
24 # else 26 # ifdef __cplusplus
25 # define PARAMS(Args) () 27 extern "C" {
26 # endif
27 # endif 28 # endif
28 29
30
29 # ifndef __attribute__ 31 # ifndef __attribute__
30 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__ 32 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
31 # define __attribute__(x) 33 # define __attribute__(x)
...@@ -36,52 +38,42 @@ ...@@ -36,52 +38,42 @@
36 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) 38 # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
37 # endif 39 # endif
38 40
39 /* Exit value when the requested amount of memory is not available. 41 /* This function is always triggered when memory is exhausted.
40 It is initialized to EXIT_FAILURE, but the caller may set it to 42 It must be defined by the application, either explicitly
41 some other value. */ 43 or by using gnulib's xalloc-die module. This is the
42 extern int xalloc_exit_failure;
43
44 /* If this pointer is non-zero, run the specified function upon each
45 allocation failure. It is initialized to zero. */
46 extern void (*xalloc_fail_func) PARAMS ((void));
47
48 /* If XALLOC_FAIL_FUNC is undefined or a function that returns, this
49 message must be non-NULL. It is translated via gettext.
50 The default value is "Memory exhausted". */
51 extern char *const xalloc_msg_memory_exhausted;
52
53 /* This function is always triggered when memory is exhausted. It is
54 in charge of honoring the three previous items. This is the
55 function to call when one wants the program to die because of a 44 function to call when one wants the program to die because of a
56 memory allocation failure. */ 45 memory allocation failure. */
57 extern void xalloc_die PARAMS ((void)) ATTRIBUTE_NORETURN; 46 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
58 47
59 void *xmalloc PARAMS ((size_t n)); 48 void *xmalloc (size_t s);
60 void *xcalloc PARAMS ((size_t n, size_t s)); 49 void *xnmalloc (size_t n, size_t s);
61 void *xrealloc PARAMS ((void *p, size_t n)); 50 void *xzalloc (size_t s);
62 char *xstrdup PARAMS ((const char *str)); 51 void *xcalloc (size_t n, size_t s);
63 52 void *xrealloc (void *p, size_t s);
64 # define XMALLOC(Type, N_items) ((Type *) xmalloc (sizeof (Type) * (N_items))) 53 void *xnrealloc (void *p, size_t n, size_t s);
65 # define XCALLOC(Type, N_items) ((Type *) xcalloc (sizeof (Type), (N_items))) 54 void *x2realloc (void *p, size_t *pn);
66 # define XREALLOC(Ptr, Type, N_items) \ 55 void *x2nrealloc (void *p, size_t *pn, size_t s);
67 ((Type *) xrealloc ((void *) (Ptr), sizeof (Type) * (N_items))) 56 void *xmemdup (void const *p, size_t s);
68 57 char *xstrdup (char const *str);
69 /* Declare and alloc memory for VAR of type TYPE. */ 58
70 # define NEW(Type, Var) Type *(Var) = XMALLOC (Type, 1) 59 /* Return 1 if an array of N objects, each of size S, cannot exist due
71 60 to size arithmetic overflow. S must be positive and N must be
72 /* Free VAR only if non NULL. */ 61 nonnegative. This is a macro, not an inline function, so that it
73 # define XFREE(Var) \ 62 works correctly even when SIZE_MAX < N.
74 do { \ 63
75 if (Var) \ 64 By gnulib convention, SIZE_MAX represents overflow in size
76 free (Var); \ 65 calculations, so the conservative dividend to use here is
77 } while (0) 66 SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value.
78 67 However, malloc (SIZE_MAX) fails on all known hosts where
79 /* Return a pointer to a malloc'ed copy of the array SRC of NUM elements. */ 68 sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for
80 # define CCLONE(Src, Num) \ 69 exactly-SIZE_MAX allocations on such hosts; this avoids a test and
81 (memcpy (xmalloc (sizeof (*Src) * (Num)), (Src), sizeof (*Src) * (Num))) 70 branch when S is known to be 1. */
82 71 # define xalloc_oversized(n, s) \
83 /* Return a malloc'ed copy of SRC. */ 72 ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
84 # define CLONE(Src) CCLONE (Src, 1) 73
74 # ifdef __cplusplus
75 }
76 # endif
85 77
86 78
87 #endif /* !XALLOC_H_ */ 79 #endif /* !XALLOC_H_ */
......
1 /* xmalloc.c -- malloc with out of memory checking 1 /* xmalloc.c -- malloc with out of memory checking
2 Copyright (C) 1990-1997, 98, 99 Free Software Foundation, Inc. 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
3 5
4 This program is free software; you can redistribute it and/or modify 6 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 7 it under the terms of the GNU General Public License as published by
...@@ -19,64 +21,31 @@ ...@@ -19,64 +21,31 @@
19 # include <config.h> 21 # include <config.h>
20 #endif 22 #endif
21 23
22 #include <sys/types.h>
23
24 #include <mailutils/error.h>
25
26 #if STDC_HEADERS
27 # include <stdlib.h>
28 #else
29 void *calloc ();
30 void *malloc ();
31 void *realloc ();
32 void free ();
33 #endif
34
35 #if ENABLE_NLS
36 # include <libintl.h>
37 # define _(Text) gettext (Text)
38 #else
39 # define textdomain(Domain)
40 # define _(Text) Text
41 #endif
42 #define N_(Text) Text
43
44 #include "error.h"
45 #include "xalloc.h" 24 #include "xalloc.h"
46 25
47 #ifndef EXIT_FAILURE 26 #include <stdlib.h>
48 # define EXIT_FAILURE 1 27 #include <string.h>
49 #endif
50
51 #ifndef HAVE_MALLOC
52 # error "you must run the autoconf test for a properly working malloc -- see malloc.m4"
53 #endif
54 28
55 #ifndef HAVE_REALLOC 29 #ifndef SIZE_MAX
56 # error "you must run the autoconf test for a properly working realloc -- see realloc.m4" 30 # define SIZE_MAX ((size_t) -1)
57 #endif 31 #endif
58 32
59 /* Exit value when the requested amount of memory is not available. 33 /* Allocate an array of N objects, each with S bytes of memory,
60 The caller may set it to some other value. */ 34 dynamically, with error checking. S must be nonzero. */
61 int xalloc_exit_failure = EXIT_FAILURE;
62
63 /* If non NULL, call this function when memory is exhausted. */
64 void (*xalloc_fail_func) PARAMS ((void)) = 0;
65 35
66 /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message 36 static inline void *
67 before exiting when memory is exhausted. Goes through gettext. */ 37 xnmalloc_inline (size_t n, size_t s)
68 char *const xalloc_msg_memory_exhausted = N_("Memory exhausted"); 38 {
39 void *p;
40 if (xalloc_oversized (n, s) || (! (p = malloc (n * s)) && n != 0))
41 xalloc_die ();
42 return p;
43 }
69 44
70 void 45 void *
71 xalloc_die (void) 46 xnmalloc (size_t n, size_t s)
72 { 47 {
73 if (xalloc_fail_func) 48 return xnmalloc_inline (n, s);
74 (*xalloc_fail_func) ();
75 mu_error ("%s", _(xalloc_msg_memory_exhausted));
76 /* The `noreturn' cannot be given to error, since it may return if
77 its first argument is 0. To help compilers understand the
78 xalloc_die does terminate, call exit. */
79 exit (xalloc_exit_failure);
80 } 49 }
81 50
82 /* Allocate N bytes of memory dynamically, with error checking. */ 51 /* Allocate N bytes of memory dynamically, with error checking. */
...@@ -84,36 +53,177 @@ xalloc_die (void) ...@@ -84,36 +53,177 @@ xalloc_die (void)
84 void * 53 void *
85 xmalloc (size_t n) 54 xmalloc (size_t n)
86 { 55 {
87 void *p; 56 return xnmalloc_inline (n, 1);
57 }
58
59 /* Change the size of an allocated block of memory P to an array of N
60 objects each of S bytes, with error checking. S must be nonzero. */
88 61
89 p = malloc (n); 62 static inline void *
90 if (p == 0) 63 xnrealloc_inline (void *p, size_t n, size_t s)
64 {
65 if (xalloc_oversized (n, s) || (! (p = realloc (p, n * s)) && n != 0))
91 xalloc_die (); 66 xalloc_die ();
92 return p; 67 return p;
93 } 68 }
94 69
70 void *
71 xnrealloc (void *p, size_t n, size_t s)
72 {
73 return xnrealloc_inline (p, n, s);
74 }
75
95 /* Change the size of an allocated block of memory P to N bytes, 76 /* Change the size of an allocated block of memory P to N bytes,
96 with error checking. 77 with error checking. */
97 If P is NULL, run xmalloc. */
98 78
99 void * 79 void *
100 xrealloc (void *p, size_t n) 80 xrealloc (void *p, size_t n)
101 { 81 {
102 p = realloc (p, n); 82 return xnrealloc_inline (p, n, 1);
103 if (p == 0)
104 xalloc_die ();
105 return p;
106 } 83 }
107 84
108 /* Allocate memory for N elements of S bytes, with error checking. */ 85
86 /* If P is null, allocate a block of at least *PN such objects;
87 otherwise, reallocate P so that it contains more than *PN objects
88 each of S bytes. *PN must be nonzero unless P is null, and S must
89 be nonzero. Set *PN to the new number of objects, and return the
90 pointer to the new block. *PN is never set to zero, and the
91 returned pointer is never null.
92
93 Repeated reallocations are guaranteed to make progress, either by
94 allocating an initial block with a nonzero size, or by allocating a
95 larger block.
96
97 In the following implementation, nonzero sizes are doubled so that
98 repeated reallocations have O(N log N) overall cost rather than
99 O(N**2) cost, but the specification for this function does not
100 guarantee that sizes are doubled.
101
102 Here is an example of use:
103
104 int *p = NULL;
105 size_t used = 0;
106 size_t allocated = 0;
107
108 void
109 append_int (int value)
110 {
111 if (used == allocated)
112 p = x2nrealloc (p, &allocated, sizeof *p);
113 p[used++] = value;
114 }
115
116 This causes x2nrealloc to allocate a block of some nonzero size the
117 first time it is called.
118
119 To have finer-grained control over the initial size, set *PN to a
120 nonzero value before calling this function with P == NULL. For
121 example:
122
123 int *p = NULL;
124 size_t used = 0;
125 size_t allocated = 0;
126 size_t allocated1 = 1000;
127
128 void
129 append_int (int value)
130 {
131 if (used == allocated)
132 {
133 p = x2nrealloc (p, &allocated1, sizeof *p);
134 allocated = allocated1;
135 }
136 p[used++] = value;
137 }
138
139 */
140
141 static inline void *
142 x2nrealloc_inline (void *p, size_t *pn, size_t s)
143 {
144 size_t n = *pn;
145
146 if (! p)
147 {
148 if (! n)
149 {
150 /* The approximate size to use for initial small allocation
151 requests, when the invoking code specifies an old size of
152 zero. 64 bytes is the largest "small" request for the
153 GNU C library malloc. */
154 enum { DEFAULT_MXFAST = 64 };
155
156 n = DEFAULT_MXFAST / s;
157 n += !n;
158 }
159 }
160 else
161 {
162 if (SIZE_MAX / 2 / s < n)
163 xalloc_die ();
164 n *= 2;
165 }
166
167 *pn = n;
168 return xrealloc (p, n * s);
169 }
170
171 void *
172 x2nrealloc (void *p, size_t *pn, size_t s)
173 {
174 return x2nrealloc_inline (p, pn, s);
175 }
176
177 /* If P is null, allocate a block of at least *PN bytes; otherwise,
178 reallocate P so that it contains more than *PN bytes. *PN must be
179 nonzero unless P is null. Set *PN to the new block's size, and
180 return the pointer to the new block. *PN is never set to zero, and
181 the returned pointer is never null. */
182
183 void *
184 x2realloc (void *p, size_t *pn)
185 {
186 return x2nrealloc_inline (p, pn, 1);
187 }
188
189 /* Allocate S bytes of zeroed memory dynamically, with error checking.
190 There's no need for xnzalloc (N, S), since it would be equivalent
191 to xcalloc (N, S). */
192
193 void *
194 xzalloc (size_t s)
195 {
196 return memset (xmalloc (s), 0, s);
197 }
198
199 /* Allocate zeroed memory for N elements of S bytes, with error
200 checking. S must be nonzero. */
109 201
110 void * 202 void *
111 xcalloc (size_t n, size_t s) 203 xcalloc (size_t n, size_t s)
112 { 204 {
113 void *p; 205 void *p;
114 206 /* Test for overflow, since some calloc implementations don't have
115 p = calloc (n, s); 207 proper overflow checks. */
116 if (p == 0) 208 if (xalloc_oversized (n, s) || (! (p = calloc (n, s)) && n != 0))
117 xalloc_die (); 209 xalloc_die ();
118 return p; 210 return p;
119 } 211 }
212
213 /* Clone an object P of size S, with error checking. There's no need
214 for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
215 need for an arithmetic overflow check. */
216
217 void *
218 xmemdup (void const *p, size_t s)
219 {
220 return memcpy (xmalloc (s), p, s);
221 }
222
223 /* Clone STRING. */
224
225 char *
226 xstrdup (char const *string)
227 {
228 return xmemdup (string, strlen (string) + 1);
229 }
......
1 /* A more useful interface to strtol. 1 /* A more useful interface to strtol.
2 Copyright 1995, 1996, 1998, 1999 Free Software Foundation, Inc. 2
3 Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004 Free
4 Software Foundation, Inc.
3 5
4 This program is free software; you can redistribute it and/or modify 6 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 7 it under the terms of the GNU General Public License as published by
...@@ -25,48 +27,32 @@ ...@@ -25,48 +27,32 @@
25 # define __strtol strtol 27 # define __strtol strtol
26 # define __strtol_t long int 28 # define __strtol_t long int
27 # define __xstrtol xstrtol 29 # define __xstrtol xstrtol
30 # define STRTOL_T_MINIMUM LONG_MIN
31 # define STRTOL_T_MAXIMUM LONG_MAX
28 #endif 32 #endif
29 33
30 /* Some pre-ANSI implementations (e.g. SunOS 4) 34 /* Some pre-ANSI implementations (e.g. SunOS 4)
31 need stderr defined if assertion checking is enabled. */ 35 need stderr defined if assertion checking is enabled. */
32 #include <stdio.h> 36 #include <stdio.h>
33 37
34 #if STDC_HEADERS
35 # include <stdlib.h>
36 #endif
37
38 #if HAVE_STRING_H
39 # include <string.h>
40 #else
41 # include <strings.h>
42 # ifndef strchr
43 # define strchr index
44 # endif
45 #endif
46
47 #include <assert.h> 38 #include <assert.h>
48 #include <ctype.h> 39 #include <ctype.h>
49
50 #include <errno.h> 40 #include <errno.h>
51 #ifndef errno 41 #include <limits.h>
52 extern int errno; 42 #include <stdlib.h>
53 #endif 43 #include <string.h>
54
55 #if HAVE_LIMITS_H
56 # include <limits.h>
57 #endif
58
59 #ifndef CHAR_BIT
60 # define CHAR_BIT 8
61 #endif
62 44
63 /* The extra casts work around common compiler bugs. */ 45 /* The extra casts work around common compiler bugs. */
64 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1)) 46 #define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
65 /* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
66 It is necessary at least when t == time_t. */
67 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \ 47 #define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
68 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0)) 48 ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) \
69 #define TYPE_MAXIMUM(t) (~ (t) 0 - TYPE_MINIMUM (t)) 49 : (t) 0))
50 #define TYPE_MAXIMUM(t) ((t) (~ (t) 0 - TYPE_MINIMUM (t)))
51
52 #ifndef STRTOL_T_MINIMUM
53 # define STRTOL_T_MINIMUM TYPE_MINIMUM (__strtol_t)
54 # define STRTOL_T_MAXIMUM TYPE_MAXIMUM (__strtol_t)
55 #endif
70 56
71 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII)) 57 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
72 # define IN_CTYPE_DOMAIN(c) 1 58 # define IN_CTYPE_DOMAIN(c) 1
...@@ -78,36 +64,38 @@ extern int errno; ...@@ -78,36 +64,38 @@ extern int errno;
78 64
79 #include "xstrtol.h" 65 #include "xstrtol.h"
80 66
81 #ifndef strtol 67 #if !HAVE_DECL_STRTOIMAX && !defined strtoimax
82 long int strtol (); 68 intmax_t strtoimax ();
83 #endif 69 #endif
84 70
85 #ifndef strtoul 71 #if !HAVE_DECL_STRTOUMAX && !defined strtoumax
86 unsigned long int strtoul ();
87 #endif
88
89 #ifndef strtoumax
90 uintmax_t strtoumax (); 72 uintmax_t strtoumax ();
91 #endif 73 #endif
92 74
93 static int 75 static strtol_error
94 bkm_scale (__strtol_t *x, int scale_factor) 76 bkm_scale (__strtol_t *x, int scale_factor)
95 { 77 {
96 __strtol_t product = *x * scale_factor; 78 if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
97 if (*x != product / scale_factor) 79 {
98 return 1; 80 *x = STRTOL_T_MINIMUM;
99 *x = product; 81 return LONGINT_OVERFLOW;
100 return 0; 82 }
83 if (STRTOL_T_MAXIMUM / scale_factor < *x)
84 {
85 *x = STRTOL_T_MAXIMUM;
86 return LONGINT_OVERFLOW;
87 }
88 *x *= scale_factor;
89 return LONGINT_OK;
101 } 90 }
102 91
103 static int 92 static strtol_error
104 bkm_scale_by_power (__strtol_t *x, int base, int power) 93 bkm_scale_by_power (__strtol_t *x, int base, int power)
105 { 94 {
95 strtol_error err = LONGINT_OK;
106 while (power--) 96 while (power--)
107 if (bkm_scale (x, base)) 97 err |= bkm_scale (x, base);
108 return 1; 98 return err;
109
110 return 0;
111 } 99 }
112 100
113 /* FIXME: comment. */ 101 /* FIXME: comment. */
...@@ -119,6 +107,7 @@ __xstrtol (const char *s, char **ptr, int strtol_base, ...@@ -119,6 +107,7 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
119 char *t_ptr; 107 char *t_ptr;
120 char **p; 108 char **p;
121 __strtol_t tmp; 109 __strtol_t tmp;
110 strtol_error err = LONGINT_OK;
122 111
123 assert (0 <= strtol_base && strtol_base <= 36); 112 assert (0 <= strtol_base && strtol_base <= 36);
124 113
...@@ -127,18 +116,31 @@ __xstrtol (const char *s, char **ptr, int strtol_base, ...@@ -127,18 +116,31 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
127 if (! TYPE_SIGNED (__strtol_t)) 116 if (! TYPE_SIGNED (__strtol_t))
128 { 117 {
129 const char *q = s; 118 const char *q = s;
130 while (ISSPACE ((unsigned char) *q)) 119 unsigned char ch = *q;
131 ++q; 120 while (ISSPACE (ch))
132 if (*q == '-') 121 ch = *++q;
122 if (ch == '-')
133 return LONGINT_INVALID; 123 return LONGINT_INVALID;
134 } 124 }
135 125
136 errno = 0; 126 errno = 0;
137 tmp = __strtol (s, p, strtol_base); 127 tmp = __strtol (s, p, strtol_base);
138 if (errno != 0) 128
139 return LONGINT_OVERFLOW;
140 if (*p == s) 129 if (*p == s)
141 return LONGINT_INVALID; 130 {
131 /* If there is no number but there is a valid suffix, assume the
132 number is 1. The string is invalid otherwise. */
133 if (valid_suffixes && **p && strchr (valid_suffixes, **p))
134 tmp = 1;
135 else
136 return LONGINT_INVALID;
137 }
138 else if (errno != 0)
139 {
140 if (errno != ERANGE)
141 return LONGINT_INVALID;
142 err = LONGINT_OVERFLOW;
143 }
142 144
143 /* Let valid_suffixes == NULL mean `allow any suffix'. */ 145 /* Let valid_suffixes == NULL mean `allow any suffix'. */
144 /* FIXME: update all callers except the ones that allow suffixes 146 /* FIXME: update all callers except the ones that allow suffixes
...@@ -146,34 +148,39 @@ __xstrtol (const char *s, char **ptr, int strtol_base, ...@@ -146,34 +148,39 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
146 if (!valid_suffixes) 148 if (!valid_suffixes)
147 { 149 {
148 *val = tmp; 150 *val = tmp;
149 return LONGINT_OK; 151 return err;
150 } 152 }
151 153
152 if (**p != '\0') 154 if (**p != '\0')
153 { 155 {
154 int base = 1024; 156 int base = 1024;
155 int suffixes = 1; 157 int suffixes = 1;
156 int overflow; 158 strtol_error overflow;
157 159
158 if (!strchr (valid_suffixes, **p)) 160 if (!strchr (valid_suffixes, **p))
159 { 161 {
160 *val = tmp; 162 *val = tmp;
161 return LONGINT_INVALID_SUFFIX_CHAR; 163 return err | LONGINT_INVALID_SUFFIX_CHAR;
162 } 164 }
163 165
164 if (strchr (valid_suffixes, '0')) 166 if (strchr (valid_suffixes, '0'))
165 { 167 {
166 /* The ``valid suffix'' '0' is a special flag meaning that 168 /* The ``valid suffix'' '0' is a special flag meaning that
167 an optional second suffix is allowed, which can change 169 an optional second suffix is allowed, which can change
168 the base, e.g. "100MD" for 100 megabytes decimal. */ 170 the base. A suffix "B" (e.g. "100MB") stands for a power
171 of 1000, whereas a suffix "iB" (e.g. "100MiB") stands for
172 a power of 1024. If no suffix (e.g. "100M"), assume
173 power-of-1024. */
169 174
170 switch (p[0][1]) 175 switch (p[0][1])
171 { 176 {
172 case 'B': 177 case 'i':
173 suffixes++; 178 if (p[0][2] == 'B')
179 suffixes += 2;
174 break; 180 break;
175 181
176 case 'D': 182 case 'B':
183 case 'D': /* 'D' is obsolescent */
177 base = 1000; 184 base = 1000;
178 suffixes++; 185 suffixes++;
179 break; 186 break;
...@@ -194,28 +201,31 @@ __xstrtol (const char *s, char **ptr, int strtol_base, ...@@ -194,28 +201,31 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
194 overflow = 0; 201 overflow = 0;
195 break; 202 break;
196 203
197 case 'E': /* Exa */ 204 case 'E': /* exa or exbi */
198 overflow = bkm_scale_by_power (&tmp, base, 6); 205 overflow = bkm_scale_by_power (&tmp, base, 6);
199 break; 206 break;
200 207
201 case 'G': /* Giga */ 208 case 'G': /* giga or gibi */
209 case 'g': /* 'g' is undocumented; for compatibility only */
202 overflow = bkm_scale_by_power (&tmp, base, 3); 210 overflow = bkm_scale_by_power (&tmp, base, 3);
203 break; 211 break;
204 212
205 case 'k': /* kilo */ 213 case 'k': /* kilo */
214 case 'K': /* kibi */
206 overflow = bkm_scale_by_power (&tmp, base, 1); 215 overflow = bkm_scale_by_power (&tmp, base, 1);
207 break; 216 break;
208 217
209 case 'M': /* Mega */ 218 case 'M': /* mega or mebi */
210 case 'm': /* 'm' is undocumented; for backward compatibility only */ 219 case 'm': /* 'm' is undocumented; for compatibility only */
211 overflow = bkm_scale_by_power (&tmp, base, 2); 220 overflow = bkm_scale_by_power (&tmp, base, 2);
212 break; 221 break;
213 222
214 case 'P': /* Peta */ 223 case 'P': /* peta or pebi */
215 overflow = bkm_scale_by_power (&tmp, base, 5); 224 overflow = bkm_scale_by_power (&tmp, base, 5);
216 break; 225 break;
217 226
218 case 'T': /* Tera */ 227 case 'T': /* tera or tebi */
228 case 't': /* 't' is undocumented; for compatibility only */
219 overflow = bkm_scale_by_power (&tmp, base, 4); 229 overflow = bkm_scale_by_power (&tmp, base, 4);
220 break; 230 break;
221 231
...@@ -223,28 +233,27 @@ __xstrtol (const char *s, char **ptr, int strtol_base, ...@@ -223,28 +233,27 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
223 overflow = bkm_scale (&tmp, 2); 233 overflow = bkm_scale (&tmp, 2);
224 break; 234 break;
225 235
226 case 'Y': /* Yotta */ 236 case 'Y': /* yotta or 2**80 */
227 overflow = bkm_scale_by_power (&tmp, base, 8); 237 overflow = bkm_scale_by_power (&tmp, base, 8);
228 break; 238 break;
229 239
230 case 'Z': /* Zetta */ 240 case 'Z': /* zetta or 2**70 */
231 overflow = bkm_scale_by_power (&tmp, base, 7); 241 overflow = bkm_scale_by_power (&tmp, base, 7);
232 break; 242 break;
233 243
234 default: 244 default:
235 *val = tmp; 245 *val = tmp;
236 return LONGINT_INVALID_SUFFIX_CHAR; 246 return err | LONGINT_INVALID_SUFFIX_CHAR;
237 break;
238 } 247 }
239 248
240 if (overflow) 249 err |= overflow;
241 return LONGINT_OVERFLOW; 250 *p += suffixes;
242 251 if (**p)
243 (*p) += suffixes; 252 err |= LONGINT_INVALID_SUFFIX_CHAR;
244 } 253 }
245 254
246 *val = tmp; 255 *val = tmp;
247 return LONGINT_OK; 256 return err;
248 } 257 }
249 258
250 #ifdef TESTING_XSTRTO 259 #ifdef TESTING_XSTRTO
...@@ -255,7 +264,7 @@ __xstrtol (const char *s, char **ptr, int strtol_base, ...@@ -255,7 +264,7 @@ __xstrtol (const char *s, char **ptr, int strtol_base,
255 char *program_name; 264 char *program_name;
256 265
257 int 266 int
258 main (int argc, char** argv) 267 main (int argc, char **argv)
259 { 268 {
260 strtol_error s_err; 269 strtol_error s_err;
261 int i; 270 int i;
......
1 /* A more useful interface to strtol.
2
3 Copyright (C) 1995, 1996, 1998, 1999, 2001, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
1 #ifndef XSTRTOL_H_ 20 #ifndef XSTRTOL_H_
2 # define XSTRTOL_H_ 1 21 # define XSTRTOL_H_ 1
3 22
23 # include "exitfail.h"
24
4 # if HAVE_INTTYPES_H 25 # if HAVE_INTTYPES_H
5 # include <inttypes.h> /* for uintmax_t */ 26 # include <inttypes.h>
6 # endif 27 # endif
7 28 # if HAVE_STDINT_H
8 # ifndef PARAMS 29 # include <stdint.h>
9 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
10 # define PARAMS(Args) Args
11 # else
12 # define PARAMS(Args) ()
13 # endif
14 # endif 30 # endif
15 31
16 # ifndef _STRTOL_ERROR 32 # ifndef _STRTOL_ERROR
17 enum strtol_error 33 enum strtol_error
18 { 34 {
19 LONGINT_OK, LONGINT_INVALID, LONGINT_INVALID_SUFFIX_CHAR, LONGINT_OVERFLOW 35 LONGINT_OK = 0,
36
37 /* These two values can be ORed together, to indicate that both
38 errors occurred. */
39 LONGINT_OVERFLOW = 1,
40 LONGINT_INVALID_SUFFIX_CHAR = 2,
41
42 LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW = (LONGINT_INVALID_SUFFIX_CHAR
43 | LONGINT_OVERFLOW),
44 LONGINT_INVALID = 4
20 }; 45 };
21 typedef enum strtol_error strtol_error; 46 typedef enum strtol_error strtol_error;
22 # endif 47 # endif
23 48
24 # define _DECLARE_XSTRTOL(name, type) \ 49 # define _DECLARE_XSTRTOL(name, type) \
25 strtol_error \ 50 strtol_error name (const char *, char **, int, type *, const char *);
26 name PARAMS ((const char *s, char **ptr, int base, \
27 type *val, const char *valid_suffixes));
28 _DECLARE_XSTRTOL (xstrtol, long int) 51 _DECLARE_XSTRTOL (xstrtol, long int)
29 _DECLARE_XSTRTOL (xstrtoul, unsigned long int) 52 _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
53 _DECLARE_XSTRTOL (xstrtoimax, intmax_t)
30 _DECLARE_XSTRTOL (xstrtoumax, uintmax_t) 54 _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
31 55
32 # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err) \ 56 # define _STRTOL_ERROR(Exit_code, Str, Argument_type_string, Err) \
...@@ -34,7 +58,7 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t) ...@@ -34,7 +58,7 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
34 { \ 58 { \
35 switch ((Err)) \ 59 switch ((Err)) \
36 { \ 60 { \
37 case LONGINT_OK: \ 61 default: \
38 abort (); \ 62 abort (); \
39 \ 63 \
40 case LONGINT_INVALID: \ 64 case LONGINT_INVALID: \
...@@ -43,7 +67,8 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t) ...@@ -43,7 +67,8 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
43 break; \ 67 break; \
44 \ 68 \
45 case LONGINT_INVALID_SUFFIX_CHAR: \ 69 case LONGINT_INVALID_SUFFIX_CHAR: \
46 error ((Exit_code), 0, "invalid character following %s `%s'", \ 70 case LONGINT_INVALID_SUFFIX_CHAR | LONGINT_OVERFLOW: \
71 error ((Exit_code), 0, "invalid character following %s in `%s'", \
47 (Argument_type_string), (Str)); \ 72 (Argument_type_string), (Str)); \
48 break; \ 73 break; \
49 \ 74 \
...@@ -56,7 +81,7 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t) ...@@ -56,7 +81,7 @@ _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
56 while (0) 81 while (0)
57 82
58 # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err) \ 83 # define STRTOL_FATAL_ERROR(Str, Argument_type_string, Err) \
59 _STRTOL_ERROR (2, Str, Argument_type_string, Err) 84 _STRTOL_ERROR (exit_failure, Str, Argument_type_string, Err)
60 85
61 # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err) \ 86 # define STRTOL_FAIL_WARN(Str, Argument_type_string, Err) \
62 _STRTOL_ERROR (0, Str, Argument_type_string, Err) 87 _STRTOL_ERROR (0, Str, Argument_type_string, Err)
......
1 ## $Id$ 1 # getopt.m4 serial 7
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.
2 6
3 ## Check for getopt_long. This can't be done in AC_CHECK_FUNCS since 7 # The getopt module assume you want GNU getopt, with getopt_long etc,
4 ## the function can be present in different libraries (namely, libmysqlclient) 8 # rather than vanilla POSIX getopt. This means your your code should
5 ## but the necessary header files may be absent, thus AC_CHECK_FUNCS will 9 # always include <getopt.h> for the getopt prototypes.
6 ## mark function as existent, whereas the compilation will bail out.
7 10
8 AH_TEMPLATE(HAVE_GNU_GETOPT, [Define if your system has GNU getopt functions]) 11 AC_DEFUN([gl_GETOPT_SUBSTITUTE],
12 [
13 GETOPT_H=getopt.h
14 MU_LIBOBJ([getopt])
15 MU_LIBOBJ([getopt1])
16 AC_DEFINE([__GETOPT_PREFIX], [[rpl_]],
17 [Define to rpl_ if the getopt replacement functions and variables
18 should be used.])
19 AC_SUBST([GETOPT_H])
20 ])
9 21
10 AC_DEFUN([MU_REPLACE_GNU_GETOPT], 22 AC_DEFUN([gl_GETOPT],
11 [ 23 [
12 AC_CHECK_HEADER([getopt.h], 24 gl_PREREQ_GETOPT
13 mu_cv_have_getopt_h=yes
14 AC_DEFINE(HAVE_GETOPT_H,1,[Define if the system has getopt.h]),
15 mu_cv_have_getopt_h=no)
16 AC_CACHE_CHECK([for GNU getopt], mu_cv_have_gnu_getopt,
17 [
18 AC_TRY_RUN([
19 #include <unistd.h>
20 #ifdef HAVE_GETOPT_H
21 # include <getopt.h>
22 #endif
23 25
24 struct option longopt[] = { 26 if test -z "$GETOPT_H"; then
25 "help", no_argument, 0, 'h', 27 GETOPT_H=
26 (char*)0 28 AC_CHECK_HEADERS([getopt.h], [], [GETOPT_H=getopt.h])
27 }; 29 AC_CHECK_FUNCS([getopt_long_only], [], [GETOPT_H=getopt.h])
28 30
29 main(argc, argv) 31 dnl BSD getopt_long uses an incompatible method to reset option processing,
30 int argc; char **argv; 32 dnl and (as of 2004-10-15) mishandles optional option-arguments.
31 { 33 AC_CHECK_DECL([optreset], [GETOPT_H=getopt.h], [], [#include <getopt.h>])
32 getopt_long_only(argc, argv, "h", longopt, (int*)0);
33 return 0;
34 } ],
35 mu_cv_have_gnu_getopt=yes,
36 mu_cv_have_gnu_getopt=no,
37 mu_cv_have_gnu_getopt=no)])
38 34
39 if test x"$mu_cv_have_gnu_getopt" != xyes ; then 35 if test -n "$GETOPT_H"; then
40 mu_cv_have_getopt_h=no 36 gl_GETOPT_SUBSTITUTE
41 MU_LIBOBJ(getopt) 37 fi
42 MU_LIBOBJ(getopt1) 38 fi
43 else
44 AC_DEFINE(HAVE_GNU_GETOPT)
45 fi
46 if test "$mu_cv_have_getopt_h" = no; then
47 MU_HEADER(getopt.h)
48 fi
49 ]) 39 ])
50 40
51 41 # Prerequisites of lib/getopt*.
52 42 AC_DEFUN([gl_PREREQ_GETOPT], [:])
53
......
1 #serial 12 1 #serial 22
2
3 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004 Free
4 # Software Foundation, Inc.
5 #
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
2 9
3 dnl Initially derived from code in GNU grep. 10 dnl Initially derived from code in GNU grep.
4 dnl Mostly written by Jim Meyering. 11 dnl Mostly written by Jim Meyering.
5 12
6 dnl Usage: jm_INCLUDED_REGEX([lib/regex.c]) 13 AC_DEFUN([gl_REGEX],
14 [
15 gl_INCLUDED_REGEX([lib/regex.c])
16 ])
17
18 dnl Usage: gl_INCLUDED_REGEX([lib/regex.c])
7 dnl 19 dnl
8 AC_DEFUN([jm_INCLUDED_REGEX], 20 AC_DEFUN([gl_INCLUDED_REGEX],
9 [ 21 [
10 dnl Even packages that don't use regex.c can use this macro. 22 dnl Even packages that don't use regex.c can use this macro.
11 dnl Of course, for them it doesn't do anything. 23 dnl Of course, for them it doesn't do anything.
...@@ -22,6 +34,7 @@ AC_DEFUN([jm_INCLUDED_REGEX], ...@@ -22,6 +34,7 @@ AC_DEFUN([jm_INCLUDED_REGEX],
22 jm_cv_func_working_re_compile_pattern, 34 jm_cv_func_working_re_compile_pattern,
23 AC_TRY_RUN( 35 AC_TRY_RUN(
24 [#include <stdio.h> 36 [#include <stdio.h>
37 #include <string.h>
25 #include <regex.h> 38 #include <regex.h>
26 int 39 int
27 main () 40 main ()
...@@ -30,12 +43,14 @@ AC_DEFUN([jm_INCLUDED_REGEX], ...@@ -30,12 +43,14 @@ AC_DEFUN([jm_INCLUDED_REGEX],
30 const char *s; 43 const char *s;
31 struct re_registers regs; 44 struct re_registers regs;
32 re_set_syntax (RE_SYNTAX_POSIX_EGREP); 45 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
46 memset (&regex, 0, sizeof (regex));
33 [s = re_compile_pattern ("a[[:@:>@:]]b\n", 9, &regex);] 47 [s = re_compile_pattern ("a[[:@:>@:]]b\n", 9, &regex);]
34 /* This should fail with _Invalid character class name_ error. */ 48 /* This should fail with _Invalid character class name_ error. */
35 if (!s) 49 if (!s)
36 exit (1); 50 exit (1);
37 51
38 /* This should succeed, but doesn't for e.g. glibc-2.1.3. */ 52 /* This should succeed, but doesn't for e.g. glibc-2.1.3. */
53 memset (&regex, 0, sizeof (regex));
39 s = re_compile_pattern ("{1", 2, &regex); 54 s = re_compile_pattern ("{1", 2, &regex);
40 55
41 if (s) 56 if (s)
...@@ -43,7 +58,8 @@ AC_DEFUN([jm_INCLUDED_REGEX], ...@@ -43,7 +58,8 @@ AC_DEFUN([jm_INCLUDED_REGEX],
43 58
44 /* The following example is derived from a problem report 59 /* The following example is derived from a problem report
45 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */ 60 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
46 s = re_compile_pattern ("[[anù]]*n", 7, &regex); 61 memset (&regex, 0, sizeof (regex));
62 s = re_compile_pattern ("[[an\371]]*n", 7, &regex);
47 if (s) 63 if (s)
48 exit (1); 64 exit (1);
49 65
...@@ -51,6 +67,16 @@ AC_DEFUN([jm_INCLUDED_REGEX], ...@@ -51,6 +67,16 @@ AC_DEFUN([jm_INCLUDED_REGEX],
51 if (re_match (&regex, "an", 2, 0, &regs) != 2) 67 if (re_match (&regex, "an", 2, 0, &regs) != 2)
52 exit (1); 68 exit (1);
53 69
70 memset (&regex, 0, sizeof (regex));
71 s = re_compile_pattern ("x", 1, &regex);
72 if (s)
73 exit (1);
74
75 /* The version of regex.c in e.g. GNU libc-2.2.93 didn't
76 work with a negative RANGE argument. */
77 if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
78 exit (1);
79
54 exit (0); 80 exit (0);
55 } 81 }
56 ], 82 ],
...@@ -67,16 +93,34 @@ AC_DEFUN([jm_INCLUDED_REGEX], ...@@ -67,16 +93,34 @@ AC_DEFUN([jm_INCLUDED_REGEX],
67 ifelse(m4_sysval, 0, 93 ifelse(m4_sysval, 0,
68 [ 94 [
69 AC_ARG_WITH(included-regex, 95 AC_ARG_WITH(included-regex,
70 AC_HELP_STRING([--without-included-regex], 96 [ --without-included-regex don't compile regex; this is the default on
71 [don't compile regex; this is the default on systems with version 2 of the GNU C library (use with caution on other system)]), 97 systems with version 2 of the GNU C library
98 (use with caution on other system)],
72 jm_with_regex=$withval, 99 jm_with_regex=$withval,
73 jm_with_regex=$ac_use_included_regex) 100 jm_with_regex=$ac_use_included_regex)
74 if test "$jm_with_regex" = yes; then 101 if test "$jm_with_regex" = yes; then
75 MU_LIBOBJ(regex) 102 MU_LIBOBJ(regex)
76 MU_HEADER(regex.h) 103 gl_PREREQ_REGEX
77 MU_HEADER(posix/regex.h)
78 fi 104 fi
79 ], 105 ],
80 ) 106 )
81 ] 107 ]
82 ) 108 )
109
110 # Prerequisites of lib/regex.c.
111 AC_DEFUN([gl_PREREQ_REGEX],
112 [
113 dnl FIXME: Maybe provide a btowc replacement someday: Solaris 2.5.1 lacks it.
114 dnl FIXME: Check for wctype and iswctype, and and add -lw if necessary
115 dnl to get them.
116
117 dnl Persuade glibc <string.h> to declare mempcpy().
118 AC_REQUIRE([AC_GNU_SOURCE])
119
120 AC_REQUIRE([gl_C_RESTRICT])
121 AC_REQUIRE([AC_FUNC_ALLOCA])
122 AC_REQUIRE([AC_HEADER_STDC])
123 AC_CHECK_HEADERS_ONCE(wchar.h wctype.h)
124 AC_CHECK_FUNCS_ONCE(isascii mempcpy)
125 AC_CHECK_FUNCS(btowc)
126 ])
......
...@@ -3,20 +3,19 @@ ...@@ -3,20 +3,19 @@
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 /* If set by the user program, it should point to string that is the 20 /* If set by the user program, it should point to string that is the
22 bug-reporting address for the program. It will be printed by argp_help if 21 bug-reporting address for the program. It will be printed by argp_help if
......
1 /* Default definition for ARGP_ERR_EXIT_STATUS 1 /* Default definition for ARGP_ERR_EXIT_STATUS
2 Copyright (C) 1997, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
22 #include <config.h> 21 #include <config.h>
23 #endif 22 #endif
24 23
25 #ifdef HAVE_SYSEXITS_H
26 #include <sysexits.h> 24 #include <sysexits.h>
27 #endif
28
29 #ifndef EX_USAGE
30 #define EX_USAGE 64
31 #endif
32 25
33 #include "argp.h" 26 #include "argp.h"
34 27
......
1 /* Word-wrapping and line-truncating streams 1 /* Word-wrapping and line-truncating streams
2 Copyright (C) 1997, 1998, 1999, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997,1998,1999,2001,2002,2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 /* This package emulates glibc `line_wrap_stream' semantics for systems that 20 /* This package emulates glibc `line_wrap_stream' semantics for systems that
22 don't have that. */ 21 don't have that. */
...@@ -40,6 +39,12 @@ ...@@ -40,6 +39,12 @@
40 #define isblank(ch) ((ch)==' ' || (ch)=='\t') 39 #define isblank(ch) ((ch)==' ' || (ch)=='\t')
41 #endif 40 #endif
42 41
42 #if defined _LIBC && defined USE_IN_LIBIO
43 # include <wchar.h>
44 # include <libio/libioP.h>
45 # define __vsnprintf(s, l, f, a) _IO_vsnprintf (s, l, f, a)
46 #endif
47
43 #define INIT_BUF_SIZE 200 48 #define INIT_BUF_SIZE 200
44 #define PRINTF_SIZE_GUESS 150 49 #define PRINTF_SIZE_GUESS 150
45 50
...@@ -53,8 +58,10 @@ argp_fmtstream_t ...@@ -53,8 +58,10 @@ argp_fmtstream_t
53 __argp_make_fmtstream (FILE *stream, 58 __argp_make_fmtstream (FILE *stream,
54 size_t lmargin, size_t rmargin, ssize_t wmargin) 59 size_t lmargin, size_t rmargin, ssize_t wmargin)
55 { 60 {
56 argp_fmtstream_t fs = malloc (sizeof (struct argp_fmtstream)); 61 argp_fmtstream_t fs;
57 if (fs) 62
63 fs = (struct argp_fmtstream *) malloc (sizeof (struct argp_fmtstream));
64 if (fs != NULL)
58 { 65 {
59 fs->stream = stream; 66 fs->stream = stream;
60 67
...@@ -64,7 +71,7 @@ __argp_make_fmtstream (FILE *stream, ...@@ -64,7 +71,7 @@ __argp_make_fmtstream (FILE *stream,
64 fs->point_col = 0; 71 fs->point_col = 0;
65 fs->point_offs = 0; 72 fs->point_offs = 0;
66 73
67 fs->buf = malloc (INIT_BUF_SIZE); 74 fs->buf = (char *) malloc (INIT_BUF_SIZE);
68 if (! fs->buf) 75 if (! fs->buf)
69 { 76 {
70 free (fs); 77 free (fs);
...@@ -79,9 +86,12 @@ __argp_make_fmtstream (FILE *stream, ...@@ -79,9 +86,12 @@ __argp_make_fmtstream (FILE *stream,
79 86
80 return fs; 87 return fs;
81 } 88 }
89 #if 0
90 /* Not exported. */
82 #ifdef weak_alias 91 #ifdef weak_alias
83 weak_alias (__argp_make_fmtstream, argp_make_fmtstream) 92 weak_alias (__argp_make_fmtstream, argp_make_fmtstream)
84 #endif 93 #endif
94 #endif
85 95
86 /* Flush FS to its stream, and free it (but don't close the stream). */ 96 /* Flush FS to its stream, and free it (but don't close the stream). */
87 void 97 void
...@@ -89,13 +99,23 @@ __argp_fmtstream_free (argp_fmtstream_t fs) ...@@ -89,13 +99,23 @@ __argp_fmtstream_free (argp_fmtstream_t fs)
89 { 99 {
90 __argp_fmtstream_update (fs); 100 __argp_fmtstream_update (fs);
91 if (fs->p > fs->buf) 101 if (fs->p > fs->buf)
92 fwrite (fs->buf, 1, fs->p - fs->buf, fs->stream); 102 {
103 #ifdef USE_IN_LIBIO
104 if (_IO_fwide (fs->stream, 0) > 0)
105 __fwprintf (fs->stream, L"%.*s", (int) (fs->p - fs->buf), fs->buf);
106 else
107 #endif
108 fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
109 }
93 free (fs->buf); 110 free (fs->buf);
94 free (fs); 111 free (fs);
95 } 112 }
113 #if 0
114 /* Not exported. */
96 #ifdef weak_alias 115 #ifdef weak_alias
97 weak_alias (__argp_fmtstream_free, argp_fmtstream_free) 116 weak_alias (__argp_fmtstream_free, argp_fmtstream_free)
98 #endif 117 #endif
118 #endif
99 119
100 /* Process FS's buffer so that line wrapping is done from POINT_OFFS to the 120 /* Process FS's buffer so that line wrapping is done from POINT_OFFS to the
101 end of its buffer. This code is mostly from glibc stdio/linewrap.c. */ 121 end of its buffer. This code is mostly from glibc stdio/linewrap.c. */
...@@ -129,7 +149,14 @@ __argp_fmtstream_update (argp_fmtstream_t fs) ...@@ -129,7 +149,14 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
129 /* No buffer space for spaces. Must flush. */ 149 /* No buffer space for spaces. Must flush. */
130 size_t i; 150 size_t i;
131 for (i = 0; i < pad; i++) 151 for (i = 0; i < pad; i++)
132 putc (' ', fs->stream); 152 {
153 #ifdef USE_IN_LIBIO
154 if (_IO_fwide (fs->stream, 0) > 0)
155 putwc_unlocked (L' ', fs->stream);
156 else
157 #endif
158 putc_unlocked (' ', fs->stream);
159 }
133 } 160 }
134 fs->point_col = pad; 161 fs->point_col = pad;
135 } 162 }
...@@ -245,9 +272,10 @@ __argp_fmtstream_update (argp_fmtstream_t fs) ...@@ -245,9 +272,10 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
245 at the end of the buffer, and NEXTLINE is in fact empty (and so 272 at the end of the buffer, and NEXTLINE is in fact empty (and so
246 we need not be careful to maintain its contents). */ 273 we need not be careful to maintain its contents). */
247 274
248 if (nextline == buf + len + 1 275 if ((nextline == buf + len + 1
249 ? fs->end - nl < fs->wmargin + 1 276 ? fs->end - nl < fs->wmargin + 1
250 : nextline - (nl + 1) < fs->wmargin) 277 : nextline - (nl + 1) < fs->wmargin)
278 && fs->p > nextline)
251 { 279 {
252 /* The margin needs more blanks than we removed. */ 280 /* The margin needs more blanks than we removed. */
253 if (fs->end - fs->p > fs->wmargin + 1) 281 if (fs->end - fs->p > fs->wmargin + 1)
...@@ -262,9 +290,17 @@ __argp_fmtstream_update (argp_fmtstream_t fs) ...@@ -262,9 +290,17 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
262 else 290 else
263 /* Output the first line so we can use the space. */ 291 /* Output the first line so we can use the space. */
264 { 292 {
265 if (nl > fs->buf) 293 #ifdef USE_IN_LIBIO
266 fwrite (fs->buf, 1, nl - fs->buf, fs->stream); 294 if (_IO_fwide (fs->stream, 0) > 0)
267 putc ('\n', fs->stream); 295 __fwprintf (fs->stream, L"%.*s\n",
296 (int) (nl - fs->buf), fs->buf);
297 else
298 #endif
299 {
300 if (nl > fs->buf)
301 fwrite_unlocked (fs->buf, 1, nl - fs->buf, fs->stream);
302 putc_unlocked ('\n', fs->stream);
303 }
268 len += buf - fs->buf; 304 len += buf - fs->buf;
269 nl = buf = fs->buf; 305 nl = buf = fs->buf;
270 } 306 }
...@@ -281,7 +317,12 @@ __argp_fmtstream_update (argp_fmtstream_t fs) ...@@ -281,7 +317,12 @@ __argp_fmtstream_update (argp_fmtstream_t fs)
281 *nl++ = ' '; 317 *nl++ = ' ';
282 else 318 else
283 for (i = 0; i < fs->wmargin; ++i) 319 for (i = 0; i < fs->wmargin; ++i)
284 putc (' ', fs->stream); 320 #ifdef USE_IN_LIBIO
321 if (_IO_fwide (fs->stream, 0) > 0)
322 putwc_unlocked (L' ', fs->stream);
323 else
324 #endif
325 putc_unlocked (' ', fs->stream);
285 326
286 /* Copy the tail of the original buffer into the current buffer 327 /* Copy the tail of the original buffer into the current buffer
287 position. */ 328 position. */
...@@ -318,7 +359,15 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount) ...@@ -318,7 +359,15 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
318 /* Flush FS's buffer. */ 359 /* Flush FS's buffer. */
319 __argp_fmtstream_update (fs); 360 __argp_fmtstream_update (fs);
320 361
321 wrote = fwrite (fs->buf, 1, fs->p - fs->buf, fs->stream); 362 #ifdef USE_IN_LIBIO
363 if (_IO_fwide (fs->stream, 0) > 0)
364 {
365 __fwprintf (fs->stream, L"%.*s", (int) (fs->p - fs->buf), fs->buf);
366 wrote = fs->p - fs->buf;
367 }
368 else
369 #endif
370 wrote = fwrite_unlocked (fs->buf, 1, fs->p - fs->buf, fs->stream);
322 if (wrote == fs->p - fs->buf) 371 if (wrote == fs->p - fs->buf)
323 { 372 {
324 fs->p = fs->buf; 373 fs->p = fs->buf;
...@@ -335,12 +384,13 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount) ...@@ -335,12 +384,13 @@ __argp_fmtstream_ensure (struct argp_fmtstream *fs, size_t amount)
335 if ((size_t) (fs->end - fs->buf) < amount) 384 if ((size_t) (fs->end - fs->buf) < amount)
336 /* Gotta grow the buffer. */ 385 /* Gotta grow the buffer. */
337 { 386 {
338 size_t new_size = fs->end - fs->buf + amount; 387 size_t old_size = fs->end - fs->buf;
339 char *new_buf = realloc (fs->buf, new_size); 388 size_t new_size = old_size + amount;
389 char *new_buf;
340 390
341 if (! new_buf) 391 if (new_size < old_size || ! (new_buf = realloc (fs->buf, new_size)))
342 { 392 {
343 errno = ENOMEM; 393 __set_errno (ENOMEM);
344 return 0; 394 return 0;
345 } 395 }
346 396
...@@ -369,19 +419,22 @@ __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...) ...@@ -369,19 +419,22 @@ __argp_fmtstream_printf (struct argp_fmtstream *fs, const char *fmt, ...)
369 419
370 va_start (args, fmt); 420 va_start (args, fmt);
371 avail = fs->end - fs->p; 421 avail = fs->end - fs->p;
372 out = vsnprintf (fs->p, avail, fmt, args); 422 out = __vsnprintf (fs->p, avail, fmt, args);
373 va_end (args); 423 va_end (args);
374 if (out >= avail) 424 if ((size_t) out >= avail)
375 size_guess = out + 1; 425 size_guess = out + 1;
376 } 426 }
377 while (out >= avail); 427 while ((size_t) out >= avail);
378 428
379 fs->p += out; 429 fs->p += out;
380 430
381 return out; 431 return out;
382 } 432 }
433 #if 0
434 /* Not exported. */
383 #ifdef weak_alias 435 #ifdef weak_alias
384 weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf) 436 weak_alias (__argp_fmtstream_printf, argp_fmtstream_printf)
385 #endif 437 #endif
438 #endif
386 439
387 #endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */ 440 #endif /* !ARGP_FMTSTREAM_USE_LINEWRAP */
......
1 /* Word-wrapping and line-truncating streams. 1 /* Word-wrapping and line-truncating streams.
2 Copyright (C) 1997, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 /* This package emulates glibc `line_wrap_stream' semantics for systems that 20 /* This package emulates glibc `line_wrap_stream' semantics for systems that
22 don't have that. If the system does have it, it is just a wrapper for 21 don't have that. If the system does have it, it is just a wrapper for
...@@ -34,6 +33,19 @@ ...@@ -34,6 +33,19 @@
34 #include <string.h> 33 #include <string.h>
35 #include <unistd.h> 34 #include <unistd.h>
36 35
36 #ifndef __attribute__
37 /* This feature is available in gcc versions 2.5 and later. */
38 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || __STRICT_ANSI__
39 # define __attribute__(Spec) /* empty */
40 # endif
41 /* The __-protected variants of `format' and `printf' attributes
42 are accepted by gcc versions 2.6.4 (effectively 2.7) and later. */
43 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7) || __STRICT_ANSI__
44 # define __format__ format
45 # define __printf__ printf
46 # endif
47 #endif
48
37 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \ 49 #if (_LIBC - 0 && !defined (USE_IN_LIBIO)) \
38 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H)) 50 || (defined (__GNU_LIBRARY__) && defined (HAVE_LINEWRAP_H))
39 /* line_wrap_stream is available, so use that. */ 51 /* line_wrap_stream is available, so use that. */
...@@ -82,6 +94,9 @@ typedef FILE *argp_fmtstream_t; ...@@ -82,6 +94,9 @@ typedef FILE *argp_fmtstream_t;
82 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */ 94 #else /* !ARGP_FMTSTREAM_USE_LINEWRAP */
83 /* Guess we have to define our own version. */ 95 /* Guess we have to define our own version. */
84 96
97 #ifndef __const
98 #define __const const
99 #endif
85 100
86 struct argp_fmtstream 101 struct argp_fmtstream
87 { 102 {
...@@ -122,20 +137,22 @@ extern void __argp_fmtstream_free (argp_fmtstream_t __fs); ...@@ -122,20 +137,22 @@ extern void __argp_fmtstream_free (argp_fmtstream_t __fs);
122 extern void argp_fmtstream_free (argp_fmtstream_t __fs); 137 extern void argp_fmtstream_free (argp_fmtstream_t __fs);
123 138
124 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs, 139 extern ssize_t __argp_fmtstream_printf (argp_fmtstream_t __fs,
125 const char *__fmt, ...); 140 __const char *__fmt, ...)
141 __attribute__ ((__format__ (printf, 2, 3)));
126 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs, 142 extern ssize_t argp_fmtstream_printf (argp_fmtstream_t __fs,
127 const char *__fmt, ...); 143 __const char *__fmt, ...)
144 __attribute__ ((__format__ (printf, 2, 3)));
128 145
129 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch); 146 extern int __argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
130 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch); 147 extern int argp_fmtstream_putc (argp_fmtstream_t __fs, int __ch);
131 148
132 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str); 149 extern int __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
133 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str); 150 extern int argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str);
134 151
135 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs, 152 extern size_t __argp_fmtstream_write (argp_fmtstream_t __fs,
136 const char *__str, size_t __len); 153 __const char *__str, size_t __len);
137 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs, 154 extern size_t argp_fmtstream_write (argp_fmtstream_t __fs,
138 const char *__str, size_t __len); 155 __const char *__str, size_t __len);
139 156
140 /* Access macros for various bits of state. */ 157 /* Access macros for various bits of state. */
141 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin) 158 #define argp_fmtstream_lmargin(__fs) ((__fs)->lmargin)
...@@ -194,7 +211,7 @@ extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount); ...@@ -194,7 +211,7 @@ extern int __argp_fmtstream_ensure (argp_fmtstream_t __fs, size_t __amount);
194 211
195 ARGP_FS_EI size_t 212 ARGP_FS_EI size_t
196 __argp_fmtstream_write (argp_fmtstream_t __fs, 213 __argp_fmtstream_write (argp_fmtstream_t __fs,
197 const char *__str, size_t __len) 214 __const char *__str, size_t __len)
198 { 215 {
199 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len)) 216 if (__fs->p + __len <= __fs->end || __argp_fmtstream_ensure (__fs, __len))
200 { 217 {
...@@ -207,7 +224,7 @@ __argp_fmtstream_write (argp_fmtstream_t __fs, ...@@ -207,7 +224,7 @@ __argp_fmtstream_write (argp_fmtstream_t __fs,
207 } 224 }
208 225
209 ARGP_FS_EI int 226 ARGP_FS_EI int
210 __argp_fmtstream_puts (argp_fmtstream_t __fs, const char *__str) 227 __argp_fmtstream_puts (argp_fmtstream_t __fs, __const char *__str)
211 { 228 {
212 size_t __len = strlen (__str); 229 size_t __len = strlen (__str);
213 if (__len) 230 if (__len)
......
1 /* Real definitions for extern inline functions in argp-fmtstream.h 1 /* Real definitions for extern inline functions in argp-fmtstream.h
2 Copyright (C) 1997 Free Software Foundation, Inc. 2 Copyright (C) 1997, 2003, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
22 #include <config.h> 21 #include <config.h>
...@@ -24,9 +23,11 @@ ...@@ -24,9 +23,11 @@
24 23
25 #define ARGP_FS_EI 24 #define ARGP_FS_EI
26 #undef __OPTIMIZE__ 25 #undef __OPTIMIZE__
27 #define __OPTIMIZE__ 26 #define __OPTIMIZE__ 1
28 #include "argp-fmtstream.h" 27 #include "argp-fmtstream.h"
29 28
29 #if 0
30 /* Not exported. */
30 /* Add weak aliases. */ 31 /* Add weak aliases. */
31 #if _LIBC - 0 && !defined (ARGP_FMTSTREAM_USE_LINEWRAP) && defined (weak_alias) 32 #if _LIBC - 0 && !defined (ARGP_FMTSTREAM_USE_LINEWRAP) && defined (weak_alias)
32 33
...@@ -39,3 +40,4 @@ weak_alias (__argp_fmtstream_set_wmargin, argp_fmtstream_set_wmargin) ...@@ -39,3 +40,4 @@ weak_alias (__argp_fmtstream_set_wmargin, argp_fmtstream_set_wmargin)
39 weak_alias (__argp_fmtstream_point, argp_fmtstream_point) 40 weak_alias (__argp_fmtstream_point, argp_fmtstream_point)
40 41
41 #endif 42 #endif
43 #endif
......
1 /* Name frobnication for compiling argp outside of glibc 1 /* Name frobnication for compiling argp outside of glibc
2 Copyright (C) 1997, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 #if !_LIBC 20 #if !_LIBC
22 /* This code is written for inclusion in gnu-libc, and uses names in the 21 /* This code is written for inclusion in gnu-libc, and uses names in the
...@@ -77,12 +76,81 @@ ...@@ -77,12 +76,81 @@
77 #undef __argp_fmtstream_wmargin 76 #undef __argp_fmtstream_wmargin
78 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin 77 #define __argp_fmtstream_wmargin argp_fmtstream_wmargin
79 78
79 #include "mempcpy.h"
80 #include "strcase.h"
81 #include "strchrnul.h"
82 #include "strndup.h"
83
80 /* normal libc functions we call */ 84 /* normal libc functions we call */
85 #undef __flockfile
86 #define __flockfile flockfile
87 #undef __funlockfile
88 #define __funlockfile funlockfile
89 #undef __mempcpy
90 #define __mempcpy mempcpy
81 #undef __sleep 91 #undef __sleep
82 #define __sleep sleep 92 #define __sleep sleep
83 #undef __strcasecmp 93 #undef __strcasecmp
84 #define __strcasecmp strcasecmp 94 #define __strcasecmp strcasecmp
95 #undef __strchrnul
96 #define __strchrnul strchrnul
97 #undef __strerror_r
98 #define __strerror_r strerror_r
99 #undef __strndup
100 #define __strndup strndup
85 #undef __vsnprintf 101 #undef __vsnprintf
86 #define __vsnprintf vsnprintf 102 #define __vsnprintf vsnprintf
87 103
104 #if defined(HAVE_DECL_CLEARERR_UNLOCKED) && !HAVE_DECL_CLEARERR_UNLOCKED
105 # define clearerr_unlocked(x) clearerr (x)
106 #endif
107 #if defined(HAVE_DECL_FEOF_UNLOCKED) && !HAVE_DECL_FEOF_UNLOCKED
108 # define feof_unlocked(x) feof (x)
109 # endif
110 #if defined(HAVE_DECL_FERROR_UNLOCKED) && !HAVE_DECL_FERROR_UNLOCKED
111 # define ferror_unlocked(x) ferror (x)
112 # endif
113 #if defined(HAVE_DECL_FFLUSH_UNLOCKED) && !HAVE_DECL_FFLUSH_UNLOCKED
114 # define fflush_unlocked(x) fflush (x)
115 # endif
116 #if defined(HAVE_DECL_FGETS_UNLOCKED) && !HAVE_DECL_FGETS_UNLOCKED
117 # define fgets_unlocked(x,y,z) fgets (x,y,z)
118 # endif
119 #if defined(HAVE_DECL_FPUTC_UNLOCKED) && !HAVE_DECL_FPUTC_UNLOCKED
120 # define fputc_unlocked(x,y) fputc (x,y)
121 # endif
122 #if defined(HAVE_DECL_FPUTS_UNLOCKED) && !HAVE_DECL_FPUTS_UNLOCKED
123 # define fputs_unlocked(x,y) fputs (x,y)
124 # endif
125 #if defined(HAVE_DECL_FREAD_UNLOCKED) && !HAVE_DECL_FREAD_UNLOCKED
126 # define fread_unlocked(w,x,y,z) fread (w,x,y,z)
127 # endif
128 #if defined(HAVE_DECL_FWRITE_UNLOCKED) && !HAVE_DECL_FWRITE_UNLOCKED
129 # define fwrite_unlocked(w,x,y,z) fwrite (w,x,y,z)
130 # endif
131 #if defined(HAVE_DECL_GETC_UNLOCKED) && !HAVE_DECL_GETC_UNLOCKED
132 # define getc_unlocked(x) getc (x)
133 # endif
134 #if defined(HAVE_DECL_GETCHAR_UNLOCKED) && !HAVE_DECL_GETCHAR_UNLOCKED
135 # define getchar_unlocked() getchar ()
136 # endif
137 #if defined(HAVE_DECL_PUTC_UNLOCKED) && !HAVE_DECL_PUTC_UNLOCKED
138 # define putc_unlocked(x,y) putc (x,y)
139 # endif
140 #if defined(HAVE_DECL_PUTCHAR_UNLOCKED) && !HAVE_DECL_PUTCHAR_UNLOCKED
141 # define putchar_unlocked(x) putchar (x)
142 # endif
143
144 extern char *__argp_basename (char *name);
145
88 #endif /* !_LIBC */ 146 #endif /* !_LIBC */
147
148 #ifndef __set_errno
149 #define __set_errno(e) (errno = (e))
150 #endif
151
152 #if defined _LIBC || HAVE_DECL_PROGRAM_INVOCATION_SHORT_NAME
153 # define __argp_short_program_name() (program_invocation_short_name)
154 #else
155 extern char *__argp_short_program_name (void);
156 #endif
......
...@@ -3,20 +3,19 @@ ...@@ -3,20 +3,19 @@
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 /* If set by the user program to a non-zero value, then a default option 20 /* If set by the user program to a non-zero value, then a default option
22 --version is added (unless the ARGP_NO_HELP flag is used), which will 21 --version is added (unless the ARGP_NO_HELP flag is used), which will
......
1 /* Default definition for ARGP_PROGRAM_VERSION_HOOK. 1 /* Default definition for ARGP_PROGRAM_VERSION_HOOK.
2 Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc. 2 Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
22 #include <config.h> 21 #include <config.h>
...@@ -29,4 +28,4 @@ ...@@ -29,4 +28,4 @@
29 this function with a stream to print the version to and a pointer to the 28 this function with a stream to print the version to and a pointer to the
30 current parsing state, and then exits (unless the ARGP_NO_EXIT flag is 29 current parsing state, and then exits (unless the ARGP_NO_EXIT flag is
31 used). This variable takes precedent over ARGP_PROGRAM_VERSION. */ 30 used). This variable takes precedent over ARGP_PROGRAM_VERSION. */
32 void (*argp_program_version_hook) (FILE *stream, struct argp_state *state); 31 void (*argp_program_version_hook) (FILE *stream, struct argp_state *state) = NULL;
......
1 /* Real definitions for extern inline functions in argp.h 1 /* Real definitions for extern inline functions in argp.h
2 Copyright (C) 1997, 1998, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1997, 1998, 2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 Written by Miles Bader <miles@gnu.ai.mit.edu>. 4 Written by Miles Bader <miles@gnu.ai.mit.edu>.
5 5
6 The GNU C Library is free software; you can redistribute it and/or 6 This program is free software; you can redistribute it and/or modify
7 modify it under the terms of the GNU Library General Public License as 7 it under the terms of the GNU General Public License as published by
8 published by the Free Software Foundation; either version 2 of the 8 the Free Software Foundation; either version 2, or (at your option)
9 License, or (at your option) any later version. 9 any later version.
10 10
11 The GNU C Library is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 Library General Public License for more details. 14 GNU General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License along
17 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 with this program; if not, write to the Free Software Foundation,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 Boston, MA 02111-1307, USA. */
20 19
21 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
22 #include <config.h> 21 #include <config.h>
23 #endif 22 #endif
24 23
24 #if defined _LIBC || defined HAVE_FEATURES_H
25 # include <features.h>
26 #endif
27
25 #ifndef __USE_EXTERN_INLINES 28 #ifndef __USE_EXTERN_INLINES
26 # define __USE_EXTERN_INLINES 1 29 # define __USE_EXTERN_INLINES 1
27 #endif 30 #endif
28 #define ARGP_EI 31 #define ARGP_EI
29 #undef __OPTIMIZE__ 32 #undef __OPTIMIZE__
30 #define __OPTIMIZE__ 33 #define __OPTIMIZE__ 1
31 #include "argp.h" 34 #include "argp.h"
32 35
33 /* Add weak aliases. */ 36 /* Add weak aliases. */
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* getline.c -- Replacement for GNU C library function getline
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 2
4 This library is free software; you can redistribute it and/or 3 Copyright (C) 1993, 1996, 1997, 1998, 2000, 2003, 2004 Free
5 modify it under the terms of the GNU Lesser General Public 4 Software Foundation, Inc.
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8 5
9 This library is distributed in the hope that it will be useful, 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,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 Lesser General Public License for more details. 14 GNU General Public License for more details.
13 15
14 You should have received a copy of the GNU Lesser General Public 16 You should have received a copy of the GNU General Public License
15 License along with this library; if not, write to the Free Software 17 along with this program; if not, write to the Free Software Foundation,
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 19
18 /* First implementation by Alain Magloire */ 20 /* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */
19 21
20 #ifdef HAVE_CONFIG_H 22 #if HAVE_CONFIG_H
21 # include <config.h> 23 # include <config.h>
22 #endif 24 #endif
23 25
24 #include <unistd.h> 26 #include "getline.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 ssize_t
30 getline (char **lineptr, size_t *n, FILE *stream)
31 {
32 return getdelim (lineptr, n, '\n', stream);
33 }
34 27
35 #ifndef HAVE_GETDELIM 28 #if ! (defined __GNU_LIBRARY__ && HAVE_GETDELIM)
36 29
37 /* Default value for line length. */ 30 # include "getndelim2.h"
38 static const int line_size = 128;
39 31
40 ssize_t 32 ssize_t
41 getdelim (char **lineptr, size_t *n, int delim, FILE *stream) 33 getdelim (char **lineptr, size_t *linesize, int delimiter, FILE *stream)
42 { 34 {
43 int indx = 0; 35 return getndelim2 (lineptr, linesize, 0, GETNLINE_NO_LIMIT, delimiter, EOF,
44 int c; 36 stream);
45
46 /* Sanity checks. */
47 if (lineptr == NULL || n == NULL || stream == NULL)
48 return -1;
49
50 /* Allocate the line the first time. */
51 if (*lineptr == NULL)
52 {
53 *lineptr = malloc (line_size);
54 if (*lineptr == NULL)
55 return -1;
56 *n = line_size;
57 }
58
59 while ((c = getc (stream)) != EOF)
60 {
61 /* Check if more memory is needed. */
62 if (indx >= *n)
63 {
64 *lineptr = realloc (*lineptr, *n + line_size);
65 if (*lineptr == NULL)
66 return -1;
67 *n += line_size;
68 }
69
70 /* Push the result in the line. */
71 (*lineptr)[indx++] = c;
72
73 /* Bail out. */
74 if (c == delim)
75 break;
76 }
77
78 /* Make room for the null character. */
79 if (indx >= *n)
80 {
81 *lineptr = realloc (*lineptr, *n + line_size);
82 if (*lineptr == NULL)
83 return -1;
84 *n += line_size;
85 }
86
87 /* Null terminate the buffer. */
88 (*lineptr)[indx++] = 0;
89
90 /* The last line may not have the delimiter, we have to
91 * return what we got and the error will be seen on the
92 * next iteration. */
93 return (c == EOF && (indx - 1) == 0) ? -1 : indx - 1;
94 } 37 }
38 #endif
95 39
96 #endif /* HAVE_GETDELIM */ 40 ssize_t
97 41 getline (char **lineptr, size_t *linesize, FILE *stream)
98
99 #ifdef STANDALONE
100 int main(void)
101 { 42 {
102 FILE * fp; 43 return getdelim (lineptr, linesize, '\n', stream);
103 char * line = NULL;
104 size_t len = 0;
105 ssize_t read;
106 fp = fopen("/etc/passwd", "r");
107 if (fp == NULL)
108 exit(EXIT_FAILURE);
109 while ((read = getline(&line, &len, fp)) != -1) {
110 printf("Retrieved line of length %zu :\n", read);
111 printf("%s", line);
112 }
113 if (line)
114 free(line);
115 return EXIT_SUCCESS;
116 } 44 }
117 #endif
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail 1 /* Replacement for GNU C library function getline
2 Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 2
4 This library is free software; you can redistribute it and/or 3 Copyright (C) 1995, 1997, 1999, 2000, 2001, 2002, 2003 Free
5 modify it under the terms of the GNU Lesser General Public 4 Software Foundation, Inc.
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8 5
9 This library is distributed in the hope that it will be useful, 6 This program is free software; you can redistribute it and/or modify
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 7 it under the terms of the GNU General Public License as published by
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 8 the Free Software Foundation; either version 2, or (at your option)
12 Lesser General Public License for more details. 9 any later version.
13 10
14 You should have received a copy of the GNU Lesser General Public 11 This program is distributed in the hope that it will be useful,
15 License along with this library; if not, write to the Free Software 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
17 15
18 #ifndef _GETLINE_H_ 16 You should have received a copy of the GNU General Public License
19 # define _GETLINE_H_ 1 17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 19
20 #ifndef GETLINE_H_
21 # define GETLINE_H_ 1
22
23 # include <stddef.h>
21 # include <stdio.h> 24 # include <stdio.h>
22 25
23 # ifndef PARAMS 26 /* Get ssize_t. */
24 # if defined (__GNUC__) || __STDC__ 27 # include <sys/types.h>
25 # define PARAMS(args) args 28
26 # else 29 /* glibc2 has these functions declared in <stdio.h>. Avoid redeclarations. */
27 # define PARAMS(args) () 30 # if __GLIBC__ < 2
28 # endif
29 # endif
30 31
31 extern int getline PARAMS ((char **_lineptr, size_t *_n, FILE *_stream)); 32 extern ssize_t getline (char **_lineptr, size_t *_linesize, FILE *_stream);
32 33
33 extern int getdelim PARAMS ((char **_lineptr, size_t *_n, int _delimiter, FILE *_stream)); 34 extern ssize_t getdelim (char **_lineptr, size_t *_linesize, int _delimiter,
35 FILE *_stream);
36
37 # endif
34 38
35 #endif /* ! _GETLINE_H_ */ 39 #endif /* not GETLINE_H_ */
......
1 /* getopt_long and getopt_long_only entry points for GNU getopt. 1 /* getopt_long and getopt_long_only entry points for GNU getopt.
2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 2 Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98,2004
3 Free Software Foundation, Inc. 3 Free Software Foundation, Inc.
4 NOTE: The canonical source of this file is maintained with the GNU C Library. 4 This file is part of the GNU C Library.
5 Bugs can be reported to bug-glibc@gnu.org.
6 5
7 This program is free software; you can redistribute it and/or modify it 6 This program is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by the 7 it under the terms of the GNU General Public License as published by
9 Free Software Foundation; either version 2, or (at your option) any 8 the Free Software Foundation; either version 2, or (at your option)
10 later version. 9 any later version.
11 10
12 This program is distributed in the hope that it will be useful, 11 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details. 14 GNU General Public License for more details.
16 15
17 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License along
18 along with this program; if not, write to the Free Software Foundation, 17 with this program; if not, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 19
21 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
22 #include <config.h> 21 #include <config.h>
23 #endif 22 #endif
24 23
25 #include "getopt.h" 24 #ifdef _LIBC
26 25 # include <getopt.h>
27 #if !defined __STDC__ || !__STDC__ 26 #else
28 /* This is a separate conditional since some stdc systems 27 # include "getopt.h"
29 reject `defined (const)'. */
30 #ifndef const
31 #define const
32 #endif
33 #endif 28 #endif
29 #include "getopt_int.h"
34 30
35 #include <stdio.h> 31 #include <stdio.h>
36 32
37 /* Comment out all this code if we are using the GNU C Library, and are not
38 actually compiling the library itself. This code is part of the GNU C
39 Library, but also included in many other GNU distributions. Compiling
40 and linking in this code is a waste when using the GNU C library
41 (especially if it is a shared library). Rather than having every GNU
42 program understand `configure --with-gnu-libc' and omit the object files,
43 it is simpler to just do this in the source for each such file. */
44
45 #define GETOPT_INTERFACE_VERSION 2
46 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
47 #include <gnu-versions.h>
48 #if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
49 #define ELIDE_CODE
50 #endif
51 #endif
52
53 #ifndef ELIDE_CODE
54
55
56 /* This needs to come after some library #include 33 /* This needs to come after some library #include
57 to get __GNU_LIBRARY__ defined. */ 34 to get __GNU_LIBRARY__ defined. */
58 #ifdef __GNU_LIBRARY__ 35 #ifdef __GNU_LIBRARY__
...@@ -64,14 +41,20 @@ ...@@ -64,14 +41,20 @@
64 #endif 41 #endif
65 42
66 int 43 int
67 getopt_long (argc, argv, options, long_options, opt_index) 44 getopt_long (int argc, char *__getopt_argv_const *argv, const char *options,
68 int argc; 45 const struct option *long_options, int *opt_index)
69 char *const *argv;
70 const char *options;
71 const struct option *long_options;
72 int *opt_index;
73 { 46 {
74 return _getopt_internal (argc, argv, options, long_options, opt_index, 0); 47 return _getopt_internal (argc, (char **) argv, options, long_options,
48 opt_index, 0, 0);
49 }
50
51 int
52 _getopt_long_r (int argc, char **argv, const char *options,
53 const struct option *long_options, int *opt_index,
54 struct _getopt_data *d)
55 {
56 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
57 0, 0, d);
75 } 58 }
76 59
77 /* Like getopt_long, but '-' as well as '--' can indicate a long option. 60 /* Like getopt_long, but '-' as well as '--' can indicate a long option.
...@@ -80,27 +63,30 @@ getopt_long (argc, argv, options, long_options, opt_index) ...@@ -80,27 +63,30 @@ getopt_long (argc, argv, options, long_options, opt_index)
80 instead. */ 63 instead. */
81 64
82 int 65 int
83 getopt_long_only (argc, argv, options, long_options, opt_index) 66 getopt_long_only (int argc, char *__getopt_argv_const *argv,
84 int argc; 67 const char *options,
85 char *const *argv; 68 const struct option *long_options, int *opt_index)
86 const char *options;
87 const struct option *long_options;
88 int *opt_index;
89 { 69 {
90 return _getopt_internal (argc, argv, options, long_options, opt_index, 1); 70 return _getopt_internal (argc, (char **) argv, options, long_options,
71 opt_index, 1, 0);
91 } 72 }
92 73
74 int
75 _getopt_long_only_r (int argc, char **argv, const char *options,
76 const struct option *long_options, int *opt_index,
77 struct _getopt_data *d)
78 {
79 return _getopt_internal_r (argc, argv, options, long_options, opt_index,
80 1, 0, d);
81 }
93 82
94 #endif /* Not ELIDE_CODE. */
95 83
96 #ifdef TEST 84 #ifdef TEST
97 85
98 #include <stdio.h> 86 #include <stdio.h>
99 87
100 int 88 int
101 main (argc, argv) 89 main (int argc, char **argv)
102 int argc;
103 char **argv;
104 { 90 {
105 int c; 91 int c;
106 int digit_optind = 0; 92 int digit_optind = 0;
......
1 #ifndef MD5_H 1 /* md5.h - Declaration of functions and data types used for MD5 sum
2 #define MD5_H 2 computing library functions.
3 3
4 #ifdef __alpha 4 Copyright (C) 1995, 1996, 1999, 2000, 2003, 2004 Free Software
5 typedef unsigned int uint32; 5 Foundation, Inc.
6 #else 6
7 typedef unsigned long uint32; 7 NOTE: The canonical source of this file is maintained with the GNU C
8 Library. Bugs can be reported to bug-glibc@prep.ai.mit.edu.
9
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
13 later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software Foundation,
22 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23
24 #ifndef _MD5_H
25 #define _MD5_H 1
26
27 #include <stdio.h>
28
29 #if HAVE_INTTYPES_H
30 # include <inttypes.h>
31 #endif
32 #if HAVE_STDINT_H || _LIBC
33 # include <stdint.h>
8 #endif 34 #endif
9 35
10 struct MD5Context { 36 typedef uint32_t md5_uint32;
11 uint32 buf[4]; 37
12 uint32 bits[2]; 38 /* Structure to save state of computation between the single steps. */
13 unsigned char in[64]; 39 struct md5_ctx
40 {
41 md5_uint32 A;
42 md5_uint32 B;
43 md5_uint32 C;
44 md5_uint32 D;
45
46 md5_uint32 total[2];
47 md5_uint32 buflen;
48 char buffer[128];
14 }; 49 };
15 50
16 void MD5Init(struct MD5Context *context); 51 /*
17 void MD5Update(struct MD5Context *context, unsigned char const *buf, 52 * The following three functions are build up the low level used in
18 unsigned len); 53 * the functions `md5_stream' and `md5_buffer'.
19 void MD5Final(unsigned char digest[16], struct MD5Context *context); 54 */
20 void MD5Transform(uint32 buf[4], uint32 const in[16]); 55
56 /* Initialize structure containing state of computation.
57 (RFC 1321, 3.3: Step 3) */
58 extern void md5_init_ctx (struct md5_ctx *ctx);
21 59
22 typedef struct MD5Context MD5_CTX; 60 /* Starting with the result of former calls of this function (or the
61 initialization function update the context for the next LEN bytes
62 starting at BUFFER.
63 It is necessary that LEN is a multiple of 64!!! */
64 extern void md5_process_block (const void *buffer, size_t len,
65 struct md5_ctx *ctx);
23 66
24 #endif /* !MD5_H */ 67 /* Starting with the result of former calls of this function (or the
68 initialization function update the context for the next LEN bytes
69 starting at BUFFER.
70 It is NOT required that LEN is a multiple of 64. */
71 extern void md5_process_bytes (const void *buffer, size_t len,
72 struct md5_ctx *ctx);
73
74 /* Process the remaining bytes in the buffer and put result from CTX
75 in first 16 bytes following RESBUF. The result is always in little
76 endian byte order, so that a byte-wise output yields to the wanted
77 ASCII representation of the message digest.
78
79 IMPORTANT: On some systems it is required that RESBUF be correctly
80 aligned for a 32 bits value. */
81 extern void *md5_finish_ctx (struct md5_ctx *ctx, void *resbuf);
82
83
84 /* Put result from CTX in first 16 bytes following RESBUF. The result is
85 always in little endian byte order, so that a byte-wise output yields
86 to the wanted ASCII representation of the message digest.
87
88 IMPORTANT: On some systems it is required that RESBUF is correctly
89 aligned for a 32 bits value. */
90 extern void *md5_read_ctx (const struct md5_ctx *ctx, void *resbuf);
91
92
93 /* Compute MD5 message digest for bytes read from STREAM. The
94 resulting message digest number will be written into the 16 bytes
95 beginning at RESBLOCK. */
96 extern int md5_stream (FILE *stream, void *resblock);
97
98 /* Compute MD5 message digest for LEN bytes beginning at BUFFER. The
99 result is always in little endian byte order, so that a byte-wise
100 output yields to the wanted ASCII representation of the message
101 digest. */
102 extern void *md5_buffer (const char *buffer, size_t len, void *resblock);
103
104 #define rol(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
105
106 #endif
......
This diff could not be displayed because it is too large.
1 /* Copyright (C) 1991,93,94,95,96,97,99,2000, 2001 Free Software Foundation, Inc. 1 /* Searching in a string.
2 Based on strlen implementation by Torbjorn Granlund (tege@sics.se), 2 Copyright (C) 2003 Free Software Foundation, Inc.
3 with help from Dan Sahlin (dan@sics.se) and
4 bug fix and commentary by Jim Blandy (jimb@ai.mit.edu);
5 adaptation to strchr suggested by Dick Karpinski (dick@cca.ucsf.edu),
6 and implemented by Roland McGrath (roland@ai.mit.edu).
7 3
8 The GNU C Library is free software; you can redistribute it and/or 4 This program is free software; you can redistribute it and/or modify
9 modify it under the terms of the GNU Library General Public License as 5 it under the terms of the GNU General Public License as published by
10 published by the Free Software Foundation; either version 2 of the 6 the Free Software Foundation; either version 2, or (at your option)
11 License, or (at your option) any later version. 7 any later version.
12 8
13 The GNU C Library is distributed in the hope that it will be useful, 9 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of 10 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 Library General Public License for more details. 12 GNU General Public License for more details.
17 13
18 You should have received a copy of the GNU Library General Public 14 You should have received a copy of the GNU General Public License
19 License along with the GNU C Library; see the file COPYING.LIB. If not, 15 along with this program; if not, write to the Free Software Foundation,
20 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 Boston, MA 02111-1307, USA. */
22 17
23 #include <string.h> 18 /* Specification. */
24 #include <stdlib.h> 19 #include "strchrnul.h"
25 20
26 /* Find the first occurrence of C in S or the final NUL byte. */ 21 /* Find the first occurrence of C in S or the final NUL byte. */
27 char * 22 char *
28 strchrnul (s, c_in) 23 strchrnul (const char *s, int c_in)
29 const char *s;
30 int c_in;
31 { 24 {
32 const char *char_ptr; 25 char c = c_in;
33 const unsigned long int *longword_ptr; 26 while (*s && (*s != c))
34 unsigned long int longword, magic_bits, charmask; 27 s++;
35 unsigned char c;
36 28
37 c = (unsigned char) c_in; 29 return (char *) s;
38
39 /* Handle the first few characters by reading one character at a time.
40 Do this until CHAR_PTR is aligned on a longword boundary. */
41 for (char_ptr = s; ((unsigned long int) char_ptr
42 & (sizeof (longword) - 1)) != 0;
43 ++char_ptr)
44 if (*char_ptr == c || *char_ptr == '\0')
45 return (void *) char_ptr;
46
47 /* All these elucidatory comments refer to 4-byte longwords,
48 but the theory applies equally well to 8-byte longwords. */
49
50 longword_ptr = (unsigned long int *) char_ptr;
51
52 /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
53 the "holes." Note that there is a hole just to the left of
54 each byte, with an extra at the end:
55
56 bits: 01111110 11111110 11111110 11111111
57 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
58
59 The 1-bits make sure that carries propagate to the next 0-bit.
60 The 0-bits provide holes for carries to fall into. */
61 switch (sizeof (longword))
62 {
63 case 4: magic_bits = 0x7efefeffL; break;
64 case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break;
65 default:
66 abort ();
67 }
68
69 /* Set up a longword, each of whose bytes is C. */
70 charmask = c | (c << 8);
71 charmask |= charmask << 16;
72 if (sizeof (longword) > 4)
73 /* Do the shift in two steps to avoid a warning if long has 32 bits. */
74 charmask |= (charmask << 16) << 16;
75 if (sizeof (longword) > 8)
76 abort ();
77
78 /* Instead of the traditional loop which tests each character,
79 we will test a longword at a time. The tricky part is testing
80 if *any of the four* bytes in the longword in question are zero. */
81 for (;;)
82 {
83 /* We tentatively exit the loop if adding MAGIC_BITS to
84 LONGWORD fails to change any of the hole bits of LONGWORD.
85
86 1) Is this safe? Will it catch all the zero bytes?
87 Suppose there is a byte with all zeros. Any carry bits
88 propagating from its left will fall into the hole at its
89 least significant bit and stop. Since there will be no
90 carry from its most significant bit, the LSB of the
91 byte to the left will be unchanged, and the zero will be
92 detected.
93
94 2) Is this worthwhile? Will it ignore everything except
95 zero bytes? Suppose every byte of LONGWORD has a bit set
96 somewhere. There will be a carry into bit 8. If bit 8
97 is set, this will carry into bit 16. If bit 8 is clear,
98 one of bits 9-15 must be set, so there will be a carry
99 into bit 16. Similarly, there will be a carry into bit
100 24. If one of bits 24-30 is set, there will be a carry
101 into bit 31, so all of the hole bits will be changed.
102
103 The one misfire occurs when bits 24-30 are clear and bit
104 31 is set; in this case, the hole at bit 31 is not
105 changed. If we had access to the processor carry flag,
106 we could close this loophole by putting the fourth hole
107 at bit 32!
108
109 So it ignores everything except 128's, when they're aligned
110 properly.
111
112 3) But wait! Aren't we looking for C as well as zero?
113 Good point. So what we do is XOR LONGWORD with a longword,
114 each of whose bytes is C. This turns each byte that is C
115 into a zero. */
116
117 longword = *longword_ptr++;
118
119 /* Add MAGIC_BITS to LONGWORD. */
120 if ((((longword + magic_bits)
121
122 /* Set those bits that were unchanged by the addition. */
123 ^ ~longword)
124
125 /* Look at only the hole bits. If any of the hole bits
126 are unchanged, most likely one of the bytes was a
127 zero. */
128 & ~magic_bits) != 0 ||
129
130 /* That caught zeroes. Now test for C. */
131 ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
132 & ~magic_bits) != 0)
133 {
134 /* Which of the bytes was C or zero?
135 If none of them were, it was a misfire; continue the search. */
136
137 const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
138
139 if (*cp == c || *cp == '\0')
140 return (char *) cp;
141 if (*++cp == c || *cp == '\0')
142 return (char *) cp;
143 if (*++cp == c || *cp == '\0')
144 return (char *) cp;
145 if (*++cp == c || *cp == '\0')
146 return (char *) cp;
147 if (sizeof (longword) > 4)
148 {
149 if (*++cp == c || *cp == '\0')
150 return (char *) cp;
151 if (*++cp == c || *cp == '\0')
152 return (char *) cp;
153 if (*++cp == c || *cp == '\0')
154 return (char *) cp;
155 if (*++cp == c || *cp == '\0')
156 return (char *) cp;
157 }
158 }
159 }
160
161 /* This should never happen. */
162 return NULL;
163 } 30 }
......
1 /* Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc. 1 /* Copyright (C) 1996, 1997, 1998, 2000, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 2
4 The GNU C Library is free software; you can redistribute it and/or 3 NOTE: The canonical source of this file is maintained with the GNU C Library.
5 modify it under the terms of the GNU Library General Public License as 4 Bugs can be reported to bug-glibc@prep.ai.mit.edu.
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8 5
9 The GNU C Library is distributed in the hope that it will be useful, 6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 Library General Public License for more details. 14 GNU General Public License for more details.
13 15
14 You should have received a copy of the GNU Library General Public 16 You should have received a copy of the GNU General Public License
15 License along with the GNU C Library; see the file COPYING.LIB. If not, 17 along with this program; if not, write to the Free Software Foundation,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
17 Boston, MA 02111-1307, USA. */
18 19
19 #ifdef HAVE_CONFIG_H 20 #ifdef HAVE_CONFIG_H
20 # include "config.h" 21 # include "config.h"
21 #endif 22 #endif
22 23
23 #include <stdio.h> 24 #include <stdlib.h>
24 #include <sys/types.h> 25 #include <string.h>
26
27 #ifndef HAVE_DECL_STRNLEN
28 "this configure-time declaration test was not run"
29 #endif
30 #if !HAVE_DECL_STRNLEN
31 size_t strnlen ();
32 #endif
33
34 #undef __strndup
35 #undef strndup
25 36
26 #if defined _LIBC || defined STDC_HEADERS 37 #ifndef weak_alias
27 # include <stdlib.h> 38 # define __strndup strndup
28 # include <string.h>
29 #else
30 char *malloc ();
31 #endif 39 #endif
32 40
33 char * 41 char *
34 strndup (s, n) 42 __strndup (const char *s, size_t n)
35 const char *s;
36 size_t n;
37 { 43 {
38 size_t len = strnlen (s, n); 44 size_t len = strnlen (s, n);
39 char *nouveau = malloc (len + 1); 45 char *new = malloc (len + 1);
40 46
41 if (nouveau == NULL) 47 if (new == NULL)
42 return NULL; 48 return NULL;
43 49
44 nouveau[len] = '\0'; 50 new[len] = '\0';
45 return (char *) memcpy (nouveau, s, len); 51 return memcpy (new, s, len);
46 } 52 }
53 #ifdef weak_alias
54 weak_alias (__strndup, strndup)
55 #endif
......
1 /* Find the length of STRING, but scan at most MAXLEN characters. 1 /* Find the length of STRING, but scan at most MAXLEN characters.
2 Copyright (C) 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1996, 1997, 1998, 2000-2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 4
5 The GNU C Library is free software; you can redistribute it and/or 5 This program is free software; you can redistribute it and/or modify
6 modify it under the terms of the GNU Library General Public License as 6 it under the terms of the GNU General Public License as published by
7 published by the Free Software Foundation; either version 2 of the 7 the Free Software Foundation; either version 2, or (at your option)
8 License, or (at your option) any later version. 8 any later version.
9 9
10 The GNU C Library is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 Library General Public License for more details. 13 GNU General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public 15 You should have received a copy of the GNU General Public License along
16 License along with the GNU C Library; see the file COPYING.LIB. If not, 16 with this program; if not, write to the Free Software Foundation,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 Boston, MA 02111-1307, USA. */ 18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22 #undef strnlen
19 23
20 #include <string.h> 24 #include <string.h>
21 25
26 #undef __strnlen
27 #undef strnlen
28
29 #ifndef _LIBC
30 # define strnlen rpl_strnlen
31 #endif
32
33 #ifndef weak_alias
34 # define __strnlen strnlen
35 #endif
36
22 /* Find the length of STRING, but scan at most MAXLEN characters. 37 /* Find the length of STRING, but scan at most MAXLEN characters.
23 If no '\0' terminator is found in that many characters, return MAXLEN. */ 38 If no '\0' terminator is found in that many characters, return MAXLEN. */
24 39
25 size_t 40 size_t
26 strnlen (const char *string, size_t maxlen) 41 __strnlen (const char *string, size_t maxlen)
27 { 42 {
28 const char *end = memchr (string, '\0', maxlen); 43 const char *end = memchr (string, '\0', maxlen);
29 return end ? (size_t) (end - string) : maxlen; 44 return end ? (size_t) (end - string) : maxlen;
30 } 45 }
46 #ifdef weak_alias
47 weak_alias (__strnlen, strnlen)
48 #endif
......
1 /* Reentrant string tokenizer. Generic version. 1 /* Reentrant string tokenizer. Generic version.
2 Copyright (C) 1991, 1996, 1997, 1998, 1999, 2001 Free Software Foundation, Inc. 2 Copyright (C) 1991,1996-1999,2001,2004 Free Software Foundation, Inc.
3 This file is part of the GNU C Library. 3 This file is part of the GNU C Library.
4 4
5 The GNU C Library is free software; you can redistribute it and/or 5 This program is free software; you can redistribute it and/or modify
6 modify it under the terms of the GNU Library General Public License as 6 it under the terms of the GNU General Public License as published by
7 published by the Free Software Foundation; either version 2 of the 7 the Free Software Foundation; either version 2, or (at your option)
8 License, or (at your option) any later version. 8 any later version.
9 9
10 The GNU C Library is distributed in the hope that it will be useful, 10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 Library General Public License for more details. 13 GNU General Public License for more details.
14 14
15 You should have received a copy of the GNU Library General Public 15 You should have received a copy of the GNU General Public License along
16 License along with the GNU C Library; see the file COPYING.LIB. If not, 16 with this program; if not, write to the Free Software Foundation,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 Boston, MA 02111-1307, USA. */ 18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
19 22
20 #include <string.h> 23 #include <string.h>
21 24
25 #undef strtok_r
26 #undef __strtok_r
27
28 #ifndef _LIBC
29 /* Get specification. */
30 # include "strtok_r.h"
31 # define __strtok_r strtok_r
32 # define __rawmemchr strchr
33 #endif
34
22 /* Parse S into tokens separated by characters in DELIM. 35 /* Parse S into tokens separated by characters in DELIM.
23 If S is NULL, the saved pointer in SAVE_PTR is used as 36 If S is NULL, the saved pointer in SAVE_PTR is used as
24 the next starting point. For example: 37 the next starting point. For example:
...@@ -30,10 +43,7 @@ ...@@ -30,10 +43,7 @@
30 // s = "abc\0-def\0" 43 // s = "abc\0-def\0"
31 */ 44 */
32 char * 45 char *
33 strtok_r (s, delim, save_ptr) 46 __strtok_r (char *s, const char *delim, char **save_ptr)
34 char *s;
35 const char *delim;
36 char **save_ptr;
37 { 47 {
38 char *token; 48 char *token;
39 49
...@@ -53,8 +63,7 @@ strtok_r (s, delim, save_ptr) ...@@ -53,8 +63,7 @@ strtok_r (s, delim, save_ptr)
53 s = strpbrk (token, delim); 63 s = strpbrk (token, delim);
54 if (s == NULL) 64 if (s == NULL)
55 /* This token finishes the string. */ 65 /* This token finishes the string. */
56 /* *save_ptr = __rawmemchr (token, '\0'); */ 66 *save_ptr = __rawmemchr (token, '\0');
57 *save_ptr = token + strlen (token);
58 else 67 else
59 { 68 {
60 /* Terminate the token and make *SAVE_PTR point past it. */ 69 /* Terminate the token and make *SAVE_PTR point past it. */
...@@ -63,4 +72,7 @@ strtok_r (s, delim, save_ptr) ...@@ -63,4 +72,7 @@ strtok_r (s, delim, save_ptr)
63 } 72 }
64 return token; 73 return token;
65 } 74 }
66 /* weak_alias (__strtok_r, strtok_r) */ 75 #ifdef weak_alias
76 libc_hidden_def (__strtok_r)
77 weak_alias (__strtok_r, strtok_r)
78 #endif
......