]> git.sur5r.net Git - cc65/blob - testcode/lib/seek.c
Added C64 Chameleon accelerator code and documentation.
[cc65] / testcode / lib / seek.c
1 /*
2 ** seek test program
3 */
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <errno.h>
8 #include <unistd.h>
9
10 int main(int argc, char **argv)
11 {
12     char *filename,*x;
13     char buf[20];
14     FILE *file;
15     long pos;
16     off_t fsz;
17     int fd;
18
19     if (argc <= 1) {
20         printf("\nfilename: ");
21         x = fgets(buf,19,stdin);
22         printf("\n");
23         if (!x) {
24             return(0);
25         }
26         filename = x;
27     }
28     else {
29         filename = *(argv+1);
30     }
31
32     file = fopen(filename,"rb");
33     if (!file) {
34         fprintf(stderr,"cannot open %s: %s\n",filename,strerror(errno));
35         return(1);
36     }
37
38     if (fread(buf, 10, 1, file) != 1) {
39         fprintf(stderr,"short read, aborted\n");
40         fclose(file);
41         return(1);
42     }
43
44     pos = ftell(file);
45     if (pos == -1) {
46         fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
47         fclose(file);
48         return(1);
49     }
50
51     printf("reading 10 bytes from file\n");
52     printf("current file pos: %ld\n", pos);
53
54     printf("get file size (lseek): ");
55     fd = *(char *)file;   /* kids, don't do this at home */
56     fsz = lseek(fd, 0, SEEK_END);
57     if (fsz == -1) {
58         fprintf(stderr,"lseek returned -1: %s\n", strerror(errno));
59         fclose(file);
60         return(1);
61     }
62     printf("%ld (fd = %d)\n", (long)fsz, fd);
63
64     printf("get file size (fseek): ");
65     pos = fseek(file, 0, SEEK_END);
66     if (pos != 0) {
67         fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
68         fclose(file);
69         return(1);
70     }
71
72     pos = ftell(file);
73     if (pos == -1) {
74         fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
75         fclose(file);
76         return(1);
77     }
78     printf("%ld\n",pos);
79
80     printf("positioning at offset 100: ");
81     pos = fseek(file, 100, SEEK_SET);
82     if (pos != 0) {
83         fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
84         fclose(file);
85         return(1);
86     }
87     pos = ftell(file);
88     if (pos == -1) {
89         fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
90         fclose(file);
91         return(1);
92     }
93     if (pos == 100) {
94         printf("Ok\n");
95     }
96     else {
97         printf("failed! cur pos = %ld\n",pos);
98         fclose(file);
99         return(1);
100     }
101
102     printf("seeking back 44 bytes: ");
103     pos = fseek(file, -44, SEEK_CUR);
104     if (pos != 0) {
105         fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
106         fclose(file);
107         return(1);
108     }
109     pos = ftell(file);
110     if (pos == -1) {
111         fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
112         fclose(file);
113         return(1);
114     }
115     if (pos == 56) {
116         printf("Ok\n");
117     }
118     else {
119         printf("failed! cur pos = %ld\n",pos);
120         fclose(file);
121         return(1);
122     }
123
124     printf("seeking forward 111 bytes: ");
125     pos = fseek(file, 111, SEEK_CUR);
126     if (pos != 0) {
127         fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
128         fclose(file);
129         return(1);
130     }
131     pos = ftell(file);
132     if (pos == -1) {
133         fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
134         fclose(file);
135         return(1);
136     }
137     if (pos == 167) {
138         printf("Ok\n");
139     }
140     else {
141         printf("failed! cur pos = %ld\n",pos);
142         fclose(file);
143         return(1);
144     }
145
146     printf("seeking 13 bytes before eof: ");
147     pos = fseek(file, -13, SEEK_END);
148     if (pos != 0) {
149         fprintf(stderr,"fseek returned -1: %s\n", strerror(errno));
150         fclose(file);
151         return(1);
152     }
153     pos = ftell(file);
154     if (pos == -1) {
155         fprintf(stderr,"ftell returned -1: %s\n", strerror(errno));
156         fclose(file);
157         return(1);
158     }
159     if (pos == fsz - 13) {
160         printf("Ok\n");
161     }
162     else {
163         printf("failed! cur pos = %ld\n",pos);
164         fclose(file);
165         return(1);
166     }
167
168     printf("seeking before sof:\n\t");
169     pos = fseek(file, -fsz, SEEK_CUR);
170     if (pos != 0) {
171         printf("Ok, error %s\n", strerror(errno));
172     }
173     else {
174         printf("NOT OK, no error\n");
175     }
176
177     fclose(file);
178     return(0);
179 }