~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
cvs/emstar/fusd/examples/echo.c


  1 /*
  2  *
  3  * Copyright (c) 2003 The Regents of the University of California.  All 
  4  * rights reserved.
  5  *
  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  *
 10  * - Redistributions of source code must retain the above copyright
 11  *   notice, this list of conditions and the following disclaimer.
 12  *
 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.
 16  *
 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.
 28  *
 29  */
 30  
 31 
 32 /*
 33  * FUSD - The Framework for UserSpace Devices - Example program
 34  *
 35  * Jeremy Elson <jelson@circlemud.org>
 36  *
 37  * echo.c: Example of how to use both the 'read' and 'write' callbacks.
 38  *
 39  * This example creates a single device, /dev/echo.  If you write
 40  * something to /dev/echo (e.g., "echo HI THERE > /dev/echo"), it gets
 41  * stored.  Then, when you read (e.g. "cat /dev/echo"), you get back
 42  * whatever you wrote most recently.
 43  *
 44  * $Id: echo.c,v 1.7 2007-12-08 04:05:01 girod Exp $ 
 45  */
 46 
 47 #include <stdio.h>
 48 #include <stdlib.h>
 49 #include <string.h>
 50 #include <errno.h>
 51 #include <unistd.h>
 52 
 53 #include "fusd.h"
 54 
 55 #define MIN(x, y) ((x) < (y) ? (x) : (y))
 56 
 57 /* EXAMPLE START echo.c */
 58 char *data = NULL;
 59 int data_length = 0;
 60 
 61 ssize_t echo_read(struct fusd_file_info *file, char *user_buffer,
 62               size_t user_length, loff_t *offset)
 63 {
 64   /* if the user has read past the end of the data, return EOF */
 65   if (*offset >= data_length)
 66     return 0;
 67 
 68   /* only return as much data as we have */
 69   user_length = MIN(user_length, data_length - *offset);
 70 
 71   /* copy data to user starting from the first byte they haven't seen */
 72   memcpy(user_buffer, data + *offset, user_length);
 73   *offset += user_length;
 74 
 75   /* tell them how much data they got */
 76   return user_length;
 77 }
 78 
 79 ssize_t echo_write(struct fusd_file_info *file, const char *user_buffer,
 80                    size_t user_length, loff_t *offset)
 81 {
 82   /* free the old data, if any */
 83   if (data != NULL) {
 84     free(data);
 85     data = NULL;
 86     data_length = 0;
 87   }
 88 
 89   /* allocate space for new data; return error if that fails */
 90   if ((data = malloc(user_length)) == NULL)
 91     return -ENOMEM;
 92 
 93   /* make a copy of user's data; tell the user we copied everything */
 94   memcpy(data, user_buffer, user_length);
 95   data_length = user_length;
 96   return user_length;
 97 }
 98 /* EXAMPLE STOP */
 99 
100 int do_open_or_close(struct fusd_file_info *file)
101 {
102   return 0; /* opens and closes always succeed */
103 }
104 
105 
106 struct fusd_file_operations echo_fops = {
107   open: do_open_or_close,
108   read: echo_read,
109   write: echo_write,
110   close: do_open_or_close
111 };
112 
113 
114 int main(int argc, char *argv[])
115 {
116   if (fusd_register("/dev/echo", 0666, NULL, &echo_fops) < 0) {
117     perror("register of /dev/echo failed");
118     exit(1);
119   }
120 
121   fprintf(stderr, "calling fusd_run...\n");
122   fusd_run();
123   return 0;
124 }
125 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.