(root)/
findutils-4.9.0/
gnulib-tests/
test-fpurge.c
       1  /* Test of fpurge() function.
       2     Copyright (C) 2007-2022 Free Software Foundation, Inc.
       3  
       4     This program is free software: you can redistribute it and/or modify
       5     it under the terms of the GNU General Public License as published by
       6     the Free Software Foundation, either version 3 of the License, or
       7     (at your option) any later version.
       8  
       9     This program is distributed in the hope that it will be useful,
      10     but WITHOUT ANY WARRANTY; without even the implied warranty of
      11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12     GNU General Public License for more details.
      13  
      14     You should have received a copy of the GNU General Public License
      15     along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
      16  
      17  /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
      18  
      19  #include <config.h>
      20  
      21  /* None of the files accessed by this test are large, so disable the
      22     fseek link warning if we are not using the gnulib fseek module.  */
      23  #define _GL_NO_LARGE_FILES
      24  #include <stdio.h>
      25  
      26  #include <string.h>
      27  
      28  #include "macros.h"
      29  
      30  #define TESTFILE "t-fpurge.tmp"
      31  
      32  int
      33  main (void)
      34  {
      35    int check_filepos;
      36  
      37    for (check_filepos = 0; check_filepos <= 1; check_filepos++)
      38      {
      39        FILE *fp;
      40  
      41        /* Create a file with some contents.  */
      42        fp = fopen (TESTFILE, "w");
      43        if (fp == NULL)
      44          goto skip;
      45        if (fwrite ("foobarsh", 1, 8, fp) < 8)
      46          goto skip;
      47        if (fclose (fp))
      48          goto skip;
      49  
      50        /* The file's contents is now "foobarsh".  */
      51  
      52        /* Open it in read-write mode.  */
      53        fp = fopen (TESTFILE, "r+");
      54        if (fp == NULL)
      55          goto skip;
      56        if (fseek (fp, 3, SEEK_CUR))
      57          goto skip;
      58        if (fwrite ("g", 1, 1, fp) < 1)
      59          goto skip;
      60        if (fflush (fp))
      61          goto skip;
      62        if (fwrite ("bz", 1, 2, fp) < 2)
      63          goto skip;
      64        /* Discard pending write.  */
      65        ASSERT (fpurge (fp) == 0);
      66        /* Verify that when discarding pending output, the file position is set
      67           back to where it was before the write calls.  */
      68        if (check_filepos)
      69          ASSERT (ftell (fp) == 4);
      70        ASSERT (fclose (fp) == 0);
      71  
      72        /* Open it in read-only mode.  */
      73        fp = fopen (TESTFILE, "r");
      74        if (fp == NULL)
      75          goto skip;
      76        /* Verify that the pending writes before the fpurge were really
      77           discarded.  */
      78        {
      79          char buf[8];
      80          if (fread (buf, 1, 7, fp) < 7)
      81            goto skip;
      82          ASSERT (memcmp (buf, "foogars", 7) == 0);
      83        }
      84        /* Discard the buffered 'h'.  */
      85        if (check_filepos)
      86          ASSERT (ftell (fp) == 7);
      87        ASSERT (fpurge (fp) == 0);
      88        /* Verify that when discarding pending input, the file position is
      89           advanced to match the end of the previously read input.  */
      90        if (check_filepos)
      91          ASSERT (ftell (fp) == 8);
      92        ASSERT (getc (fp) == EOF);
      93        ASSERT (fclose (fp) == 0);
      94  
      95        /* The file's contents is now "foogarsh".  */
      96  
      97        /* Ensure that purging a read does not corrupt subsequent writes.  */
      98        fp = fopen (TESTFILE, "r+");
      99        if (fp == NULL)
     100          goto skip;
     101        if (fseek (fp, -1, SEEK_END))
     102          goto skip;
     103        ASSERT (getc (fp) == 'h');
     104        ASSERT (getc (fp) == EOF);
     105        if (check_filepos)
     106          ASSERT (ftell (fp) == 8);
     107        ASSERT (fpurge (fp) == 0);
     108        if (check_filepos)
     109          ASSERT (ftell (fp) == 8);
     110        ASSERT (putc ('!', fp) == '!');
     111        if (check_filepos)
     112          ASSERT (ftell (fp) == 9);
     113        ASSERT (fclose (fp) == 0);
     114        fp = fopen (TESTFILE, "r");
     115        if (fp == NULL)
     116          goto skip;
     117        {
     118          char buf[10];
     119          ASSERT (fread (buf, 1, 10, fp) == 9);
     120          ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
     121        }
     122        ASSERT (fclose (fp) == 0);
     123  
     124        /* The file's contents is now "foogarsh!".  */
     125      }
     126  
     127    remove (TESTFILE);
     128    return 0;
     129  
     130   skip:
     131    fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");
     132    remove (TESTFILE);
     133    return 77;
     134  }