5766afef97e930c0c2645285a3b5422822f8be54
[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 (memcmp(cmpbuf, outbuf, max(end, strlen(outbuf))) == 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
79 if (argc == 1)
80 {
81 fprintf(stderr, "Not enough arguments\n");
82 return(1);
83 }
84
85 outbuf = malloc(256 * 1024);
86 if (outbuf == NULL)
87 {
88 fprintf(stderr, "Out of memory 1\n");
89 return(1);
90 }
91
92 s = outbuf;
93 s = s + sprintf(s, "/* Automatically generated, don't edit */\n");
94 for (i = 2; i < argc; i++)
95 {
96 s = s + sprintf(s, "#define %s\n", argv[i]);
97 }
98
99 return(write_if_change(outbuf, argv[1]));
100 }