home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / EMXLIB8F.ZIP / EMX / LIB / IO / FSEEKHDR.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-02  |  1.2 KB  |  48 lines

  1. /* fseekhdr.c (emx+gcc) -- Copyright (c) 1990-1993 by Eberhard Mattes */
  2.  
  3. #include <stdio.h>
  4. #include <memory.h>
  5. #include <errno.h>
  6.  
  7. int _fseek_hdr (FILE *stream)
  8. {
  9.   struct
  10.     {
  11.       char magic[2];
  12.       char fill1[6];
  13.       unsigned short hdr_size;
  14.     } exe_hdr;
  15.   struct
  16.     {
  17.       char sig[16];
  18.       char bound;
  19.       char fill1;
  20.       unsigned short hdr_loc_lo;      /* cannot use long, alignment! */
  21.       unsigned short hdr_loc_hi;
  22.     } patch;
  23.   long original_pos;
  24.   int saved_errno;
  25.  
  26.   original_pos = ftell (stream);
  27.   if (fread (&exe_hdr, sizeof (exe_hdr), 1, stream) != 1)
  28.     goto failure;
  29.   if (memcmp (exe_hdr.magic, "MZ", 2) != 0)
  30.     return (fseek (stream, original_pos, SEEK_SET) == -1 ? -1 : 0);
  31.   if (fseek (stream, original_pos + 16 * exe_hdr.hdr_size, SEEK_SET) == -1)
  32.     goto failure;
  33.   if (fread (&patch, sizeof (patch), 1, stream) != 1)
  34.     goto failure;
  35.   if (memcmp (patch.sig, "emx", 3) != 0)
  36.     goto failure;
  37.   if (fseek (stream, original_pos + patch.hdr_loc_lo
  38.              + 65536L * patch.hdr_loc_hi, SEEK_SET) == -1)
  39.     goto failure;
  40.   return (0);
  41.  
  42. failure:
  43.   saved_errno = errno;
  44.   fseek (stream, original_pos, SEEK_SET);
  45.   errno = saved_errno;
  46.   return (-1);
  47. }
  48.