2007-12-10 18:26:56 +00:00
|
|
|
/* Test of fseeko() function.
|
2008-04-29 19:52:26 +00:00
|
|
|
Copyright (C) 2007, 2008 Free Software Foundation, Inc.
|
2007-12-10 18:26:56 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
|
|
|
|
|
|
/* Written by Bruno Haible <bruno@clisp.org>, 2007. */
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2008-04-29 19:52:26 +00:00
|
|
|
#include <stdlib.h>
|
2007-12-10 18:26:56 +00:00
|
|
|
|
2008-04-29 19:52:26 +00:00
|
|
|
#define ASSERT(expr) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (!(expr)) \
|
|
|
|
{ \
|
|
|
|
fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
|
|
|
|
fflush (stderr); \
|
|
|
|
abort (); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while (0)
|
2007-12-10 18:26:56 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
2008-04-29 19:52:26 +00:00
|
|
|
/* Assume stdin is non-empty, seekable, and starts with '#!/bin/sh'
|
|
|
|
iff argc > 1. */
|
2007-12-10 18:26:56 +00:00
|
|
|
int expected = argc > 1 ? 0 : -1;
|
|
|
|
/* Exit with success only if fseek/fseeko agree. */
|
2008-04-29 19:52:26 +00:00
|
|
|
int r1 = fseeko (stdin, 0, SEEK_CUR);
|
|
|
|
int r2 = fseek (stdin, 0, SEEK_CUR);
|
|
|
|
ASSERT (r1 == r2 && r1 == expected);
|
2008-01-14 14:07:25 +00:00
|
|
|
if (argc > 1)
|
|
|
|
{
|
2008-04-29 19:52:26 +00:00
|
|
|
/* Test that fseek discards previously read ungetc data. */
|
2008-01-14 14:07:25 +00:00
|
|
|
int ch = fgetc (stdin);
|
2008-04-29 19:52:26 +00:00
|
|
|
ASSERT (ch == '#');
|
|
|
|
ASSERT (ungetc (ch, stdin) == ch);
|
|
|
|
ASSERT (fseeko (stdin, 2, SEEK_SET) == 0);
|
|
|
|
/* Test that fseek discards random ungetc data. */
|
|
|
|
ch = fgetc (stdin);
|
|
|
|
ASSERT (ch == '/');
|
|
|
|
ASSERT (ungetc (ch ^ 0xff, stdin) == (ch ^ 0xff));
|
|
|
|
ASSERT (fseeko (stdin, 0, SEEK_END) == 0);
|
|
|
|
ASSERT (fgetc (stdin) == EOF);
|
2008-01-14 14:07:25 +00:00
|
|
|
/* Test that fseek resets end-of-file marker. */
|
2008-04-29 19:52:26 +00:00
|
|
|
ASSERT (feof (stdin));
|
|
|
|
ASSERT (fseeko (stdin, 0, SEEK_END) == 0);
|
|
|
|
ASSERT (!feof (stdin));
|
2008-01-14 14:07:25 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2007-12-10 18:26:56 +00:00
|
|
|
}
|