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

Linux Cross Reference
cvs/emstar/fusd/examples/console-read.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  * console-read: This example demonstrates the easiest possible way to
 38  * block a client system call: by blocking the driver itself.  Not
 39  * recommended for anything but the most trivial drivers -- if you
 40  * need a template from which to start on a real driver, use pager.c
 41  * instead.
 42  *
 43  * $Id: console-read.c,v 1.6 2007-12-08 04:05:01 girod Exp $
 44  */
 45 
 46 #include <stdio.h>
 47 #include <string.h>
 48 #include <errno.h>
 49 
 50 #include <fusd/cygwin_compat.h>
 51 
 52 #include "fusd.h"
 53 
 54 #define MIN(x, y) ((x) < (y) ? (x) : (y))
 55 
 56 int do_open_or_close(struct fusd_file_info *file)
 57 {
 58   return 0; /* attempts to open and close this file always succeed */
 59 }
 60 
 61 /* EXAMPLE START console-read.c */
 62 ssize_t do_read(struct fusd_file_info *file, char *user_buffer, 
 63             size_t user_length, loff_t *offset)
 64 {
 65   char buf[128];
 66   
 67   if (*offset > 0)
 68     return 0;
 69 
 70   /* print a prompt */
 71   printf("Got a read from pid %d.  What do we say?\n> ", file->pid);
 72   fflush(stdout);
 73 
 74   /* get a response from the console */
 75   if (fgets(buf, sizeof(buf) - 1, stdin) == NULL)
 76     return 0;
 77 
 78   /* compute length of the response, and return */
 79   user_length = MIN(user_length, strlen(buf));
 80   memcpy(user_buffer, buf, user_length);
 81   *offset += user_length;
 82   return user_length;
 83 }
 84 /* EXAMPLE STOP */
 85 
 86 int main(int argc, char *argv[])
 87 {
 88   struct fusd_file_operations fops = {
 89     open: do_open_or_close,
 90     read: do_read,
 91     close: do_open_or_close };
 92   
 93   if (fusd_register("/dev/console-read", 0666, NULL, &fops) < 0)
 94     perror("Unable to register device");
 95   else {
 96     printf("/dev/console-read should now exist - calling fusd_run...\n");
 97     fusd_run();
 98   }
 99   return 0;
100 }
101 
102 

~ [ 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.