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