Update.
[glibc.git] / string / stratcliff.c
blobebe565968e129110b2369daeceafe8c3bacef302
1 /* Test for string function add boundaries of usable memory.
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C 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 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #define _GNU_SOURCE 1
22 #include <errno.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/param.h>
29 #ifndef MAX
30 #define MAX(a, b) ((a) > (b) ? (a) : (b))
31 #endif
33 int
34 main (int argc, char *argv[])
36 int size = sysconf (_SC_PAGESIZE);
37 char *adr, *dest;
38 int result = 0;
40 adr = (char *) mmap (NULL, 3 * size, PROT_READ|PROT_WRITE,
41 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
42 dest = (char *) mmap (NULL, 3*size, PROT_READ|PROT_WRITE,
43 MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
44 if (adr == (char *)-1L || dest == (char *)-1L)
46 if (errno == ENOSYS)
47 puts ("No test, mmap not available.");
48 else
50 printf ("mmap failed: %m");
51 result = 1;
54 else
56 int inner, middle, outer;
58 mprotect(adr, size, PROT_NONE);
59 mprotect(adr+2*size, size, PROT_NONE);
60 adr += size;
62 mprotect(dest, size, PROT_NONE);
63 mprotect(dest+2*size, size, PROT_NONE);
64 dest += size;
66 memset (adr, 'T', size);
68 /* strlen test */
69 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
71 for (inner = MAX (outer, size - 64); inner < size; ++inner)
73 adr[inner] = '\0';
75 if (strlen (&adr[outer]) != (size_t) (inner - outer))
77 printf ("strlen flunked for outer = %d, inner = %d\n",
78 outer, inner);
79 result = 1;
82 adr[inner] = 'T';
86 /* strchr test */
87 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
89 for (middle = MAX (outer, size - 64); middle < size; ++middle)
91 for (inner = middle; inner < size; ++inner)
93 char *cp;
94 adr[middle] = 'V';
95 adr[inner] = '\0';
97 cp = strchr (&adr[outer], 'V');
99 if ((inner == middle && cp != NULL)
100 || (inner != middle
101 && (cp - &adr[outer]) != middle - outer))
103 printf ("strchr flunked for outer = %d, middle = %d, "
104 "inner = %d\n", outer, middle, inner);
105 result = 1;
108 adr[inner] = 'T';
109 adr[middle] = 'T';
114 /* strrchr test */
115 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
117 for (middle = MAX (outer, size - 64); middle < size; ++middle)
119 for (inner = middle; inner < size; ++inner)
121 char *cp;
122 adr[middle] = 'V';
123 adr[inner] = '\0';
125 cp = strrchr (&adr[outer], 'V');
127 if ((inner == middle && cp != NULL)
128 || (inner != middle
129 && (cp - &adr[outer]) != middle - outer))
131 printf ("strrchr flunked for outer = %d, middle = %d, "
132 "inner = %d\n", outer, middle, inner);
133 result = 1;
136 adr[inner] = 'T';
137 adr[middle] = 'T';
142 /* strcpy test */
143 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
145 for (inner = MAX (outer, size - 64); inner < size; ++inner)
147 adr[inner] = '\0';
149 if (strcpy (dest, &adr[outer]) != dest
150 || strlen (dest) != (size_t) (inner - outer))
152 printf ("strcpy flunked for outer = %d, inner = %d\n",
153 outer, inner);
154 result = 1;
157 adr[inner] = 'T';
161 /* stpcpy test */
162 for (outer = size - 1; outer >= MAX (0, size - 128); --outer)
164 for (inner = MAX (outer, size - 64); inner < size; ++inner)
166 adr[inner] = '\0';
168 if ((stpcpy (dest, &adr[outer]) - dest) != inner - outer)
170 printf ("stpcpy flunked for outer = %d, inner = %d\n",
171 outer, inner);
172 result = 1;
175 adr[inner] = 'T';
180 return result;