summaryrefslogtreecommitdiff
path: root/test/misc/seek.c
blob: c5edb94c9a5594d9df5bb123916ec031d4101e7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>

#define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*arr))

int main(void)
{
	struct {
		off_t offset;
		int   whence;
	} tests[] = {
		{ 0x00, SEEK_SET },
		{ 0x01, SEEK_SET },
		{ 0xFF, SEEK_SET }
	};
	char buf[2000];
	off_t ret;
	int i, fd;
	FILE *fp;
	int tmp;

	fd = open("lseek.out", O_RDWR|O_CREAT, 0600);
	if (fd == -1) {
		perror("open(lseek.out) failed");
		return 1;
	}
	unlink("lseek.out");
	fp = fdopen(fd, "rw");
	if (fp == NULL) {
		perror("fopen(lseek.out) failed");
		return 1;
	}

	memset(buf, 0xAB, sizeof(buf));
	ret = write(fd, buf, sizeof(buf));
	if (ret != sizeof(buf)) {
		fprintf(stderr, "write() failed to write %zi bytes (wrote %li): ", sizeof(buf), (long)ret);
		perror("");
		return 1;
	}

	tmp = fseeko(fp, 1024, SEEK_SET);
	assert(tmp == 0);
	tmp = fseeko(fp, (off_t)-16, SEEK_CUR);
	assert(tmp == 0);
	ret = ftell(fp);
	if (ret != (1024-16)) {
		fprintf(stderr, "ftell() failed, we wanted pos %i but got %li: ", (1024-16), (long)ret);
		perror("");
		return 1;
	}

	for (i = 0; i < ARRAY_SIZE(tests); ++i) {
		ret = lseek(fd, tests[i].offset, tests[i].whence);
		if (ret != tests[i].offset) {
			fprintf(stderr, "lseek(%li,%i) failed (wanted %li, got %li): ", (long)tests[i].offset,
			        tests[i].whence, (long)tests[i].offset, (long)ret);
			perror("");
			return 1;
		}
		ret = fseek(fp, tests[i].offset, tests[i].whence);
		if (ret != 0) {
			fprintf(stderr, "fseek(%li,%i) failed (wanted 0, got %li): ", (long)tests[i].offset,
			        tests[i].whence, (long)ret);
			perror("");
			return 1;
		}
	}

	fclose(fp);
	close(fd);

	printf("Success!\n");

	return 0;
}