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