X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=tests%2Ftest-lseek.c;h=ebba02ec6dc24350ef27a095d05efc88a8fc5092;hb=e9b552cf52b6200633c335b19a2f703f72f8683a;hp=ae53d98cad58530d02169c7bea2ca27cb9b3a6ab;hpb=e461ff7387db733080824cb94b239d8d1d09c4f3;p=gnulib.git diff --git a/tests/test-lseek.c b/tests/test-lseek.c index ae53d98ca..ebba02ec6 100644 --- a/tests/test-lseek.c +++ b/tests/test-lseek.c @@ -1,10 +1,10 @@ /* Test of lseek() function. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007-2008 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or modify + 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 2, or (at your option) - any later version. + 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 @@ -12,18 +12,92 @@ 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, write to the Free Software Foundation, - Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + along with this program. If not, see . */ /* Written by Eric Blake, 2007. */ #include +#include +#include #include +#define ASSERT(expr) \ + do \ + { \ + if (!(expr)) \ + { \ + fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \ + fflush (stderr); \ + abort (); \ + } \ + } \ + while (0) + +/* ARGC must be 2; *ARGV[1] is '0' if stdin and stdout are files, '1' + if they are pipes, and '2' if they are closed. Check for proper + semantics of lseek. */ int -main () +main (int argc, char **argv) { - /* Exit with success only if stdin is seekable. */ - return lseek (0, (off_t)0, SEEK_CUR) < 0; + if (argc != 2) + return 2; + switch (*argv[1]) + { + case '0': /* regular files */ + ASSERT (lseek (0, (off_t)2, SEEK_SET) == 2); + ASSERT (lseek (0, (off_t)-4, SEEK_CUR) == -1); + ASSERT (errno == EINVAL); + errno = 0; +#if ! defined __BEOS__ + /* POSIX says that the last lseek call, when failing, does not change + the current offset. But BeOS sets it to 0. */ + ASSERT (lseek (0, (off_t)0, SEEK_CUR) == 2); +#endif +#if 0 /* leads to SIGSYS on IRIX 6.5 */ + ASSERT (lseek (0, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1); + ASSERT (errno == EINVAL); +#endif + ASSERT (lseek (1, (off_t)2, SEEK_SET) == 2); + errno = 0; + ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1); + ASSERT (errno == EINVAL); + errno = 0; +#if ! defined __BEOS__ + /* POSIX says that the last lseek call, when failing, does not change + the current offset. But BeOS sets it to 0. */ + ASSERT (lseek (1, (off_t)0, SEEK_CUR) == 2); +#endif +#if 0 /* leads to SIGSYS on IRIX 6.5 */ + ASSERT (lseek (1, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1); + ASSERT (errno == EINVAL); +#endif + break; + + case '1': /* pipes */ + errno = 0; + ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == ESPIPE); + errno = 0; + ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == ESPIPE); + break; + + case '2': /* closed */ + /* Explicitly close file descriptors 0 and 1. The <&- and >&- in the + invoking shell are not enough on HP-UX. */ + close (0); + close (1); + errno = 0; + ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == EBADF); + errno = 0; + ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1); + ASSERT (errno == EBADF); + break; + + default: + return 1; + } + return 0; }