1 | /* Copyright (C) 1991,1992,1996,1997,1998,2000 Free Software Foundation, Inc.
|
---|
2 | This file is part of the GNU C Library.
|
---|
3 |
|
---|
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.
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU Lesser General Public
|
---|
15 | License along with the GNU C Library; if not, write to the Free
|
---|
16 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
17 | 02111-1307 USA. */
|
---|
18 |
|
---|
19 | #include <errno.h>
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <string.h>
|
---|
23 |
|
---|
24 |
|
---|
25 | int
|
---|
26 | main (int argc, char **argv)
|
---|
27 | {
|
---|
28 | static const char hello[] = "Hello, world.\n";
|
---|
29 | static const char replace[] = "Hewwo, world.\n";
|
---|
30 | static const size_t replace_from = 2, replace_to = 4;
|
---|
31 | char filename[FILENAME_MAX];
|
---|
32 | char *name = strrchr (*argv, '/');
|
---|
33 | char buf[BUFSIZ];
|
---|
34 | FILE *f;
|
---|
35 | int lose = 0;
|
---|
36 |
|
---|
37 | if (name != NULL)
|
---|
38 | ++name;
|
---|
39 | else
|
---|
40 | name = *argv;
|
---|
41 |
|
---|
42 | (void) sprintf (filename, "/tmp/%s.test", name);
|
---|
43 |
|
---|
44 | f = fopen (filename, "w+");
|
---|
45 | if (f == NULL)
|
---|
46 | {
|
---|
47 | perror (filename);
|
---|
48 | exit (1);
|
---|
49 | }
|
---|
50 |
|
---|
51 | (void) fputs (hello, f);
|
---|
52 | rewind (f);
|
---|
53 | (void) fgets (buf, sizeof (buf), f);
|
---|
54 | rewind (f);
|
---|
55 | (void) fputs (buf, f);
|
---|
56 | rewind (f);
|
---|
57 | {
|
---|
58 | size_t i;
|
---|
59 | for (i = 0; i < replace_from; ++i)
|
---|
60 | {
|
---|
61 | int c = getc (f);
|
---|
62 | if (c == EOF)
|
---|
63 | {
|
---|
64 | printf ("EOF at %Zu.\n", i);
|
---|
65 | lose = 1;
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | else if (c != hello[i])
|
---|
69 | {
|
---|
70 | printf ("Got '%c' instead of '%c' at %Zu.\n",
|
---|
71 | (unsigned char) c, hello[i], i);
|
---|
72 | lose = 1;
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | {
|
---|
79 | long int where = ftell (f);
|
---|
80 | if (where == (long int) replace_from)
|
---|
81 | {
|
---|
82 | register size_t i;
|
---|
83 | for (i = replace_from; i < replace_to; ++i)
|
---|
84 | if (putc(replace[i], f) == EOF)
|
---|
85 | {
|
---|
86 | printf ("putc('%c') got %s at %Zu.\n",
|
---|
87 | replace[i], strerror (errno), i);
|
---|
88 | lose = 1;
|
---|
89 | break;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | else if (where == -1L)
|
---|
93 | {
|
---|
94 | printf ("ftell got %s (should be at %Zu).\n",
|
---|
95 | strerror (errno), replace_from);
|
---|
96 | lose = 1;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | {
|
---|
100 | printf ("ftell returns %lu; should be %Zu.\n", where, replace_from);
|
---|
101 | lose = 1;
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | if (!lose)
|
---|
106 | {
|
---|
107 | rewind (f);
|
---|
108 | if (fgets (buf, sizeof (buf), f) == NULL)
|
---|
109 | {
|
---|
110 | printf ("fgets got %s.\n", strerror(errno));
|
---|
111 | lose = 1;
|
---|
112 | }
|
---|
113 | else if (strcmp (buf, replace))
|
---|
114 | {
|
---|
115 | printf ("Read \"%s\" instead of \"%s\".\n", buf, replace);
|
---|
116 | lose = 1;
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | if (lose)
|
---|
121 | printf ("Test FAILED! Losing file is \"%s\".\n", filename);
|
---|
122 | else
|
---|
123 | {
|
---|
124 | (void) remove (filename);
|
---|
125 | puts ("Test succeeded.");
|
---|
126 | }
|
---|
127 |
|
---|
128 | return lose ? EXIT_FAILURE : EXIT_SUCCESS;
|
---|
129 | }
|
---|