Sync usage with man page.
[netbsd-mini2440.git] / share / examples / puffs / rot13fs / rot13fs.c
blobc3300f4f177818d7e3c081666e721557c1b8d560
1 /* $NetBSD: rot13fs.c,v 1.16 2007/11/30 19:02:38 pooka Exp $ */
3 /*
4 * Copyright (c) 2007 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.
29 * rot13fs: puffs layering experiment
30 * (unfinished, as is probably fairly easy to tell)
32 * This also demonstrates how namemod can be easily set to any
33 * function which reverses itself (argument -f provides a case-flipping
34 * file system).
37 #include <ctype.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <puffs.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
45 PUFFSOP_PROTOS(rot13)
47 static void usage(void);
49 static void
50 usage()
53 errx(1, "usage: %s [-s] [-o mntopts] rot13path mountpath",
54 getprogname());
57 static void (*flipflop)(void *, size_t);
59 static uint8_t tbl[256];
61 static void
62 dorot13(void *buf, size_t buflen)
64 uint8_t *b = buf;
66 while (buflen--) {
67 *b = tbl[*b];
68 b++;
72 static void
73 docase(void *buf, size_t buflen)
75 unsigned char *b = buf;
77 while (buflen--) {
78 if (isalpha(*b))
79 *b ^= 0x20;
80 b++;
84 static int
85 rot13path(struct puffs_usermount *pu, struct puffs_pathobj *base,
86 struct puffs_cn *pcn)
89 flipflop(pcn->pcn_name, pcn->pcn_namelen);
91 return 0;
94 int
95 main(int argc, char *argv[])
97 struct puffs_usermount *pu;
98 struct puffs_ops *pops;
99 struct puffs_pathobj *po_root;
100 struct puffs_node *pn_root;
101 struct stat sb;
102 mntoptparse_t mp;
103 int mntflags, pflags;
104 int detach;
105 int ch;
106 int i;
108 setprogname(argv[0]);
110 if (argc < 3)
111 usage();
113 flipflop = dorot13;
114 pflags = mntflags = 0;
115 detach = 1;
116 while ((ch = getopt(argc, argv, "fo:s")) != -1) {
117 switch (ch) {
118 case 'f':
119 flipflop = docase;
120 break;
121 case 'o':
122 mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
123 if (mp == NULL)
124 err(1, "getmntopts");
125 freemntopts(mp);
126 break;
127 case 's':
128 detach = 0;
129 break;
132 pflags |= PUFFS_FLAG_BUILDPATH;
133 argv += optind;
134 argc -= optind;
136 if (pflags & PUFFS_FLAG_OPDUMP)
137 detach = 0;
139 if (argc != 2)
140 usage();
142 if (lstat(argv[0], &sb) == -1)
143 err(1, "stat %s", argv[0]);
144 if ((sb.st_mode & S_IFDIR) == 0)
145 errx(1, "%s is not a directory", argv[0]);
147 PUFFSOP_INIT(pops);
148 puffs_null_setops(pops);
150 PUFFSOP_SET(pops, rot13, node, readdir);
151 PUFFSOP_SET(pops, rot13, node, read);
152 PUFFSOP_SET(pops, rot13, node, write);
154 if ((pu = puffs_init(pops, argv[0], "rot13", NULL, pflags)) == NULL)
155 err(1, "mount");
157 pn_root = puffs_pn_new(pu, NULL);
158 if (pn_root == NULL)
159 err(1, "puffs_pn_new");
160 puffs_setroot(pu, pn_root);
162 po_root = puffs_getrootpathobj(pu);
163 if (po_root == NULL)
164 err(1, "getrootpathobj");
165 po_root->po_path = argv[0];
166 po_root->po_len = strlen(argv[0]);
167 puffs_stat2vattr(&pn_root->pn_va, &sb);
169 puffs_set_namemod(pu, rot13path);
171 /* initialize rot13 tables */
172 for (i = 0; i < 256; i++)
173 tbl[i] = (uint8_t)i;
174 for (i = 0; i <= 26; i++)
175 tbl[i + 'a'] = 'a' + ((i + 13) % 26);
176 for (i = 0; i < 26; i++)
177 tbl[i + 'A'] = 'A' + ((i + 13) % 26);
179 if (detach)
180 if (puffs_daemon(pu, 1, 1) == -1)
181 err(1, "puffs_daemon");
183 if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1)
184 err(1, "puffs_mount");
185 if (puffs_mainloop(pu) == -1)
186 err(1, "mainloop");
188 return 0;
192 rot13_node_readdir(struct puffs_usermount *pu, void *opc, struct dirent *dent,
193 off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
194 int *eofflag, off_t *cookies, size_t *ncookies)
196 struct dirent *dp;
197 size_t rl;
198 int rv;
200 dp = dent;
201 rl = *reslen;
203 rv = puffs_null_node_readdir(pu, opc, dent, readoff, reslen, pcr,
204 eofflag, cookies, ncookies);
205 if (rv)
206 return rv;
208 while (rl > *reslen) {
209 flipflop((uint8_t *)dp->d_name, dp->d_namlen);
210 rl -= _DIRENT_SIZE(dp);
211 dp = _DIRENT_NEXT(dp);
214 return 0;
218 rot13_node_read(struct puffs_usermount *pu, void *opc,
219 uint8_t *buf, off_t offset, size_t *resid,
220 const struct puffs_cred *pcr, int ioflag)
222 uint8_t *prebuf = buf;
223 size_t preres = *resid;
224 int rv;
226 rv = puffs_null_node_read(pu, opc, buf, offset, resid, pcr, ioflag);
227 if (rv)
228 return rv;
230 flipflop(prebuf, preres - *resid);
232 return rv;
236 rot13_node_write(struct puffs_usermount *pu, void *opc,
237 uint8_t *buf, off_t offset, size_t *resid,
238 const struct puffs_cred *pcr, int ioflag)
241 flipflop(buf, *resid);
242 return puffs_null_node_write(pu, opc, buf, offset, resid, pcr, ioflag);