initial check-in, version 0.5.0
[musl] / src / regex / regerror.c
1 /*
2   regerror.c - POSIX regerror() implementation for TRE.
3
4   Copyright (c) 2001-2006 Ville Laurikari <vl@iki.fi>.
5
6   This library is free software; you can redistribute it and/or
7   modify it under the terms of the GNU Lesser General Public
8   License as published by the Free Software Foundation; either
9   version 2.1 of the License, or (at your option) any later version.
10
11   This library 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 GNU
14   Lesser General Public License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with this library; if not, write to the Free Software
18   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
20 */
21
22 #include <string.h>
23 #include <regex.h>
24
25 /* Error message strings for error codes listed in `regex.h'.  This list
26    needs to be in sync with the codes listed there, naturally. */
27
28 /* Converted to single string by Rich Felker to remove the need for
29  * data relocations at runtime, 27 Feb 2006. */
30
31 static const char tre_error_messages[] = {
32   "No error\0"
33   "No match\0"
34   "Invalid regexp\0"
35   "Unknown collating element\0"
36   "Unknown character class name\0"
37   "Trailing backslash\0"
38   "Invalid back reference\0"
39   "Missing ']'\0"
40   "Missing ')'\0"
41   "Missing '}'\0"
42   "Invalid contents of {}\0"
43   "Invalid character range\0"
44   "Out of memory\0"
45   "XXX\0"
46 };
47
48 size_t
49 regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size)
50 {
51   const char *err;
52   size_t err_len;
53
54   if (errcode >= 0 && errcode <= REG_BADRPT)
55     for (err=tre_error_messages; errcode; errcode--, err+=strlen(err)+1);
56   else
57     err = "Unknown error";
58
59   err_len = strlen(err) + 1;
60   if (errbuf_size > 0 && errbuf != NULL)
61     {
62       if (err_len > errbuf_size)
63         {
64           memcpy(errbuf, err, errbuf_size - 1);
65           errbuf[errbuf_size - 1] = '\0';
66         }
67       else
68         {
69           strcpy(errbuf, err);
70         }
71     }
72   return err_len;
73 }
74
75 /* EOF */