]>
Commit | Line | Data |
---|---|---|
568035b7 | 1 | /* Copyright (C) 1991-2013 Free Software Foundation, Inc. |
c84142e8 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
c84142e8 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
c84142e8 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
8f5ca04b | 18 | #include <errno.h> |
28f540f4 RM |
19 | #include <stdio.h> |
20 | #include <stdlib.h> | |
21 | #include <string.h> | |
22 | ||
23 | ||
24 | int | |
ba1ffaa1 | 25 | main (int argc, char **argv) |
28f540f4 | 26 | { |
ba1ffaa1 UD |
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; | |
28f540f4 | 30 | char filename[FILENAME_MAX]; |
c84142e8 | 31 | char *name = strrchr (*argv, '/'); |
28f540f4 RM |
32 | char buf[BUFSIZ]; |
33 | FILE *f; | |
34 | int lose = 0; | |
35 | ||
36 | if (name != NULL) | |
37 | ++name; | |
38 | else | |
39 | name = *argv; | |
40 | ||
c84142e8 | 41 | (void) sprintf (filename, "/tmp/%s.test", name); |
28f540f4 | 42 | |
c84142e8 | 43 | f = fopen (filename, "w+"); |
28f540f4 RM |
44 | if (f == NULL) |
45 | { | |
c84142e8 UD |
46 | perror (filename); |
47 | exit (1); | |
28f540f4 RM |
48 | } |
49 | ||
c84142e8 UD |
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); | |
28f540f4 | 56 | { |
c84142e8 | 57 | size_t i; |
28f540f4 RM |
58 | for (i = 0; i < replace_from; ++i) |
59 | { | |
c84142e8 | 60 | int c = getc (f); |
28f540f4 RM |
61 | if (c == EOF) |
62 | { | |
ba9234d9 | 63 | printf ("EOF at %Zu.\n", i); |
28f540f4 RM |
64 | lose = 1; |
65 | break; | |
66 | } | |
67 | else if (c != hello[i]) | |
68 | { | |
ba9234d9 | 69 | printf ("Got '%c' instead of '%c' at %Zu.\n", |
c84142e8 | 70 | (unsigned char) c, hello[i], i); |
28f540f4 RM |
71 | lose = 1; |
72 | break; | |
73 | } | |
74 | } | |
75 | } | |
76 | ||
77 | { | |
c84142e8 | 78 | long int where = ftell (f); |
ba1ffaa1 | 79 | if (where == (long int) replace_from) |
28f540f4 RM |
80 | { |
81 | register size_t i; | |
82 | for (i = replace_from; i < replace_to; ++i) | |
83 | if (putc(replace[i], f) == EOF) | |
84 | { | |
ba9234d9 | 85 | printf ("putc('%c') got %s at %Zu.\n", |
c84142e8 | 86 | replace[i], strerror (errno), i); |
28f540f4 RM |
87 | lose = 1; |
88 | break; | |
89 | } | |
90 | } | |
91 | else if (where == -1L) | |
92 | { | |
8eaaffde | 93 | printf ("ftell got %s (should be at %Zu).\n", |
c84142e8 | 94 | strerror (errno), replace_from); |
28f540f4 RM |
95 | lose = 1; |
96 | } | |
97 | else | |
98 | { | |
ba9234d9 | 99 | printf ("ftell returns %lu; should be %Zu.\n", where, replace_from); |
28f540f4 RM |
100 | lose = 1; |
101 | } | |
102 | } | |
103 | ||
104 | if (!lose) | |
105 | { | |
c84142e8 UD |
106 | rewind (f); |
107 | if (fgets (buf, sizeof (buf), f) == NULL) | |
28f540f4 | 108 | { |
c84142e8 | 109 | printf ("fgets got %s.\n", strerror(errno)); |
28f540f4 RM |
110 | lose = 1; |
111 | } | |
c84142e8 | 112 | else if (strcmp (buf, replace)) |
28f540f4 | 113 | { |
c84142e8 | 114 | printf ("Read \"%s\" instead of \"%s\".\n", buf, replace); |
28f540f4 RM |
115 | lose = 1; |
116 | } | |
117 | } | |
118 | ||
119 | if (lose) | |
c84142e8 | 120 | printf ("Test FAILED! Losing file is \"%s\".\n", filename); |
28f540f4 RM |
121 | else |
122 | { | |
c84142e8 UD |
123 | (void) remove (filename); |
124 | puts ("Test succeeded."); | |
28f540f4 RM |
125 | } |
126 | ||
bf4de8f3 | 127 | return lose ? EXIT_FAILURE : EXIT_SUCCESS; |
28f540f4 | 128 | } |