(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.c-torture/
execute/
fprintf-2.c
       1  /* Verify that calls to fprintf don't get eliminated even if their
       2     result on success can be computed at compile time (they can fail).
       3     The calls can still be transformed into those of other functions.
       4     { dg-require-effective-target fileio }
       5     { dg-prune-output "warning: warning: \[^\n\r\]* possibly used unsafely" }
       6     { dg-skip-if "requires io" { avr-*-* } }
       7     { dg-skip-if "requires io" { freestanding } } */
       8  
       9  #include <stdio.h>
      10  #include <stdlib.h>
      11  #include <string.h>
      12  #include "gcc_tmpnam.h"
      13  
      14  int main (void)
      15  {
      16    char *tmpfname = gcc_tmpnam (0);
      17    FILE *f = fopen (tmpfname, "w");
      18    if (!f)
      19      {
      20        perror ("fopen for writing");
      21        return 1;
      22      }
      23  
      24    fprintf (f, "1");
      25    fprintf (f, "%c", '2');
      26    fprintf (f, "%c%c", '3', '4');
      27    fprintf (f, "%s", "5");
      28    fprintf (f, "%s%s", "6", "7");
      29    fprintf (f, "%i", 8);
      30    fprintf (f, "%.1s\n", "9x");
      31    fclose (f);
      32  
      33    f = fopen (tmpfname, "r");
      34    if (!f)
      35      {
      36        perror ("fopen for reading");
      37        remove (tmpfname);
      38        return 1;
      39      }
      40  
      41    char buf[12] = "";
      42    if (1 != fscanf (f, "%s", buf))
      43      {
      44        perror ("fscanf");
      45        fclose (f);
      46        remove (tmpfname);
      47        return 1;
      48      }
      49  
      50    fclose (f);
      51    remove (tmpfname);
      52  
      53    if (strcmp (buf, "123456789"))
      54      abort ();
      55  
      56    return 0;
      57  }