Back to index

glibc  2.9
test-vfprintf.c
Go to the documentation of this file.
00001 /* Tests of *printf for very large strings.
00002    Copyright (C) 2000, 2002, 2003, 2007 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
00005 
00006    The GNU C Library is free software; you can redistribute it and/or
00007    modify it under the terms of the GNU Lesser General Public
00008    License as published by the Free Software Foundation; either
00009    version 2.1 of the License, or (at your option) any later version.
00010 
00011    The GNU C Library is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014    Lesser General Public License for more details.
00015 
00016    You should have received a copy of the GNU Lesser General Public
00017    License along with the GNU C Library; if not, write to the Free
00018    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00019    02111-1307 USA.  */
00020 
00021 #include <locale.h>
00022 #include <mcheck.h>
00023 #include <stdint.h>
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 #include <string.h>
00027 #include <unistd.h>
00028 #include <sys/stat.h>
00029 
00030 
00031 const char *locs[] =
00032 {
00033   "C", "de_DE.ISO-8859-1", "de_DE.UTF-8", "ja_JP.EUC-JP"
00034 };
00035 #define nlocs (sizeof (locs) / sizeof (locs[0]))
00036 
00037 
00038 char large[50000];
00039 
00040 int
00041 main (void)
00042 {
00043   char buf[25];
00044   size_t i;
00045   int res = 0;
00046   int fd;
00047 
00048   mtrace ();
00049 
00050   strcpy (buf, "/tmp/test-vfprintfXXXXXX");
00051   fd = mkstemp (buf);
00052   if (fd == -1)
00053     {
00054       printf ("cannot open temporary file: %m\n");
00055       exit (1);
00056     }
00057   unlink (buf);
00058 
00059   for (i = 0; i < nlocs; ++i)
00060     {
00061       FILE *fp;
00062       struct stat st;
00063       int fd2;
00064 
00065       setlocale (LC_ALL, locs[i]);
00066 
00067       memset (large, '\1', sizeof (large));
00068       large[sizeof (large) - 1] = '\0';
00069 
00070       fd2 = dup (fd);
00071       if (fd2 == -1)
00072        {
00073          printf ("cannot dup for locale %s: %m\n",
00074                 setlocale (LC_ALL, NULL));
00075          exit (1);
00076        }
00077 
00078       if (ftruncate (fd2, 0) != 0)
00079        {
00080          printf ("cannot truncate file for locale %s: %m\n",
00081                 setlocale (LC_ALL, NULL));
00082          exit (1);
00083        }
00084 
00085       fp = fdopen (fd2, "a");
00086       if (fp == NULL)
00087        {
00088          printf ("cannot create FILE for locale %s: %m\n",
00089                 setlocale (LC_ALL, NULL));
00090          exit (1);
00091        }
00092 
00093       fprintf (fp, "%s", large);
00094       fprintf (fp, "%.*s", 30000, large);
00095       large[20000] = '\0';
00096       fprintf (fp, large);
00097       fprintf (fp, "%-1.300000000s", "hello");
00098 
00099       if (fflush (fp) != 0 || ferror (fp) != 0 || fclose (fp) != 0)
00100        {
00101          printf ("write error for locale %s: %m\n",
00102                 setlocale (LC_ALL, NULL));
00103          exit (1);
00104        }
00105 
00106       if (fstat (fd, &st) != 0)
00107        {
00108          printf ("cannot stat for locale %s: %m\n",
00109                 setlocale (LC_ALL, NULL));
00110          exit (1);
00111        }
00112       else if (st.st_size != 50000 + 30000 + 19999 + 5)
00113        {
00114          printf ("file size incorrect for locale %s: %jd instead of %jd\n",
00115                 setlocale (LC_ALL, NULL),
00116                 (intmax_t) st.st_size,
00117                 (intmax_t) 50000 + 30000 + 19999 + 5);
00118          res = 1;
00119        }
00120       else
00121        printf ("locale %s OK\n", setlocale (LC_ALL, NULL));
00122     }
00123 
00124   close (fd);
00125 
00126   return res;
00127 }