maint: update copyright
[gnulib.git] / tests / test-ftell3.c
1 /* Test of ftell() function.
2    Copyright (C) 2007-2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include <config.h>
18
19 /* None of the files accessed by this test are large, so disable the
20    fseek link warning if we are not using the gnulib fseek module.  */
21 #define _GL_NO_LARGE_FILES
22 #include <stdio.h>
23
24 #include <string.h>
25
26 #include "macros.h"
27
28 #define TESTFILE "t-ftell3.tmp"
29
30 int
31 main (void)
32 {
33   FILE *fp;
34
35   /* Create a file with some contents.  */
36   fp = fopen (TESTFILE, "w");
37   if (fp == NULL)
38     goto skip;
39   if (fwrite ("foogarsh", 1, 8, fp) < 8)
40     goto skip;
41   if (fclose (fp))
42     goto skip;
43
44   /* The file's contents is now "foogarsh".  */
45
46   /* Try writing after reading to EOF.  */
47   fp = fopen (TESTFILE, "r+");
48   if (fp == NULL)
49     goto skip;
50   if (fseek (fp, -1, SEEK_END))
51     goto skip;
52   ASSERT (getc (fp) == 'h');
53   ASSERT (getc (fp) == EOF);
54   ASSERT (ftell (fp) == 8);
55   ASSERT (ftell (fp) == 8);
56   ASSERT (putc ('!', fp) == '!');
57   ASSERT (ftell (fp) == 9);
58   ASSERT (fclose (fp) == 0);
59   fp = fopen (TESTFILE, "r");
60   if (fp == NULL)
61     goto skip;
62   {
63     char buf[10];
64     ASSERT (fread (buf, 1, 10, fp) == 9);
65     ASSERT (memcmp (buf, "foogarsh!", 9) == 0);
66   }
67   ASSERT (fclose (fp) == 0);
68
69   /* The file's contents is now "foogarsh!".  */
70
71   remove (TESTFILE);
72   return 0;
73
74  skip:
75   fprintf (stderr, "Skipping test: prerequisite file operations failed.\n");
76   remove (TESTFILE);
77   return 77;
78 }