summaryrefslogtreecommitdiff
path: root/test/misc/seek.c
blob: 450fc04768acaf830ef95bc30d3050fd24bfe1c5 (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
#include <stdio.h>
#include <stdlib.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;

	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 %i bytes (wrote %i): ", sizeof(buf), ret);
		perror("");
		return 1;
	}

	assert(fseeko(fp, 1024, SEEK_SET) == 0);
	assert(fseeko(fp, (off_t)-16, SEEK_CUR) == 0);
	ret = ftell(fp);
	if (ret != (1024-16)) {
		fprintf(stderr, "ftell() failed, we wanted pos %i but got %i: ", (1024-16), 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(%i,%i) failed (wanted %i, got %i): ", tests[i].offset,
			        tests[i].whence, tests[i].offset, ret);
			perror("");
			return 1;
		}
		ret = fseek(fp, tests[i].offset, tests[i].whence);
		if (ret != 0) {
			fprintf(stderr, "fseek(%i,%i) failed (wanted 0, got %i): ", tests[i].offset,
			        tests[i].whence, ret);
			perror("");
			return 1;
		}
	}

	fclose(fp);
	close(fd);

	printf("Success!\n");

	return 0;
}