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