Test yesno in combination with closein.
[gnulib.git] / tests / test-yesno.c
1 /* Test of yesno module.
2    Copyright (C) 2007 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, or (at your option)
7    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
18 #include <config.h>
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #include "closein.h"
25 #include "yesno.h"
26
27 char *program_name;
28
29 /* Test yesno.  Without arguments, read one line.  If first argument
30    is zero, close stdin before attempting to read one line.
31    Otherwise, read the number of lines specified by first
32    argument.  */
33 int
34 main (int argc, char **argv)
35 {
36   int i = 1;
37   program_name = argv[0];
38   /* yesno recommends that all clients use close_stdin in main.  */
39   atexit (close_stdin);
40
41   if (1 < argc)
42     i = atoi (argv[1]);
43   if (!i)
44     {
45       i = 1;
46       close (0);
47     }
48   while (i--)
49     puts (yesno () ? "Y" : "N");
50   return 0;
51 }