Expand PMF_FN_* macros.
[netbsd-mini2440.git] / share / examples / rump / sdread / sdread.c
blobe2b51f3423c24e8834f60bf9233a0dee366e232f
1 /* $NetBSD: sdread.c,v 1.1 2009/10/05 13:05:31 pooka Exp $ */
3 /*
4 * Copyright (c) 2009 Antti Kantee. All Rights Reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/dirent.h>
30 #include <sys/mount.h>
32 #include <ufs/ufs/ufsmount.h>
33 #include <msdosfs/msdosfsmount.h>
35 #include <rump/rump.h>
36 #include <rump/rump_syscalls.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
47 * Proof-of-concept program:
49 * Mount rump file system from device driver stack included in the
50 * rump kernel. Optionally copy a file out of the mounted file system.
53 int
54 main(int argc, char *argv[])
56 char buf[2048];
57 struct msdosfs_args args;
58 struct ufs_args uargs;
59 struct dirent *dp;
60 const char *msg = NULL;
61 int fd, n, fd_h, sverrno;
63 if (argc > 1 && argc != 3) {
64 fprintf(stderr, "usage: a.out [src hostdest]\n");
65 exit(1);
68 memset(&args, 0, sizeof(args));
69 args.fspec = strdup("/dev/sd0e");
70 args.version = MSDOSFSMNT_VERSION;
72 memset(&uargs, 0, sizeof(uargs));
73 uargs.fspec = strdup("/dev/sd0e");
75 rump_init();
77 if (rump_sys_mkdir("/mp", 0777) == -1)
78 err(1, "mkdir");
79 if (rump_sys_mount(MOUNT_MSDOS, "/mp", MNT_RDONLY,
80 &args, sizeof(args)) == -1) {
81 if (rump_sys_mount(MOUNT_FFS, "/mp", MNT_RDONLY,
82 &uargs, sizeof(uargs)) == -1) {
83 err(1, "mount");
87 fd = rump_sys_open("/mp", O_RDONLY, 0);
88 if (fd == -1) {
89 msg = "open dir";
90 goto out;
93 while ((n = rump_sys_getdents(fd, buf, sizeof(buf))) > 0) {
94 for (dp = (struct dirent *)buf;
95 (char *)dp - buf < n;
96 dp = _DIRENT_NEXT(dp)) {
97 printf("%" PRIu64 ": %s\n", dp->d_fileno, dp->d_name);
100 rump_sys_close(fd);
101 if (argc == 1)
102 goto out;
104 rump_sys_chdir("/mp");
105 fd = rump_sys_open(argv[1], O_RDONLY, 0);
106 if (fd == -1) {
107 msg = "open fs file";
108 goto out;
111 fd_h = open(argv[2], O_RDWR | O_CREAT, 0777);
112 if (fd_h == -1) {
113 msg = "open host file";
114 goto out;
117 while ((n = rump_sys_read(fd, buf, sizeof(buf))) == sizeof(buf)) {
118 if (write(fd_h, buf, sizeof(buf)) != sizeof(buf)) {
119 msg = "write host file";
120 goto out;
123 if (n == -1) {
124 msg = "read fs file";
125 goto out;
128 if (n > 0) {
129 if (write(fd_h, buf, n) == -1)
130 msg = "write tail";
133 out:
134 sverrno = errno;
135 rump_sys_chdir("/");
136 rump_sys_close(fd);
137 close(fd_h);
138 if (rump_sys_unmount("/mp", 0) == -1)
139 err(1, "unmount");
141 if (msg) {
142 errno = sverrno;
143 err(1, "%s", msg);
146 return 0;