Git conversion: Make reactos the root directory, move rosapps, rostests, wallpapers...
[reactos.git] / sdk / tools / mkconfig.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #ifndef max
6 #define max(a, b) ((a) > (b) ? (a) : (b))
7 #endif
8
9 int
10 write_if_change(char* outbuf, char* filename)
11 {
12 FILE* out;
13 unsigned int end;
14 char* cmpbuf;
15 unsigned int stat;
16
17 out = fopen(filename, "rb");
18 if (out == NULL)
19 {
20 out = fopen(filename, "wb");
21 if (out == NULL)
22 {
23 fprintf(stderr, "Unable to create output file\n");
24 return(1);
25 }
26 fputs(outbuf, out);
27 fclose(out);
28 return(0);
29 }
30
31 fseek(out, 0, SEEK_END);
32 end = ftell(out);
33 cmpbuf = malloc(end);
34 if (cmpbuf == NULL)
35 {
36 fprintf(stderr, "Out of memory\n");
37 fclose(out);
38 return(1);
39 }
40
41 fseek(out, 0, SEEK_SET);
42 stat = fread(cmpbuf, 1, end, out);
43 if (stat != end)
44 {
45 fprintf(stderr, "Failed to read data\n");
46 fclose(out);
47 free(cmpbuf);
48 return(1);
49 }
50 if (end == strlen(outbuf) && memcmp(cmpbuf, outbuf, end) == 0)
51 {
52 fclose(out);
53 free(cmpbuf);
54 return(0);
55 }
56
57 fclose(out);
58 free(cmpbuf);
59 out = fopen(filename, "wb");
60 if (out == NULL)
61 {
62 fprintf(stderr, "Unable to create output file\n");
63 return(1);
64 }
65
66 stat = fwrite(outbuf, 1, strlen(outbuf), out);
67 if (strlen(outbuf) != stat)
68 {
69 fprintf(stderr, "Unable to write output file\n");
70 fclose(out);
71 return(1);
72 }
73 fclose(out);
74 return(0);
75 }
76
77 int
78 main(int argc, char* argv[])
79 {
80 int include_tests;
81 unsigned int i;
82 char* outbuf;
83 char* s;
84 char config[512];
85
86 if (argc == 1)
87 {
88 fprintf(stderr, "Not enough arguments\n");
89 return(1);
90 }
91
92 outbuf = malloc(256 * 1024);
93 if (outbuf == NULL)
94 {
95 fprintf(stderr, "Out of memory 1\n");
96 return(1);
97 }
98
99 s = outbuf;
100 s = s + sprintf(s, "/* Automatically generated, ");
101 s = s + sprintf(s, "Edit the Makefile to change configuration */\n");
102 s = s + sprintf(s, "#ifndef __INCLUDE_CONFIG_H\n");
103 s = s + sprintf(s, "#define __INCLUDE_CONFIG_H\n");
104 strcpy(config, "");
105 include_tests = 0;
106 for (i = 2; i < argc; i++)
107 {
108 if (strcmp(argv[i], "REGTESTS") == 0)
109 {
110 include_tests = 1;
111 }
112 else
113 {
114 s = s + sprintf(s, "#ifndef %s\n", argv[i]);
115 s = s + sprintf(s, "#define %s\n", argv[i]);
116 s = s + sprintf(s, "#endif /* %s */\n", argv[i]);
117 }
118 strcat(config, argv[i]);
119 if (i != (argc - 1))
120 {
121 strcat(config, " ");
122 }
123 }
124 if (include_tests)
125 {
126 s = s + sprintf(s, "#ifndef __ASM__\n");
127 s = s + sprintf(s, "extern void PrepareTests();\n");
128 s = s + sprintf(s, "#define PREPARE_TESTS PrepareTests();\n");
129 s = s + sprintf(s, "#endif /* __ASM__ */\n");
130 }
131 else
132 {
133 s = s + sprintf(s, "#define PREPARE_TESTS\n");
134 }
135 s = s + sprintf(s, "#define CONFIG \"%s\"\n", config);
136 s = s + sprintf(s, "#endif /* __INCLUDE_CONFIG_H */\n");
137
138 return(write_if_change(outbuf, argv[1]));
139 }