Update some functions from GLIBC.
Showing
6 changed files
with
194 additions
and
65 deletions
1 | AUTOMAKE_OPTIONS = ansi2knr | ||
1 | noinst_LIBRARIES = libmailutils.a | 2 | noinst_LIBRARIES = libmailutils.a |
2 | 3 | ||
3 | libmailutils_a_SOURCES = basename.c getopt.c getopt1.c md5.c getline.c \ | 4 | libmailutils_a_SOURCES = basename.c getopt.c getopt1.c md5.c getline.c \ |
4 | xstrdup.c xmalloc.c argcv.c | 5 | xstrdup.c xmalloc.c argcv.c |
5 | 6 | ||
6 | EXTRA_DIST = alloca.c snprintf.c strtok_r.c xstrtol.c | 7 | EXTRA_DIST = alloca.c snprintf.c strchrnul.c strndup.c strnlen.c strtok_r.c \ |
8 | xstrtol.c vasprintf.c | ||
7 | 9 | ||
8 | noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h \ | 10 | noinst_HEADERS = getopt.h md5.h getline.h snprintf.h xstrtol.h xalloc.h \ |
9 | argcv.h error.h | 11 | argcv.h error.h | ... | ... |
This diff is collapsed.
Click to expand it.
1 | /* Declarations for getopt. | 1 | /* Declarations for getopt. |
2 | Copyright (C) 1989, 1990, 1991, 1992, 1993 Free Software Foundation, Inc. | 2 | Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc. |
3 | 3 | NOTE: The canonical source of this file is maintained with the GNU C Library. | |
4 | Bugs can be reported to bug-glibc@gnu.org. | ||
4 | This program is free software; you can redistribute it and/or modify it | 5 | This program is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU General Public License as published by the | 6 | under the terms of the GNU General Public License as published by the |
6 | Free Software Foundation; either version 2, or (at your option) any | 7 | Free Software Foundation; either version 2, or (at your option) any |
... | @@ -13,14 +14,17 @@ | ... | @@ -13,14 +14,17 @@ |
13 | 14 | ||
14 | You should have received a copy of the GNU General Public License | 15 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software | 16 | along with this program; if not, write to the Free Software |
16 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | 17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
18 | USA. */ | ||
17 | 19 | ||
18 | #ifndef _GETOPT_H | 20 | #ifndef _GETOPT_H |
19 | #define _GETOPT_H 1 | 21 | |
22 | #ifndef __need_getopt | ||
23 | # define _GETOPT_H 1 | ||
24 | #endif | ||
20 | 25 | ||
21 | #ifdef __cplusplus | 26 | #ifdef __cplusplus |
22 | extern "C" | 27 | extern "C" { |
23 | { | ||
24 | #endif | 28 | #endif |
25 | 29 | ||
26 | /* For communication from `getopt' to the caller. | 30 | /* For communication from `getopt' to the caller. |
... | @@ -29,7 +33,7 @@ extern "C" | ... | @@ -29,7 +33,7 @@ extern "C" |
29 | Also, when `ordering' is RETURN_IN_ORDER, | 33 | Also, when `ordering' is RETURN_IN_ORDER, |
30 | each non-option ARGV-element is returned here. */ | 34 | each non-option ARGV-element is returned here. */ |
31 | 35 | ||
32 | extern char *optarg; | 36 | extern char *optarg; |
33 | 37 | ||
34 | /* Index in ARGV of the next element to be scanned. | 38 | /* Index in ARGV of the next element to be scanned. |
35 | This is used for communication to and from the caller | 39 | This is used for communication to and from the caller |
... | @@ -37,32 +41,33 @@ extern "C" | ... | @@ -37,32 +41,33 @@ extern "C" |
37 | 41 | ||
38 | On entry to `getopt', zero means this is the first call; initialize. | 42 | On entry to `getopt', zero means this is the first call; initialize. |
39 | 43 | ||
40 | When `getopt' returns EOF, this is the index of the first of the | 44 | When `getopt' returns -1, this is the index of the first of the |
41 | non-option elements that the caller should itself scan. | 45 | non-option elements that the caller should itself scan. |
42 | 46 | ||
43 | Otherwise, `optind' communicates from one call to the next | 47 | Otherwise, `optind' communicates from one call to the next |
44 | how much of ARGV has been scanned so far. */ | 48 | how much of ARGV has been scanned so far. */ |
45 | 49 | ||
46 | extern int optind; | 50 | extern int optind; |
47 | 51 | ||
48 | /* Callers store zero here to inhibit the error message `getopt' prints | 52 | /* Callers store zero here to inhibit the error message `getopt' prints |
49 | for unrecognized options. */ | 53 | for unrecognized options. */ |
50 | 54 | ||
51 | extern int opterr; | 55 | extern int opterr; |
52 | 56 | ||
53 | /* Set to an option character which was unrecognized. */ | 57 | /* Set to an option character which was unrecognized. */ |
54 | 58 | ||
55 | extern int optopt; | 59 | extern int optopt; |
56 | 60 | ||
61 | #ifndef __need_getopt | ||
57 | /* Describe the long-named options requested by the application. | 62 | /* Describe the long-named options requested by the application. |
58 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector | 63 | The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector |
59 | of `struct option' terminated by an element containing a name which is | 64 | of `struct option' terminated by an element containing a name which is |
60 | zero. | 65 | zero. |
61 | 66 | ||
62 | The field `has_arg' is: | 67 | The field `has_arg' is: |
63 | no_argument (or 0) if the option does not take an argument, | 68 | no_argument (or 0) if the option does not take an argument, |
64 | required_argument (or 1) if the option requires an argument, | 69 | required_argument (or 1) if the option requires an argument, |
65 | optional_argument (or 2) if the option takes an optional argument. | 70 | optional_argument (or 2) if the option takes an optional argument. |
66 | 71 | ||
67 | If the field `flag' is not NULL, it points to a variable that is set | 72 | If the field `flag' is not NULL, it points to a variable that is set |
68 | to the value given in the field `val' when the option is found, but | 73 | to the value given in the field `val' when the option is found, but |
... | @@ -75,56 +80,90 @@ extern "C" | ... | @@ -75,56 +80,90 @@ extern "C" |
75 | one). For long options that have a zero `flag' field, `getopt' | 80 | one). For long options that have a zero `flag' field, `getopt' |
76 | returns the contents of the `val' field. */ | 81 | returns the contents of the `val' field. */ |
77 | 82 | ||
78 | struct option | 83 | struct option |
79 | { | 84 | { |
80 | #if __STDC__ | 85 | # if defined __STDC__ && __STDC__ |
81 | const char *name; | 86 | const char *name; |
82 | #else | 87 | # else |
83 | char *name; | 88 | char *name; |
84 | #endif | 89 | # endif |
85 | /* has_arg can't be an enum because some compilers complain about | 90 | /* has_arg can't be an enum because some compilers complain about |
86 | type mismatches in all the code that assumes it is an int. */ | 91 | type mismatches in all the code that assumes it is an int. */ |
87 | int has_arg; | 92 | int has_arg; |
88 | int *flag; | 93 | int *flag; |
89 | int val; | 94 | int val; |
90 | }; | 95 | }; |
91 | 96 | ||
92 | /* Names for the values of the `has_arg' field of `struct option'. */ | 97 | /* Names for the values of the `has_arg' field of `struct option'. */ |
93 | 98 | ||
94 | #define no_argument 0 | 99 | # define no_argument 0 |
95 | #define required_argument 1 | 100 | # define required_argument 1 |
96 | #define optional_argument 2 | 101 | # define optional_argument 2 |
102 | #endif /* need getopt */ | ||
103 | |||
97 | 104 | ||
98 | #if __STDC__ | 105 | /* Get definitions and prototypes for functions to process the |
99 | #if defined(__GNU_LIBRARY__) | 106 | arguments in ARGV (ARGC of them, minus the program name) for |
107 | options given in OPTS. | ||
108 | |||
109 | Return the option character from OPTS just read. Return -1 when | ||
110 | there are no more options. For unrecognized options, or options | ||
111 | missing arguments, `optopt' is set to the option letter, and '?' is | ||
112 | returned. | ||
113 | |||
114 | The OPTS string is a list of characters which are recognized option | ||
115 | letters, optionally followed by colons, specifying that that letter | ||
116 | takes an argument, to be placed in `optarg'. | ||
117 | |||
118 | If a letter in OPTS is followed by two colons, its argument is | ||
119 | optional. This behavior is specific to the GNU `getopt'. | ||
120 | |||
121 | The argument `--' causes premature termination of argument | ||
122 | scanning, explicitly telling `getopt' that there are no more | ||
123 | options. | ||
124 | |||
125 | If OPTS begins with `--', then non-option arguments are treated as | ||
126 | arguments to the option '\0'. This behavior is specific to the GNU | ||
127 | `getopt'. */ | ||
128 | |||
129 | #if defined __STDC__ && __STDC__ | ||
130 | # ifdef __GNU_LIBRARY__ | ||
100 | /* Many other libraries have conflicting prototypes for getopt, with | 131 | /* Many other libraries have conflicting prototypes for getopt, with |
101 | differences in the consts, in stdlib.h. To avoid compilation | 132 | differences in the consts, in stdlib.h. To avoid compilation |
102 | errors, only prototype getopt for the GNU C library. */ | 133 | errors, only prototype getopt for the GNU C library. */ |
103 | extern int getopt (int argc, char *const *argv, const char *shortopts); | 134 | extern int getopt (int __argc, char *const *__argv, const char *__shortopts); |
104 | #else /* not __GNU_LIBRARY__ */ | 135 | # else /* not __GNU_LIBRARY__ */ |
105 | extern int getopt (); | 136 | extern int getopt (); |
106 | #endif /* not __GNU_LIBRARY__ */ | 137 | # endif /* __GNU_LIBRARY__ */ |
107 | extern int getopt_long (int argc, char *const *argv, const char *shortopts, | 138 | |
108 | const struct option *longopts, int *longind); | 139 | # ifndef __need_getopt |
109 | extern int getopt_long_only (int argc, char *const *argv, | 140 | extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts, |
110 | const char *shortopts, | 141 | const struct option *__longopts, int *__longind); |
111 | const struct option *longopts, int *longind); | 142 | extern int getopt_long_only (int __argc, char *const *__argv, |
143 | const char *__shortopts, | ||
144 | const struct option *__longopts, int *__longind); | ||
112 | 145 | ||
113 | /* Internal only. Users should not call this directly. */ | 146 | /* Internal only. Users should not call this directly. */ |
114 | extern int _getopt_internal (int argc, char *const *argv, | 147 | extern int _getopt_internal (int __argc, char *const *__argv, |
115 | const char *shortopts, | 148 | const char *__shortopts, |
116 | const struct option *longopts, int *longind, | 149 | const struct option *__longopts, int *__longind, |
117 | int long_only); | 150 | int __long_only); |
118 | #else /* not __STDC__ */ | 151 | # endif |
119 | extern int getopt (); | 152 | #else /* not __STDC__ */ |
120 | extern int getopt_long (); | 153 | extern int getopt (); |
121 | extern int getopt_long_only (); | 154 | # ifndef __need_getopt |
122 | 155 | extern int getopt_long (); | |
123 | extern int _getopt_internal (); | 156 | extern int getopt_long_only (); |
124 | #endif /* not __STDC__ */ | 157 | |
158 | extern int _getopt_internal (); | ||
159 | # endif | ||
160 | #endif /* __STDC__ */ | ||
125 | 161 | ||
126 | #ifdef __cplusplus | 162 | #ifdef __cplusplus |
127 | } | 163 | } |
128 | #endif | 164 | #endif |
129 | 165 | ||
130 | #endif /* _GETOPT_H */ | 166 | /* Make sure we later can get all the definitions and declarations. */ |
167 | #undef __need_getopt | ||
168 | |||
169 | #endif /* getopt.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, 1993 | 2 | Copyright (C) 1987,88,89,90,91,92,93,94,96,97,98 |
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. | ||
5 | Bugs can be reported to bug-glibc@gnu.org. | ||
4 | 6 | ||
5 | This program is free software; you can redistribute it and/or modify it | 7 | This program is free software; you can redistribute it and/or modify it |
6 | under the terms of the GNU General Public License as published by the | 8 | under the terms of the GNU General Public License as published by the |
... | @@ -13,18 +15,22 @@ | ... | @@ -13,18 +15,22 @@ |
13 | GNU General Public License for more details. | 15 | GNU General Public License for more details. |
14 | 16 | ||
15 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software | 18 | along with this program; if not, write to the Free Software Foundation, |
17 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | 19 | Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
18 | 20 | ||
19 | #ifdef HAVE_CONFIG_H | 21 | #ifdef HAVE_CONFIG_H |
20 | #include "config.h" | 22 | #include <config.h> |
21 | #endif | 23 | #endif |
22 | 24 | ||
23 | #include "getopt.h" | 25 | #include "getopt.h" |
24 | 26 | ||
25 | #if !__STDC__ && !defined(const) && IN_GCC | 27 | #if !defined __STDC__ || !__STDC__ |
28 | /* This is a separate conditional since some stdc systems | ||
29 | reject `defined (const)'. */ | ||
30 | #ifndef const | ||
26 | #define const | 31 | #define const |
27 | #endif | 32 | #endif |
33 | #endif | ||
28 | 34 | ||
29 | #include <stdio.h> | 35 | #include <stdio.h> |
30 | 36 | ||
... | @@ -36,15 +42,21 @@ | ... | @@ -36,15 +42,21 @@ |
36 | program understand `configure --with-gnu-libc' and omit the object files, | 42 | program understand `configure --with-gnu-libc' and omit the object files, |
37 | it is simpler to just do this in the source for each such file. */ | 43 | it is simpler to just do this in the source for each such file. */ |
38 | 44 | ||
39 | #if defined (_LIBC) || !defined (__GNU_LIBRARY__) | 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 | ||
40 | 54 | ||
41 | 55 | ||
42 | /* This needs to come after some library #include | 56 | /* This needs to come after some library #include |
43 | to get __GNU_LIBRARY__ defined. */ | 57 | to get __GNU_LIBRARY__ defined. */ |
44 | #ifdef __GNU_LIBRARY__ | 58 | #ifdef __GNU_LIBRARY__ |
45 | #include <stdlib.h> | 59 | #include <stdlib.h> |
46 | #else | ||
47 | char *getenv (); | ||
48 | #endif | 60 | #endif |
49 | 61 | ||
50 | #ifndef NULL | 62 | #ifndef NULL |
... | @@ -79,7 +91,7 @@ getopt_long_only (argc, argv, options, long_options, opt_index) | ... | @@ -79,7 +91,7 @@ getopt_long_only (argc, argv, options, long_options, opt_index) |
79 | } | 91 | } |
80 | 92 | ||
81 | 93 | ||
82 | #endif /* _LIBC or not __GNU_LIBRARY__. */ | 94 | #endif /* Not ELIDE_CODE. */ |
83 | 95 | ||
84 | #ifdef TEST | 96 | #ifdef TEST |
85 | 97 | ||
... | @@ -110,7 +122,7 @@ main (argc, argv) | ... | @@ -110,7 +122,7 @@ main (argc, argv) |
110 | 122 | ||
111 | c = getopt_long (argc, argv, "abc:d:0123456789", | 123 | c = getopt_long (argc, argv, "abc:d:0123456789", |
112 | long_options, &option_index); | 124 | long_options, &option_index); |
113 | if (c == EOF) | 125 | if (c == -1) |
114 | break; | 126 | break; |
115 | 127 | ||
116 | switch (c) | 128 | switch (c) | ... | ... |
lib/strndup.c
0 → 100644
1 | /* Copyright (C) 1996, 1997, 1998, 2001 Free Software Foundation, Inc. | ||
2 | This file is part of the GNU C Library. | ||
3 | |||
4 | The GNU C Library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Library General Public License as | ||
6 | published by the Free Software Foundation; either version 2 of the | ||
7 | License, or (at your option) any later version. | ||
8 | |||
9 | The GNU C Library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Library General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public | ||
15 | License along with the GNU C Library; see the file COPYING.LIB. If not, | ||
16 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
17 | Boston, MA 02111-1307, USA. */ | ||
18 | |||
19 | #ifdef HAVE_CONFIG_H | ||
20 | # include "config.h" | ||
21 | #endif | ||
22 | |||
23 | #include <stdio.h> | ||
24 | #include <sys/types.h> | ||
25 | |||
26 | #if defined _LIBC || defined STDC_HEADERS | ||
27 | # include <stdlib.h> | ||
28 | # include <string.h> | ||
29 | #else | ||
30 | char *malloc (); | ||
31 | #endif | ||
32 | |||
33 | char * | ||
34 | strndup (s, n) | ||
35 | const char *s; | ||
36 | size_t n; | ||
37 | { | ||
38 | size_t len = strnlen (s, n); | ||
39 | char *nouveau = malloc (len + 1); | ||
40 | |||
41 | if (nouveau == NULL) | ||
42 | return NULL; | ||
43 | |||
44 | nouveau[len] = '\0'; | ||
45 | return (char *) memcpy (nouveau, s, len); | ||
46 | } |
lib/strnlen.c
0 → 100644
1 | /* Find the length of STRING, but scan at most MAXLEN characters. | ||
2 | Copyright (C) 1996, 1997, 1998, 2000, 2001 Free Software Foundation, Inc. | ||
3 | This file is part of the GNU C Library. | ||
4 | |||
5 | The GNU C Library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Library General Public License as | ||
7 | published by the Free Software Foundation; either version 2 of the | ||
8 | License, or (at your option) any later version. | ||
9 | |||
10 | The GNU C Library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Library General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Library General Public | ||
16 | License along with the GNU C Library; see the file COPYING.LIB. If not, | ||
17 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. */ | ||
19 | |||
20 | #include <string.h> | ||
21 | |||
22 | /* Find the length of STRING, but scan at most MAXLEN characters. | ||
23 | If no '\0' terminator is found in that many characters, return MAXLEN. */ | ||
24 | |||
25 | size_t | ||
26 | strnlen (const char *string, size_t maxlen) | ||
27 | { | ||
28 | const char *end = memchr (string, '\0', maxlen); | ||
29 | return end ? (size_t) (end - string) : maxlen; | ||
30 | } |
-
Please register or sign in to post a comment