Back to index

glibc  2.9
tst-fmemopen2.c
Go to the documentation of this file.
00001 #include <assert.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <sys/types.h>
00005 
00006 
00007 static int
00008 do_test (void)
00009 {
00010   int result = 0;
00011   char buf[100];
00012   FILE *fp = fmemopen (buf, sizeof (buf), "w");
00013   if (fp == NULL)
00014     {
00015       puts ("fmemopen failed");
00016       return 0;
00017     }
00018   static const char str[] = "hello world";
00019 #define nstr (sizeof (str) - 1)
00020   fputs (str, fp);
00021   off_t o = ftello (fp);
00022   if (o != nstr)
00023     {
00024       printf ("first ftello returned %ld, expected %zu\n", o, nstr);
00025       result = 1;
00026     }
00027   rewind (fp);
00028   o = ftello (fp);
00029   if (o != 0)
00030     {
00031       printf ("second ftello returned %ld, expected 0\n", o);
00032       result = 1;
00033     }
00034   if (fseeko (fp, 0, SEEK_END) != 0)
00035     {
00036       puts ("fseeko failed");
00037       return 1;
00038     }
00039   o = ftello (fp);
00040   if (o != nstr)
00041     {
00042       printf ("third ftello returned %ld, expected %zu\n", o, nstr);
00043       result = 1;
00044     }
00045   rewind (fp);
00046   static const char str2[] = "just hello";
00047 #define nstr2 (sizeof (str2) - 1)
00048   assert (nstr2 < nstr);
00049   fputs (str2, fp);
00050   o = ftello (fp);
00051   if (o != nstr2)
00052     {
00053       printf ("fourth ftello returned %ld, expected %zu\n", o, nstr2);
00054       result = 1;
00055     }
00056   fclose (fp);
00057   static const char str3[] = "just hellod";
00058   if (strcmp (buf, str3) != 0)
00059     {
00060       printf ("final string is \"%s\", expected \"%s\"\n",
00061               buf, str3);
00062       result = 1;
00063     }
00064   return result;
00065 }
00066 
00067 #define TEST_FUNCTION do_test ()
00068 #include "../test-skeleton.c"