Explicitly close file descriptors where needed.
[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       ASSERT (lseek (0, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
54       ASSERT (errno == EINVAL);
55       ASSERT (lseek (1, (off_t)2, SEEK_SET) == 2);
56       errno = 0;
57       ASSERT (lseek (1, (off_t)-4, SEEK_CUR) == -1);
58       ASSERT (errno == EINVAL);
59       errno = 0;
60       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == 2);
61       ASSERT (lseek (1, (off_t)0, (SEEK_SET | SEEK_CUR | SEEK_END) + 1) == -1);
62       ASSERT (errno == EINVAL);
63       break;
64
65     case '1': /* pipes */
66       errno = 0;
67       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
68       ASSERT (errno == ESPIPE);
69       errno = 0;
70       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
71       ASSERT (errno == ESPIPE);
72       break;
73
74     case '2': /* closed */
75       /* Explicitly close file descriptors 0 and 1.  The <&- and >&- in the
76          invoking shell are not enough on HP-UX.  */
77       close (0);
78       close (1);
79       errno = 0;
80       ASSERT (lseek (0, (off_t)0, SEEK_CUR) == -1);
81       ASSERT (errno == EBADF);
82       errno = 0;
83       ASSERT (lseek (1, (off_t)0, SEEK_CUR) == -1);
84       ASSERT (errno == EBADF);
85       break;
86
87     default:
88       return 1;
89     }
90   return 0;
91 }