Merge branch 'upstream' into stable
[gnulib.git] / tests / test-lseek.c
1 /* Test of lseek() function.
2    Copyright (C) 2007-2008 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           fflush (stderr);                                                   \
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 #if ! defined __BEOS__
53       /* POSIX says that the last lseek call, when failing, does not change
54          the current offset.  But BeOS sets it to 0.  */
55       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == 2);
56 #endif
57 #if 0 /* leads to SIGSYS on IRIX 6.5 */
58       ASSERT (lseek (0, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
59       ASSERT (errno == EINVAL);
60 #endif
61       ASSERT (lseek (1, (off_t)2, SEEK_SET) == 2);
62       errno = 0;
63       ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1);
64       ASSERT (errno == EINVAL);
65       errno = 0;
66 #if ! defined __BEOS__
67       /* POSIX says that the last lseek call, when failing, does not change
68          the current offset.  But BeOS sets it to 0.  */
69       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == 2);
70 #endif
71 #if 0 /* leads to SIGSYS on IRIX 6.5 */
72       ASSERT (lseek (1, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
73       ASSERT (errno == EINVAL);
74 #endif
75       break;
76
77     case '1': /* pipes */
78       errno = 0;
79       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
80       ASSERT (errno == ESPIPE);
81       errno = 0;
82       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
83       ASSERT (errno == ESPIPE);
84       break;
85
86     case '2': /* closed */
87       /* Explicitly close file descriptors 0 and 1.  The <&- and >&- in the
88          invoking shell are not enough on HP-UX.  */
89       close (0);
90       close (1);
91       errno = 0;
92       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
93       ASSERT (errno == EBADF);
94       errno = 0;
95       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
96       ASSERT (errno == EBADF);
97       break;
98
99     default:
100       return 1;
101     }
102   return 0;
103 }