annotate tests/test-freadptr2.c @ 9889:0be6f1ab456d

Flush the standard error stream before aborting.
author Bruno Haible <bruno@clisp.org>
date Fri, 11 Apr 2008 16:31:39 +0200
parents 569a44f4dd4d
children e8d2c6fc33ad
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9779
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Test of freadptr() function.
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2007-2008 Free Software Foundation, Inc.
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 (at your option) any later version.
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007. */
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 #include "freadptr.h"
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23 #include <stdio.h>
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdlib.h>
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25 #include <unistd.h>
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27 #define ASSERT(expr) \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 do \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 { \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 if (!(expr)) \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31 { \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
9889
0be6f1ab456d Flush the standard error stream before aborting.
Bruno Haible <bruno@clisp.org>
parents: 9779
diff changeset
33 fflush (stderr); \
9779
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 abort (); \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 } \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 } \
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 while (0)
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 static int
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 freadptrbufsize (FILE *fp)
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 {
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 size_t size = 0;
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44 freadptr (fp, &size);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 return size;
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 }
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 int
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 main (int argc, char **argv)
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 {
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51 int nbytes = atoi (argv[1]);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 if (nbytes > 0)
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 {
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 void *buf = malloc (nbytes);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 }
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 if (nbytes == 0)
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 ASSERT (freadptrbufsize (stdin) == 0);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 else
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61 {
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 if (lseek (0, 0, SEEK_CUR) == nbytes)
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 /* An unbuffered stdio, such as BeOS or on uClibc compiled without
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 __STDIO_BUFFERS. */
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 ASSERT (freadptrbufsize (stdin) == 0);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 else
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 /* Normal buffered stdio. */
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 ASSERT (freadptrbufsize (stdin) != 0);
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 }
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 return 0;
569a44f4dd4d Extend freadptr to return also the buffer size.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72 }