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
main(void)13 int main (void)
14 {
15 char *tmpfname = tmpnam (0);
16 FILE *f = fopen (tmpfname, "w");
17 if (!f)
18 {
19 perror ("fopen for writing");
20 return 1;
21 }
22
23 fprintf (f, "1");
24 fprintf (f, "%c", '2');
25 fprintf (f, "%c%c", '3', '4');
26 fprintf (f, "%s", "5");
27 fprintf (f, "%s%s", "6", "7");
28 fprintf (f, "%i", 8);
29 fprintf (f, "%.1s\n", "9x");
30 fclose (f);
31
32 f = fopen (tmpfname, "r");
33 if (!f)
34 {
35 perror ("fopen for reading");
36 remove (tmpfname);
37 return 1;
38 }
39
40 char buf[12] = "";
41 if (1 != fscanf (f, "%s", buf))
42 {
43 perror ("fscanf");
44 fclose (f);
45 remove (tmpfname);
46 return 1;
47 }
48
49 fclose (f);
50 remove (tmpfname);
51
52 if (strcmp (buf, "123456789"))
53 abort ();
54
55 return 0;
56 }
57