Sufficiently updated docs, build system and a few of the examples for
[xiph/unicode.git] / fusd / examples / helloworld.c
blob28df8a6d014714fde9216b397317bf40dd4f2bbb
1 /*
3 * Copyright (c) 2003 The Regents of the University of California. All
4 * 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:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Neither the name of the University nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
21 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * FUSD - The Framework for UserSpace Devices - Example program
35 * Jeremy Elson <jelson@circlemud.org>
37 * hello-world: Simply creates a device called /dev/hello-world, which
38 * greets you when you try to get it.
40 * $Id$
43 /* EXAMPLE START helloworld.c */
44 #include <stdio.h>
45 #include <string.h>
46 #include <errno.h>
47 #include "fusd.h"
49 #define GREETING "Hello, world!\n"
51 int do_open_or_close(struct fusd_file_info *file)
53 return 0; /* attempts to open and close this file always succeed */
56 ssize_t do_read(struct fusd_file_info *file, char *user_buffer,
57 size_t user_length, loff_t *offset)
59 int retval = 0;
61 /* The first read to the device returns a greeting. The second read
62 * returns EOF. */
63 if (*offset == 0) {
64 if (user_length < strlen(GREETING))
65 retval = -EINVAL; /* the user must supply a big enough buffer! */
66 else {
67 memcpy(user_buffer, GREETING, strlen(GREETING)); /* greet user */
68 retval = strlen(GREETING); /* retval = number of bytes returned */
69 *offset += retval; /* advance user's file pointer */
73 return retval;
76 int main(int argc, char *argv[])
78 struct fusd_file_operations fops = {
79 open: do_open_or_close,
80 read: do_read,
81 close: do_open_or_close };
83 if (fusd_register("/dev/hello-world", "test", "hello-world", 0666, NULL, &fops) < 0)
84 perror("Unable to register device");
85 else {
86 printf("/dev/hello-world should now exist - calling fusd_run...\n");
87 fusd_run();
89 return 0;
91 /* EXAMPLE STOP helloworld.c */