home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / aufstols.zoo / aufstools.1 / binhex / gethead.c < prev    next >
C/C++ Source or Header  |  1991-02-26  |  4KB  |  173 lines

  1. /*
  2.  * Change a .info file into a proper header for a .hqx file
  3.  *
  4.  * David Gentzel, Lexeme Corporation
  5.  *
  6.  * Based on code written by ????.
  7.  */
  8.  
  9. #include <stdio.h>
  10. #ifdef VMS
  11. # include <types.h>
  12. # include <stat.h>
  13. #else
  14. # include <sys/types.h>
  15. # include <sys/stat.h>
  16. #endif
  17.  
  18. #include "aufs.h"
  19.  
  20. #define NAMEBYTES 63
  21. #define H_NLENOFF 1
  22. #define H_NAMEOFF 2
  23.  
  24. /* 65 <-> 80 is the FInfo structure */
  25. #define H_TYPEOFF 65
  26. #define H_AUTHOFF 69
  27. #define H_FLAGOFF 73
  28.  
  29. #define H_LOCKOFF 81
  30. #define H_DLENOFF 83
  31. #define H_RLENOFF 87
  32. #define H_CTIMOFF 91
  33. #define H_MTIMOFF 95
  34.  
  35. /* Append cnt bytes to the output buffer starting at head[offset]. */
  36. #define put(cnt, offset) \
  37. { \
  38.     register char *a = &head[(int) offset]; \
  39.     register int b = (int) (cnt); \
  40.  \
  41.     while (b--) \
  42.     *out++ = *a++; \
  43. }
  44.  
  45. /* Append cnt bytes to the output buffer starting at string. */
  46. #define put2(cnt, string) \
  47. { \
  48.     register int b = (int) (cnt); \
  49.     register char *a = (char *) (string); \
  50.  \
  51.     while (b--) \
  52.     *out++ = *a++; \
  53. }
  54.  
  55. /* Append cnt bytes to the output buffer starting at string + (cnt - 1) and
  56.    working backwards. */
  57. #define put2rev(cnt, string) \
  58. { \
  59.     register int b = (int) (cnt); \
  60.     register char *a = (char *) (string) + b; \
  61.  \
  62.     while (b--) \
  63.     *out++ = *--a; \
  64. }
  65.  
  66. /* Build a usable header out of the .info information.  head is the text from
  67.    the .info file, out is an output buffer. */
  68. void gethead(head, out)
  69. register char *head, *out;
  70. {
  71.     put(1, H_NLENOFF);        /* Name length */
  72.     put(head[1], H_NAMEOFF);    /* Name */
  73.     put(1, 0);            /* NULL */
  74.     put(4, H_TYPEOFF);        /* Type */
  75.     put(4, H_AUTHOFF);        /* Author */
  76.     put(2, H_FLAGOFF);        /* Flags */
  77.     put(4, H_DLENOFF);        /* Data length */
  78.     put(4, H_RLENOFF);        /* Resource length */
  79. }
  80.  
  81. /* Build a usable header out of the .finderinfo information.
  82.    out is an output buffer. */
  83. void aufs_gethead(info, data, rsrc, out)
  84. register char *out;
  85. register FinderInfo *info;
  86. FILE *data, *rsrc;
  87. {   register int len;
  88.     long rlen, dlen;
  89.     struct stat st;
  90.  
  91.     if(info->fi_bitmap & FI_BM_MACINTOSHFILENAME)
  92.     {   len = strlen(info->fi_macfilename);
  93.     *out++ = (char)len;
  94.     put2(len+1, info->fi_macfilename);
  95.     }
  96.     else
  97.     {   len = strlen(info->fi_shortfilename);
  98.     *out++ = (char)len;
  99.     put2(len+1, info->fi_shortfilename);
  100.     }
  101.     put2(4, &info->fndr_type);        /* Type */
  102.     put2(4, &info->fndr_creator);    /* Author */
  103.     put2(2, &info->fndr_flags);        /* Flags */
  104.  
  105.     if (rsrc != NULL)
  106.     {
  107.     (void) fstat(fileno(rsrc), &st);
  108.     rlen = (long) st.st_size;
  109.     }
  110.     else
  111.     rlen = 0L;
  112.     if (data != NULL)
  113.     {
  114.     (void) fstat(fileno(data), &st);
  115.     dlen = (long) st.st_size;
  116.     }
  117.     else
  118.     dlen = 0L;
  119.     put2(4, &dlen);        /* Data length */
  120.     put2(4, &rlen);        /* Resource length */
  121. }
  122.  
  123. /* Fake a usable header (there was no .info file). */
  124. /* VMS NOTE:
  125.     It is possible that the use of fstat to figure the sizes of the
  126.     .data and .rsrc files will not work correctly if they are not
  127.     Stream_LF files.  Not easy to get around, but not very common either
  128.     (will only cause problem if .info file is missing and either .data
  129.     or .rsrc is not Stream_LF, and xbin creates Stream_LF files).
  130. */
  131. void fakehead(file, rsrc, data, out)
  132. char *file;
  133. FILE *rsrc, *data;
  134. register char *out;
  135. {
  136.     unsigned char flen;
  137.     long rlen, dlen;
  138.     char flags[2];
  139.     struct stat st;
  140.  
  141.     flen = (unsigned char) strlen(file);
  142.     if (rsrc != NULL)
  143.     {
  144.     (void) fstat(fileno(rsrc), &st);
  145.     rlen = (long) st.st_size;
  146.     }
  147.     else
  148.     rlen = 0L;
  149.     if (data != NULL)
  150.     {
  151.     (void) fstat(fileno(data), &st);
  152.     dlen = (long) st.st_size;
  153.     }
  154.     else
  155.     dlen = 0L;
  156.     flags[0] = '\0';
  157.     flags[1] = '\0';
  158.  
  159.     put2(1, &flen);        /* Name length */
  160.     put2(flen, file);        /* Name */
  161.     put2(1, "");        /* NULL */
  162.     put2(4, "TEXT");        /* Type */
  163.     put2(4, "????");        /* Author */
  164.     put2(2, flags);        /* Flags */
  165. #ifdef DONTSWAPINT
  166.     put2(4, dlen);        /* Data length */
  167.     put2(4, rlen);        /* Resource length */
  168. #else
  169.     put2rev(4, dlen);        /* Data length */
  170.     put2rev(4, rlen);        /* Resource length */
  171. #endif
  172. }
  173.