]> gerrit.simantics Code Review - simantics/fmil.git/blob - org.simantics.fmil.core/native/FMILibrary/ThirdParty/c99_snprintf/c99-snprintf_1.1/example.c
Switch to full JavaSE-11+ compatibility
[simantics/fmil.git] / org.simantics.fmil.core / native / FMILibrary / ThirdParty / c99_snprintf / c99-snprintf_1.1 / example.c
1 /* $Id: example.c,v 1.1.1.1 2008/01/06 03:24:00 holger Exp $ */
2
3 #if HAVE_CONFIG_H
4 #include <config.h>
5 #endif  /* HAVE_CONFIG_H */
6
7 #include <stdio.h>
8 #include <stdlib.h>     /* For free(3). */
9 #include "system.h"
10
11 int
12 main(void)
13 {
14         FILE *fp;
15         char *buf;
16         unsigned int random;
17
18         if ((fp = fopen("/dev/urandom", "r")) == NULL &&
19             (fp = fopen("/dev/random", "r")) == NULL) {
20                 perror("Cannot open random device");
21                 return 1;
22         }
23         if (fread(&random, sizeof(random), 1, fp) != 1) {
24                 (void)fputs("Cannot read random device.", stderr);
25                 return 1;
26         }
27         if (fclose(fp) != 0) {
28                 perror("Cannot close random device");
29                 return 1;
30         }
31
32         /* We never heard of printf(3), so we use asprintf(3)/puts(3) :-) */
33         if (asprintf(&buf, "Random %zu-bit integer: %#.*x",
34             sizeof(random) * 8, (int)sizeof(random) * 2, random) < 0) {
35                 perror("asprintf(3) failed");
36                 return 1;
37         }
38         (void)puts(buf);
39         free(buf);
40         return 0;
41 }