FIXCRC.CPP 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <dos.h>
  6. #include <io.h>
  7. #include <fcntl.h>
  8. #include "typedefs.h"
  9. #include "getopt.h"
  10. #include "misc.h"
  11. struct FNODE
  12. {
  13. FNODE *next;
  14. char name[1];
  15. };
  16. FNODE head = { &head, "" };
  17. FNODE *tail = &head;
  18. ulong gMapCRC;
  19. void ShowUsage(void)
  20. {
  21. printf("Usage:\n");
  22. printf(" FIXCRC map1 map2 (wild cards ok)\n");
  23. exit(0);
  24. }
  25. void QuitMessage(char * fmt, ...)
  26. {
  27. char msg[80];
  28. va_list argptr;
  29. va_start( argptr, fmt );
  30. vsprintf( msg, fmt, argptr );
  31. va_end(argptr);
  32. printf(msg);
  33. exit(1);
  34. }
  35. void ProcessFile(char *filename)
  36. {
  37. int hFile;
  38. int length;
  39. char *buffer;
  40. printf("%s: ", filename);
  41. hFile = open(filename, O_RDONLY | O_BINARY);
  42. length = filelength(hFile);
  43. buffer = (char *)malloc(length);
  44. read(hFile, buffer, length);
  45. close(hFile);
  46. gMapCRC = CRC32(buffer, length - sizeof(gMapCRC));
  47. hFile = open(filename, O_CREAT | O_TRUNC | O_WRONLY | O_BINARY, S_IWUSR);
  48. write(hFile, buffer, length - sizeof(gMapCRC));
  49. write(hFile, &gMapCRC, sizeof(gMapCRC));
  50. close(hFile);
  51. free(buffer);
  52. printf("CRC fixed.\n");
  53. }
  54. void InsertFilename( char *fname )
  55. {
  56. FNODE *n = (FNODE *)malloc(sizeof(FNODE) + strlen(fname));
  57. strcpy(n->name, fname);
  58. // insert the node at the tail, so it stays in order
  59. n->next = tail->next;
  60. tail->next = n;
  61. tail = n;
  62. }
  63. void ProcessArgument(char *s)
  64. {
  65. char filespec[_MAX_PATH];
  66. char buffer[_MAX_PATH2];
  67. char path[_MAX_PATH];
  68. strcpy(filespec, s);
  69. ChangeExtension(filespec, ".MAP");
  70. char *drive, *dir;
  71. // separate the path from the filespec
  72. _splitpath2(s, buffer, &drive, &dir, NULL, NULL);
  73. _makepath(path, drive, dir, NULL, NULL);
  74. struct find_t fileinfo;
  75. unsigned r = _dos_findfirst(s, _A_NORMAL, &fileinfo);
  76. if (r != 0)
  77. printf("%s not found\n", s);
  78. while ( r == 0 )
  79. {
  80. strcpy(filespec, path);
  81. strcat(filespec, fileinfo.name);
  82. InsertFilename(filespec);
  83. r = _dos_findnext( &fileinfo );
  84. }
  85. _dos_findclose(&fileinfo);
  86. }
  87. /***********************************************************************
  88. * Process command line arguments
  89. **********************************************************************/
  90. void ParseOptions( int argc, char *argv[])
  91. {
  92. int c;
  93. while ( (c = GetOptions(argc, argv, "")) != GO_EOF ) {
  94. switch (c) {
  95. case GO_INVALID:
  96. QuitMessage("Invalid argument: %s", OptArgument);
  97. case GO_FULL:
  98. ProcessArgument(OptArgument);
  99. break;
  100. }
  101. }
  102. }
  103. void main(int argc, char *argv[])
  104. {
  105. printf("Map CRC Correction Tool Copyright (c) 1995 Q Studios Corporation\n");
  106. if (argc < 2) ShowUsage();
  107. ParseOptions(argc, argv);
  108. // process the file list
  109. for (FNODE *n = head.next; n != &head; n = n->next)
  110. ProcessFile(n->name);
  111. }