Take into account the number of pushed-back bytes (ungetc).
[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 3 of the License, or
7    (at your option) 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, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Eric Blake, 2007.  */
18
19 #include <config.h>
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <unistd.h>
24
25 #define ASSERT(expr) \
26   do                                                                         \
27     {                                                                        \
28       if (!(expr))                                                           \
29         {                                                                    \
30           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
31           abort ();                                                          \
32         }                                                                    \
33     }                                                                        \
34   while (0)
35
36 /* ARGC must be 2; *ARGV[1] is '0' if stdin and stdout are files, '1'
37    if they are pipes, and '2' if they are closed.  Check for proper
38    semantics of lseek.  */
39 int
40 main (int argc, char **argv)
41 {
42   if (argc != 2)
43     return 2;
44   switch (*argv[1])
45     {
46     case '0': /* regular files */
47       ASSERT (lseek (0, (off_t)2, SEEK_SET) == 2);
48       ASSERT (lseek (0, (off_t)-4, SEEK_CUR) == -1);
49       ASSERT (errno == EINVAL);
50       errno = 0;
51 #if ! defined __BEOS__
52       /* POSIX says that the last lseek call, when failing, does not change
53          the current offset.  But BeOS sets it to 0.  */
54       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == 2);
55 #endif
56 #if 0 /* leads to SIGSYS on IRIX 6.5 */
57       ASSERT (lseek (0, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
58       ASSERT (errno == EINVAL);
59 #endif
60       ASSERT (lseek (1, (off_t)2, SEEK_SET) == 2);
61       errno = 0;
62       ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1);
63       ASSERT (errno == EINVAL);
64       errno = 0;
65 #if ! defined __BEOS__
66       /* POSIX says that the last lseek call, when failing, does not change
67          the current offset.  But BeOS sets it to 0.  */
68       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == 2);
69 #endif
70 #if 0 /* leads to SIGSYS on IRIX 6.5 */
71       ASSERT (lseek (1, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
72       ASSERT (errno == EINVAL);
73 #endif
74       break;
75
76     case '1': /* pipes */
77       errno = 0;
78       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
79       ASSERT (errno == ESPIPE);
80       errno = 0;
81       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
82       ASSERT (errno == ESPIPE);
83       break;
84
85     case '2': /* closed */
86       /* Explicitly close file descriptors 0 and 1.  The <&- and >&- in the
87          invoking shell are not enough on HP-UX.  */
88       close (0);
89       close (1);
90       errno = 0;
91       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
92       ASSERT (errno == EBADF);
93       errno = 0;
94       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
95       ASSERT (errno == EBADF);
96       break;
97
98     default:
99       return 1;
100     }
101   return 0;
102 }