1 /* Copyright (C) 1991, 1992, 1996 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 Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 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 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
26 main (int argc
, char **argv
)
28 static const char hello
[] = "Hello, world.\n";
29 static const char replace
[] = "Hewwo, world.\n";
30 static const size_t replace_from
= 2, replace_to
= 4;
31 char filename
[FILENAME_MAX
];
32 char *name
= strrchr(*argv
, '/');
42 (void) sprintf(filename
, "/tmp/%s.test", name
);
44 f
= fopen(filename
, "w+");
51 (void) fputs(hello
, f
);
53 (void) fgets(buf
, sizeof(buf
), f
);
59 for (i
= 0; i
< replace_from
; ++i
)
64 printf("EOF at %u.\n", i
);
68 else if (c
!= hello
[i
])
70 printf("Got '%c' instead of '%c' at %u.\n",
71 (unsigned char) c
, hello
[i
], i
);
79 long int where
= ftell(f
);
80 if (where
== (long int) replace_from
)
83 for (i
= replace_from
; i
< replace_to
; ++i
)
84 if (putc(replace
[i
], f
) == EOF
)
86 printf("putc('%c') got %s at %u.\n",
87 replace
[i
], strerror(errno
), i
);
92 else if (where
== -1L)
94 printf("ftell got %s (should be at %u).\n",
95 strerror(errno
), replace_from
);
100 printf("ftell returns %lu; should be %u.\n", where
, replace_from
);
108 if (fgets(buf
, sizeof(buf
), f
) == NULL
)
110 printf("fgets got %s.\n", strerror(errno
));
113 else if (strcmp(buf
, replace
))
115 printf("Read \"%s\" instead of \"%s\".\n", buf
, replace
);
121 printf("Test FAILED! Losing file is \"%s\".\n", filename
);
124 (void) remove(filename
);
125 puts("Test succeeded.");
128 exit(lose
? EXIT_FAILURE
: EXIT_SUCCESS
);