Fixed the length of the command line.
[reactos.git] / freeldr / ntvdmpat.c
1 /* Copyright (C) 2000 CW Sandmann (sandmann@clio.rice.edu) 1206 Braelinn, Sugar Land, TX 77479 */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <fcntl.h>
6 #ifdef GO32
7 #include <unistd.h>
8 #else
9 #include <io.h>
10 #endif
11
12 char view_only = 0;
13 const char *client_patch_code;
14 char buffer[20480];
15 unsigned long search_base = 0x4c800L;
16 int f;
17
18 char oldpatch[] = {0x3b, 0x05, 0xac, 0xe6 };
19 char newpatch[] = {0x3b, 0x05, 0x58, 0x5e };
20
21 void patch_image(char *filename)
22 {
23 int i,size;
24
25 view_only = 0;
26 f = open(filename, O_RDWR | O_BINARY);
27 if (f < 0) {
28 f = open(filename, O_RDONLY | O_BINARY);
29 if (f < 0) {
30 perror(filename);
31 return;
32 }
33 view_only = 1;
34 }
35
36 lseek(f, search_base, SEEK_SET);
37 size = read(f, buffer, sizeof(buffer));
38
39 client_patch_code = NULL;
40 for(i=0; i<size && !client_patch_code; i++)
41 if(!memcmp(buffer+i,oldpatch,sizeof(oldpatch)))
42 client_patch_code = (buffer+i);
43
44 if(!client_patch_code) {
45 printf("Old patch string not found in %s!\n",filename);
46 } else {
47 lseek(f, search_base+i-1, SEEK_SET); /* Ready to update */
48 if(!view_only) {
49 write(f, newpatch, sizeof(newpatch));
50 printf("%s patched\n",filename);
51 } else
52 printf("%s patchable (not changed, readonly)\n",filename);
53 }
54 close(f);
55 return;
56 }
57
58 int main(int argc, char **argv)
59 {
60 int i;
61 char filename[256];
62 char buf1[256];
63 char file2[256];
64
65 if (argc != 1) { /* If they specify names, patch them, exit */
66 for(i=1; i<argc; i++)
67 patch_image(argv[i]);
68 return 0;
69 }
70
71 fprintf(stderr, "This image patches Windows 2000 NTVDM to fix nesting DPMI bug.\n");
72
73 strcpy(filename,getenv("SYSTEMROOT"));
74 strcpy(file2,filename);
75 strcat(filename,"\\system32\\ntvdm.exe");
76 strcat(file2,"\\system32\\dllcache\\ntvdm.exe");
77
78 sprintf(buf1,"copy %s %s\\system32\\ntvdm.ori",filename,getenv("SYSTEMROOT"));
79 printf("%s\n",buf1);
80 system(buf1);
81
82 patch_image(file2);
83 patch_image(filename);
84 return 0;
85 }