home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / pgmutil / idtag.zip / IDTAG.C < prev    next >
C/C++ Source or Header  |  1990-01-15  |  3KB  |  164 lines

  1. /*
  2.  * IDtag: making an .obj file
  3.  * for putting messages into an executable module
  4.  * (You can do the same thing with
  5.  * #pragma comment (exestr, "string")
  6.  * on MSC 5.1 or later)
  7.  * Copyright (c) 1990 by Kenji Rikitake <kenji@ybbs.c.u-tokyo.ac.jp>
  8.  * You can use this source code and the executable module for any purpose
  9.  * provided retaining this notice within.
  10.  *
  11.  * Version 1.00 January 15, 1990
  12.  */
  13.  
  14. /*
  15.  * PD getopt.c from UUPC interim version
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "getopt.h"
  21.  
  22. #define FALSE 0
  23. #define TRUE 1
  24.  
  25. #define PATHLEN 132
  26.  
  27. #define R_THEADR 0x80
  28. #define R_COMENT 0x88
  29. #define R_MODEND 0x8a
  30.  
  31. #define COMCLASS_LANGUAGE 0x00
  32. /* Hmmmm, you can't find this in MS-DOS Encyclopedia */
  33. #define COMCLASS_EXESTR 0xa4
  34. #define COMAT_NOPURGE 0x80
  35. #define COMAT_NOLIST 0x40
  36.  
  37. char modname[PATHLEN];
  38. char objname[PATHLEN];
  39. char srcname[PATHLEN];
  40. FILE *srcfp, *objfp;
  41.  
  42. char version[] = "1.00";
  43.  
  44. void write_obj_record(char type, int len, char *body)
  45. {
  46.     int chksum;
  47.     register int i, c;
  48.     
  49.     chksum = 0;
  50.  
  51.     putc(type, objfp);
  52.     chksum += type;
  53.     chksum &= 0x00ff;
  54.     
  55.     len++;
  56.     
  57.     c = len & 0x00ff;
  58.     putc(c, objfp);
  59.     chksum += c;
  60.     chksum &= 0x00ff;
  61.     
  62.     c = (len >> 8) & 0x00ff;
  63.     putc(c, objfp);
  64.     chksum += c;
  65.     chksum &= 0x00ff;
  66.     
  67.     len--;
  68.     
  69.     for (i = 0; i < len; i++) {
  70.         c = *body++;
  71.         putc(c, objfp);
  72.         chksum += c;
  73.         chksum &= 0x00ff;
  74.         }
  75.     
  76.     c = 0x0100 - chksum;
  77.     putc(c, objfp);
  78.  
  79.     return;
  80. }
  81.  
  82. int main(int argc, char *argv[])
  83. {
  84.     int option;
  85.     int nolist;
  86.     char comment_attrib;
  87.     int i;
  88.     static char buf[BUFSIZ];
  89.     
  90.     nolist = TRUE;
  91.     
  92.     strcpy(modname, "IDtag_output");
  93.     strcpy(objname, "idtagout.obj");
  94.     
  95.     while ((option = getopt(argc, argv, "lm:o:")) != EOF)
  96.         switch (option) {
  97.         case 'l':
  98.             nolist = FALSE;
  99.             break;
  100.         case 'm':
  101.             strcpy(modname, optarg);
  102.             break;
  103.         case 'o':
  104.             strcpy(objname, optarg);
  105.             break;
  106.         case '?':
  107.             printf("idtag Version %s by Kenji Rikitake\n", version);
  108.             puts("Usage: idtag [-l] [-m module-name] [-o objfile-name] comment-file");
  109.             return -1;
  110.         default:
  111.             puts("idtag: getopt error");
  112.             return -2;
  113.         }
  114.  
  115.     strcpy(srcname, argv[optind]);
  116.     
  117.     if (nolist) {
  118.         comment_attrib = COMAT_NOPURGE | COMAT_NOLIST;
  119.         }
  120.     else {
  121.         comment_attrib = COMAT_NOPURGE;
  122.         }
  123.     
  124.     if ((objfp = fopen(objname, "wb")) == NULL) {
  125.         fprintf(stderr, "idtag: can't open output file \"%s\"\n", objname);
  126.         exit(-1);
  127.         }
  128.     
  129.     i = strlen(modname);
  130.     buf[0] = i & 0x00ff;
  131.     strcpy((buf + 1), modname);
  132.     write_obj_record(R_THEADR, strlen(buf), buf);
  133.     
  134.     buf[0] = COMAT_NOPURGE | COMAT_NOLIST;
  135.     buf[1] = COMCLASS_LANGUAGE;
  136.     sprintf((buf + 2), "This comment created by Kenji's IDtag %s", version);
  137.     
  138.     write_obj_record(R_COMENT, 2 + strlen(buf + 2), buf);
  139.     
  140.     if ((srcfp = fopen(srcname, "rb")) == NULL) {
  141.         fprintf(stderr, "idtag: can't open source file \"%s\"\n", srcname);
  142.         exit(-1);
  143.         }
  144.     
  145.     buf[0] = comment_attrib;
  146.     buf[1] = COMCLASS_EXESTR;
  147.     
  148.     for ( ; ; ) {
  149.         if (fgets((buf + 2), (BUFSIZ - 2), srcfp) == (char *)NULL) {
  150.             break;
  151.             }
  152.         write_obj_record(R_COMENT, 2 + strlen(buf + 2), buf);
  153.         }
  154.     
  155.     fclose(srcfp);
  156.     
  157.     buf[0] = 0x00;
  158.     write_obj_record(R_MODEND, 1, buf);
  159.     
  160.     fclose(objfp);
  161.     exit(0);
  162.     return;
  163. }
  164.