Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  1 /* sculltest.c
  2  * A simple example of a C program to test some of the
  3  * operations of the "/dev/scull" device (a.k.a "scull0"),
  4  * and the 
  5  * ($Id: sculltest.c,v 1.1 2010/05/19 20:40:00 baker Exp baker $)
  6  */
  7 
  8 #include <unistd.h>
  9 #include <string.h>
 10 #include <stdio.h>
 11 #include <fcntl.h>
 12 
 13 int main() {
 14         int fd, result, len;
 15         char buf[10];
 16         const char *str;
 17         if ((fd = open ("/dev/scull", O_WRONLY)) == -1) {
 18                 perror("open failed");
 19                 return -1;
 20         }
 21         str = "abcde"; len = strlen(str);
 22         if ((result = write (fd, str, len)) != len) {
 23                 perror("write failed");
 24                 return -1;
 25         }
 26         close(fd);
 27         if ((result = read (fd, &buf, sizeof(buf))) != len) {
 28                 perror("read failed");
 29                 return -1;
 30         } 
 31         buf[result] = '\0';
 32         if (strncmp (buf, str, len)) {
 33                 fprintf (stdout, "failed: read back \"%s\"\n", buf);
 34         } else {
 35                 fprintf (stdout, "passed\n");
 36         }
 37         close(fd);
 38 
 39 
 40         str = "xyz"; len = strlen(str);
 41         if ((fd = open ("/dev/scullpipe", O_RDWR)) == -1) {
 42                 perror("open failed");
 43                 return -1;
 44         }
 45         if ((result = write (fd, str, len)) != len) {
 46                 perror("write failed");
 47                 return -1;
 48         }
 49         if ((result = read (fd, &buf, sizeof(buf))) != len) {
 50                 perror("read failed");
 51                 return -1;
 52         }
 53         buf[result] = '\0';
 54         if (strncmp (buf, str, len)) {
 55                 fprintf (stdout, "failed: read back \"%s\"\n", buf);
 56         } else {
 57                 fprintf (stdout, "passed\n");
 58         }
 59         close(fd);
 60         return 0;
 61 
 62 }
 63 
  This page was automatically generated by the LXR engine.