Regenerated: autoconf -m /gd/gnu/autoconf sysdeps/unix/configure.in
[glibc/history.git] / stdio-common / test_rdwr.c
blobf987f16cd4cc69b055c6e2ab92fab3aaefe0e94c
1 /* Copyright (C) 1991, 1992 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
26 int
27 DEFUN(main, (argc, argv), int argc AND char **argv)
29 static CONST char hello[] = "Hello, world.\n";
30 static CONST char replace[] = "Hewwo, world.\n";
31 static CONST size_t replace_from = 2, replace_to = 4;
32 char filename[FILENAME_MAX];
33 char *name = strrchr(*argv, '/');
34 char buf[BUFSIZ];
35 FILE *f;
36 int lose = 0;
38 if (name != NULL)
39 ++name;
40 else
41 name = *argv;
43 (void) sprintf(filename, "/tmp/%s.test", name);
45 f = fopen(filename, "w+");
46 if (f == NULL)
48 perror(filename);
49 exit(1);
52 (void) fputs(hello, f);
53 rewind(f);
54 (void) fgets(buf, sizeof(buf), f);
55 rewind(f);
56 (void) fputs(buf, f);
57 rewind(f);
59 register size_t i;
60 for (i = 0; i < replace_from; ++i)
62 int c = getc(f);
63 if (c == EOF)
65 printf("EOF at %u.\n", i);
66 lose = 1;
67 break;
69 else if (c != hello[i])
71 printf("Got '%c' instead of '%c' at %u.\n",
72 (unsigned char) c, hello[i], i);
73 lose = 1;
74 break;
80 long int where = ftell(f);
81 if (where == replace_from)
83 register size_t i;
84 for (i = replace_from; i < replace_to; ++i)
85 if (putc(replace[i], f) == EOF)
87 printf("putc('%c') got %s at %u.\n",
88 replace[i], strerror(errno), i);
89 lose = 1;
90 break;
93 else if (where == -1L)
95 printf("ftell got %s (should be at %u).\n",
96 strerror(errno), replace_from);
97 lose = 1;
99 else
101 printf("ftell returns %u; should be %u.\n", where, replace_from);
102 lose = 1;
106 if (!lose)
108 rewind(f);
109 if (fgets(buf, sizeof(buf), f) == NULL)
111 printf("fgets got %s.\n", strerror(errno));
112 lose = 1;
114 else if (strcmp(buf, replace))
116 printf("Read \"%s\" instead of \"%s\".\n", buf, replace);
117 lose = 1;
121 if (lose)
122 printf("Test FAILED! Losing file is \"%s\".\n", filename);
123 else
125 (void) remove(filename);
126 puts("Test succeeded.");
129 exit(lose ? EXIT_FAILURE : EXIT_SUCCESS);