remove transient debugging code
[reactos.git] / reactos / 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 return(1);
48 }
49 if (end == strlen(outbuf) && memcmp(cmpbuf, outbuf, end) == 0)
50 {
51 fclose(out);
52 return(0);
53 }
54
55 fclose(out);
56 out = fopen(filename, "wb");
57 if (out == NULL)
58 {
59 fprintf(stderr, "Unable to create output file\n");
60 return(1);
61 }
62
63 stat = fwrite(outbuf, 1, strlen(outbuf), out);
64 if (strlen(outbuf) != stat)
65 {
66 fprintf(stderr, "Unable to write output file\n");
67 fclose(out);
68 return(1);
69 }
70 fclose(out);
71 return(0);
72 }
73
74 int
75 main(int argc, char* argv[])
76 {
77 int include_tests;
78 unsigned int i;
79 char* outbuf;
80 char* s;
81 char config[512];
82
83 if (argc == 1)
84 {
85 fprintf(stderr, "Not enough arguments\n");
86 return(1);
87 }
88
89 outbuf = malloc(256 * 1024);
90 if (outbuf == NULL)
91 {
92 fprintf(stderr, "Out of memory 1\n");
93 return(1);
94 }
95
96 s = outbuf;
97 s = s + sprintf(s, "/* Automatically generated, ");
98 s = s + sprintf(s, "Edit the Makefile to change configuration */\n");
99 s = s + sprintf(s, "#ifndef __INCLUDE_CONFIG_H\n");
100 s = s + sprintf(s, "#define __INCLUDE_CONFIG_H\n");
101 strcpy(config, "");
102 include_tests = 0;
103 for (i = 2; i < argc; i++)
104 {
105 if (strcmp(argv[i], "REGTESTS") == 0)
106 {
107 include_tests = 1;
108 }
109 else
110 {
111 s = s + sprintf(s, "#ifndef %s\n", argv[i]);
112 s = s + sprintf(s, "#define %s\n", argv[i]);
113 s = s + sprintf(s, "#endif /* %s */\n", argv[i]);
114 }
115 strcat(config, argv[i]);
116 if (i != (argc - 1))
117 {
118 strcat(config, " ");
119 }
120 }
121 if (include_tests)
122 {
123 s = s + sprintf(s, "#ifndef __ASM__\n");
124 s = s + sprintf(s, "extern void PrepareTests();\n");
125 s = s + sprintf(s, "#define PREPARE_TESTS PrepareTests();\n");
126 s = s + sprintf(s, "#endif /* __ASM__ */\n");
127 }
128 else
129 {
130 s = s + sprintf(s, "#define PREPARE_TESTS\n");
131 }
132 s = s + sprintf(s, "#define CONFIG \"%s\"\n", config);
133 s = s + sprintf(s, "#endif /* __INCLUDE_CONFIG_H */\n");
134
135 return(write_if_change(outbuf, argv[1]));
136 }