Back to index

glibc  2.9
tst-cookie.c
Go to the documentation of this file.
00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <fcntl.h>
00004 #include <errno.h>
00005 
00006 #include <stdio.h>
00007 
00008 
00009 #define THE_COOKIE ((void *) 0xdeadbeeful)
00010 
00011 static int errors;
00012 
00013 
00014 static int cookieread_called;
00015 static ssize_t
00016 cookieread (void *cookie, char *buf, size_t count)
00017 {
00018   printf ("`%s' called with cookie %#lx\n", __FUNCTION__,
00019          (unsigned long int) cookie);
00020   if (cookie != THE_COOKIE)
00021     ++errors;
00022   cookieread_called = 1;
00023   return 42;
00024 }
00025 
00026 
00027 static int cookiewrite_called;
00028 static ssize_t
00029 cookiewrite (void *cookie, const char *buf, size_t count)
00030 {
00031   printf ("`%s' called with cookie %#lx\n", __FUNCTION__,
00032          (unsigned long int) cookie);
00033   if (cookie != THE_COOKIE)
00034     ++errors;
00035   cookiewrite_called = 1;
00036   return 43;
00037 }
00038 
00039 
00040 static int cookieseek_called;
00041 static int
00042 cookieseek (void *cookie, off64_t *offset, int whence)
00043 {
00044   printf ("`%s' called with cookie %#lx\n", __FUNCTION__,
00045          (unsigned long int) cookie);
00046   if (cookie != THE_COOKIE)
00047     ++errors;
00048   cookieseek_called = 1;
00049   return 44;
00050 }
00051 
00052 
00053 static int cookieclose_called;
00054 static int
00055 cookieclose (void *cookie)
00056 {
00057   printf ("`%s' called with cookie %#lx\n", __FUNCTION__,
00058          (unsigned long int) cookie);
00059   if (cookie != THE_COOKIE)
00060     ++errors;
00061   cookieclose_called = 1;
00062   return 45;
00063 }
00064 
00065 
00066 int
00067 main (void)
00068 {
00069   cookie_io_functions_t fcts;
00070   char buf[1];
00071   FILE *f;
00072 
00073   fcts.read = cookieread;
00074   fcts.seek = cookieseek;
00075   fcts.close = cookieclose;
00076   fcts.write = cookiewrite;
00077 
00078   f = fopencookie (THE_COOKIE, "r+", fcts);
00079 
00080   fread (buf, 1, 1, f);
00081   fwrite (buf, 1, 1, f);
00082   fseek (f, 0, SEEK_CUR);
00083   fclose (f);
00084 
00085   if (cookieread_called == 0
00086       || cookiewrite_called == 0
00087       || cookieseek_called == 0
00088       || cookieclose_called == 0)
00089     ++errors;
00090 
00091   return errors != 0;
00092 }