comparison gcc/testsuite/gcc.c-torture/execute/fprintf-2.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
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-prune-output "warning: warning: \[^\n\r\]* possibly used unsafely" }
5 { dg-skip-if "requires io" { avr-*-* } }
6 { dg-skip-if "requires io" { freestanding } } */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 int main (void)
13 {
14 char *tmpfname = tmpnam (0);
15 FILE *f = fopen (tmpfname, "w");
16 if (!f)
17 {
18 perror ("fopen for writing");
19 return 1;
20 }
21
22 fprintf (f, "1");
23 fprintf (f, "%c", '2');
24 fprintf (f, "%c%c", '3', '4');
25 fprintf (f, "%s", "5");
26 fprintf (f, "%s%s", "6", "7");
27 fprintf (f, "%i", 8);
28 fprintf (f, "%.1s\n", "9x");
29 fclose (f);
30
31 f = fopen (tmpfname, "r");
32 if (!f)
33 {
34 perror ("fopen for reading");
35 remove (tmpfname);
36 return 1;
37 }
38
39 char buf[12] = "";
40 if (1 != fscanf (f, "%s", buf))
41 {
42 perror ("fscanf");
43 fclose (f);
44 remove (tmpfname);
45 return 1;
46 }
47
48 fclose (f);
49 remove (tmpfname);
50
51 if (strcmp (buf, "123456789"))
52 abort ();
53
54 return 0;
55 }