10 int main(int argc, char **argv)
20 printf("\nfilename: ");
21 x = fgets(buf,19,stdin);
26 x[strcspn(x, "\r\n")] = 0;
33 file = fopen(filename,"rb");
35 fprintf(stderr,"cannot open %s: %s\n",filename,strerror(errno));
39 if (fread(buf, 10, 1, file) != 1) {
40 fprintf(stderr,"short read, aborted\n");
47 fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
52 printf("reading 10 bytes from file\n");
53 printf("current file pos: %ld\n", pos);
55 printf("get file size (lseek): ");
56 fd = *(char *)file; /* kids, don't do this at home */
57 fsz = lseek(fd, 0, SEEK_END);
59 fprintf(stderr,"lseek returned -1: %s\n", strerror(errno));
63 printf("%ld (fd = %d)\n", (long)fsz, fd);
65 printf("get file size (fseek): ");
66 pos = fseek(file, 0, SEEK_END);
68 fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
75 fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
81 printf("positioning at offset 100: ");
82 pos = fseek(file, 100, SEEK_SET);
84 fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
90 fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
98 printf("failed! cur pos = %ld\n",pos);
103 printf("seeking back 44 bytes: ");
104 pos = fseek(file, -44, SEEK_CUR);
106 fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
112 fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
120 printf("failed! cur pos = %ld\n",pos);
125 printf("seeking forward 111 bytes: ");
126 pos = fseek(file, 111, SEEK_CUR);
128 fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
134 fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
142 printf("failed! cur pos = %ld\n",pos);
147 printf("seeking 13 bytes before eof: ");
148 pos = fseek(file, -13, SEEK_END);
150 fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
156 fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
160 if (pos == fsz - 13) {
164 printf("failed! cur pos = %ld\n",pos);
169 printf("seeking before sof:\n\t");
170 pos = fseek(file, -fsz, SEEK_CUR);
172 printf("Ok, error %s\n", strerror(errno));
175 printf("NOT OK, no error\n");
180 /* ProDOS on the Apple II only supports 24-bit file offsets,
181 ** so anything beyond that should be an error. I don't know
182 ** about other platforms, but I'm guessing no 6502-based
183 ** operating systems support 32-bit offsets?
185 printf("seeking to position 2^24:\n\t");
186 pos = lseek(fd, 0x1000000L, SEEK_SET);
188 printf("Ok, error %s\n", strerror(errno));
191 printf("NOT OK, returned %ld but expected -1\n", pos);
196 printf("trying invalid value for whence:\n\t");
197 pos = lseek(fd, 0L, 3);
199 printf("Ok, error %s\n", strerror(errno));
202 printf("NOT OK, returned %ld but expected -1\n", pos);