Back to index

glibc  2.9
tst-freopen.c
Go to the documentation of this file.
00001 /* Test freopen with 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
00027 main (void)
00028 {
00029   char name[] = "/tmp/tst-freopen.XXXXXX";
00030   const char * const test = "Let's test freopen.\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, "r");
00052   if (f == NULL)
00053     {
00054       printf ("%u: cannot fopen temporary file: %m\n", __LINE__);
00055       exit (1);
00056     }
00057 
00058   if (fread (temp, 1, strlen (test), f) != strlen (test))
00059     {
00060       printf ("%u: couldn't read the file back: %m\n", __LINE__);
00061       exit (1);
00062     }
00063   temp [strlen (test)] = '\0';
00064 
00065   if (strcmp (test, temp))
00066     {
00067       printf ("%u: read different string than was written:\n%s%s",
00068              __LINE__, test, temp);
00069       exit (1);
00070     }
00071 
00072   f = freopen (name, "r+", f);
00073   if (f == NULL)
00074     {
00075       printf ("%u: cannot freopen temporary file: %m\n", __LINE__);
00076       exit (1);
00077     }
00078 
00079   if (fseek (f, 0, SEEK_SET) != 0)
00080     {
00081       printf ("%u: couldn't fseek to start: %m\n", __LINE__);
00082       exit (1);
00083     }
00084 
00085   if (fread (temp, 1, strlen (test), f) != strlen (test))
00086     {
00087       printf ("%u: couldn't read the file back: %m\n", __LINE__);
00088       exit (1);
00089     }
00090   temp [strlen (test)] = '\0';
00091 
00092   if (strcmp (test, temp))
00093     {
00094       printf ("%u: read different string than was written:\n%s%s",
00095              __LINE__, test, temp);
00096       exit (1);
00097     }
00098 
00099   fclose (f);
00100 
00101   unlink (name);
00102   exit (0);
00103 }