aboutsummaryrefslogtreecommitdiffstats
path: root/tools/copyinf.c
blob: 0dfb125ad9ced24d0d0f852d6ca7799ebdcc33e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define MAX_SECTION 128

#define MAX_LINE 2048
static char linebuf[MAX_LINE];

int read_line(FILE *fr)
{
	int i = 0;
	int c;
	for(;;)
	{
		c = fgetc(fr);
		switch(c)
		{
			case '\r':
				break;
			case '\n':
			case EOF:
				linebuf[i] = '\0';
				break;
			default:
				if(i < MAX_LINE-1)
				{
					linebuf[i] = c;
					i++;
				}
				break;
		}
		
		if(c == '\n') break;
		if(c == EOF)
		{
			if(i == 0) return 0;
		}
	}
	
	return 1;
}

void put_line(FILE *fw)
{
	size_t s = strlen(linebuf);
	fwrite(linebuf, 1, s, fw);
	fputc('\r', fw);
	fputc('\n', fw);
}

void help(const char *progname)
{
	printf("%s [options] <source inf> <dest. inf> [enabled-section-1, [enable-section-2, [...]]]\n"
		"options:\n"
		"\t--softgpu\n"
		"\t--build <num>\n"
		"\t--day <num>\n"
		"\t--month <num>\n"
		"\t--year <num>\n\n",
		progname);
}

int main(int argc, char **argv)
{
	int i = 0;
	char **sections = calloc(argc, sizeof(char**));
	int sections_count = 0;
	char *src = NULL;
	char *dst = NULL;
	int softgpu = 0;
	int build = 0;
	int day;
	int month;
	int year;
	int print_help = 0;
	char section_test[MAX_SECTION+3];
	
	time_t atime;
  struct tm *gmt;

  time(&atime);
  gmt = gmtime(&atime);
  
  day   = gmt->tm_mday;
  month = gmt->tm_mon + 1;
  year  = gmt->tm_year + 1900;
	
	for(i = 1; i < argc; i++)
	{
		if(strcmp(argv[i], "--help") == 0)
		{
			print_help = 1;
		}
		else if(strcmp(argv[i], "--softgpu") == 0)
		{
			softgpu = 1;
		}
		else if(strcmp(argv[i], "--build") == 0)
		{
			if(i+1 < argc)
			{
				build = atoi(argv[i+1]);
				i++;
			}
		}
		else if(strcmp(argv[i], "--day") == 0)
		{
			if(i+1 < argc)
			{
				day = atoi(argv[i+1]);
				i++;
			}
		}
		else if(strcmp(argv[i], "--month") == 0)
		{
			if(i+1 < argc)
			{
				month = atoi(argv[i+1]);
				i++;
			}
		}
		else if(strcmp(argv[i], "--year") == 0)
		{
			if(i+1 < argc)
			{
				year = atoi(argv[i+1]);
				i++;
			}
		}
		else
		{
			if(src == NULL)
			{
				src = argv[i];
			}
			else if(dst == NULL)
			{
				dst = argv[i];
			}
			else
			{
				sections[sections_count] = argv[i];
				sections_count++;
			}
		}
	}
	
	if(print_help)
	{
		help(argv[0]);
		return 0;
	}
	
	if(dst == NULL)
	{
		printf("source and destination are required!\n\n");
		help(argv[0]);
		return 1;
	}
	
	FILE *out = NULL;
	FILE *in  = fopen(src, "rb");
	if(in)
	{
		out = fopen(dst, "wb");
		if(out)
		{
			while(read_line(in))
			{
				if(strstr(linebuf, "DriverVer=") == linebuf)
				{
					sprintf(linebuf, "DriverVer=%02d/%02d/%04d, %d.%d.0.%d", month, day, year, softgpu+1, year, build);
				}
				else
				{
					for(i = 0; i < sections_count; i++)
					{
						sprintf(section_test, ";%s:", sections[i]);
						
						if(strstr(linebuf, section_test) == linebuf)
						{
							size_t lb_len = strlen(linebuf);
							size_t s_len  = strlen(section_test);
							
							memmove(linebuf, linebuf+s_len, lb_len-s_len+1);
						}
					}
				}
				
				put_line(out);
			}
		}
		else
		{
			fprintf(stderr, "Failed to open output: %s\n", dst);
		}
		
		fclose(in);
	}
	else
	{
		fprintf(stderr, "Failed to open input: %s\n", src);
	}
	
	return 0;
}