(root)/
coreutils-9.4/
gnulib-tests/
test-readtokens.c
       1  /* Test the readtokens module.
       2     Copyright (C) 2012-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 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 <https://www.gnu.org/licenses/>.  */
      16  
      17  #include <config.h>
      18  #include <sys/types.h>
      19  #include <sys/stat.h>
      20  #include <fcntl.h>
      21  #include <string.h>
      22  #include <stdio.h>
      23  #include <unistd.h>
      24  
      25  #include "readtokens.h"
      26  #include "closeout.h"
      27  #include "macros.h"
      28  
      29  static void
      30  basic (void)
      31  {
      32    char const *filename = "in.827";
      33    int fd = open (filename, O_CREAT | O_WRONLY, 0600);
      34    ASSERT (fd >= 0);
      35    ASSERT (write (fd, "a|b;c+d", 7) == 7);
      36    ASSERT (close (fd) == 0);
      37  
      38    {
      39      token_buffer tb;
      40      FILE *fp = fopen (filename, "r");
      41      ASSERT (fp);
      42  
      43      init_tokenbuffer (&tb);
      44      ASSERT (readtoken (fp, "|;", 2, &tb)  == 1 && tb.buffer[0] == 'a');
      45      ASSERT (readtoken (fp, "|;", 2, &tb) == 1 && tb.buffer[0] == 'b');
      46      ASSERT (readtoken (fp, "+", 1, &tb)  == 1 && tb.buffer[0] == 'c');
      47      ASSERT (readtoken (fp, "-", 1, &tb) == 1 && tb.buffer[0] == 'd');
      48      ASSERT (readtoken (fp, "%", 0, &tb) == (size_t) -1);
      49      ASSERT ( ! ferror (fp));
      50      ASSERT (fclose (fp) == 0);
      51    }
      52  }
      53  
      54  int
      55  main (int argc, char **argv)
      56  {
      57    token_buffer tb;
      58    char const *delim;
      59    size_t delim_len;
      60  
      61    atexit (close_stdout);
      62  
      63    if (argc == 1)
      64      {
      65        basic ();
      66        return 0;
      67      }
      68  
      69    init_tokenbuffer (&tb);
      70  
      71    if (argc != 2)
      72      return 99;
      73  
      74    delim = argv[1];
      75    delim_len = strlen (delim);
      76  
      77    if (STREQ (delim, "\\0"))
      78      {
      79        delim = "";
      80        delim_len = 1;
      81      }
      82  
      83    while (1)
      84      {
      85        size_t token_length = readtoken (stdin, delim, delim_len, &tb);
      86        if (token_length == (size_t) -1)
      87          break;
      88        fwrite (tb.buffer, 1, token_length, stdout);
      89        putchar (':');
      90      }
      91    putchar ('\n');
      92    free (tb.buffer);
      93  
      94    ASSERT ( ! ferror (stdin));
      95  
      96    return 0;
      97  }