1 /* Copyright (C) 1991-2016 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/>. */
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
, '/');
41 (void) sprintf (filename
, "/tmp/%s.test", name
);
43 f
= fopen (filename
, "w+");
50 (void) fputs (hello
, f
);
52 (void) fgets (buf
, sizeof (buf
), f
);
54 (void) fputs (buf
, f
);
58 for (i
= 0; i
< replace_from
; ++i
)
63 printf ("EOF at %Zu.\n", i
);
67 else if (c
!= hello
[i
])
69 printf ("Got '%c' instead of '%c' at %Zu.\n",
70 (unsigned char) c
, hello
[i
], i
);
78 long int where
= ftell (f
);
79 if (where
== (long int) replace_from
)
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
);
91 else if (where
== -1L)
93 printf ("ftell got %s (should be at %Zu).\n",
94 strerror (errno
), replace_from
);
99 printf ("ftell returns %lu; should be %Zu.\n", where
, replace_from
);
107 if (fgets (buf
, sizeof (buf
), f
) == NULL
)
109 printf ("fgets got %s.\n", strerror(errno
));
112 else if (strcmp (buf
, replace
))
114 printf ("Read \"%s\" instead of \"%s\".\n", buf
, replace
);
120 printf ("Test FAILED! Losing file is \"%s\".\n", filename
);
123 (void) remove (filename
);
124 puts ("Test succeeded.");
127 return lose
? EXIT_FAILURE
: EXIT_SUCCESS
;