]> git.ipfire.org Git - thirdparty/glibc.git/blame - posix/tst-regex.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / posix / tst-regex.c
CommitLineData
b168057a 1/* Copyright (C) 2001-2015 Free Software Foundation, Inc.
d8f00d46
UD
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
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
d8f00d46
UD
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
41bdb6e2 12 Lesser General Public License for more details.
d8f00d46 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6
PE
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
d8f00d46
UD
17
18#include <spawn.h>
19#include "spawn_int.h"
20
21#include <assert.h>
22#include <errno.h>
23#include <error.h>
24#include <fcntl.h>
bb3f4825 25#include <getopt.h>
d8f00d46
UD
26#include <iconv.h>
27#include <locale.h>
28#include <mcheck.h>
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <time.h>
33#include <unistd.h>
34#include <sys/stat.h>
35#include <sys/types.h>
36#include <regex.h>
37
38
bf7c04cd 39#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
d8f00d46
UD
40static clockid_t cl;
41static int use_clock;
42#endif
73111f03
UD
43static iconv_t cd;
44static char *mem;
45static char *umem;
46static size_t memlen;
3c0fb574 47static size_t umemlen;
bb3f4825 48static int timing;
d8f00d46 49
3c0fb574 50static int test_expr (const char *expr, int expected, int expectedicase);
73111f03 51static int run_test (const char *expr, const char *mem, size_t memlen,
3c0fb574
UD
52 int icase, int expected);
53static int run_test_backwards (const char *expr, const char *mem,
54 size_t memlen, int icase, int expected);
d8f00d46
UD
55
56
7166d23f
UD
57static int
58do_test (void)
d8f00d46
UD
59{
60 const char *file;
d8f00d46
UD
61 int fd;
62 struct stat st;
d8f00d46
UD
63 int result;
64 char *inmem;
65 char *outmem;
66 size_t inlen;
67 size_t outlen;
d8f00d46
UD
68
69 mtrace ();
70
71 /* Make the content of the file available in memory. */
72 file = "../ChangeLog.8";
73 fd = open (file, O_RDONLY);
74 if (fd == -1)
75 error (EXIT_FAILURE, errno, "cannot open %s", basename (file));
76
77 if (fstat (fd, &st) != 0)
78 error (EXIT_FAILURE, errno, "cannot stat %s", basename (file));
79 memlen = st.st_size;
80
81 mem = (char *) malloc (memlen + 1);
82 if (mem == NULL)
83 error (EXIT_FAILURE, errno, "while allocating buffer");
84
3c0fb574 85 if ((size_t) read (fd, mem, memlen) != memlen)
d8f00d46
UD
86 error (EXIT_FAILURE, 0, "cannot read entire file");
87 mem[memlen] = '\0';
88
89 close (fd);
90
2f07975d 91 /* We have to convert a few things from Latin-1 to UTF-8. */
d8f00d46
UD
92 cd = iconv_open ("UTF-8", "ISO-8859-1");
93 if (cd == (iconv_t) -1)
94 error (EXIT_FAILURE, errno, "cannot get conversion descriptor");
95
2f07975d
UD
96 /* For the second test we have to convert the file content to UTF-8.
97 Since the text is mostly ASCII it should be enough to allocate
98 twice as much memory for the UTF-8 text than for the Latin-1
99 text. */
100 umem = (char *) calloc (2, memlen);
101 if (umem == NULL)
102 error (EXIT_FAILURE, errno, "while allocating buffer");
103
d8f00d46
UD
104 inmem = mem;
105 inlen = memlen;
106 outmem = umem;
2f07975d 107 outlen = 2 * memlen - 1;
d8f00d46 108 iconv (cd, &inmem, &inlen, &outmem, &outlen);
3c0fb574 109 umemlen = outmem - umem;
d8f00d46
UD
110 if (inlen != 0)
111 error (EXIT_FAILURE, errno, "cannot convert buffer");
112
bf7c04cd
UD
113#if defined _POSIX_CPUTIME && _POSIX_CPUTIME >= 0
114# if _POSIX_CPUTIME == 0
115 if (sysconf (_SC_CPUTIME) < 0)
116 use_clock = 0;
117 else
118# endif
119 /* See whether we can use the CPU clock. */
120 use_clock = clock_getcpuclockid (0, &cl) == 0;
73111f03
UD
121#endif
122
2f07975d
UD
123#ifdef DEBUG
124 re_set_syntax (RE_DEBUG);
125#endif
126
73111f03
UD
127 /* Run the actual tests. All tests are run in a single-byte and a
128 multi-byte locale. */
3c0fb574
UD
129