Sync usage with man page.
[netbsd-mini2440.git] / regress / sys / fs / lseek / lseek.c
blobd96b0d6d4652d265b3d282356bb82f59f1d4069a
1 #include <stdio.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <errno.h>
5 #include <sys/stat.h>
6 #include <sys/unistd.h>
7 #include <sys/inttypes.h>
9 int main(int argc, char **argv)
11 int fd;
12 off_t cur;
13 struct stat st;
14 int error;
16 if (argc != 2) {
17 printf("seektest filename\n");
18 return EXIT_FAILURE;
20 fd = open(argv[1], 0, O_RDONLY);
21 if (fd <= 0) {
22 printf("can't open `%s` : %s\n", argv[1], strerror(errno));
23 return EXIT_FAILURE;
26 printf("Statting file\n");
27 error = fstat(fd, &st);
28 if (error) {
29 printf("can't stat file\n");
30 return EXIT_FAILURE;
32 printf("fstat() returns %"PRIi64" as size\n", st.st_size);
34 error = stat(argv[1], &st);
35 if (error) {
36 printf("can't stat file\n");
37 return EXIT_FAILURE;
39 printf("stat() returns %"PRIi64" as size\n", st.st_size);
41 error = lstat(argv[1], &st);
42 if (error) {
43 printf("can't lstat file\n");
44 return EXIT_FAILURE;
46 printf("lstat() returns %"PRIi64" as size\n", st.st_size);
48 printf("\nTesting normal seeking\n");
49 printf("get initial position\n");
50 cur = lseek(fd, 0, SEEK_CUR);
51 printf("seek start %"PRIi64"\n", cur);
52 if (cur != 0) {
53 printf("seek initial position wrong\n");
54 return EXIT_FAILURE;
57 printf("seeking end (filesize = %"PRIi64")\n", st.st_size);
58 cur = lseek(fd, 0, SEEK_END);
59 printf("seek now %"PRIi64"\n", cur);
60 if (cur != st.st_size) {
61 printf("seek to the end went wrong\n");
62 return EXIT_FAILURE;
65 printf("seeking backwards filesize-150 steps\n");
66 cur = lseek(fd, -(st.st_size - 150), SEEK_CUR);
67 printf("seek now %"PRIi64"\n", cur);
68 if (cur != 150) {
69 printf("relative seek from end to 150 failed\n");
70 return EXIT_FAILURE;
73 printf("seek set 1000\n");
74 cur = lseek(fd, 1000, SEEK_SET);
75 printf("seek now %"PRIi64"\n", cur);
76 if (cur != 1000) {
77 printf("seek 1000 went wrong\n");
78 return EXIT_FAILURE;
81 #if defined SEEK_DATA
82 printf("\nOne piece non sparse file checking:\n");
83 printf("seeking for sparse file data offset\n");
84 cur = lseek(fd, 0, SEEK_DATA);
85 if (cur != 0) {
86 printf("Not getting start of data segment at 0\n");
87 return EXIT_FAILURE;
90 printf("if seek_data returns a 2nd part on a non-sparse file\n");
91 cur = lseek(fd, st.st_size, SEEK_DATA);
92 if (cur != -1) {
93 printf("Seek data gave 2nd part at end of file\n");
94 return EXIT_FAILURE;
96 if (errno != ENXIO) {
97 printf( "Seek data on the end of file didn't"
98 " raise ENXIO\n");
99 return EXIT_FAILURE;
102 printf( "if seek_data in the file's last block of a non-sparse file "
103 "returns the current position\n");
104 cur = lseek(fd, st.st_size-50, SEEK_DATA);
105 if (cur != st.st_size - 50) {
106 printf("Seek data didn't give passed seek position back\n");
107 printf("%"PRIi64" should be %"PRIi64"\n", cur, st.st_size-50);
108 return EXIT_FAILURE;
111 printf( "if seek_data in the middle of the of a non-sparse file "
112 "returns the current position\n");
113 cur = lseek(fd, st.st_size-100*1024, SEEK_DATA);
114 if (cur != st.st_size - 100*1024) {
115 printf("Seek data didn't give passed seek position back\n");
116 printf("%"PRIi64" should be %"PRIi64"\n", cur,
117 st.st_size-100*1024);
118 return EXIT_FAILURE;
121 printf("seeking for hole\n");
122 cur = lseek(fd, 0, SEEK_HOLE);
123 if (cur != st.st_size) {
124 printf("Seek hole didn't return end of file\n");
125 return EXIT_FAILURE;
128 printf("seeking if the end of the file is a hole\n");
129 cur = lseek(fd, st.st_size, SEEK_HOLE);
130 if (cur != st.st_size) {
131 printf("At the end of the file, no virtual hole is returned\n");
132 return EXIT_FAILURE;
135 printf("seeking if a 2nd hole is returned outside the file range\n");
136 cur = lseek(fd, st.st_size + 1, SEEK_HOLE);
137 if (cur != -1) {
138 printf( "Past the end of file, seek hole returned another hole "
139 "instead of raising an error\n");
140 return EXIT_FAILURE;
142 if (errno != ENXIO) {
143 printf("Seek hole past the end of file didn't raise ENXIO\n");
144 return EXIT_FAILURE;
146 #endif
148 return EXIT_SUCCESS;