Back to index

glibc  2.9
tst-mmap-setvbuf.c
Go to the documentation of this file.
00001 /* Test setvbuf on readonly fopen (using mmap stdio).
00002    Copyright (C) 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Jakub Jelinek <jakub@redhat.com>, 2002.
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 <stdio.h>
00022 #include <stdlib.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025 
00026 int main (void)
00027 {
00028   char name[] = "/tmp/tst-mmap-setvbuf.XXXXXX";
00029   char buf[4096];
00030   const char * const test = "Let's see if mmap stdio works with setvbuf.\n";
00031   char temp[strlen (test) + 1];
00032   int fd = mkstemp (name);
00033   FILE *f;
00034 
00035   if (fd == -1)
00036     {
00037       printf ("%u: cannot open temporary file: %m\n", __LINE__);
00038       exit (1);
00039     }
00040 
00041   f = fdopen (fd, "w");
00042   if (f == NULL)
00043     {
00044       printf ("%u: cannot fdopen temporary file: %m\n", __LINE__);
00045       exit (1);
00046     }
00047 
00048   fputs (test, f);
00049   fclose (f);
00050 
00051   f = fopen (name, "rm");
00052   if (f == NULL)
00053     {
00054       printf ("%u: cannot fopen temporary file: %m\n", __LINE__);
00055       exit (1);
00056     }
00057 
00058   if (setvbuf (f, buf, _IOFBF, sizeof buf))
00059     {
00060       printf ("%u: setvbuf failed: %m\n", __LINE__);
00061       exit (1);
00062     }
00063 
00064   if (fread (temp, 1, strlen (test), f) != strlen (test))
00065     {
00066       printf ("%u: couldn't read the file back: %m\n", __LINE__);
00067       exit (1);
00068     }
00069   temp [strlen (test)] = '\0';
00070 
00071   if (strcmp (test, temp))
00072     {
00073       printf ("%u: read different string than was written:\n%s%s",
00074              __LINE__, test, temp);
00075       exit (1);
00076     }
00077 
00078   fclose (f);
00079 
00080   unlink (name);
00081   exit (0);
00082 }