home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / except2x.zip / copydbg.c < prev    next >
Text File  |  1994-03-21  |  2KB  |  76 lines

  1. #include <io.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fcntl.h>
  5. #include <sys\stat.h>
  6. #include <share.h>
  7. /* ------------------------------------------------------------------------ */
  8. /* Last 8 bytes of 16:16 or 32 file when CODEVIEW debugging info is present */
  9. _Packed struct  _eodbug
  10.         {
  11.         unsigned short dbug;          /* 'NB' signature */
  12.         unsigned short ver;           /* version        */
  13.         unsigned long dfaBase;        /* size of codeview info */
  14.         } eodbug;
  15. #define         DBUGSIG         0x424E
  16. #define TRANSFERSIZE 0x8000
  17. char Buffer[TRANSFERSIZE];
  18. int  TotalSize;
  19. int  NewOffset=0;
  20. unsigned long lfaBase;
  21. main(int argc, char **argv,char ** envp)
  22. {
  23.    int ModuleFile;
  24.    int DbgFile;
  25.    if (argc==1) {
  26.       printf("Syntax is COPYDBG  name.exe\n");
  27.       printf("       or COPYDBG  name.dll\n");
  28.       printf("It will generate a name.DBG file\n");
  29.       printf("containing the Debug data only if any\n");
  30.       exit(0);
  31.    } /* endif */
  32.    argc--;argv++;
  33.    ModuleFile =sopen(argv[0],O_RDONLY|O_BINARY,SH_DENYNO);
  34.    if (ModuleFile==-1) {
  35.       perror("Error while opening file :");
  36.       exit(1);
  37.    }
  38.     /* See if any CODEVIEW info */
  39.     if ((TotalSize=lseek(ModuleFile,-8L,SEEK_END))==-1) {
  40.         printf("Error %u seeking CodeView table in %s\n",errno,argv[0]);
  41.         return(18);
  42.     }
  43.  
  44.     if (read(ModuleFile,(void *)&eodbug,8)==-1) {
  45.        printf("Error %u reading debug info from %s\n",errno,argv[0]);
  46.        return(99);
  47.     }
  48.     if (eodbug.dbug!=DBUGSIG) {
  49.        printf("\nNo CodeView information stored.\n");
  50.        return(99);
  51.     }
  52.    strcpy(argv[0]+strlen(argv[0])-3,"DBG"); /* Build DBG File name */
  53.    DbgFile =open(argv[0],O_CREAT |O_WRONLY | O_TRUNC|O_BINARY,S_IREAD | S_IWRITE);
  54.    if (DbgFile==-1) {
  55.       perror("Error while opening file :");
  56.       exit(1);
  57.    }
  58.     if ((lfaBase=lseek(ModuleFile,-eodbug.dfaBase,SEEK_END))==-1L) {
  59.       perror("Error seeking beginning of debug data :");
  60.       return(18);
  61.    }
  62.    TotalSize+=8; /* Include the NB0x and offset */
  63.    TotalSize-=lfaBase;
  64.    while (TotalSize>0) {
  65.       int ReadBytes;
  66.       ReadBytes=read( ModuleFile ,(void *)Buffer,min(TRANSFERSIZE,TotalSize));
  67.       if (ReadBytes>0) {
  68.          TotalSize-=ReadBytes;
  69.          write(DbgFile,(void *)Buffer,ReadBytes);
  70.       } /* endif */
  71.    } /* endwhile */
  72.    close(DbgFile);
  73.    close(ModuleFile);
  74. }
  75.  
  76.