]> git.sur5r.net Git - cc65/blob - test/ref/strptr.c
remote TABs in doc/ and test/
[cc65] / test / ref / strptr.c
1 /*
2   !!DESCRIPTION!! 
3   !!ORIGIN!!      testsuite
4   !!LICENCE!!     Public Domain
5   !!AUTHOR!!      Groepaz/Hitmen
6 */
7
8 /*
9   this test reproduces a bug that prevented the testsuites directory
10   reading stuff for the c64 from working before. the bug appears to
11   only occur when optimizations are enabled. it also disappears if
12   the buffers inside the readdir function are declared static or
13   made global.
14 */
15
16 /*#define STANDALONE*/
17
18 #ifdef STANDALONE
19
20 FILE *outfile=NULL;
21 #define OPENTEST() outfile=stdout;
22 #define CLOSETEST()
23
24 #else
25
26 #endif
27                          
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <fcntl.h>
31 #include <string.h>
32
33 #define XNAME_MAX 16
34
35 struct Xdirent
36 {
37         char d_name[XNAME_MAX+1];
38         unsigned short d_off;
39         unsigned short d_reclen;
40         unsigned char  d_type;
41         unsigned char  d_namlen;
42 };
43
44 typedef struct
45 {
46         unsigned char fd;
47         unsigned short off;
48         char name[XNAME_MAX+1];
49 } XDIR;
50
51 unsigned char b1[4];
52 unsigned char b2[0x10]={"  \"test\"  "};
53
54 struct Xdirent *Xreaddir(XDIR *dir)
55 {
56 unsigned char buffer[0x40];
57 unsigned char temp;
58 unsigned char i,ii;
59
60 static struct Xdirent entry;
61 unsigned char fd;
62 static unsigned char ch;
63
64                 entry.d_off=dir->off;
65
66                 /* basic line-link / file-length */
67                 memcpy(buffer,b1,4);
68                 
69                 dir->off=dir->off+4;    
70                 entry.d_reclen=254*(buffer[2]+(buffer[3]<<8));
71
72                 /* read file entry */
73                 memcpy(buffer,b2,0x10);
74                         
75                 dir->off=dir->off+i;    
76
77                 printf("Xreaddir: '%s'\n",buffer);
78                 
79                 /* skip until either quote (file) or b (blocks free => end) */
80                 i=0;ii=0;
81                 while(i==0){
82                         temp=buffer[ii];ii++;
83                         if(ii>16){
84                                 /* something went wrong...this shouldnt happen! */
85                                 return(NULL);
86                         }
87                         else if(temp=='\"') i++;
88                         else if(temp=='b') {
89                                 /* "blocks free" */
90                                 return(NULL);
91                         }
92                 }
93                 printf("Xreaddir: '%s'\n",buffer);
94
95                 /* process file entry */
96
97                 i=0;  temp=buffer[ii];ii++;
98                 while(temp!='\"'){
99                         entry.d_name[i]=temp;
100                         i++;
101                         temp=buffer[ii];ii++;
102                 }
103                 entry.d_name[i]=0;
104                 entry.d_namlen=i;
105
106                 /* set type flag */
107
108                 return(&entry);
109 }
110
111 int main(void)
112 {
113 char mydirname[XNAME_MAX+1]=".";
114 XDIR mydir;
115 struct Xdirent *mydirent;
116         
117     printf("start\n");
118     
119     if((mydirent=Xreaddir(&mydir))==NULL)
120     {
121             printf("NULL\n");
122     }
123     else
124     {
125             printf("=%s\n",mydirent->d_name);
126     }
127     printf("done\n");
128
129     return 0;
130 }