Back to index

glibc  2.9
tst_wprintf2.c
Go to the documentation of this file.
00001 /* Test case by Yoshito Kawada <KAWADA@jp.ibm.com>.  */
00002 #include <errno.h>
00003 #include <error.h>
00004 #include <fcntl.h>
00005 #include <locale.h>
00006 #include <stdio.h>
00007 #include <stdlib.h>
00008 #include <string.h>
00009 #include <unistd.h>
00010 #include <wchar.h>
00011 
00012 int
00013 main (int argc, char *argv[])
00014 {
00015   int a = 3;
00016   int fd;
00017   char name[] = "/tmp/wprintf.out.XXXXXX";
00018   FILE *fp;
00019   char buf[100];
00020   size_t len;
00021   int res = 0;
00022 
00023   fd = mkstemp (name);
00024   if (fd == -1)
00025     error (EXIT_FAILURE, errno, "cannot open temporary file");
00026 
00027   unlink (name);
00028 
00029   setlocale (LC_ALL, "");
00030 
00031   fp = fdopen (dup (fd), "w");
00032   if (fp == NULL)
00033     error (EXIT_FAILURE, errno, "fdopen(,\"w\")");
00034 
00035   fwprintf (fp, L"test start");
00036   fwprintf (fp, L" int %d\n", a);
00037 
00038   /* String with precision.  */
00039   fwprintf (fp, L"1[%6.3s]\n", argv[1]);
00040 
00041   fclose (fp);
00042 
00043   fp = fdopen (dup (fd), "a");
00044   if (fp == NULL)
00045     error (EXIT_FAILURE, errno, "fdopen(,\"a\")");
00046 
00047   setvbuf (fp, NULL, _IONBF, 0);
00048 
00049   /* fwprintf to unbuffered stream.   */
00050   fwprintf (fp, L"hello.\n");
00051 
00052   fclose (fp);
00053 
00054 
00055   /* Now read it back in.  This time using multibyte functions.  */
00056   lseek (fd, SEEK_SET, 0);
00057   fp = fdopen (fd, "r");
00058   if (fp == NULL)
00059     error (EXIT_FAILURE, errno, "fdopen(,\"r\")");
00060 
00061   if (fgets (buf, sizeof buf, fp) != buf)
00062     error (EXIT_FAILURE, errno, "first fgets");
00063   len = strlen (buf);
00064   if (buf[len - 1] == '\n')
00065     --len;
00066   else
00067     {
00068       puts ("newline missing after first line");
00069       res = 1;
00070     }
00071   printf ("1st line: \"%.*s\" -> %s\n", (int) len, buf,
00072          strncmp (buf, "test start int 3", len) == 0 ? "OK" : "FAIL");
00073   res |= strncmp (buf, "test start int 3", len) != 0;
00074 
00075   if (fgets (buf, sizeof buf, fp) != buf)
00076     error (EXIT_FAILURE, errno, "second fgets");
00077   len = strlen (buf);
00078   if (buf[len - 1] == '\n')
00079     --len;
00080   else
00081     {
00082       puts ("newline missing after second line");
00083       res = 1;
00084     }
00085   printf ("2nd line: \"%.*s\" -> %s\n", (int) len, buf,
00086          strncmp (buf, "1[   Som]", len) == 0 ? "OK" : "FAIL");
00087   res |= strncmp (buf, "1[   Som]", len) != 0;
00088 
00089   if (fgets (buf, sizeof buf, fp) != buf)
00090     error (EXIT_FAILURE, errno, "third fgets");
00091   len = strlen (buf);
00092   if (buf[len - 1] == '\n')
00093     --len;
00094   else
00095     {
00096       puts ("newline missing after third line");
00097       res = 1;
00098     }
00099   printf ("3rd line: \"%.*s\" -> %s\n", (int) len, buf,
00100          strncmp (buf, "hello.", len) == 0 ? "OK" : "FAIL");
00101   res |= strncmp (buf, "hello.", len) != 0;
00102 
00103   return res;
00104 }