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