|
Packit |
6c4009 |
/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
int
|
|
Packit |
6c4009 |
main (int argc, char **argv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
static const char hello[] = "Hello, world.\n";
|
|
Packit |
6c4009 |
static const char replace[] = "Hewwo, world.\n";
|
|
Packit |
6c4009 |
static const size_t replace_from = 2, replace_to = 4;
|
|
Packit |
6c4009 |
char filename[FILENAME_MAX];
|
|
Packit |
6c4009 |
char *name = strrchr (*argv, '/');
|
|
Packit |
6c4009 |
char buf[BUFSIZ];
|
|
Packit |
6c4009 |
FILE *f;
|
|
Packit |
6c4009 |
int lose = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (name != NULL)
|
|
Packit |
6c4009 |
++name;
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
name = *argv;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(void) sprintf (filename, OBJPFX "%s.test", name);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
f = fopen (filename, "w+");
|
|
Packit |
6c4009 |
if (f == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
perror (filename);
|
|
Packit |
6c4009 |
exit (1);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
(void) fputs (hello, f);
|
|
Packit |
6c4009 |
rewind (f);
|
|
Packit |
6c4009 |
(void) fgets (buf, sizeof (buf), f);
|
|
Packit |
6c4009 |
rewind (f);
|
|
Packit |
6c4009 |
(void) fputs (buf, f);
|
|
Packit |
6c4009 |
rewind (f);
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t i;
|
|
Packit |
6c4009 |
for (i = 0; i < replace_from; ++i)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int c = getc (f);
|
|
Packit |
6c4009 |
if (c == EOF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("EOF at %Zu.\n", i);
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (c != hello[i])
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("Got '%c' instead of '%c' at %Zu.\n",
|
|
Packit |
6c4009 |
(unsigned char) c, hello[i], i);
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
long int where = ftell (f);
|
|
Packit |
6c4009 |
if (where == (long int) replace_from)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
size_t i;
|
|
Packit |
6c4009 |
for (i = replace_from; i < replace_to; ++i)
|
|
Packit |
6c4009 |
if (putc(replace[i], f) == EOF)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("putc('%c') got %s at %Zu.\n",
|
|
Packit |
6c4009 |
replace[i], strerror (errno), i);
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (where == -1L)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("ftell got %s (should be at %Zu).\n",
|
|
Packit |
6c4009 |
strerror (errno), replace_from);
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("ftell returns %lu; should be %Zu.\n", where, replace_from);
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!lose)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
rewind (f);
|
|
Packit |
6c4009 |
if (fgets (buf, sizeof (buf), f) == NULL)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("fgets got %s.\n", strerror(errno));
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
else if (strcmp (buf, replace))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("Read \"%s\" instead of \"%s\".\n", buf, replace);
|
|
Packit |
6c4009 |
lose = 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (lose)
|
|
Packit |
6c4009 |
printf ("Test FAILED! Losing file is \"%s\".\n", filename);
|
|
Packit |
6c4009 |
else
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
(void) remove (filename);
|
|
Packit |
6c4009 |
puts ("Test succeeded.");
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return lose ? EXIT_FAILURE : EXIT_SUCCESS;
|
|
Packit |
6c4009 |
}
|