Blame gnulib-tests/test-fseeko.c

Packit 9e4112
/* Test of fseeko() function.
Packit 9e4112
   Copyright (C) 2007-2018 Free Software Foundation, Inc.
Packit 9e4112
Packit 9e4112
   This program is free software: you can redistribute it and/or modify
Packit 9e4112
   it under the terms of the GNU General Public License as published by
Packit 9e4112
   the Free Software Foundation; either version 3 of the License, or
Packit 9e4112
   (at your option) any later version.
Packit 9e4112
Packit 9e4112
   This program is distributed in the hope that it will be useful,
Packit 9e4112
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 9e4112
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 9e4112
   GNU General Public License for more details.
Packit 9e4112
Packit 9e4112
   You should have received a copy of the GNU General Public License
Packit 9e4112
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 9e4112
Packit 9e4112
/* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
Packit 9e4112
Packit 9e4112
#include <config.h>
Packit 9e4112
Packit 9e4112
/* None of the files accessed by this test are large, so disable the
Packit 9e4112
   fseek link warning if we are not using the gnulib fseek module.  */
Packit 9e4112
#define _GL_NO_LARGE_FILES
Packit 9e4112
#include <stdio.h>
Packit 9e4112
Packit 9e4112
#include "signature.h"
Packit 9e4112
SIGNATURE_CHECK (fseeko, int, (FILE *, off_t, int));
Packit 9e4112
Packit 9e4112
Packit 9e4112
#include "macros.h"
Packit 9e4112
Packit 9e4112
#ifndef FUNC_UNGETC_BROKEN
Packit 9e4112
# define FUNC_UNGETC_BROKEN 0
Packit 9e4112
#endif
Packit 9e4112
Packit 9e4112
int
Packit 9e4112
main (int argc, char **argv _GL_UNUSED)
Packit 9e4112
{
Packit 9e4112
  /* Assume stdin is non-empty, seekable, and starts with '#!/bin/sh'
Packit 9e4112
     iff argc > 1.  */
Packit 9e4112
  int expected = argc > 1 ? 0 : -1;
Packit 9e4112
  /* Exit with success only if fseek/fseeko agree.  */
Packit 9e4112
  int r1 = fseeko (stdin, 0, SEEK_CUR);
Packit 9e4112
  int r2 = fseek (stdin, 0, SEEK_CUR);
Packit 9e4112
  ASSERT (r1 == r2 && r1 == expected);
Packit 9e4112
  if (argc > 1)
Packit 9e4112
    {
Packit 9e4112
      /* Test that fseek discards previously read ungetc data.  */
Packit 9e4112
      int ch = fgetc (stdin);
Packit 9e4112
      ASSERT (ch == '#');
Packit 9e4112
      ASSERT (ungetc (ch, stdin) == ch);
Packit 9e4112
      ASSERT (fseeko (stdin, 2, SEEK_SET) == 0);
Packit 9e4112
      ch = fgetc (stdin);
Packit 9e4112
      ASSERT (ch == '/');
Packit 9e4112
      if (2 < argc)
Packit 9e4112
        {
Packit 9e4112
          if (FUNC_UNGETC_BROKEN)
Packit 9e4112
            {
Packit 9e4112
              fputs ("Skipping test: ungetc cannot handle arbitrary bytes\n",
Packit 9e4112
                     stderr);
Packit 9e4112
              return 77;
Packit 9e4112
            }
Packit 9e4112
          /* Test that fseek discards random ungetc data.  */
Packit 9e4112
          ASSERT (ungetc (ch ^ 0xff, stdin) == (ch ^ 0xff));
Packit 9e4112
        }
Packit 9e4112
      ASSERT (fseeko (stdin, 0, SEEK_END) == 0);
Packit 9e4112
      ASSERT (fgetc (stdin) == EOF);
Packit 9e4112
      /* Test that fseek resets end-of-file marker.  */
Packit 9e4112
      ASSERT (feof (stdin));
Packit 9e4112
      ASSERT (fseeko (stdin, 0, SEEK_END) == 0);
Packit 9e4112
      ASSERT (!feof (stdin));
Packit 9e4112
    }
Packit 9e4112
  return 0;
Packit 9e4112
}