Blame gettext-tools/gnulib-tests/test-ftell3.c

Packit Bot 06c835
/* Test of ftell() function.
Packit Bot 06c835
   Copyright (C) 2007-2015 Free Software Foundation, Inc.
Packit Bot 06c835
Packit Bot 06c835
   This program is free software: you can redistribute it and/or modify
Packit Bot 06c835
   it under the terms of the GNU General Public License as published by
Packit Bot 06c835
   the Free Software Foundation; either version 3 of the License, or
Packit Bot 06c835
   (at your option) any later version.
Packit Bot 06c835
Packit Bot 06c835
   This program is distributed in the hope that it will be useful,
Packit Bot 06c835
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot 06c835
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Bot 06c835
   GNU General Public License for more details.
Packit Bot 06c835
Packit Bot 06c835
   You should have received a copy of the GNU General Public License
Packit Bot 06c835
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Bot 06c835
Packit Bot 06c835
#include <config.h>
Packit Bot 06c835
Packit Bot 06c835
/* None of the files accessed by this test are large, so disable the
Packit Bot 06c835
   fseek link warning if we are not using the gnulib fseek module.  */
Packit Bot 06c835
#define _GL_NO_LARGE_FILES
Packit Bot 06c835
#include <stdio.h>
Packit Bot 06c835
Packit Bot 06c835
#include <string.h>
Packit Bot 06c835
Packit Bot 06c835
#include "macros.h"
Packit Bot 06c835
Packit Bot 06c835
#define TESTFILE "t-ftell3.tmp"
Packit Bot 06c835
Packit Bot 06c835
int
Packit Bot 06c835
main (void)
Packit Bot 06c835
{
Packit Bot 06c835
  FILE *fp;
Packit Bot 06c835
Packit Bot 06c835
  /* Create a file with some contents.  */
Packit Bot 06c835
  fp = fopen (TESTFILE, "w");
Packit Bot 06c835
  if (fp == NULL)
Packit Bot 06c835
    goto skip;
Packit Bot 06c835
  if (fwrite ("foogarsh", 1, 8, fp) < 8)
Packit Bot 06c835
    goto skip;
Packit Bot 06c835
  if (fclose (fp))
Packit Bot 06c835
    goto skip;
Packit Bot 06c835
Packit Bot 06c835
  /* The file's contents is now "foogarsh".  */
Packit Bot 06c835
Packit Bot 06c835
  /* Try writing after reading to EOF.  */
Packit Bot 06c835
  fp = fopen (TESTFILE, "r+");
Packit Bot 06c835
  if (fp == NULL)
Packit Bot 06c835
    goto skip;
Packit Bot 06c835
  if (fseek (fp, -1, SEEK_END))
Packit Bot 06c835
    goto skip;
Packit Bot 06c835
  ASSERT (getc (fp) == 'h');
Packit Bot 06c835
  ASSERT (getc (fp) == EOF);
Packit Bot 06c835
  ASSERT (ftell (fp) == 8);
Packit Bot 06c835
  ASSERT (ftell (fp) == 8);
Packit Bot 06c835
  ASSERT (putc ('!', fp) == '!');
Packit Bot 06c835
  ASSERT (ftell (fp) == 9);
Packit Bot 06c835
  ASSERT (fclose (fp) == 0);
Packit Bot 06c835
  fp = fopen (TESTFILE, "r");
Packit Bot 06c835
  if (fp == NULL)
Packit Bot 06c835
    goto skip;
Packit Bot 06c835
  {
Packit Bot 06c835
    char buf[10];
Packit Bot 06c835
    ASSERT (fread (buf, 1, 10, fp) == 9);
Packit Bot 06c835
    ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
Packit Bot 06c835
  }
Packit Bot 06c835
  ASSERT (fclose (fp) == 0);
Packit Bot 06c835
Packit Bot 06c835
  /* The file's contents is now "foogarsh!".  */
Packit Bot 06c835
Packit Bot 06c835
  remove (TESTFILE);
Packit Bot 06c835
  return 0;
Packit Bot 06c835
Packit Bot 06c835
 skip:
Packit Bot 06c835
  fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");
Packit Bot 06c835
  remove (TESTFILE);
Packit Bot 06c835
  return 77;
Packit Bot 06c835
}