Back to index

glibc  2.9
tst-unlockedio.c
Go to the documentation of this file.
00001 /* Test for some *_unlocked stdio interfaces.
00002    Copyright (C) 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2004.
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 <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 
00025 int fd;
00026 static void do_prepare (void);
00027 static int do_test (void);
00028 #define PREPARE(argc, argv) do_prepare ()
00029 #define TEST_FUNCTION do_test ()
00030 #include "../test-skeleton.c"
00031 
00032 static int
00033 do_test (void)
00034 {
00035   const char blah[] = "BLAH";
00036   char buf[strlen (blah) + 1];
00037   FILE *fp, *f;
00038   const char *cp;
00039   char *wp;
00040 
00041   if ((fp = fdopen (fd, "w+")) == NULL)
00042     exit (1);
00043 
00044   flockfile (fp);
00045 
00046   f = fp;
00047   cp = blah;
00048   if (ftello (fp) != 0
00049       || fwrite_unlocked (blah, blah - blah, strlen (blah), f++) != 0
00050       || f != fp + 1
00051       || fwrite_unlocked ("", 5.0, 0, --f) != 0
00052       || f != fp
00053       || fwrite_unlocked (cp++, 16, 0.25, fp) != 0
00054       || cp != blah + 1
00055       || fwrite_unlocked (--cp, 0.25, 16, fp) != 0
00056       || cp != blah
00057       || fwrite_unlocked (blah, 0, -0.0, fp) != 0
00058       || ftello (fp) != 0)
00059     {
00060       puts ("One of fwrite_unlocked tests failed");
00061       exit (1);
00062     }
00063 
00064   if (fwrite_unlocked (blah, 1, strlen (blah) - 2, fp) != strlen (blah) - 2)
00065     {
00066       puts ("Could not write string into file");
00067       exit (1);
00068     }
00069 
00070   if (putc_unlocked ('A' + 0x1000000, fp) != 'A')
00071     {
00072       puts ("putc_unlocked failed");
00073       exit (1);
00074     }
00075 
00076   f = fp;
00077   cp = blah + strlen (blah) - 1;
00078   if (putc_unlocked (*cp++, f++) != 'H'
00079       || f != fp + 1
00080       || cp != strchr (blah, '\0'))
00081     {
00082       puts ("fputc_unlocked failed");
00083       exit (1);
00084     }
00085 
00086   if (ftello (fp) != (off_t) strlen (blah))
00087     {
00088       printf ("Failed to write %zd bytes to temporary file", strlen (blah));
00089       exit (1);
00090     }
00091 
00092   rewind (fp);
00093 
00094   f = fp;
00095   wp = buf;
00096   memset (buf, ' ', sizeof (buf));
00097   if (ftello (fp) != 0
00098       || fread_unlocked (buf, buf - buf, strlen (blah), f++) != 0
00099       || f != fp + 1
00100       || fread_unlocked (buf, 5.0, 0, --f) != 0
00101       || f != fp
00102       || fread_unlocked (wp++, 16, 0.25, fp) != 0
00103       || wp != buf + 1
00104       || fread_unlocked (--wp, 0.25, 16, fp) != 0
00105       || wp != buf
00106       || fread_unlocked (buf, 0, -0.0, fp) != 0
00107       || ftello (fp) != 0
00108       || memcmp (buf, "     ", sizeof (buf)) != 0)
00109     {
00110       puts ("One of fread_unlocked tests failed");
00111       exit (1);
00112     }
00113 
00114   if (fread_unlocked (buf, 1, strlen (blah) - 2, fp) != strlen (blah) - 2)
00115     {
00116       puts ("Could not read string from file");
00117       exit (1);
00118     }
00119 
00120   if (getc_unlocked (fp) != 'A')
00121     {
00122       puts ("getc_unlocked failed");
00123       exit (1);
00124     }
00125 
00126   f = fp;
00127   if (fgetc_unlocked (f++) != 'H'
00128       || f != fp + 1
00129       || fgetc_unlocked (--f) != EOF
00130       || f != fp)
00131     {
00132       puts ("fgetc_unlocked failed");
00133       exit (1);
00134     }
00135 
00136   if (ftello (fp) != (off_t) strlen (blah))
00137     {
00138       printf ("Failed to read %zd bytes from temporary file", strlen (blah));
00139       exit (1);
00140     }
00141 
00142   funlockfile (fp);
00143   fclose (fp);
00144 
00145   return 0;
00146 }
00147 
00148 static void
00149 do_prepare (void)
00150 {
00151   fd = create_temp_file ("tst-unlockedio.", NULL);
00152   if (fd == -1)
00153     {
00154       printf ("cannot create temporary file: %m\n");
00155       exit (1);
00156     }
00157 }