pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-freadptr.c
1 /* Test of freadptr() function.
2    Copyright (C) 2007-2013 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program 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
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2008.  */
18
19 #include <config.h>
20
21 #include "freadptr.h"
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "macros.h"
28
29 int
30 main (int argc, char **argv)
31 {
32   int nbytes = atoi (argv[1]);
33   void *buf = malloc (nbytes);
34   ASSERT (fread (buf, 1, nbytes, stdin) == nbytes);
35
36   if (lseek (0, 0, SEEK_CUR) == nbytes)
37     {
38       /* An unbuffered stdio, such as BeOS or on uClibc compiled without
39          __STDIO_BUFFERS.  Or stdin is a pipe.  */
40       size_t size;
41       ASSERT (freadptr (stdin, &size) == NULL);
42     }
43   else
44     {
45       /* Normal buffered stdio.  */
46       const char stdin_contents[] =
47         "#!/bin/sh\n\n./test-freadptr${EXEEXT} 5 < \"$srcdir/test-freadptr.sh\" || exit 1\ncat \"$srcdir/test-freadptr.sh\" | ./test-freadptr${EXEEXT} 5 || exit 1\nexit 0\n";
48       const char *expected = stdin_contents + nbytes;
49       size_t available1;
50       size_t available2;
51       size_t available3;
52
53       /* Test normal behaviour.  */
54       {
55         const char *ptr = freadptr (stdin, &available1);
56
57         ASSERT (ptr != NULL);
58         ASSERT (available1 != 0);
59         ASSERT (available1 <= strlen (expected));
60         ASSERT (memcmp (ptr, expected, available1) == 0);
61       }
62
63       /* Test behaviour after normal ungetc.  */
64       ungetc (fgetc (stdin), stdin);
65       {
66         const char *ptr = freadptr (stdin, &available2);
67
68         if (ptr != NULL)
69           {
70             ASSERT (available2 == available1);
71             ASSERT (memcmp (ptr, expected, available2) == 0);
72           }
73       }
74
75       /* Test behaviour after arbitrary ungetc.  */
76       fgetc (stdin);
77       ungetc ('@', stdin);
78       {
79         const char *ptr = freadptr (stdin, &available3);
80
81         if (ptr != NULL)
82           {
83             ASSERT (available3 == 1 || available3 == available1);
84             ASSERT (ptr[0] == '@');
85             if (available3 > 1)
86               {
87                 ASSERT (memcmp (ptr + 1, expected + 1, available3 - 1) == 0);
88               }
89           }
90       }
91     }
92
93   return 0;
94 }