Add fsx file test and split up basic tests.
[yaffs2.git] / rtems / rtems-y-test / basic-test / yaffs-rtems-basic-test.c
1 /*
2  *  Simple test program -- demonstrating use of IMFS
3  */
4
5 #include <bsp.h>
6
7 #include <fcntl.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11
12 #include <dirent.h>
13
14 #include <rtems/libio.h>
15 #include <yaffs/rtems_yaffs.h>
16
17
18 #define YPATH "/yaffs_mount_pt"
19
20
21
22
23
24 void set_uint8_t_buffer(uint8_t *buf, uint32_t n, uint8_t start, uint8_t inc)
25 {
26         while (n) {
27                 *buf = start;
28                 buf++;
29                 start += inc;
30                 n--;
31         }
32 }
33
34
35 #define FNAME YPATH"/test"
36 #define DIRNAME YPATH"/dirtest"
37
38 void dump_directory_tree_worker(const char *dname,int recursive)
39 {
40         DIR *d;
41         struct dirent *de;
42         struct stat s;
43         char str[1000];
44
45         d = opendir(dname);
46
47         if(!d)
48         {
49                 printf("opendir failed\n");
50         }
51         else
52         {
53                 while((de = readdir(d)) != NULL)
54                 {
55                         sprintf(str,"%s/%s",dname,de->d_name);
56
57                         lstat(str,&s);
58
59                         printf("%s inode %d length %d mode %X ",
60                                 str, (int)s.st_ino, (int)s.st_size, s.st_mode);
61                         switch(s.st_mode & S_IFMT)
62                         {
63                                 case S_IFREG: printf("data file"); break;
64                                 case S_IFDIR: printf("directory"); break;
65                                 case S_IFLNK: printf("symlink -->");
66                                                           if(readlink(str,str,100) < 0)
67                                                                 printf("no alias");
68                                                           else
69                                                                 printf("\"%s\"",str);
70                                                           break;
71                                 default: printf("unknown"); break;
72                         }
73
74                         printf("\n");
75
76                         if((s.st_mode & S_IFMT) == S_IFDIR && recursive)
77                                 dump_directory_tree_worker(str,1);
78
79                 }
80
81                 closedir(d);
82         }
83
84 }
85
86 static void dump_directory_tree(const char *dname)
87 {
88         dump_directory_tree_worker(dname,1);
89 }
90
91
92 void dumpDir(const char *dname)
93 {       dump_directory_tree_worker(dname,0);
94 }
95
96 int run_the_test(void)
97 {
98         int fd;
99         int ret;
100         uint8_t buf[100];
101         uint8_t buf2[100];
102
103
104         dump_directory_tree(YPATH);
105
106         ret = mkdir(DIRNAME, 0777);
107         if (ret < 0)
108                 perror("mkdir "DIRNAME);
109
110         fd = open(FNAME, O_RDWR | O_CREAT | O_TRUNC, 0666);
111         printf("open %s  = %d\n", FNAME, fd);
112         if (fd < 0)
113                 perror("opening " FNAME);
114
115         set_uint8_t_buffer(buf, sizeof(buf), 0xAA, 1);
116
117         ret = write(fd, buf, sizeof(buf));
118
119         printf("write returned %d\n", ret);
120
121         if (ret == -1)
122                 perror("writing file");
123
124         ret = lseek(fd, 0, SEEK_END);
125
126         printf("lseek end ret = %d\n", ret);
127
128         ret = lseek(fd, 0, SEEK_SET);
129         printf("lseek start ret = %d\n", ret);
130
131         ret = read(fd, buf2, sizeof(buf2));
132
133         printf("reading file ret = %d\n", ret);
134
135         if (ret == -1)
136                 perror("reading file");
137
138
139         return ret;
140
141 #if 0
142
143
144    fd = open("test1", O_CREAT);
145    printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) );
146    close(fd);
147
148    fd = open("test", O_RDONLY);
149    if (fd == -1) {
150      printf("Starting on the wrong foot....\n");
151      exit(-1);
152    }
153
154    printf( "fcntl flags =0x%x\n", fcntl( fd, F_GETFL ) );
155
156    fp = fdopen(fd, "r");
157    if (fp == NULL) {
158       printf("Nothing ever goes my way!\n");
159       close(fd);
160       exit(-1);
161    } else {
162       printf("Soon, I will be able to take over the world!\n");
163       fgets(str, 200, fp);
164       printf("%s\n", str);
165       fclose(fp);
166    }
167 #endif
168 }
169