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