OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [posix/] [test0/] [src/] [dirtest.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
 
2
#include <sys/types.h>
3
#include <sys/stat.h>
4
#include <unistd.h>
5
#include <errno.h>
6
#include <fcntl.h>
7
#include <stdio.h>
8
#include <string.h>
9
#include <sys/syscall.h>
10
#include <dirent.h>
11
#include <l4lib/os/posix/readdir.h>
12
#include <tests.h>
13
 
14
#define DENTS_TOTAL     50
15
 
16
void print_fsize(struct stat *s)
17
{
18
        printf("%lu", s->st_size);
19
}
20
 
21
void print_flink(struct stat *s)
22
{
23
        printf("%d", s->st_nlink);
24
}
25
 
26
void print_fuser(struct stat *s)
27
{
28
        printf("%d", s->st_uid);
29
        printf("%c", ' ');
30
        printf("%c", ' ');
31
        printf("%d", s->st_gid);
32
}
33
 
34
void print_ftype(struct stat *s)
35
{
36
        unsigned int type = s->st_mode & S_IFMT;
37
 
38
        if (type == S_IFDIR)
39
                printf("%c", 'd');
40
        else if (type == S_IFSOCK)
41
                printf("%c", 's');
42
        else if (type == S_IFCHR)
43
                printf("%c", 'c');
44
        else if (type == S_IFLNK)
45
                printf("%c", 'l');
46
        else if (type == S_IFREG)
47
                printf("%c", '-');
48
}
49
 
50
void print_fperm(struct stat *s)
51
{
52
        if (s->st_mode & S_IRUSR)
53
                printf("%c", 'r');
54
        else
55
                printf("%c", '-');
56
        if (s->st_mode & S_IWUSR)
57
                printf("%c", 'w');
58
        else
59
                printf("%c", '-');
60
        if (s->st_mode & S_IXUSR)
61
                printf("%c", 'x');
62
        else
63
                printf("%c", '-');
64
}
65
 
66
void print_fstat(struct stat *s)
67
{
68
        print_ftype(s);
69
        print_fperm(s);
70
        printf("%c", ' ');
71
        printf("%c", ' ');
72
        print_fsize(s);
73
        printf("%c", ' ');
74
}
75
 
76
void print_dirents(char *path, void *buf, int cnt)
77
{
78
        int i = 0;
79
        struct dirent *dp = buf;
80
        // struct stat statbuf;
81
        char pathbuf[256];
82
 
83
        strncpy(pathbuf, path, 256);
84
        while (cnt > 0) {
85
                strcpy(pathbuf, path);
86
                strcpy(&pathbuf[strlen(pathbuf)],"/");
87
                strcpy(&pathbuf[strlen(pathbuf)],dp->d_name);
88
                //printf("Dirent %d:\n", i);
89
                //printf("Inode: %d\n", dp->d_ino);
90
                //printf("Offset: %d\n", dp->d_off);
91
                //printf("Reclen: %d\n", dp->d_reclen);
92
                //if (stat(pathbuf, &statbuf) < 0)
93
                //      perror("STAT");
94
                // print_fstat(&statbuf);
95
                test_printf("%s\n", dp->d_name);
96
                cnt -= dp->d_reclen;
97
                dp = (struct dirent *)((void *)dp + dp->d_reclen);
98
                i++;
99
        }
100
}
101
 
102
int lsdir(char *path)
103
{
104
        struct dirent dents[DENTS_TOTAL];
105
        int bytes;
106
        int fd;
107
 
108
        memset(dents, 0, sizeof(struct dirent) * DENTS_TOTAL);
109
 
110
        if ((fd = open(path, O_RDONLY)) < 0) {
111
                test_printf("OPEN failed.\n");
112
                return -1;
113
        } else
114
                test_printf("Got fd: %d for opening %s\n", fd, path);
115
 
116
        if ((bytes = os_readdir(fd, dents, sizeof(struct dirent) * DENTS_TOTAL)) < 0) {
117
                test_printf("GETDENTS error: %d\n", bytes);
118
                return -1;
119
        } else {
120
                print_dirents(path, dents, bytes);
121
        }
122
 
123
        return 0;
124
}
125
 
126
int dirtest(void)
127
{
128
        if (lsdir(".") < 0) {
129
                test_printf("lsdir failed.\n");
130
                goto out_err;
131
        }
132
        if (lsdir("/") < 0) {
133
                test_printf("lsdir failed.\n");
134
                goto out_err;
135
        }
136
 
137
        test_printf("\nCreating directories: usr, etc, tmp, var, home, opt, bin, boot, lib, dev\n");
138
        if (mkdir("/usr", 0) < 0) {
139
                test_printf("MKDIR: %d\n", errno);
140
                goto out_err;
141
        }
142
        if (mkdir("/etc", 0) < 0) {
143
                test_printf("MKDIR: %d\n", errno);
144
                goto out_err;
145
        }
146
        if (mkdir("/tmp", 0) < 0) {
147
                test_printf("MKDIR: %d\n", errno);
148
                goto out_err;
149
        }
150
        if (mkdir("/var", 0) < 0) {
151
                test_printf("MKDIR: %d\n", errno);
152
                goto out_err;
153
        }
154
        if (mkdir("/bin", 0) < 0) {
155
                test_printf("MKDIR: %d\n", errno);
156
                goto out_err;
157
        }
158
        if (mkdir("/boot", 0) < 0) {
159
                test_printf("MKDIR: %d\n", errno);
160
                goto out_err;
161
        }
162
        if (mkdir("/lib", 0) < 0) {
163
                test_printf("MKDIR: %d\n", errno);
164
                goto out_err;
165
        }
166
        if (mkdir("/dev", 0) < 0) {
167
                test_printf("MKDIR: %d\n", errno);
168
                goto out_err;
169
        }
170
        if (mkdir("/usr/bin", 0) < 0) {
171
                test_printf("MKDIR: %d\n", errno);
172
                goto out_err;
173
        }
174
        if (mkdir("/home/", 0) < 0) {
175
                test_printf("MKDIR: %d\n", errno);
176
                goto out_err;
177
        }
178
        if (mkdir("/home/bahadir", 0) < 0) {
179
                test_printf("MKDIR: %d\n", errno);
180
                goto out_err;
181
        }
182
        if (chdir("/home/bahadir") < 0) {
183
                test_printf("MKDIR: %d\n", errno);
184
                goto out_err;
185
        }
186
        test_printf("Changed curdir to /home/bahadir\n");
187
 
188
        test_printf("\nlsdir root directory:\n");
189
        if (lsdir("/") < 0)
190
                goto out_err;
191
 
192
        test_printf("\nlsdir /usr:\n");
193
        if (lsdir("/usr") < 0)
194
                goto out_err;
195
 
196
        test_printf("\nlsdir current directory:\n");
197
        if (lsdir(".") < 0)
198
                goto out_err;
199
        test_printf("\nlsdir /usr/./././bin//\n");
200
        if (lsdir("/usr/./././bin//") < 0)
201
                goto out_err;
202
 
203
        printf("DIR TEST            -- PASSED --\n");
204
        return 0;
205
 
206
out_err:
207
        printf("DIR TEST            -- FAILED --\n");
208
        return 0;
209
}
210
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.