Back to index

glibc  2.9
test-fwrite.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 
00004 int
00005 main (int argc, char *argv[])
00006 {
00007   FILE *f = tmpfile ();
00008   char obuf[99999], ibuf[sizeof obuf];
00009   char *line;
00010   size_t linesz;
00011 
00012   if (! f)
00013     {
00014       perror ("tmpfile");
00015       return 1;
00016     }
00017 
00018   if (fputs ("line\n", f) == EOF)
00019     {
00020       perror ("fputs");
00021       return 1;
00022     }
00023 
00024   memset (obuf, 'z', sizeof obuf);
00025   memset (ibuf, 'y', sizeof ibuf);
00026 
00027   if (fwrite (obuf, sizeof obuf, 1, f) != 1)
00028     {
00029       perror ("fwrite");
00030       return 1;
00031     }
00032 
00033   rewind (f);
00034 
00035   line = NULL;
00036   linesz = 0;
00037   if (getline (&line, &linesz, f) != 5)
00038     {
00039       perror ("getline");
00040       return 1;
00041     }
00042   if (strcmp (line, "line\n"))
00043     {
00044       puts ("Lines differ.  Test FAILED!");
00045       return 1;
00046     }
00047 
00048   if (fread (ibuf, sizeof ibuf, 1, f) != 1)
00049     {
00050       perror ("fread");
00051       return 1;
00052     }
00053 
00054   if (memcmp (ibuf, obuf, sizeof ibuf))
00055     {
00056       puts ("Buffers differ.  Test FAILED!");
00057       return 1;
00058     }
00059 
00060   asprintf (&line, "\
00061 GDB is free software and you are welcome to distribute copies of it\n\
00062  under certain conditions; type \"show copying\" to see the conditions.\n\
00063 There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\
00064 ");
00065 
00066   puts ("Test succeeded.");
00067   return 0;
00068 }