Back to index

glibc  2.9
tst-eof.c
Go to the documentation of this file.
00001 #include <fcntl.h>
00002 #include <stdio.h>
00003 #include <string.h>
00004 #include <unistd.h>
00005 
00006 
00007 static void do_prepare (void);
00008 #define PREPARE(argc, argv) do_prepare ()
00009 static int do_test (void);
00010 #define TEST_FUNCTION do_test ()
00011 #include <test-skeleton.c>
00012 
00013 
00014 int fd;
00015 
00016 
00017 static void
00018 do_prepare (void)
00019 {
00020   fd = create_temp_file ("tst-eof.", NULL);
00021   if (fd == -1)
00022     {
00023       printf ("cannot create temporary file: %m\n");
00024       exit (1);
00025     }
00026 }
00027 
00028 
00029 static int
00030 do_test (void)
00031 {
00032   char buf[40];
00033   FILE *fp;
00034 
00035   if (write (fd, "some string\n", 12) != 12)
00036     {
00037       printf ("cannot write temporary file: %m\n");
00038       return 1;
00039     }
00040 
00041   if (lseek (fd, 0, SEEK_SET) == (off_t) -1)
00042     {
00043       printf ("cannot reposition temporary file: %m\n");
00044       return 1;
00045     }
00046 
00047   fp = fdopen (fd, "r");
00048   if (fp == NULL)
00049     {
00050       printf ("cannot create stream: %m\n");
00051       return 1;
00052     }
00053 
00054   if (feof (fp))
00055     {
00056       puts ("EOF set after fdopen");
00057       return 1;
00058     }
00059 
00060   if (fread (buf, 1, 20, fp) != 12)
00061     {
00062       puts ("didn't read the correct number of bytes");
00063       return 1;
00064     }
00065 
00066   if (! feof (fp))
00067     {
00068       puts ("EOF not set after fread");
00069       return 1;
00070     }
00071 
00072   fclose (fp);
00073 
00074   return 0;
00075 }