(root)/
glibc-2.38/
stdio-common/
scanf19.c
       1  /* Copyright (C) 2008-2023 Free Software Foundation, Inc.
       2     This file is part of the GNU C Library.
       3  
       4     The GNU C Library is free software; you can redistribute it and/or
       5     modify it under the terms of the GNU Lesser General Public
       6     License as published by the Free Software Foundation; either
       7     version 2.1 of the License, or (at your option) any later version.
       8  
       9     The GNU C Library 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 GNU
      12     Lesser General Public License for more details.
      13  
      14     You should have received a copy of the GNU Lesser General Public
      15     License along with the GNU C Library; if not, see
      16     <https://www.gnu.org/licenses/>.  */
      17  
      18  /* This test exercises the deprecated GNU %as, %aS, and %a[...] scanf
      19     modifiers, which are not available to programs compiled as C99
      20     anymore; therefore, this file is compiled with -std=gnu89 and C99
      21     syntax must not be used.  */
      22  
      23  #include <stdarg.h>
      24  #include <stdio.h>
      25  #include <stdlib.h>
      26  #include <string.h>
      27  #include <wchar.h>
      28  
      29  #if !__GLIBC_USE_DEPRECATED_SCANF
      30  # error "This file should be compiled with deprecated scanf"
      31  #endif
      32  
      33  #define FAIL() \
      34    do {							\
      35      result = 1;						\
      36      printf ("test at line %d failed\n", __LINE__);	\
      37    } while (0)
      38  
      39  static int __attribute__ ((format (scanf, 2, 3)))
      40  xsscanf (const char *str, const char *fmt, ...)
      41  {
      42    va_list ap;
      43    va_start (ap, fmt);
      44    int ret = vsscanf (str, fmt, ap);
      45    va_end (ap);
      46    return ret;
      47  }
      48  
      49  static int __attribute__ ((format (scanf, 1, 2)))
      50  xscanf (const char *fmt, ...)
      51  {
      52    va_list ap;
      53    va_start (ap, fmt);
      54    int ret = vscanf (fmt, ap);
      55    va_end (ap);
      56    return ret;
      57  }
      58  
      59  static int __attribute__ ((format (scanf, 2, 3)))
      60  xfscanf (FILE *f, const char *fmt, ...)
      61  {
      62    va_list ap;
      63    va_start (ap, fmt);
      64    int ret = vfscanf (f, fmt, ap);
      65    va_end (ap);
      66    return ret;
      67  }
      68  
      69  int
      70  main (void)
      71  {
      72    wchar_t *lsp;
      73    char *sp;
      74    float f;
      75    double d;
      76    char c[8];
      77    int result = 0;
      78  
      79    if (xsscanf (" 0.25s x", "%e%3c", &f, c) != 2)
      80      FAIL ();
      81    else if (f != 0.25 || memcmp (c, "s x", 3) != 0)
      82      FAIL ();
      83    if (xsscanf (" 1.25s x", "%as%2c", &sp, c) != 2)
      84      FAIL ();
      85    else
      86      {
      87        if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
      88  	FAIL ();
      89        memset (sp, 'x', sizeof "1.25s");
      90        free (sp);
      91      }
      92    if (xsscanf (" 2.25s x", "%las%2c", &d, c) != 2)
      93      FAIL ();
      94    else if (d != 2.25 || memcmp (c, " x", 2) != 0)
      95      FAIL ();
      96    if (xsscanf (" 3.25S x", "%4aS%3c", &lsp, c) != 2)
      97      FAIL ();
      98    else
      99      {
     100        if (wcscmp (lsp, L"3.25") != 0 || memcmp (c, "S x", 3) != 0)
     101  	FAIL ();
     102        memset (lsp, 'x', sizeof L"3.25");
     103        free (lsp);
     104      }
     105    if (xsscanf ("4.25[0-9.] x", "%a[0-9.]%8c", &sp, c) != 2)
     106      FAIL ();
     107    else
     108      {
     109        if (strcmp (sp, "4.25") != 0 || memcmp (c, "[0-9.] x", 8) != 0)
     110  	FAIL ();
     111        memset (sp, 'x', sizeof "4.25");
     112        free (sp);
     113      }
     114    if (xsscanf ("5.25[0-9.] x", "%la[0-9.]%2c", &d, c) != 2)
     115      FAIL ();
     116    else if (d != 5.25 || memcmp (c, " x", 2) != 0)
     117      FAIL ();
     118  
     119    const char *tmpdir = getenv ("TMPDIR");
     120    if (tmpdir == NULL || tmpdir[0] == '\0')
     121      tmpdir = "/tmp";
     122  
     123    char fname[strlen (tmpdir) + sizeof "/tst-scanf16.XXXXXX"];
     124    sprintf (fname, "%s/tst-scanf16.XXXXXX", tmpdir);
     125  
     126    /* Create a temporary file.   */
     127    int fd = mkstemp (fname);
     128    if (fd == -1)
     129      FAIL ();
     130  
     131    FILE *fp = fdopen (fd, "w+");
     132    if (fp == NULL)
     133      FAIL ();
     134    else
     135      {
     136        if (fputs (" 1.25s x", fp) == EOF)
     137  	FAIL ();
     138        if (fseek (fp, 0, SEEK_SET) != 0)
     139  	FAIL ();
     140        if (xfscanf (fp, "%as%2c", &sp, c) != 2)
     141  	FAIL ();
     142        else
     143  	{
     144  	  if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
     145  	    FAIL ();
     146  	  memset (sp, 'x', sizeof "1.25s");
     147  	  free (sp);
     148  	}
     149  
     150        if (freopen (fname, "r", stdin) == NULL)
     151  	FAIL ();
     152        else
     153  	{
     154  	  if (xscanf ("%as%2c", &sp, c) != 2)
     155  	    FAIL ();
     156  	  else
     157  	    {
     158  	      if (strcmp (sp, "1.25s") != 0 || memcmp (c, " x", 2) != 0)
     159  		FAIL ();
     160  	      memset (sp, 'x', sizeof "1.25s");
     161  	      free (sp);
     162  	    }
     163  	}
     164  
     165        fclose (fp);
     166      }
     167  
     168    remove (fname);
     169  
     170    return result;
     171  }