Back to index

glibc  2.9
scanf13.c
Go to the documentation of this file.
00001 #include <locale.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 #include <wchar.h>
00006 
00007 int
00008 main (void)
00009 {
00010   char *sp1, *sp2, *sp3, *sp4;
00011   wchar_t *lsp1, *lsp2, *lsp3, *lsp4;
00012   int result = 0;
00013   char buf[2048+64];
00014   wchar_t wbuf[2048+64];
00015   size_t i;
00016 
00017 #define FAIL() \
00018   do {                                           \
00019     result = 1;                                         \
00020     printf ("test at line %d failed\n", __LINE__);      \
00021   } while (0)
00022 
00023   setlocale (LC_ALL, "de_DE.UTF-8");
00024   if (sscanf ("A  \xc3\x84-\t\t\xc3\x84-abcdefbcd\t\xc3\x84-B",
00025              "A%ms%10ms%4m[bcd]%4mcB", &sp1, &sp2, &sp3, &sp4) != 4)
00026     FAIL ();
00027   else
00028     {
00029       if (strcmp (sp1, "\xc3\x84-") != 0)
00030        FAIL ();
00031       free (sp1);
00032       if (strcmp (sp2, "\xc3\x84-abcdefb") != 0)
00033        FAIL ();
00034       free (sp2);
00035       if (strcmp (sp3, "cd") != 0)
00036        FAIL ();
00037       free (sp3);
00038       if (memcmp (sp4, "\t\xc3\x84-", 4) != 0)
00039        FAIL ();
00040       free (sp4);
00041     }
00042 
00043   if (sscanf ("A  \xc3\x84-\t\t\xc3\x84-abcdefbcd\t\xc3\x84-BB",
00044              "A%mS%10mls%4ml[bcd]%4mCB", &lsp1, &lsp2, &lsp3, &lsp4) != 4)
00045     FAIL ();
00046   else
00047     {
00048       if (wcscmp (lsp1, L"\xc4-") != 0)
00049        FAIL ();
00050       free (lsp1);
00051       if (wcscmp (lsp2, L"\xc4-abcdefbc") != 0)
00052        FAIL ();
00053       free (lsp2);
00054       if (wcscmp (lsp3, L"d") != 0)
00055        FAIL ();
00056       free (lsp3);
00057       if (memcmp (lsp4, L"\t\xc4-B", 4 * sizeof (wchar_t)) != 0)
00058        FAIL ();
00059       free (lsp4);
00060     }
00061 
00062   memset (buf, '/', sizeof (buf));
00063   buf[0] = '\t';
00064   buf[1] = ' ';
00065   buf[2] = 0xc3;
00066   buf[3] = 0x84;
00067   buf[2048] = 0xc3;
00068   buf[2049] = 0x84;
00069   buf[2058] = '\t';
00070   buf[2059] = 'a';
00071   if (sscanf (buf, "%ms%mc", &sp1, &sp2) != 2)
00072     FAIL ();
00073   else
00074     {
00075       if (sp1[0] != '\xc3' || sp1[1] != '\x84'
00076          || sp1[2046] != '\xc3' || sp1[2047] != '\x84'
00077          || sp1[2056] != '\0')
00078        FAIL ();
00079       sp1[2046] = '/';
00080       sp1[2047] = '/';
00081       for (i = 2; i < 2056; i++)
00082        if (sp1[i] != '/')
00083          FAIL ();
00084       free (sp1);
00085       if (sp2[0] != '\t')
00086        FAIL ();
00087       free (sp2);
00088     }
00089   if (sscanf (buf, "%2048ms%mc", &sp3, &sp4) != 2)
00090     FAIL ();
00091   else
00092     {
00093       if (sp3[0] != '\xc3' || sp3[1] != '\x84'
00094          || sp3[2046] != '\xc3' || sp3[2047] != '\x84'
00095          || sp3[2048] != '\0')
00096        FAIL ();
00097       for (i = 2; i < 2046; i++)
00098        if (sp3[i] != '/')
00099          FAIL ();
00100       free (sp3);
00101       if (sp4[0] != '/')
00102        FAIL ();
00103       free (sp4);
00104     }
00105   if (sscanf (buf, "%4mc%1500m[dr/]%548m[abc/d]%3mc", &sp1, &sp2, &sp3, &sp4)
00106       != 4)
00107     FAIL ();
00108   else
00109     {
00110       if (memcmp (sp1, "\t \xc3\x84", 4) != 0)
00111        FAIL ();
00112       free (sp1);
00113       for (i = 0; i < 1500; i++)
00114        if (sp2[i] != '/')
00115          FAIL ();
00116       if (sp2[1500] != '\0')
00117        FAIL ();
00118       free (sp2);
00119       for (i = 0; i < 544; i++)
00120        if (sp3[i] != '/')
00121          FAIL ();
00122       if (sp3[544] != '\0')
00123        FAIL ();
00124       free (sp3);
00125       if (memcmp (sp4, "\xc3\x84/", 3) != 0)
00126        FAIL ();
00127       free (sp4);
00128     }
00129   if (sscanf (buf, "%mS%mC", &lsp1, &lsp2) != 2)
00130     FAIL ();
00131   else
00132     {
00133       if (lsp1[0] != L'\xc4' || lsp1[2045] != L'\xc4'
00134          || lsp1[2054] != L'\0')
00135        FAIL ();
00136       lsp1[2045] = L'/';
00137       for (i = 1; i < 2054; i++)
00138        if (lsp1[i] != L'/')
00139          FAIL ();
00140       free (lsp1);
00141       if (lsp2[0] != L'\t')
00142        FAIL ();
00143       free (lsp2);
00144     }
00145   if (sscanf (buf, "%2048mls%mlc", &lsp3, &lsp4) != 2)
00146     FAIL ();
00147   else
00148     {
00149       if (lsp3[0] != L'\xc4' || lsp3[2045] != L'\xc4'
00150          || lsp3[2048] != L'\0')
00151        FAIL ();
00152       lsp3[2045] = L'/';
00153       for (i = 1; i < 2048; i++)
00154        if (lsp3[i] != L'/')
00155          FAIL ();
00156       free (lsp3);
00157       if (lsp4[0] != L'/')
00158        FAIL ();
00159       free (lsp4);
00160     }
00161   if (sscanf (buf, "%4mC%1500ml[dr/]%548ml[abc/d]%3mlc",
00162              &lsp1, &lsp2, &lsp3, &lsp4) != 4)
00163     FAIL ();
00164   else
00165     {
00166       if (memcmp (lsp1, L"\t \xc4/", 4 * sizeof (wchar_t)) != 0)
00167        FAIL ();
00168       free (lsp1);
00169       for (i = 0; i < 1500; i++)
00170        if (lsp2[i] != L'/')
00171          FAIL ();
00172       if (lsp2[1500] != L'\0')
00173        FAIL ();
00174       free (lsp2);
00175       for (i = 0; i < 543; i++)
00176        if (lsp3[i] != L'/')
00177          FAIL ();
00178       if (lsp3[543] != L'\0')
00179        FAIL ();
00180       free (lsp3);
00181       if (memcmp (lsp4, L"\xc4//", 3 * sizeof (wchar_t)) != 0)
00182        FAIL ();
00183       free (lsp4);
00184     }
00185 
00186   return result;
00187 }