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