1 // Copyright 2011 The Emscripten Authors.  All rights reserved.
2 // Emscripten is available under two separate licenses, the MIT license and the
3 // University of Illinois/NCSA Open Source License.  Both these licenses can be
4 // found in the LICENSE file.
5 
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
main()11 int main()
12 {
13   // Reading
14 
15   FILE *file = fopen("somefile.binary", "rb");
16   assert(file);
17 
18   fseek(file, 0, SEEK_END);
19   int size = ftell(file);
20   rewind (file);
21   printf("size: %d\n", size);
22 
23   char *buffer = (char*) malloc (sizeof(char)*size);
24   assert(buffer);
25 
26   size_t read = fread(buffer, 1, size, file);
27   assert(read == size);
28 
29   printf("data: %d", buffer[0]);
30   for (int i = 1; i < size; i++)
31     printf(",%d", buffer[i]);
32   printf("\n");
33 
34   fclose (file);
35   free (buffer);
36 
37   // Do it again, with a loop on feof
38 
39   printf("loop: ");
40   file = fopen("somefile.binary", "rb");
41   assert(file);
42   while (!feof(file)) {
43     char c = fgetc(file);
44     if (c != EOF) printf("%d ", c);
45   }
46   fclose (file);
47   printf("\n");
48 
49   // Standard streams
50 
51   printf("input:%s\n", gets((char*)malloc(1024)));
52   fwrite("texto\n", 1, 6, stdout);
53   fwrite("texte\n", 1, 6, stderr);
54   putchar('$');
55   putc('\n', stdout);
56 
57   // Writing
58 
59   char data[5] = { 10, 30, 20, 11, 88 };
60   FILE *outf = fopen("go.out", "wb");
61   fwrite(data, 1, 5, outf);
62   fclose(outf);
63 
64   FILE *devNull = fopen("/dev/null", "rb");
65   assert(devNull);
66 
67   char data2[10];
68   FILE *inf = fopen("go.out", "rb");
69   int num = fread(data2, 1, 10, inf);
70   fclose(inf);
71   printf("%d : %d,%d,%d,%d,%d\n", num, data2[0], data2[1], data2[2], data2[3], data2[4]);
72 
73   // Test reading a file that has not been cached
74 
75   FILE *other = fopen("test.file", "r");
76   assert(other);
77 
78   char otherData[1000];
79   num = fread(otherData, 1, 9, other);
80   otherData[num] = 0;
81   printf("other=%s.\n", otherData);
82 
83   // Seeking
84 
85   fseek(other, 2, SEEK_SET);
86   num = fread(otherData, 1, 5, other);
87   otherData[num] = 0;
88   printf("seeked=%s.\n", otherData);
89 
90   fseek(other, -1, SEEK_CUR);
91   num = fread(otherData, 1, 3, other);
92   otherData[num] = 0;
93   printf("seeked=%s.\n", otherData);
94 
95   fseek(other, -2, SEEK_END);
96   num = fread(otherData, 1, 2, other);
97   otherData[num] = 0;
98   printf("seeked=%s.\n", otherData);
99 
100   fclose(other);
101 
102   // fscanf
103 
104   outf = fopen("fscan.f", "w");
105   fprintf(outf, "10 hello");
106   fclose(outf);
107 
108   int number;
109   char text[100];
110   inf = fopen("fscan.f", "r");
111   num = fscanf(inf, "%d %s", &number, text);
112   fclose(inf);
113   printf("fscanfed: %d - %s\n", number, text);
114 
115   // temp files
116   const char *tname = "file_XXXXXX";
117   char tname1[100];
118   char tname2[100];
119   strcpy(tname1, tname);
120   strcpy(tname2, tname);
121   assert(!strcmp(tname1, tname2)); // equal
122   int f1 = mkstemp(tname1);
123   int f2 = mkstemp(tname2);
124   assert(f1 != f2);
125   //printf("%d,%d,%s,%s\n", f1, f2, tname1, tname2);
126   assert(strcmp(tname1, tname2)); // not equal
127   assert(fopen(tname1, "r"));
128   assert(fopen(tname2, "r"));
129   assert(!fopen(tname2+1, "r")); // sanity check that we can't open just anything
130 
131   {
132     FILE* f = tmpfile();
133     assert(f);
134     fclose(f);
135 
136     char* str = tmpnam(NULL);
137     //printf("temp: %s\n", str);
138     assert(strncmp("/tmp/", str, 5) == 0);
139   }
140 
141   FILE *n = fopen("/dev/null", "w");
142   printf("5 bytes to dev/null: %zu\n", fwrite(data, 1, 5, n));
143   fclose(n);
144 
145   printf("ok.\n");
146 
147   return 0;
148 }
149 
150