Don't work around an lseek POSIX compliance bug on IRIX.
[gnulib.git] / tests / test-lseek.c
1 /* Test of lseek() function.
2    Copyright (C) 2007 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Eric Blake, 2007.  */
19
20 #include <config.h>
21
22 #include <errno.h>
23 #include <stdio.h>
24 #include <unistd.h>
25
26 #define ASSERT(expr) \
27   do                                                                         \
28     {                                                                        \
29       if (!(expr))                                                           \
30         {                                                                    \
31           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
32           abort ();                                                          \
33         }                                                                    \
34     }                                                                        \
35   while (0)
36
37 /* ARGC must be 2; *ARGV[1] is '0' if stdin and stdout are files, '1'
38    if they are pipes, and '2' if they are closed.  Check for proper
39    semantics of lseek.  */
40 int
41 main (int argc, char **argv)
42 {
43   if (argc != 2)
44     return 2;
45   switch (*argv[1])
46     {
47     case '0': /* regular files */
48       ASSERT (lseek (0, (off_t)2, SEEK_SET) == 2);
49       ASSERT (lseek (0, (off_t)-4, SEEK_CUR) == -1);
50       ASSERT (errno == EINVAL);
51       errno = 0;
52       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == 2);
53 #if 0 /* leads to SIGSYS on IRIX 6.5 */
54       ASSERT (lseek (0, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
55       ASSERT (errno == EINVAL);
56 #endif
57       ASSERT (lseek (1, (off_t)2, SEEK_SET) == 2);
58       errno = 0;
59       ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1);
60       ASSERT (errno == EINVAL);
61       errno = 0;
62       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == 2);
63 #if 0 /* leads to SIGSYS on IRIX 6.5 */
64       ASSERT (lseek (1, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
65       ASSERT (errno == EINVAL);
66 #endif
67       break;
68
69     case '1': /* pipes */
70       errno = 0;
71       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
72       ASSERT (errno == ESPIPE);
73       errno = 0;
74       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
75       ASSERT (errno == ESPIPE);
76       break;
77
78     case '2': /* closed */
79       /* Explicitly close file descriptors 0 and 1.  The <&- and >&- in the
80          invoking shell are not enough on HP-UX.  */
81       close (0);
82       close (1);
83       errno = 0;
84       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
85       ASSERT (errno == EBADF);
86       errno = 0;
87       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
88       ASSERT (errno == EBADF);
89       break;
90
91     default:
92       return 1;
93     }
94   return 0;
95 }