]> code.delx.au - refind/blob - filesystems/test/lslr.c
Use platform independent data types in order to support x64 platforms
[refind] / filesystems / test / lslr.c
1 /**
2 * \file lslr.c
3 * Test program for the POSIX user space environment.
4 */
5
6 /*-
7 * Copyright (c) 2006 Christoph Pfisterer
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are
11 * met:
12 *
13 * * Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the
19 * distribution.
20 *
21 * * Neither the name of Christoph Pfisterer nor the names of the
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #define FSW_DEBUG_LEVEL 3
39
40 #include "fsw_posix.h"
41
42
43 //extern struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(ext2);
44 //extern struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(reiserfs);
45 extern struct fsw_fstype_table FSW_FSTYPE_TABLE_NAME(FSTYPE);
46
47 static struct fsw_fstype_table *fstypes[] = {
48 //&FSW_FSTYPE_TABLE_NAME(ext2),
49 //&FSW_FSTYPE_TABLE_NAME(reiserfs),
50 &FSW_FSTYPE_TABLE_NAME(FSTYPE ),
51 NULL
52 };
53
54 static int listdir(struct fsw_posix_volume *vol, char *path, int level)
55 {
56 struct fsw_posix_dir *dir;
57 struct dirent *dent;
58 int i;
59 char subpath[4096];
60
61 dir = fsw_posix_opendir(vol, path);
62 if (dir == NULL) {
63 printf("opendir(%s) call failed.\n", path);
64 return 1;
65 }
66 while ((dent = fsw_posix_readdir(dir)) != NULL) {
67 for (i = 0; i < level*2; i++)
68 fputc(' ', stdout);
69 printf("%d %s\n", dent->d_type, dent->d_name);
70
71 if (dent->d_type == DT_DIR) {
72 snprintf(subpath, 4095, "%s%s/", path, dent->d_name);
73 listdir(vol, subpath, level + 1);
74 }
75 }
76 fsw_posix_closedir(dir);
77
78 return 0;
79 }
80
81 static int catfile(struct fsw_posix_volume *vol, char *path)
82 {
83 struct fsw_posix_file *file;
84 int r;
85 char buf[256];
86
87 file = fsw_posix_open(vol, path, 0, 0);
88 if (file == NULL) {
89 printf("open(%s) call failed.\n", path);
90 return 1;
91 }
92 while ((r=fsw_posix_read(file, buf, sizeof(buf))) > 0)
93 {
94 int i;
95 for (i=0; i<r; i++)
96 {
97 printf("%c", buf[i]);
98 }
99 }
100 fsw_posix_close(file);
101
102 return 0;
103 }
104
105 int main(int argc, char **argv)
106 {
107 struct fsw_posix_volume *vol;
108 int i;
109
110 if (argc != 2) {
111 printf("Usage: lslr <file/device>\n");
112 return 1;
113 }
114
115 for (i = 0; fstypes[i]; i++) {
116 vol = fsw_posix_mount(argv[1], fstypes[i]);
117 if (vol != NULL) {
118 printf("Mounted as '%s'.\n", fstypes[i]->name.data);
119 break;
120 }
121 }
122 if (vol == NULL) {
123 printf("Mounting failed.\n");
124 return 1;
125 }
126
127 listdir(vol, "/", 0);
128 catfile(vol, "/gaga/test");
129
130 fsw_posix_unmount(vol);
131
132 return 0;
133 }
134
135 // EOF