More sensible dependencies scheme for configuration
[reactos.git] / reactos / ntoskrnl / mkconfig.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #define max(a, b) ((a) > (b) ? (a) : (b))
6
7 int
8 write_if_change(char* outbuf, char* filename)
9 {
10 FILE* out;
11 unsigned int end;
12 char* cmpbuf;
13 unsigned int stat;
14
15 out = fopen(filename, "rb");
16 if (out == NULL)
17 {
18 out = fopen(filename, "wb");
19 if (out == NULL)
20 {
21 fprintf(stderr, "Unable to create output file\n");
22 return(1);
23 }
24 fputs(outbuf, out);
25 fclose(out);
26 return(0);
27 }
28
29 fseek(out, 0, SEEK_END);
30 end = ftell(out);
31 cmpbuf = malloc(end);
32 if (cmpbuf == NULL)
33 {
34 fprintf(stderr, "Out of memory\n");
35 fclose(out);
36 return(1);
37 }
38
39 fseek(out, 0, SEEK_SET);
40 stat = fread(cmpbuf, 1, end, out);
41 if (stat != end)
42 {
43 fprintf(stderr, "Failed to read data\n");
44 fclose(out);
45 return(1);
46 }
47 if (end == strlen(outbuf) && memcmp(cmpbuf, outbuf, end) == 0)
48 {
49 fclose(out);
50 return(0);
51 }
52
53 fclose(out);
54 out = fopen(filename, "wb");
55 if (out == NULL)
56 {
57 fprintf(stderr, "Unable to create output file\n");
58 return(1);
59 }
60
61 stat = fwrite(outbuf, 1, strlen(outbuf), out);
62 if (strlen(outbuf) != stat)
63 {
64 fprintf(stderr, "Unable to write output file\n");
65 fclose(out);
66 return(1);
67 }
68 fclose(out);
69 return(0);
70 }
71
72 int
73 main(int argc, char* argv[])
74 {
75 unsigned int i;
76 char* outbuf;
77 char* s;
78 char config[512];
79
80 if (argc == 1)
81 {
82 fprintf(stderr, "Not enough arguments\n");
83 return(1);
84 }
85
86 outbuf = malloc(256 * 1024);
87 if (outbuf == NULL)
88 {
89 fprintf(stderr, "Out of memory 1\n");
90 return(1);
91 }
92
93 s = outbuf;
94 s = s + sprintf(s, "/* Automatically generated, ");
95 s = s + sprintf(s, "Edit the Makefile to change configuration */\n");
96 s = s + sprintf(s, "#ifndef __NTOSKRNL_INCLUDE_INTERNAL_CONFIG_H\n");
97 s = s + sprintf(s, "#define __NTOSKRNL_INCLUDE_INTERNAL_CONFIG_H\n");
98 strcpy(config, "");
99 for (i = 2; i < argc; i++)
100 {
101 s = s + sprintf(s, "#define %s\n", argv[i]);
102 strcat(config, argv[i]);
103 if (i != (argc - 1))
104 {
105 strcat(config, " ");
106 }
107 }
108 s = s + sprintf(s, "#define CONFIG \"%s\"\n", config);
109 s = s + sprintf(s, "#endif /* __NTOSKRNL_INCLUDE_INTERNAL_CONFIG_H */\n");
110
111 return(write_if_change(outbuf, argv[1]));
112 }