Back to index

glibc  2.9
tst-perror.c
Go to the documentation of this file.
00001 /* Test of perror.
00002    Contributed by Ulrich Drepper <drepper@redhat.com>, 2001.
00003    To be used only for testing glibc.  */
00004 
00005 #include <errno.h>
00006 #include <error.h>
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <string.h>
00010 #include <unistd.h>
00011 #include <wchar.h>
00012 
00013 
00014 #define MB_EXP \
00015   "null mode test 1: Invalid or incomplete multibyte or wide character\n" \
00016   "multibyte string\n" \
00017   "<0 mode test: Invalid argument\n"
00018 #define MB_EXP_LEN (sizeof (MB_EXP) - 1)
00019 
00020 #define WC_EXP \
00021   "null mode test 2: Invalid or incomplete multibyte or wide character\n" \
00022   "wide string\n" \
00023   ">0 mode test: Invalid argument\n"
00024 #define WC_EXP_LEN (sizeof (WC_EXP) - 1)
00025 
00026 
00027 int
00028 main (void)
00029 {
00030   int fd;
00031   char fname[] = "/tmp/tst-perror.XXXXXX";
00032   int result = 0;
00033   char buf[200];
00034   ssize_t n;
00035 
00036   fd = mkstemp (fname);
00037   if (fd == -1)
00038     error (EXIT_FAILURE, errno, "cannot create temporary file");
00039 
00040   /* Make sure the file gets removed.  */
00041   unlink (fname);
00042 
00043   fclose (stderr);
00044 
00045   if (dup2 (fd, 2) == -1)
00046     {
00047       printf ("cannot create file descriptor 2: %m\n");
00048       exit (EXIT_FAILURE);
00049     }
00050 
00051   stderr = fdopen (2, "w");
00052   if (stderr == NULL)
00053     {
00054       printf ("fdopen failed: %m\n");
00055       exit (EXIT_FAILURE);
00056     }
00057 
00058   if (fwide (stderr, 0) != 0)
00059     {
00060       printf ("stderr not initially in mode 0\n");
00061       exit (EXIT_FAILURE);
00062     }
00063 
00064   errno = EILSEQ;
00065   perror ("null mode test 1");
00066 
00067   if (fwide (stderr, 0) != 0)
00068     {
00069       puts ("perror changed the mode from 0");
00070       result = 1;
00071     }
00072 
00073   fputs ("multibyte string\n", stderr);
00074 
00075   if (fwide (stderr, 0) >= 0)
00076     {
00077       puts ("fputs didn't set orientation to narrow");
00078       result = 1;
00079     }
00080 
00081   errno = EINVAL;
00082   perror ("<0 mode test");
00083 
00084   fclose (stderr);
00085 
00086   lseek (fd, 0, SEEK_SET);
00087   n = read (fd, buf, sizeof (buf));
00088   if (n != MB_EXP_LEN || memcmp (buf, MB_EXP, MB_EXP_LEN) != 0)
00089     {
00090       printf ("multibyte test failed.  Expected:\n%s\nGot:\n%.*s\n",
00091              MB_EXP, (int) n, buf);
00092       result = 1;
00093     }
00094   else
00095     puts ("multibyte test succeeded");
00096 
00097   lseek (fd, 0, SEEK_SET);
00098   ftruncate (fd, 0);
00099 
00100   if (dup2 (fd, 2) == -1)
00101     {
00102       printf ("cannot create file descriptor 2: %m\n");
00103       exit (EXIT_FAILURE);
00104     }
00105   stderr = fdopen (2, "w");
00106   if (stderr == NULL)
00107     {
00108       printf ("fdopen failed: %m\n");
00109       exit (EXIT_FAILURE);
00110     }
00111 
00112   if (fwide (stderr, 0) != 0)
00113     {
00114       printf ("stderr not initially in mode 0\n");
00115       exit (EXIT_FAILURE);
00116     }
00117 
00118   errno = EILSEQ;
00119   perror ("null mode test 2");
00120 
00121   if (fwide (stderr, 0) != 0)
00122     {
00123       puts ("perror changed the mode from 0");
00124       result = 1;
00125     }
00126 
00127   fputws (L"wide string\n", stderr);
00128 
00129   if (fwide (stderr, 0) <= 0)
00130     {
00131       puts ("fputws didn't set orientation to wide");
00132       result = 1;
00133     }
00134 
00135   errno = EINVAL;
00136   perror (">0 mode test");
00137 
00138   fclose (stderr);
00139 
00140   lseek (fd, 0, SEEK_SET);
00141   n = read (fd, buf, sizeof (buf));
00142   if (n != WC_EXP_LEN || memcmp (buf, WC_EXP, WC_EXP_LEN) != 0)
00143     {
00144       printf ("wide test failed.  Expected:\n%s\nGot:\n%.*s\n",
00145              WC_EXP, (int) n, buf);
00146       result = 1;
00147     }
00148   else
00149     puts ("wide test succeeded");
00150 
00151   close (fd);
00152 
00153   return result;
00154 }