Files
glibc/stdio-common/test_rdwr.c
T

130 lines
2.9 KiB
C
Raw Normal View History

/* Copyright (C) 1991,1992,1996,1997,1998,2000 Free Software Foundation, Inc.
1997-02-15 04:31:36 +00:00
This file is part of the GNU C Library.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is free software; you can redistribute it and/or
2001-07-06 04:58:11 +00:00
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2001-07-06 04:58:11 +00:00
Lesser General Public License for more details.
1995-02-18 01:27:10 +00:00
2001-07-06 04:58:11 +00:00
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
1995-02-18 01:27:10 +00:00
#include <errno.h>
1995-02-18 01:27:10 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
1996-10-31 02:57:12 +00:00
main (int argc, char **argv)
1995-02-18 01:27:10 +00:00
{
1996-10-31 02:57:12 +00:00
static const char hello[] = "Hello, world.\n";
static const char replace[] = "Hewwo, world.\n";
static const size_t replace_from = 2, replace_to = 4;
1995-02-18 01:27:10 +00:00
char filename[FILENAME_MAX];
1997-02-15 04:31:36 +00:00
char *name = strrchr (*argv, '/');
1995-02-18 01:27:10 +00:00
char buf[BUFSIZ];
FILE *f;
int lose = 0;
if (name != NULL)
++name;
else
name = *argv;
1997-02-15 04:31:36 +00:00
(void) sprintf (filename, "/tmp/%s.test", name);
1995-02-18 01:27:10 +00:00
1997-02-15 04:31:36 +00:00
f = fopen (filename, "w+");
1995-02-18 01:27:10 +00:00
if (f == NULL)
{
1997-02-15 04:31:36 +00:00
perror (filename);
exit (1);
1995-02-18 01:27:10 +00:00
}
1997-02-15 04:31:36 +00:00
(void) fputs (hello, f);
rewind (f);
(void) fgets (buf, sizeof (buf), f);
rewind (f);
(void) fputs (buf, f);
rewind (f);
1995-02-18 01:27:10 +00:00
{
1997-02-15 04:31:36 +00:00
size_t i;
1995-02-18 01:27:10 +00:00
for (i = 0; i < replace_from; ++i)
{
1997-02-15 04:31:36 +00:00
int c = getc (f);
1995-02-18 01:27:10 +00:00
if (c == EOF)
{
1998-07-04 10:39:13 +00:00
printf ("EOF at %Zu.\n", i);
1995-02-18 01:27:10 +00:00
lose = 1;
break;
}
else if (c != hello[i])
{
1998-07-04 10:39:13 +00:00
printf ("Got '%c' instead of '%c' at %Zu.\n",
1997-02-15 04:31:36 +00:00
(unsigned char) c, hello[i], i);
1995-02-18 01:27:10 +00:00
lose = 1;
break;
}
}
}
{
1997-02-15 04:31:36 +00:00
long int where = ftell (f);
1996-10-31 02:57:12 +00:00
if (where == (long int) replace_from)
1995-02-18 01:27:10 +00:00
{
register size_t i;
for (i = replace_from; i < replace_to; ++i)
if (putc(replace[i], f) == EOF)
{
1998-07-04 10:39:13 +00:00
printf ("putc('%c') got %s at %Zu.\n",
1997-02-15 04:31:36 +00:00
replace[i], strerror (errno), i);
1995-02-18 01:27:10 +00:00
lose = 1;
break;
}
}
else if (where == -1L)
{
1998-07-06 17:09:00 +00:00
printf ("ftell got %s (should be at %Zu).\n",
1997-02-15 04:31:36 +00:00
strerror (errno), replace_from);
1995-02-18 01:27:10 +00:00
lose = 1;
}
else
{
1998-07-04 10:39:13 +00:00
printf ("ftell returns %lu; should be %Zu.\n", where, replace_from);
1995-02-18 01:27:10 +00:00
lose = 1;
}
}
if (!lose)
{
1997-02-15 04:31:36 +00:00
rewind (f);
if (fgets (buf, sizeof (buf), f) == NULL)
1995-02-18 01:27:10 +00:00
{
1997-02-15 04:31:36 +00:00
printf ("fgets got %s.\n", strerror(errno));
1995-02-18 01:27:10 +00:00
lose = 1;
}
1997-02-15 04:31:36 +00:00
else if (strcmp (buf, replace))
1995-02-18 01:27:10 +00:00
{
1997-02-15 04:31:36 +00:00
printf ("Read \"%s\" instead of \"%s\".\n", buf, replace);
1995-02-18 01:27:10 +00:00
lose = 1;
}
}
if (lose)
1997-02-15 04:31:36 +00:00
printf ("Test FAILED! Losing file is \"%s\".\n", filename);
1995-02-18 01:27:10 +00:00
else
{
1997-02-15 04:31:36 +00:00
(void) remove (filename);
puts ("Test succeeded.");
1995-02-18 01:27:10 +00:00
}
return lose ? EXIT_FAILURE : EXIT_SUCCESS;
1995-02-18 01:27:10 +00:00
}