Back to index

glibc  2.9
tst-sprintf.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 
00005 
00006 int
00007 main (void)
00008 {
00009   char buf[100];
00010   int result = 0;
00011 
00012   if (sprintf (buf, "%.0ls", L"foo") != 0
00013       || strlen (buf) != 0)
00014     {
00015       puts ("sprintf (buf, \"%.0ls\", L\"foo\") produced some output");
00016       result = 1;
00017     }
00018 
00019 #define SIZE (1024*70000)
00020 #define STR(x) #x
00021 
00022   char *dst = malloc (SIZE + 1);
00023 
00024   if (dst == NULL)
00025     {
00026       puts ("memory allocation failure");
00027       result = 1;
00028     }
00029   else
00030     {
00031       sprintf (dst, "%*s", SIZE, "");
00032       if (strnlen (dst, SIZE + 1) != SIZE)
00033        {
00034          puts ("sprintf (dst, \"%*s\", " STR(SIZE) ", \"\") did not produce enough output");
00035          result = 1;
00036        }
00037       free (dst);
00038     }
00039 
00040   if (sprintf (buf, "%1$d%3$.*2$s%4$d", 7, 67108863, "x", 8) != 3
00041       || strcmp (buf, "7x8") != 0)
00042     {
00043       printf ("sprintf (buf, \"%%1$d%%3$.*2$s%%4$d\", 7, 67108863, \"x\", 8) produced `%s' output", buf);
00044       result = 1;
00045     }
00046 
00047   if (sprintf (buf, "%67108863.16\"%d", 7) != 14
00048       || strcmp (buf, "%67108863.16\"7") != 0)
00049     {
00050       printf ("sprintf (buf, \"%%67108863.16\\\"%%d\", 7) produced `%s' output", buf);
00051       result = 1;
00052     }
00053 
00054   if (sprintf (buf, "%*\"%d", 0x3ffffff, 7) != 11
00055       || strcmp (buf, "%67108863\"7") != 0)
00056     {
00057       printf ("sprintf (buf, \"%%*\\\"%%d\", 0x3ffffff, 7) produced `%s' output", buf);
00058       result = 1;
00059     }
00060 
00061   return result;
00062 }