1 | /* Copyright (C) 1991-2022 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, see |
16 | <https://www.gnu.org/licenses/>. */ |
17 | |
18 | #include <errno.h> |
19 | #include <stdio.h> |
20 | #include <stdlib.h> |
21 | #include <string.h> |
22 | |
23 | |
24 | int |
25 | main (int argc, char **argv) |
26 | { |
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; |
35 | |
36 | if (name != NULL) |
37 | ++name; |
38 | else |
39 | name = *argv; |
40 | |
41 | (void) sprintf (filename, OBJPFX "%s.test" , name); |
42 | |
43 | f = fopen (filename, "w+" ); |
44 | if (f == NULL) |
45 | { |
46 | perror (filename); |
47 | exit (1); |
48 | } |
49 | |
50 | (void) fputs (hello, f); |
51 | rewind (f); |
52 | (void) fgets (s: buf, n: sizeof (buf), stream: f); |
53 | rewind (f); |
54 | (void) fputs (buf, f); |
55 | rewind (f); |
56 | { |
57 | size_t i; |
58 | for (i = 0; i < replace_from; ++i) |
59 | { |
60 | int c = getc (stream: f); |
61 | if (c == EOF) |
62 | { |
63 | printf (format: "EOF at %Zu.\n" , i); |
64 | lose = 1; |
65 | break; |
66 | } |
67 | else if (c != hello[i]) |
68 | { |
69 | printf (format: "Got '%c' instead of '%c' at %Zu.\n" , |
70 | (unsigned char) c, hello[i], i); |
71 | lose = 1; |
72 | break; |
73 | } |
74 | } |
75 | } |
76 | |
77 | { |
78 | long int where = ftell (stream: f); |
79 | if (where == (long int) replace_from) |
80 | { |
81 | size_t i; |
82 | for (i = replace_from; i < replace_to; ++i) |
83 | if (putc(c: replace[i], stream: f) == EOF) |
84 | { |
85 | printf (format: "putc('%c') got %s at %Zu.\n" , |
86 | replace[i], strerror (errno), i); |
87 | lose = 1; |
88 | break; |
89 | } |
90 | } |
91 | else if (where == -1L) |
92 | { |
93 | printf (format: "ftell got %s (should be at %Zu).\n" , |
94 | strerror (errno), replace_from); |
95 | lose = 1; |
96 | } |
97 | else |
98 | { |
99 | printf (format: "ftell returns %lu; should be %Zu.\n" , where, replace_from); |
100 | lose = 1; |
101 | } |
102 | } |
103 | |
104 | if (!lose) |
105 | { |
106 | rewind (f); |
107 | if (fgets (s: buf, n: sizeof (buf), stream: f) == NULL) |
108 | { |
109 | printf (format: "fgets got %s.\n" , strerror(errno)); |
110 | lose = 1; |
111 | } |
112 | else if (strcmp (buf, replace)) |
113 | { |
114 | printf (format: "Read \"%s\" instead of \"%s\".\n" , buf, replace); |
115 | lose = 1; |
116 | } |
117 | } |
118 | |
119 | if (lose) |
120 | printf (format: "Test FAILED! Losing file is \"%s\".\n" , filename); |
121 | else |
122 | { |
123 | (void) remove (filename); |
124 | puts (s: "Test succeeded." ); |
125 | } |
126 | |
127 | return lose ? EXIT_FAILURE : EXIT_SUCCESS; |
128 | } |
129 | |