ttbin2bin.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /******************************************************************************
  2. *
  3. * project name: TI-68k Developer Utilities
  4. * file name: ttbin2bin.c
  5. * initial date: 21/08/2000
  6. * author: thomas.nussbaumer@gmx.net
  7. * description: converts a TI89 binary to a TI92p binary or
  8. * a TI92p binary to a TI89 binary by just patching the
  9. * header information
  10. *
  11. ******************************************************************************/
  12. /*
  13. This file is part of TI-68k Developer Utilities.
  14. This file is free software; you can redistribute it and/or
  15. modify it under the terms of the GNU Lesser General Public
  16. License as published by the Free Software Foundation; either
  17. version 2.1 of the License, or (at your option) any later version.
  18. As a special exception, UNMODIFIED copies of ttbin2bin may also be
  19. redistributed or sold without source code, for any purpose. (The Lesser
  20. General Public License restrictions do apply in other respects; for example,
  21. they cover modification of the program.) This exception notice must be
  22. removed on modified copies of this file.
  23. This program is distributed in the hope that it will be useful,
  24. but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  26. Lesser General Public License for more details.
  27. You should have received a copy of the GNU Lesser General Public
  28. License along with this library; if not, write to the Free Software
  29. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  30. */
  31. // if EMBEDDED_USE is defined, than we use this sourcefile from within another
  32. // sourcefile
  33. #ifndef EMBEDDED_USE
  34. #include <stdio.h>
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #include "ttversion.h"
  38. #include "revtools.h"
  39. #include "tt.h"
  40. #include "strhead.h"
  41. #ifdef FILE_REVISION
  42. #undef FILE_REVISION
  43. #endif
  44. #define FILE_REVISION "1.7"
  45. //=============================================================================
  46. // outputs usage information of this tool
  47. //=============================================================================
  48. void PrintUsage() {
  49. PRINT_ID("TTBin2Bin");
  50. fprintf(USAGE_OUT, "Usage: ttbin2bin [-quiet] <infile>\n\n" \
  51. " quiet ... don't output standard messages\n\n"\
  52. " converts TI89 binary to TI92p binary or TI92p binary to TI89 binary\n" \
  53. " by just patching the header information. the generated outputfile\n" \
  54. " will have the same name, but a different extension.\n" \
  55. " NOTE1: this should be only applied to datafiles or executables\n" \
  56. " which uses no compiled in calc depending constants\n" \
  57. " NOTE2: the program auto-detects the infile type by the extension\n" \
  58. " if the extension starts with '.89' it will treated as \n"\
  59. " TI89 inputfile. if it starts with '.9x' it will be treated\n" \
  60. " as TI92p inputfile.\n\n");
  61. }
  62. //=============================================================================
  63. // ... its not much in this file beside the main ...
  64. //=============================================================================
  65. int main(int argc,char *argv[]) {
  66. #else
  67. int TTBin2Bin(int argc,char *argv[]) {
  68. #endif
  69. char* infile;
  70. char outfile[1024];
  71. FILE* ifp;
  72. FILE* ofp;
  73. char tmp[1024];
  74. int calctype = -1;
  75. int n;
  76. char lastchar;
  77. int quiet = 0;
  78. // check for too less arguments
  79. if (argc != 2 && argc != 3) {
  80. #ifndef EMBEDDED_USE
  81. PrintUsage();
  82. #endif
  83. return 1;
  84. }
  85. if (argc == 3) {
  86. if (!strcmp(argv[1],"-quiet")) quiet = 1;
  87. else {
  88. #ifndef EMBEDDED_USE
  89. PrintUsage();
  90. #endif
  91. return 1;
  92. }
  93. infile = argv[2];
  94. }
  95. else {
  96. infile = argv[1];
  97. }
  98. #ifndef EMBEDDED_USE
  99. if (!quiet) PRINT_ID("TTBin2Bin");
  100. #endif
  101. n = strlen(infile);
  102. if (n<5) {
  103. fprintf(stderr,"ERROR: inputfile name too short\n");
  104. return 1;
  105. }
  106. if (!strncmp(&infile[n-4],".89",3)) calctype = CALC_TI89;
  107. else if (!strncmp(&infile[n-4],".9x",3)) calctype = CALC_TI92P;
  108. else {
  109. fprintf(stderr,"ERROR: no extension or not starting with '.89' or '.9x'\n");
  110. return 1;
  111. }
  112. lastchar = infile[n-1];
  113. if (!(ifp = fopen(infile,"rb"))) {
  114. fprintf(stderr,"ERROR: cannot open inputfile %s\n",infile);
  115. return 1;
  116. }
  117. if (fread(tmp,8,1,ifp) != 1) {
  118. fprintf(stderr,"ERROR: cannot read 8 characters from inputfile %s\n",infile);
  119. fclose(ifp);
  120. return 1;
  121. }
  122. if (strncmp(tmp,calctype == CALC_TI89 ? SIGNATURE_TI89 : SIGNATURE_TI92P,8)) {
  123. fprintf(stderr,"ERROR: calctype signature does not match\n");
  124. fclose(ifp);
  125. return 1;
  126. }
  127. strncpy(outfile,infile,n-3);
  128. sprintf(&outfile[n-3],calctype == CALC_TI89 ? "9x%c" : "89%c",lastchar);
  129. if (!(ofp = fopen(outfile,"wb"))) {
  130. fprintf(stderr,"ERROR: cannot open outfile %s\n",outfile);
  131. fclose(ifp);
  132. return 1;
  133. }
  134. if (fwrite(calctype == CALC_TI89 ? SIGNATURE_TI92P : SIGNATURE_TI89,8,1,ofp) != 1) {
  135. fprintf(stderr,"ERROR: problem while writing to outfile %s\n",outfile);
  136. fclose(ifp);
  137. fclose(ofp);
  138. return 1;
  139. }
  140. //-----------------------------------------------------------------------
  141. // I know this copy loop is slow - but who cares on near GHz computers ??
  142. //-----------------------------------------------------------------------
  143. do {
  144. n = fgetc(ifp);
  145. if (n == EOF) break;
  146. fputc(n,ofp);
  147. }
  148. while (1);
  149. if (!quiet) fprintf(stdout,"%ld bytes written to outputfile %s.\n",ftell(ofp),outfile);
  150. fclose(ifp);
  151. fclose(ofp);
  152. return 0;
  153. }
  154. //#############################################################################
  155. //###################### NO MORE FAKES BEYOND THIS LINE #######################
  156. //#############################################################################
  157. //
  158. //=============================================================================
  159. // Revision History
  160. //=============================================================================
  161. //
  162. // Revision 1.7 2009/01/25 Lionel Debroux
  163. // Changes by Romain Liévin and/or me for 64-bit compatibility.
  164. // Adapt to new version display (revtools.h).
  165. //
  166. // Revision 1.6 2002/03/14 11:35:19 tnussb
  167. // new commandline parameter -quiet (suppress standard messages)
  168. //
  169. // Revision 1.5 2002/03/04 14:32:41 tnussb
  170. // now tool can be used as embedded version from within other tools
  171. // by defining EMBEDDED_VERSION before including the sourcefile
  172. //
  173. // Revision 1.4 2002/02/07 09:49:36 tnussb
  174. // all local includes changed, because header files are now located in pctools folder
  175. //
  176. // Revision 1.3 2000/11/28 00:06:33 Thomas Nussbaumer
  177. // using now USAGE_OUT stream for usage info
  178. //
  179. // Revision 1.2 2000/08/23 19:28:08 Thomas Nussbaumer
  180. // adapted to automatic version printing (revtools.h)
  181. //
  182. // Revision 1.1 2000/08/21 17:12:27 Thomas Nussbaumer
  183. // initial version
  184. //
  185. //
  186. //