| 1 | /* Tests of *printf for very large strings.
|
|---|
| 2 | Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
|
|---|
| 3 | This file is part of the GNU C Library.
|
|---|
| 4 | Contributed by Ulrich Drepper <[email protected]>, 2000.
|
|---|
| 5 |
|
|---|
| 6 | The GNU C 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 | 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 | 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 the GNU C Library; if not, write to the Free
|
|---|
| 18 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|---|
| 19 | 02111-1307 USA. */
|
|---|
| 20 |
|
|---|
| 21 | #include <locale.h>
|
|---|
| 22 | #include <mcheck.h>
|
|---|
| 23 | #include <stdint.h>
|
|---|
| 24 | #include <stdio.h>
|
|---|
| 25 | #include <stdlib.h>
|
|---|
| 26 | #include <string.h>
|
|---|
| 27 | #include <unistd.h>
|
|---|
| 28 | #include <sys/stat.h>
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | const char *locs[] =
|
|---|
| 32 | {
|
|---|
| 33 | "C", "de_DE.ISO-8859-1", "de_DE.UTF-8", "ja_JP.EUC-JP"
|
|---|
| 34 | };
|
|---|
| 35 | #define nlocs (sizeof (locs) / sizeof (locs[0]))
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | char large[50000];
|
|---|
| 39 |
|
|---|
| 40 | int
|
|---|
| 41 | main (void)
|
|---|
| 42 | {
|
|---|
| 43 | char buf[25];
|
|---|
| 44 | size_t i;
|
|---|
| 45 | int res = 0;
|
|---|
| 46 | int fd;
|
|---|
| 47 |
|
|---|
| 48 | mtrace ();
|
|---|
| 49 |
|
|---|
| 50 | strcpy (buf, "/tmp/test-vfprintfXXXXXX");
|
|---|
| 51 | fd = mkstemp (buf);
|
|---|
| 52 | if (fd == -1)
|
|---|
| 53 | {
|
|---|
| 54 | printf ("cannot open temporary file: %m\n");
|
|---|
| 55 | exit (1);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | for (i = 0; i < nlocs; ++i)
|
|---|
| 59 | {
|
|---|
| 60 | FILE *fp;
|
|---|
| 61 | struct stat st;
|
|---|
| 62 | int fd2;
|
|---|
| 63 |
|
|---|
| 64 | setlocale (LC_ALL, locs[i]);
|
|---|
| 65 |
|
|---|
| 66 | memset (large, '\1', sizeof (large));
|
|---|
| 67 | large[sizeof (large) - 1] = '\0';
|
|---|
| 68 |
|
|---|
| 69 | fd2 = dup (fd);
|
|---|
| 70 | if (fd2 == -1)
|
|---|
| 71 | {
|
|---|
| 72 | printf ("cannot dup for locale %s: %m\n",
|
|---|
| 73 | setlocale (LC_ALL, NULL));
|
|---|
| 74 | exit (1);
|
|---|
| 75 | }
|
|---|
| 76 | if (ftruncate (fd2, 0) != 0)
|
|---|
| 77 | {
|
|---|
| 78 | printf ("cannot truncate file for locale %s: %m\n",
|
|---|
| 79 | setlocale (LC_ALL, NULL));
|
|---|
| 80 | exit (1);
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | fp = fdopen (fd2, "a");
|
|---|
| 84 | if (fp == NULL)
|
|---|
| 85 | {
|
|---|
| 86 | printf ("cannot create FILE for locale %s: %m\n",
|
|---|
| 87 | setlocale (LC_ALL, NULL));
|
|---|
| 88 | exit (1);
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | fprintf (fp, "%s", large);
|
|---|
| 92 | fprintf (fp, "%.*s", 30000, large);
|
|---|
| 93 | large[20000] = '\0';
|
|---|
| 94 | fprintf (fp, large);
|
|---|
| 95 |
|
|---|
| 96 | if (fflush (fp) != 0 || ferror (fp) != 0 || fclose (fp) != 0)
|
|---|
| 97 | {
|
|---|
| 98 | printf ("write error for locale %s: %m\n",
|
|---|
| 99 | setlocale (LC_ALL, NULL));
|
|---|
| 100 | exit (1);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | if (fstat (fd, &st) != 0)
|
|---|
| 104 | {
|
|---|
| 105 | printf ("cannot stat for locale %s: %m\n",
|
|---|
| 106 | setlocale (LC_ALL, NULL));
|
|---|
| 107 | exit (1);
|
|---|
| 108 | }
|
|---|
| 109 | else if (st.st_size != 99999)
|
|---|
| 110 | {
|
|---|
| 111 | printf ("file size incorrect for locale %s: %jd instead of %jd\n",
|
|---|
| 112 | setlocale (LC_ALL, NULL),
|
|---|
| 113 | (intmax_t) st.st_size, (intmax_t) 99999);
|
|---|
| 114 | res = 1;
|
|---|
| 115 | }
|
|---|
| 116 | else
|
|---|
| 117 | printf ("locale %s OK\n", setlocale (LC_ALL, NULL));
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | #ifdef __EMX__
|
|---|
| 121 | close (fd);
|
|---|
| 122 | unlink (buf);
|
|---|
| 123 | #else
|
|---|
| 124 | unlink (buf);
|
|---|
| 125 | close (fd);
|
|---|
| 126 | #endif
|
|---|
| 127 | return res;
|
|---|
| 128 | }
|
|---|