add changelog for previous commit
[glibc.git] / stdio-common / test_rdwr.c
blobe85404ae82a79294d977634340b937f139846735
1 /* Copyright (C) 1991-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library 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 GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
24 int
25 main (int argc, char **argv)
27 static const char hello[] = "Hello, world.\n";
28 static const char replace[] = "Hewwo, world.\n";
29 static const size_t replace_from = 2, replace_to = 4;
30 char filename[FILENAME_MAX];
31 char *name = strrchr (*argv, '/');
32 char buf[BUFSIZ];
33 FILE *f;
34 int lose = 0;
36 if (name != NULL)
37 ++name;
38 else
39 name = *argv;
41 (void) sprintf (filename, "/tmp/%s.test", name);
43 f = fopen (filename, "w+");
44 if (f == NULL)
46 perror (filename);
47 exit (1);
50 (void) fputs (hello, f);
51 rewind (f);
52 (void) fgets (buf, sizeof (buf), f);
53 rewind (f);
54 (void) fputs (buf, f);
55 rewind (f);
57 size_t i;
58 for (i = 0; i < replace_from; ++i)
60 int c = getc (f);
61 if (c == EOF)
63 printf ("EOF at %Zu.\n", i);
64 lose = 1;
65 break;
67 else if (c != hello[i])
69 printf ("Got '%c' instead of '%c' at %Zu.\n",
70 (unsigned char) c, hello[i], i);
71 lose = 1;
72 break;
78 long int where = ftell (f);
79 if (where == (long int) replace_from)
81 size_t i;
82 for (i = replace_from; i < replace_to; ++i)
83 if (putc(replace[i], f) == EOF)
85 printf ("putc('%c') got %s at %Zu.\n",
86 replace[i], strerror (errno), i);
87 lose = 1;
88 break;
91 else if (where == -1L)
93 printf ("ftell got %s (should be at %Zu).\n",
94 strerror (errno), replace_from);
95 lose = 1;
97 else
99 printf ("ftell returns %lu; should be %Zu.\n", where, replace_from);
100 lose = 1;
104 if (!lose)
106 rewind (f);
107 if (fgets (buf, sizeof (buf), f) == NULL)
109 printf ("fgets got %s.\n", strerror(errno));
110 lose = 1;
112 else if (strcmp (buf, replace))
114 printf ("Read \"%s\" instead of \"%s\".\n", buf, replace);
115 lose = 1;
119 if (lose)
120 printf ("Test FAILED! Losing file is \"%s\".\n", filename);
121 else
123 (void) remove (filename);
124 puts ("Test succeeded.");
127 return lose ? EXIT_FAILURE : EXIT_SUCCESS;