verify: new macro 'assume'
[gnulib.git] / tests / test-yesno.c
1 /* Test of yesno module.
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, 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 /* Specification.  */
21 #include "yesno.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "closein.h"
28 #include "binary-io.h"
29
30 char *program_name;
31
32 /* Test yesno.  Without arguments, read one line.  If first argument
33    is zero, close stdin before attempting to read one line.
34    Otherwise, read the number of lines specified by first
35    argument.  */
36 int
37 main (int argc, char **argv)
38 {
39   int i = 1;
40   program_name = argv[0];
41
42   /* yesno recommends that all clients use close_stdin in main.  */
43   atexit (close_stdin);
44   /* But on mingw, close_stdin leaves stdin's file descriptor at the expected
45      position (i.e. where this program left off reading) only if its mode has
46      been set to O_BINARY.  If it has been set to O_TEXT, and the file
47      descriptor is seekable, and stdin is buffered, the MSVCRT runtime ends up
48      setting the file descriptor's position to the expected position _minus_
49      the number of LFs not preceded by CR that were read between the expected
50      position and the last filled buffer end position.  (I.e. the repositioning
51      from the end-of-buffer to the expected position does not work if the input
52      file contains end-of-line markers in Unix convention.)  */
53   SET_BINARY (0);
54
55   if (1 < argc)
56     i = atoi (argv[1]);
57   if (!i)
58     {
59       i = 1;
60       close (0);
61     }
62   while (i--)
63     puts (yesno () ? "Y" : "N");
64   return 0;
65 }