home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Workbench / Archivers / mcvertWOS.lha / mcvertppc / mcvert.c < prev    next >
Text File  |  1998-04-14  |  56KB  |  1,414 lines

  1. /**
  2.  * mcvert.c - version 1.05 - 10 January, 1990
  3.  * Written by Doug Moore - Rice University - dougm@rice.edu - April '87
  4.  
  5.  * Sun bug fixes, assorted stuff - Jim Sasaki, March '89
  6.  
  7.  * Changed default max_line_size from 2000 to unlimited -
  8.  *                                           Doug Moore, April, '89
  9.  
  10.  * Sun 3/60 doesn't like odd-sized structs.  Bug fixed - Doug Moore, April, '89
  11.  *                                              - aided by Spencer W. Thomas
  12.  
  13.  * Didn't handle properly many hqx files combined in one file.  Bug fixed -
  14.  *                                           Doug Moore, June, '89
  15.  
  16.  * Modified to handle MacBinaryII specification. Jim Van Verth, Sept, '89
  17.  
  18.  * Fixed a bug when there are blank lines in hqx data, as happens when newline
  19.  * get translated to CRLF and then to \n\n, common for some file transfers.
  20.  * The last hqx line would be lost if the previous line was blank or junk.
  21.  *      Glenn Trewitt, Stanford University, 1990        (1.05)
  22.  
  23.  * Fixed a bug that occurred when extracting data or resource
  24.  * forks on a Sun 4.  It was a byte alignment problem.
  25.  * Rick Zaccone, zaccone@bucknell.edu.  April 1991.  Version 1.6
  26.  
  27.  * Fixed:
  28.  *   Sent all "Converting ... " lines to stdout instead of stderr
  29.  *   Changed mactypes.h for HP-UX systems
  30.  *      Alan Danziger, aland@cs.brandeis.edu.  October 1991. Version 1.6.5
  31.  
  32.  * ----------------------------------------------------------------------------
  33.  * External
  34.  * -----
  35.  * Fixed buffering bug when converting very small MacBinary files to hqx files.
  36.  * Provide helpful usage line.
  37.  * Control "Converting ... " lines separately with -S flag.
  38.  * Make encoding and decoding consistent by ignoring locked and init flags.
  39.  * Clean up some error messages; check for more errors; provide errno on error.
  40.  * Updated the man page.
  41.  * -----
  42.  * Internal
  43.  * -----
  44.  * Reformat source (sorry, local standard used by tools is tab space == 3)
  45.  * Remove compiler warning messages.
  46.  * Rename some variables.
  47.  * Added some comments to code.
  48.  * Added some offsets to struct definitions.
  49.  * Since the makefile has compilation flags,
  50.  *    make the compiles depend on the Makefile.
  51.  * -----
  52.  * Thanks to all who have gone before for creating, maintaining,
  53.  * improving, and providing this program and documentation.
  54.  * -----
  55.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  56.  * {Jskud@std.mentorg.com,Joseph_Skudlarek@mentorg.com}
  57.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  58.  * Version 1.70 09Jul92
  59.  * ----------------------------------------------------------------------------
  60.  
  61.  * ----------------------------------------------------------------------------
  62.  * External
  63.  * -----
  64.  * Added -V (Verbose) option (includes debugging information).
  65.  * Fixed bug converting hqx to MacBinary if last line is ":".
  66.  * Avoided a silent error and quick exit situation.
  67.  * -----
  68.  * Internal
  69.  * -----
  70.  * Got rid of almost all lint (SunOS and HP-UX) error messages.
  71.  * Compiled on SunOs, HP-UX, DomainOS.
  72.  * Incorporated Parag Patel <parag@netcom.com> changes for AU/X.
  73.  *    Here's some diffs for really quick cheap hacks to get mcvert to compile
  74.  *    and run under A/UX.  The main problem was that timeb does not exist, so
  75.  *    I added 2 #ifdef TIMEVAL to use the System-V timeval package instead.
  76.  *    The Makefile just has a -DTIMEVAL and a magic -U_SYSV_SOURCE to get
  77.  *    around a pre-defined type "ulong" in sys/types.h (thanks to Apple).
  78.  * Did more code overhauling:
  79.  *    add lots more comments, rename variables, reformat source.
  80.  * Put code in un_hqx to avoid suspected buffering problem.
  81.  * -----
  82.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  83.  * (503) 685-1576 (work)
  84.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  85.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  86.  * Version 1.80 15Jul92
  87.  * ----------------------------------------------------------------------------
  88.  
  89.  * ----------------------------------------------------------------------------
  90.  * External
  91.  * -----
  92.  * Made hqx file scan processing much smarter
  93.  *    so, for example, info-mac/comm/qwk-reader.hqx,
  94.  *    complete with extraneous colons in column one, converts correctly
  95.  *    (problem described by Edward John Sabol <es2j+@andrew.cmu.edu>)
  96.  * Avoid silly perror on usage message (prompted by Edward John Sabol)
  97.  * Improve error message regarding improper format
  98.  * Added more caveats to man page
  99.  * -----
  100.  * Internal
  101.  * -----
  102.  * Fixed typo's in printf lines to pass all expected arguments
  103.  *    (pointed out by Bo Holst-Christensen
  104.  *    [holst@diku.dk/dikubhc1@uts.uni-c.dk/holst@login.dkuug.dk])
  105.  * Tweak Makefile to ease shar creation and special case ulong, not A/UX
  106.  * Add yet more comments and debugging code
  107.  * -----
  108.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  109.  * (503) 685-1576 (work)
  110.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  111.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  112.  * Version 1.82 30Jul92
  113.  * ----------------------------------------------------------------------------
  114.  
  115.  * ----------------------------------------------------------------------------
  116.  * External
  117.  * -----
  118.  * relax exactly 64 characters per incoming hqx file, and
  119.  * handle files without trailing newline, so, eg, 
  120.  *    Telnet2.5docs.sit.hqx now converts correctly
  121.  *       (failure reported by Justin Sullivan <justin@f2.facts.uky.edu>)
  122.  *    now also processes info-mac/app/road-map.hqx correctly
  123.  *       (failure reported by Victor Norton<norton@andy.bgsu.edu>)
  124.  * rework the man page for improved clarity and completeness
  125.  * -----
  126.  * Internal
  127.  * -----
  128.  * avoid warning message from gcc on Sequent Balance mainframe
  129.  *    reported by Justin Sullivan <justin@f2.facts.uky.edu>
  130.  * bump max incoming line length to 2048 from 255
  131.  * add mcvert.ps target to Makefile
  132.  * -----
  133.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  134.  * (503) 685-1576 (work)
  135.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  136.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  137.  * Version 1.83 03Aug92
  138.  * ----------------------------------------------------------------------------
  139.  
  140.  * ----------------------------------------------------------------------------
  141.  * External
  142.  * -----
  143.  * Found and fixed problem with byte ordering plaguing users of
  144.  *    Sequent's Balance running DYNIX, and DEC computers.
  145.  *    The error message looked something like
  146.  *       hqx_to_bin_fork: writing nnn too many bytes
  147.  * Avoid generating debugging info if not to be printed -- cut runtime in half!
  148.  * Generalize and incorporate -I (info) option processing provided by
  149.  *    Paul Franklin, Computer Enginnering, Univ. of Calif., Davis CA 95616
  150.  *    pdfranklin@ucdavis.edu.
  151.  * Added heuristic to avoid false matches in mail headers.
  152.  *    problem expertly characterized, solution beta tested, and subsequent
  153.  *    improvement suggested by "Jim (J.) Lattanzi" <lattanzi@bnr.ca>
  154.  *    so segmented comp.binaries.mac files (either multi-file or concatenated
  155.  *    single file) should now convert correctly.
  156.  * Added -H switch to disable heuristic.
  157.  * Document heuristic in man page.
  158.  * Fixed (long-standing) bug which precluded -p option from being recognized
  159.  *    and verified decompressing and unpacking of PIT files working.
  160.  *    Thanks to Dave Clemans for providing me with a version of PackIt.
  161.  * Add the version to the extened Usage message emitted by the program.
  162.  * Tune the syntax of the summary in the program and man page.
  163.  * Cleaned up spelling mistakes in the man page.
  164.  * -----
  165.  * Internal
  166.  * -----
  167.  * Close all open streams --
  168.  *    fix for binfile by Paul Franklin <pdfranklin@ucdavis.edu>
  169.  * Incorporate changes suggested by Barry_Wolman@transarc.com
  170.  *    to mactypes.h and Makefile for support of IBM RS/6000 running AIX 3.2
  171.  *    reformat Makefile to avoid long option lines
  172.  * Identify the right Makefile lines for Irix too
  173.  *    suggested by Jack Repenning (jackr@dblues.wpd.sgi.com)
  174.  * Clean up the stream handling and add mopen/mclose
  175.  *    avoid unnecessary /dev/null opens
  176.  *    all file open/close/read/write are checked for success
  177.  * Lower lint content on SunOS and HP-UX.
  178.  *    avoiding all "sometimes ignored" lint messages.
  179.  * Improve modularity with mopen/mclose/converting routines.
  180.  * Tune debugging output information.
  181.  * Verify that passes smoke tests on DomainOS/SunOS/HP-UX/ULTRIX.
  182.  * Reformat these comments to avoid tabs.
  183.  * -----
  184.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  185.  * (503) 685-1576 (work)
  186.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  187.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  188.  * Version 1.87 25Sep92
  189.  * ----------------------------------------------------------------------------
  190.  
  191.  * ----------------------------------------------------------------------------
  192.  * External
  193.  * -----
  194.  * Add README file which describes how to configure and compile mcvert
  195.  * Handle multiple BinHex4.0 inputs in a single file again (thanks to
  196.  *    <Mark_Larimer@pigeon.cpg.cdc.com> for pointing out this regression)
  197.  * Emit the MacBinary header if verbose (to get create and modify times)
  198.  * -----
  199.  * Internal
  200.  * -----
  201.  * Rename some variables, create mac2unix (time) routine, more comments
  202.  * Keep lint content low
  203.  * Pull unnecessary include of <net/nh.h> -- avoid breaking AIX 3.1
  204.  * Fix = vs == typo dealing with protect bit
  205.  * -----
  206.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  207.  * (503) 685-1576 (work)
  208.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  209.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  210.  * Version 1.88 08Dec92
  211.  * ----------------------------------------------------------------------------
  212.  
  213.  * ----------------------------------------------------------------------------
  214.  * External
  215.  * -----
  216.  * emit input file names when verbose is enabled
  217.  *    (suggested by franklin@eecs.ucdavis.edu)
  218.  * make it easier to build on AT&T 3B2's
  219.  * -----
  220.  * Internal
  221.  * -----
  222.  * provide compile time switch to avoid bzero and bcopy, and use memset
  223.  *    and memcpy instead (pointed out by linger@drystone.attmail.com, and
  224.  *    requested again by Larry S. Staples <attjp4!lss>)
  225.  * update Makefile to include incantations required for AT&T 3B2's
  226.  * fflush all diagnostic output to ensure correct order when output to a file
  227.  * -----
  228.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  229.  * (503) 685-1576 (work)
  230.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  231.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  232.  * Version 1.89 05Jan93
  233.  * ----------------------------------------------------------------------------
  234.  
  235.  * ----------------------------------------------------------------------------
  236.  * External
  237.  * -----
  238.  * incorporate MAC_FILETYPE support provided by <root@genome.stanford.edu>
  239.  * minor edits to man page
  240.  * -----
  241.  * Internal
  242.  * -----
  243.  * update Makefile to simplify incantations required for AT&T 3B2's
  244.  * -----
  245.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  246.  * (503) 685-1576 (work)
  247.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  248.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  249.  * Version 1.90 04Mar93
  250.  * ----------------------------------------------------------------------------
  251.  
  252.  * ----------------------------------------------------------------------------
  253.  * External
  254.  * -----
  255.  * handle -b (both .data and .rsrc at same time) option
  256.  *    ( -> MacBinary suggested by bb@math.ufl.edu)
  257.  * remove anomalous file extension handling
  258.  *    (suggested by bb@math.ufl.edu)
  259.  * regularize MAC_EDITOR (author) and MAC_FILETYPE (file type) handling
  260.  * detect and report important file format errors
  261.  * emit output file name too
  262.  * revise Usage line
  263.  * massively revise man page to reflect changes and generally overhaul
  264.  * -----
  265.  * Internal
  266.  * -----
  267.  * avoid overwriting internal storage (what a chore!)
  268.  *    for example, mcvert -UI *.hqx used to abort with a segmentation violation
  269.  *    symptom reported by franklin@eecs.ucdavis.edu  Thu Sep 24 16:39:21 1992
  270.  * check return values from all getc/putc operations
  271.  * find and fix ancient bug extracting resource fork from MacBinary format
  272.  * identify failing file in EOF error messages
  273.  * clarify and amend distribution and update restrictions
  274.  * expand disclaimer (patterned after INFO-MAC CD-ROM -- Thx, Cliff and Joe!)
  275.  * -----
  276.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  277.  * (503) 685-1576 (work)
  278.  * {Jskud@std.MENTORG.Com,Joseph_Skudlarek@MENTORG.Com}
  279.  * ...{uunet,nosun,sequent,attunix,apollo}!mntgfx!Jskud
  280.  * Version 2.00 28Feb93
  281.  * ----------------------------------------------------------------------------
  282.  
  283.  * ----------------------------------------------------------------------------
  284.  * External
  285.  * -----
  286.  * unixify all relevent output files
  287.  *    (replace suspect characters with _)
  288.  * macify all relevent MacBinary files
  289.  *    (replace suspect chars with -, take first 20 and last 11 if > 31 char)
  290.  *    avoiding Mac file name > 31 chars (Rick Zaccone zaccone@bucknell.edu)
  291.  * always emit the Macintosh file name in the converting messages
  292.  *    since the UNIX file names are now provided by default
  293.  * rework man page to bring it more up to date, added OTHER SOURCES section
  294.  * emit data and rsrc len when printing bin header
  295.  * report the input character, not the mapped character, if avail,
  296.  *    else report mapped value as hex
  297.  * add -VV (Very Verbose) option
  298.  * -----
  299.  * Internal
  300.  * -----
  301.  * distribute mcvert.idraw, a postscript file
  302.  *    describing mcvert options and transformations pictorially,
  303.  *    contributed by Brian Bartholomew - bb@math.ufl.edu
  304.  * create and distribute README-conversion file
  305.  * update Makefile and README to make it more obvious how to build mcvert
  306.  *    problem reported by David Micklethwaite <mickles@cherax.super.csiro.au>
  307.  * make s/S/v/V flag processing serially reusable
  308.  * avoid obsolete ftime on HP-UX, SunOS, DomainOS -- default is now -DTIMEVAL
  309.  *    problem reported by smith@sfu.ca (Richard Smith) and
  310.  *    Adam Harris (harris@cs.uchicago.edu)
  311.  * avoid SGI bug regarding unterminated character constant within #ifdef notdef
  312.  *    problem reported by smith@sfu.ca (Richard Smith)
  313.  * clean up some FILE confusion
  314.  * make the man page work well across platforms
  315.  * re-lint on SunOS and HP-UX
  316.  * add various additional comments
  317.  * -----
  318.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  319.  * (503) 685-1576 (work)
  320.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  321.  * Version 2.09 30Jun93
  322.  * ----------------------------------------------------------------------------
  323.  
  324.  * ----------------------------------------------------------------------------
  325.  * External
  326.  * -----
  327.  * -----
  328.  * Internal
  329.  * -----
  330.  * incorporate SCO UNIX requirements into the Makefile
  331.  *    info from Fred Lenk, Camarillo, CA [fredgl@tecnet1.jcte.jcs.mil]
  332.  * backout intermediate XOBJ cleanup and continue to do what works for AT&T 3B2
  333.  *    belated & current thanks to Larry S. Staples [attjpn!lss@attibr.att.com]
  334.  *    for providing and proofing the working recipe for AT&T 3B2
  335.  * change OTHER SOURCES to OTHER PROGRAMS in man page, and mention programs
  336.  *    which run on the Mac, including CompactPro, StuffIt, and BinHex 4.0
  337.  * ship the formatted ASCII version of the man page for those without nroff
  338.  * -----
  339.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  340.  * (503) 685-1576 (work)
  341.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  342.  * Version 2.12 19Jul93
  343.  * ----------------------------------------------------------------------------
  344.  
  345.  * ----------------------------------------------------------------------------
  346.  * External
  347.  * -----
  348.  * -----
  349.  * Internal
  350.  * -----
  351.  * add StuffIt Expander mention to man page
  352.  * suggest using text (-t|-u) if data is text in the data (-d) description
  353.  * incorporate AIX Makefile improvement provided by
  354.  *    DaviD W. Sanderson (dws@ssec.wisc.edu)
  355.  * -----
  356.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  357.  * (503) 685-1576 (work)
  358.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  359.  * Version 2.13 13Sep93
  360.  * ----------------------------------------------------------------------------
  361.  
  362.  * ----------------------------------------------------------------------------
  363.  * External
  364.  * -----
  365.  * add -P (pipe to stdout) option
  366.  *    capability requested by lentz@rossi.astro.nwu.edu (Robert Lentz)
  367.  * have all info messages go to stderr, not stdout, to faciliate -P
  368.  *    (undo converting msg to stdout from version 1.6.5, dated Oct 1991)
  369.  * update man page to indicate changes
  370.  * -----
  371.  * Internal
  372.  * -----
  373.  * fiddle with info message to make it a bit clearer
  374.  * -----
  375.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  376.  * (503) 685-1576 (work)
  377.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  378.  * Version 2.14 10Nov93
  379.  * ----------------------------------------------------------------------------
  380.  
  381.  * ----------------------------------------------------------------------------
  382.  * External
  383.  * -----
  384.  * -----
  385.  * Internal
  386.  * -----
  387.  * ensure all source line lengths are less than 80
  388.  *    after someone or something wrapped the 2.14 archive, and it
  389.  *    would not compile (split a character string literal).
  390.  *    Thanks to Robert Hagopian (rhagopia@terminator.rs.itd.umich.edu)
  391.  *    and to L L Campbell (campbell@brahms.udel.edu) for bringing this
  392.  *    problem to my attention.
  393.  *    [recall that shar usually prepends X, which bumps the line length by 1]
  394.  *    [also, < 80 makes editing with emacs at 80 columns a bit better]
  395.  * add check_linelen to Makefile to help ensure compliance
  396.  * -----
  397.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  398.  * (503) 685-1576 (work)
  399.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  400.  * Version 2.15 15Nov93
  401.  * ----------------------------------------------------------------------------
  402.  
  403.  * ----------------------------------------------------------------------------
  404.  * External
  405.  * -----
  406.  * -----
  407.  * Internal
  408.  * -----
  409.  * fix original bug of failing to convert lengths when extracting data
  410.  *    and resource forks from MacBinary files -- thanks to Thomas Lange
  411.  *    <tlange@namu01.gwdg.de> for finding and supplying the fix for this bug
  412.  * tidy up for Alpha OSF1 -- thanks to Holger Debelts
  413.  *    <Debelts@rrz.Uni-Koeln.DE> for providing a fix for time()'s redeclaration
  414.  * check <= 80 on *archive*, and avoid always rebuilding shar
  415.  *    due to unfulfilled check_linelen dependency
  416.  * improve 2.15 comments
  417.  * toss uninteresting lint messages
  418.  * -----
  419.  * Joseph Skudlarek  Mentor Graphics  8005 SW Boeckman Rd  Wilsonville OR 97070
  420.  * (503) 685-1576 (work)
  421.  * {Jskud@wv.MentorG.com,Joseph_Skudlarek@MentorG.com}
  422.  * Version 2.16 14Oct94
  423.  * ----------------------------------------------------------------------------
  424.  
  425.  * ----------------------------------------------------------------------------
  426.  * This program may be freely distributed for non-profit purposes.  It
  427.  * may not be sold, by itself or as part of a collection of software.
  428.  * However, it may be distributed in source form with large
  429.  * collections of freeware and shareware, such as the INFO-MAC CD-ROM,
  430.  * which charge only a modest fee for publishing, but not selling, the
  431.  * software.  It may be freely modified as long as no modified version
  432.  * is independently distributed.  Modifications of interest to all can
  433.  * be incorporated into the program by sending them to me for
  434.  * inclusion and redistribution, or by releasing an updated mcvert to
  435.  * the info-mac archives.  Parts of the code can be used in other
  436.  * programs.  We hope you find mcvert useful, and enjoy using it.
  437.  * ----------------------------------------------------------------------------
  438.  
  439.  * ----------------------------------------------------------------------------
  440.  * DISCLAIMER -- USE mcvert software AT YOUR OWN RISK
  441.  * This mcvert software is provided "as is" without warrantee of any
  442.  * kind.  The entire risk as to the results and performance of the
  443.  * software is assumed by you, and in no event will we be liable for
  444.  * any consequential, incidental, or indirect damages suffered in the
  445.  * course of using this software.
  446.  * ----------------------------------------------------------------------------
  447.  
  448.  * ----------------------------------------------------------------------------
  449.  * Things that yet could be done:
  450.  * ---
  451.  * handle incoming BinHex4.0 files with ^M instead of ^J
  452.  * (requested by jonathan brecher brecher@husc.harvard.edu 29Mar93)
  453.  *  eg, right now we toss the entire line if it doesn't start with a colon
  454.  *  but the start of the line could be later on, following a ^M
  455.  * check for more file format errors, eg, MacBinary must be multiple of 128
  456.  * option to avoid .text extension on write (Rick Zaccone zaccone@bucknell.edu)
  457.  * check return values from fputs/fprintf
  458.  * provide header heuristic tuning option: set length and/or same sensitivity
  459.  * ----------------------------------------------------------------------------
  460.  */
  461.  
  462. /*
  463.  * Naming
  464.  *              DOWNLOAD
  465.  *                      => converting TO MacBinary
  466.  *                      => direction == FORWARDS
  467.  *                      => use un_* routines (un => UNdo encoding?)
  468.  *              UPLOAD
  469.  *                      => converting FROM MacBinary
  470.  *                      => direction == BACKWARDS
  471.  *                      => use re_* routines (re => Really Encode?)
  472.  */
  473.  
  474. #include "mactypes.h"
  475.  
  476. /* it would be natural to use an enum here, but avoid "fancy" features */
  477. #define HQX 0
  478. #define TEXT 1
  479. #define DATA 2
  480. #define RSRC 3
  481. #define BOTH 5
  482.  
  483. #define FORWARDS 0
  484. #define BACKWARDS 1
  485. #define VERSION 2.16
  486.  
  487. FILE *devnull;
  488. FILE *convert;
  489. FILE *verbose;
  490. FILE *Mydebug;
  491. int   MyDebug;
  492. int bla;
  493.  
  494. char **hqxnames, **hqxnames_left;
  495. char *dir, *ext, *mac_auth, *mac_type;
  496. int translate_eol;
  497. char *maxlines_str;
  498. int maxlines;
  499.  
  500. /* used to skip suspect mail header lines */
  501. int suspect_shorter = 12;
  502. int suspect_same = 1;
  503.  
  504. /* used to avoid writing output files */
  505. int info_only;
  506.  
  507. /* pipe appropriate output -- write to stdout */
  508. int pipe_out;
  509.  
  510.  
  511. char *cmdname;
  512.  
  513. main(int argc, char **argv)
  514. {
  515.         char *flags, *getenv();
  516.         int direction, mode, unpit_flag;
  517.  
  518.         cmdname = argv[0];
  519.  
  520.         /* Early error and clean exit if missing arguments */
  521.         if (argc < 2) {
  522.                 usage();
  523.                 /*NOTREACHED*/
  524.         }
  525.  
  526.         devnull = fopen("nil:", "w+");
  527.  
  528.         argv++;
  529.         argc--;
  530.  
  531.         convert = stderr;
  532.         verbose = stderr;
  533.         Mydebug = devnull;
  534.         MyDebug = 0;
  535.  
  536.         direction = FORWARDS;
  537.         mode = HQX;
  538.         unpit_flag = 0;
  539.  
  540.         mac_type = getenv("MAC_FILETYPE");
  541.         mac_auth = getenv("MAC_EDITOR");
  542.  
  543.         if ((ext = getenv("MAC_EXT")) == NULL)
  544.                 ext = ".bin";
  545.         if ((dir = getenv("MAC_DLOAD_DIR")) == NULL)
  546.                 dir = "";
  547.         if ((maxlines_str = getenv("MAC_LINE_LIMIT")) == NULL)
  548.                 maxlines = 0;
  549.         else {
  550.                 maxlines = atoi(maxlines_str);
  551.                 if (maxlines < MIN_HQX_LINES) {
  552.                         fprintf(stderr, "%s: %s; was %d; reset to %d\n",
  553.                                 cmdname,
  554.                                 "warning: MAC_LINE_LIMIT too small",
  555.                                 maxlines, MIN_HQX_LINES);
  556.                         fflush(stderr);
  557.                         maxlines = MIN_HQX_LINES;
  558.                 }
  559.         }
  560.  
  561.         /* Make command line arguments globally accessible */
  562.         hqxnames = (char **) calloc((unsigned)argc + 1, sizeof(char *));
  563.         hqxnames_left = hqxnames;
  564.         while (argc--)
  565.                 *hqxnames_left++ = *argv++;
  566.  
  567.         /* Flag the end of the list */
  568.         *hqxnames_left = "-";
  569.         hqxnames_left = hqxnames;
  570.  
  571.         /* While not at the end of the list */
  572.         while (strcmp(*hqxnames_left, "-")) {
  573.                 translate_eol = 0;
  574.                 if (hqxnames_left[0][0] == '-') {
  575.                         flags = *hqxnames_left++;
  576.                         while (*++flags)
  577.                                 switch (*flags) {
  578.                                 case 'x':
  579.                                         mode = HQX;
  580.                                         break;
  581.                                 case 'u':
  582.                                         translate_eol = 1;
  583.                                         mode = TEXT;
  584.                                         break;
  585.                                 case 'd':
  586.                                         mode = DATA;
  587.                                         break;
  588.                                 case 'r':
  589.                                         mode = RSRC;
  590.                                         break;
  591.                                 case 'h':
  592.                                         translate_eol = 0;
  593.                                         mode = TEXT;
  594.                                         break;
  595.                                 case 'b':
  596.                                         mode = BOTH;
  597.                                         break;
  598.                                 case 't':
  599.                                         translate_eol = 1;
  600.                                         break;
  601.                                 case 'D':
  602.                                         direction = FORWARDS;
  603.                                         break;
  604.                                 case 'U':
  605.                                         direction = BACKWARDS;
  606.                                         break;
  607.                                 case 'q':
  608.                                         unpit_flag = 0;
  609.                                         break;
  610.                                 case 'p':
  611.                                         unpit_flag = 1;
  612.                                         break;
  613.                                 case 'S':
  614.                                         convert = devnull;
  615.                                         verbose = devnull;
  616.                                         Mydebug = devnull;
  617.                                         MyDebug = 0;
  618.                                         break;
  619.                                 case 's':
  620.                                         convert = stderr;
  621.                                         verbose = devnull;
  622.                                         Mydebug = devnull;
  623.                                         MyDebug = 0;
  624.                                         break;
  625.                                 case 'v':
  626.                                         convert = stderr;
  627.                                         verbose = stderr;
  628.                                         Mydebug = devnull;
  629.                                         MyDebug = 0;
  630.                                         break;
  631.                                 case 'V':
  632.                                         convert = stderr;
  633.                                         verbose = stderr;
  634.                                         Mydebug = stderr;
  635.                                         MyDebug++;
  636.                                         break;
  637.                                 case'H':
  638.                                         suspect_shorter = suspect_same = 0;
  639.                                         break;
  640.                                 case 'I':
  641.                                         info_only = 1;
  642.                                         break;
  643.                                 case 'P':
  644.                                         pipe_out = 1;
  645.                                         break;
  646.                                 default:
  647.                                         usage();
  648.                                         /*NOTREACHED*/
  649.                                 }
  650.                 }
  651.  
  652.                 if (direction == BACKWARDS)
  653.                         if (mode == HQX && unpit_flag)
  654.                                 re_hqx();      /* no re_pit() yet */
  655.                         else if (mode == HQX)
  656.                                 re_hqx();
  657.                         else
  658.                                 re_other(mode);
  659.                 else if (mode == HQX)
  660.                         un_hqx(unpit_flag);
  661.                 else
  662.                         un_other(mode);
  663.         }
  664.  
  665.         exit(0);
  666.         /*NOTREACHED*/
  667. }
  668.  
  669. /* An array useful for CRC calculations that use 0x1021 as the "seed" */
  670. word magic[] = {
  671.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  672.     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  673.     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  674.     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  675.     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  676.     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  677.     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  678.     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  679.     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  680.     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  681.     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  682.     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  683.     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  684.     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  685.     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  686.     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  687.     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  688.     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  689.     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  690.     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  691.     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  692.     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  693.     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  694.     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  695.     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  696.     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  697.     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  698.     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  699.     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  700.     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  701.     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  702.     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0
  703. };
  704.  
  705.  
  706. /*
  707.  * calc_crc() --
  708.  *   Compute the MacBinary II-style CRC for the data pointed to by p, with the
  709.  *   crc seeded to seed.
  710.  *
  711.  *   Modified by Jim Van Verth to use the magic array for efficiency.
  712.  */
  713. short
  714. calc_mb_crc(unsigned char *p, long len, short seed)
  715. {
  716.         short hold;             /* crc computed so far */
  717.         long i;                 /* index into data */
  718.  
  719.         extern unsigned short magic[];  /* the magic array */
  720.  
  721.         hold = seed;                           /* start with seed */
  722.         for (i = 0; i < len; i++, p++) {
  723.                 hold ^= (*p << 8);
  724.                 hold = (hold << 8) ^ magic[(unsigned char) (hold >> 8)];
  725.         }
  726.  
  727.         return (hold);
  728. }                               /* calc_crc() */
  729.  
  730.  
  731. /* Report a fatal error, and exit, never return */
  732. error(char msg[], char name[])
  733. {
  734.         fprintf(stderr, msg, name);
  735.         (void)putc('\n', stderr);
  736.         perror("\nlast perror (may not be relevant)");
  737.         fprintf(stderr, "%s: exiting\n", cmdname);
  738.         if (Mydebug == stderr)
  739.                 abort();
  740.         exit(2);
  741.         /*NOTREACHED*/
  742. }
  743.  
  744. /* replace illegal Unix characters in file name */
  745. /* make sure host file name doesn't get truncated beyond recognition */
  746. unixify(register char *np)
  747. {
  748.         register ulong c;
  749.  
  750.         c = strlen(np);
  751.         if (c > SYSNAMELEN - MAXEXTENSION)
  752.                 c = SYSNAMELEN - MAXEXTENSION;
  753.         np[c] = '\0';
  754.  
  755.         /* pre-decrement to match pre-increment within loop */
  756.         np--;
  757.         while (c = *++np)
  758.                 /*
  759.                  * that is, ``if control or blank, or slash, or delete or 8bit''
  760.                  * which is the same as ``if blank, slash, or non graphic''
  761.                 */
  762.                 if (c <= ' ' || c == '/' || c > '~')
  763.                         *np = '_';
  764. }
  765.  
  766. /*
  767.  * replace illegal Macintosh characters in file name
  768.  * return resulting length
  769.  *
  770.  * According to Inside Macintosh, IV-90, valid file names
  771.  *      must be [1..31] characters long
  772.  *      must not contain a colon
  773.  *      must contain only printing characters
  774.  */
  775. macify(char *name, int len, int translate)
  776. {
  777.         register char *np;
  778.         register ulong c;
  779.         char *s, *t;
  780.         char buffer[SYSNAMELEN];
  781.  
  782.         /* make a copy to ensure null terminated */
  783.         strncpy(buffer, name, len);
  784.         buffer[len] = 0;
  785.         np = buffer;
  786.  
  787.         if (len < 1)
  788.                 error("incoming file name is null", "");
  789.         if (len > 31) {
  790.                 /* too long, so just take first 20 and last 11 */
  791.                 s = np + 20;
  792.                 t = np + len - 11;
  793.                 while (*s++ = *t++)
  794.                         ;
  795.                 len = 31;
  796.         }
  797.  
  798.         if (translate) {
  799.                 /* pre-decrement to match pre-increment within loop */
  800.                 np--;
  801.                 while (c = *++np)
  802.  
  803.          /*
  804.  
  805.           * Inside Macintosh, I-246, defines the printable characters
  806.           * for the Macintosh as 0x20 thru 0xD8 less 0x7F.  Yet, the
  807.           * apple character is above this range, at 0xF0, and the diamond
  808.           * character is below this range, at 0x13.  And Adobe Garamond
  809.           * has lots of characgters above 0xD8.
  810.           * 
  811.           * On the other hand, we only translate when processing UNIX
  812.           * file names which are usually ASCII, and ASCII printables are
  813.           * ' ' <= valid <= '~'.
  814.           * 
  815.           * But we want to avoid zapping any characters with the high
  816.           * order bit set, so 8 bit character users are not zinged.  I've
  817.           * never used a SONY-NeWS box, but this one's for you.  But how
  818.           * do we know if/that the UNIX and Macintosh extended characters
  819.           * are the same?
  820.           * 
  821.           * So what to do, what to do?
  822.           * 
  823.           * Let's look at it this way: if the UNIX file name has extended
  824.           * characters in it, they got there for a reason, hopefully on
  825.           * purpose, and we'll not gratuitously modify them.
  826.           * 
  827.           * And it looks like the 6.1.5 Finder running with the 6.0.5
  828.           * System will translate both control characters and colon into
  829.           * a dash, but leave the others alone, so so will we.
  830.           * 
  831.           * I don't know if MacOS, as opposed to the Finder, behaves
  832.           * differently, and it's too late tonight to find out.  Maybe
  833.           * some other time.
  834.  
  835.           */
  836.  
  837.           if (c < ' ' || c == ':' || c == '\177')
  838.                                  *np = '-';
  839.         }
  840.  
  841.         /* copy the resulting string back in place */
  842.         strncpy(name, buffer, len);
  843.  
  844.         return len;
  845. }
  846.  
  847. /*
  848.  * Unix time (GMT since 1-1-1970)
  849.  * Mac time (local since 1-1-1904)
  850.  */
  851. #define MACTIMEDIFF 0x7c25b080  /* Mac time of 00:00:00 GMT, Jan 1, 1970 */
  852.  
  853. /* Convert Unix time to Mac time */
  854. ulong
  855. unix2mac(ulong xtime)
  856. {
  857. #ifdef __PPC__
  858.  
  859.         return long2mac(xtime + MACTIMEDIFF);
  860.  
  861. #else /* __PPC__ */
  862.  
  863. #ifdef TIMEVAL
  864.         struct timeval t;
  865.         struct timezone tz;
  866.  
  867.         gettimeofday(&t, &tz);
  868.         return long2mac(xtime + MACTIMEDIFF
  869.            - 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
  870. #else
  871.         struct timeb tp;
  872.  
  873.         ftime(&tp);
  874.         return long2mac(xtime + MACTIMEDIFF
  875.             - 60 * (tp.timezone - 60 * tp.dstflag));
  876. #endif
  877.  
  878. #endif /* __PPC__ */
  879. }
  880.  
  881. /* Convert Mac time to Unix time */
  882. ulong
  883. mac2unix(ulong xtime)
  884. {
  885. #ifdef __PPC__
  886.  
  887.         return (mac2long(xtime) - MACTIMEDIFF);
  888.  
  889. #else /* __PPC__ */
  890.  
  891. #ifdef TIMEVAL
  892.         struct timeval t;
  893.         struct timezone tz;
  894.  
  895.         gettimeofday(&t, &tz);
  896.         return (mac2long(xtime) - MACTIMEDIFF
  897.            + 60 * (tz.tz_minuteswest - 60 * tz.tz_dsttime));
  898. #else
  899.         struct timeb tp;
  900.  
  901.         ftime(&tp);
  902.         return (mac2long(xtime) - MACTIMEDIFF
  903.             + 60 * (tp.timezone - 60 * tp.dstflag));
  904. #endif
  905.  
  906. #endif /* __PPC__ */
  907. }
  908.  
  909. /*
  910.  * computes the appropriate output files
  911.  * and the appropriate optional suffixes,
  912.  * all depending on the processing "mode"
  913.  */
  914.  
  915. mode_to_fname_suffix(
  916.         int mode, char *base_fname, char *data_fname, char **data_suffix, char *rsrc_fname, char **rsrc_suffix
  917. )
  918. {
  919.  
  920.         /* clear names to indicate nothing selected yet */
  921.         *data_fname = *rsrc_fname = 0;
  922.  
  923.         switch (mode) {
  924.         case TEXT:
  925.                 strcpy(data_fname, base_fname);
  926.                 *data_suffix = ".text";
  927.                 break;
  928.         case DATA:
  929.                 strcpy(data_fname, base_fname);
  930.                 *data_suffix = ".data";
  931.                 break;
  932.         case RSRC:
  933.                 strcpy(rsrc_fname, base_fname);
  934.                 *rsrc_suffix = ".rsrc";
  935.                 break;
  936.         case BOTH:
  937.                 strcpy(data_fname, base_fname);
  938.                 strcat(data_fname, ".data");
  939.                 *data_suffix = "";
  940.                 strcpy(rsrc_fname, base_fname);
  941.                 strcat(rsrc_fname, ".rsrc");
  942.                 *rsrc_suffix = "";
  943.                 break;
  944.         default:
  945.                 error("Internal error: unexpected mode", "");
  946.                 break;
  947.         }
  948. }
  949.  
  950. /*
  951.         This procedure basically copies the input file(s) to the output
  952.         MacBinary file; in TEXT (translate_eol) mode it changes LF's to
  953.         CR's, and in any mode it forges a Mac info header.  Author type
  954.         comes from the MAC_EDITOR environment variable if it is defined.
  955.  */
  956.  
  957. un_other(int mode)
  958. {
  959.         char data_fname[SYSNAMELEN], rsrc_fname[SYSNAMELEN], binfname[SYSNAMELEN];
  960.         FILE *data_file, *rsrc_file, *binfile;
  961.         char *base_fname, *data_suffix, *rsrc_suffix;
  962.         struct stat data_stbuf, rsrc_stbuf;
  963.         ulong dlen, rlen, mtim, ctim;
  964.  
  965.         info_header info;
  966.         register ulong b;                               /* not character, must hold EOF diagnostic */
  967.         register ulong nchars;
  968.         int extra_chars;
  969.         short crc;
  970.         long len;
  971.  
  972.         while (hqxnames_left[0][0] != '-') {
  973.  
  974.                 if (strlen(*hqxnames_left) >= SYSNAMELEN)
  975.                         error("Error: specified base file name is too long", "");
  976.                 base_fname = *hqxnames_left++;
  977.  
  978.                 /* set up file names */
  979.                 mode_to_fname_suffix(mode, base_fname,
  980.                         data_fname, &data_suffix, rsrc_fname, &rsrc_suffix);
  981.  
  982.                 /* process the data file, if requested */
  983.                 dlen = 0;
  984.                 if (*data_fname) {
  985.                         data_file = mopen(data_fname, data_suffix, "r");
  986.                         /*if (fstat(fileno(data_file), &data_stbuf))
  987.                                 error("Cannot stat %s", data_fname);*/
  988.                         mtim = unix2mac((ulong)data_stbuf.st_mtime);
  989.                         ctim = unix2mac((ulong)data_stbuf.st_ctime);
  990.                         dlen = long2mac(data_stbuf.st_size);
  991.                 }
  992.  
  993.                 /* process the rsrc file, if requested */
  994.                 rlen = 0;
  995.                 if (*rsrc_fname) {
  996.                         rsrc_file = mopen(rsrc_fname, rsrc_suffix, "r");
  997.                         /*if (fstat(fileno(rsrc_file), &rsrc_stbuf))
  998.                                 error("Cannot stat %s", rsrc_fname);*/
  999.                         mtim = unix2mac((ulong)rsrc_stbuf.st_mtime);
  1000.                         ctim = unix2mac((ulong)rsrc_stbuf.st_ctime);
  1001.                         rlen = long2mac(rsrc_stbuf.st_size);
  1002.                 }
  1003.  
  1004.                 /* stuff header data into the info header */
  1005.  
  1006.                 bzero((char*)&info, sizeof(info_header));
  1007.  
  1008.                 info.nlen = strlen(base_fname);
  1009.                 info.nlen = (info.nlen > NAMELEN) ? NAMELEN : info.nlen;
  1010.                 strncpy((char*)info.name, base_fname, (int)info.nlen);
  1011.  
  1012.                 /* now make sure the resulting name is valid */
  1013.                 info.nlen = macify((char*)info.name, (int)info.nlen, 1);
  1014.  
  1015.                 info.uploadvers = '\201';
  1016.                 info.readvers = '\201';
  1017.  
  1018.                 bcopy((char*)&mtim, (char*)info.mtim, 4);
  1019.                 bcopy((char*)&ctim, (char*)info.ctim, 4);
  1020.  
  1021.                 bcopy((char*)&dlen, (char*)info.dlen, 4);
  1022.                 bcopy((char*)&rlen, (char*)info.rlen, 4);
  1023.  
  1024.                 switch (mode) {
  1025.                 case TEXT:
  1026.                         bcopy(mac_type ? mac_type : "TEXT", (char*)info.type, 4);
  1027.                         bcopy(mac_auth ? mac_auth : "MACA", (char*)info.auth, 4);
  1028.                         break;
  1029.                 case DATA:
  1030.                 case RSRC:
  1031.                 case BOTH:
  1032.                         bcopy(mac_type ? mac_type : "????", (char*)info.type, 4);
  1033.                         bcopy(mac_auth ? mac_auth : "????", (char*)info.auth, 4);
  1034.                         break;
  1035.                 default:
  1036.                         error("Internal error: unexpected mode", "");
  1037.                         break;
  1038.                 }
  1039.  
  1040.                 /* calculate CRC */
  1041.                 crc = calc_mb_crc((unsigned char*)&info, 124L, 0);
  1042.                 info.crc[0] = (char) (crc >> 8);
  1043.                 info.crc[1] = (char) crc;
  1044.  
  1045.                 /* Create the .bin file and write the info to it */
  1046.  
  1047.                 len = strlen(dir) + strlen(base_fname) + strlen(ext) + 1;
  1048.                 if (len >= sizeof(binfname))
  1049.                         error("Error: generated binfname would be too long", "");
  1050.                 /*
  1051.                  * base_fname does not need to be unixified --
  1052.                  * was valid coming in
  1053.                  */
  1054.                 sprintf(binfname, "%s%s", dir, base_fname, ext);
  1055.                 binfile = mopen(binfname, "", "w");
  1056.  
  1057.                 converting(info.name, (int)info.nlen, info.type, info.auth);
  1058.                 print_bin_hdr("Creating", &info);
  1059.                 if (1 != fwrite((char*)&info, sizeof(info), 1, binfile))
  1060.                         error("fwrite failed on binfile", "");
  1061.  
  1062.                 /* pump out the data portion */
  1063.                 if (*data_fname) {
  1064.                         nchars = data_stbuf.st_size;
  1065.                         extra_chars = 127 - (nchars + 127) % 128;
  1066.  
  1067.                         if (translate_eol)
  1068.                                 while (nchars--) {
  1069.                                         (b = getc(data_file)) == EOF &&
  1070.                                                 error("Error: getc failed on data_file", "");
  1071.                                         if (b == LF)
  1072.                                                 b = CR;
  1073.                                         if (putc((char)b, binfile) == EOF)
  1074.                                                 {error("Error: putc failed on binfile", "");}
  1075.  
  1076.                                 }
  1077.  
  1078.                         else
  1079.                                 while (nchars--) {
  1080.                                         (b = getc(data_file)) == EOF &&
  1081.                                                 error("Error: getc failed on data_file", "");
  1082.                                         putc((char)b, binfile) == EOF &&
  1083.                                                 error("Error: putc failed on binfile", "");
  1084.                                 }
  1085.  
  1086.                         while (extra_chars--) {
  1087.                                 putc(0, binfile) == EOF &&
  1088.                                         error("Error: putc failed on binfile", "");
  1089.                         }
  1090.  
  1091.                         mclose(&data_file, "txtfile");
  1092.                 }
  1093.  
  1094.                 /* pump out the rsrc portion */
  1095.                 if (*rsrc_fname) {
  1096.                         nchars = rsrc_stbuf.st_size;
  1097.                         extra_chars = 127 - (nchars + 127) % 128;
  1098.  
  1099.                         while (nchars--) {
  1100.                                 (b = getc(rsrc_file)) == EOF &&
  1101.                                         error("Error: getc failed on rsrc_file", "");
  1102.                                 putc((char)b, binfile) == EOF &&
  1103.                                         error("Error: putc failed on binfile", "");
  1104.                         }
  1105.  
  1106.                         while (extra_chars--) {
  1107.                                 putc(0, binfile) == EOF &&
  1108.                                         error("Error: putc failed on binfile", "");
  1109.                         }
  1110.  
  1111.                         mclose(&rsrc_file, "txtfile");
  1112.                 }
  1113.  
  1114.                 mclose(&binfile, "binfile");
  1115.         }
  1116. }
  1117.  
  1118. /*
  1119.         This procedure basically copies the MacBinary input file to the
  1120.         output file(s); in TEXT (translate_eol) mode it changes CR's to
  1121.         LF's, and in any mode it skikps over the Mac info header.
  1122.  */
  1123.  
  1124. re_other(int mode)
  1125. {
  1126.         char base_fname[SYSNAMELEN];
  1127.         char data_fname[SYSNAMELEN], rsrc_fname[SYSNAMELEN], binfname[SYSNAMELEN];
  1128.         FILE *data_file, *rsrc_file, *binfile;
  1129.         char *data_suffix, *rsrc_suffix;
  1130.  
  1131.         info_header info;
  1132.         register ulong b;
  1133.         register ulong nchars;
  1134.         ulong temp;
  1135.         int extra_chars;
  1136.         long len;
  1137.  
  1138.         while (hqxnames_left[0][0] != '-') {
  1139.  
  1140.                 /* suck in the MacBinary header */
  1141.                 if (strlen(*hqxnames_left) >= sizeof(binfname))
  1142.                         error("Error: specified binfname is too long", "");
  1143.                 strcpy(binfname, *hqxnames_left++);
  1144.                 binfile = mopen(binfname, ext, "r");
  1145.                 if (1 != fread((char*)&info, sizeof(info), 1, binfile))
  1146.                         error("fread failed on binfile", "");
  1147.  
  1148.                 /* figure out the target base file name */
  1149.                 if (info.nlen >= NAMELEN)
  1150.                         error("Error: corrupt BinHex data format", "");
  1151.                 strncpy(base_fname, (char*)info.name, (int)info.nlen);
  1152.                 base_fname[info.nlen] = '\0';
  1153.                 converting(info.name, (int)info.nlen, info.type, info.auth);
  1154.                 print_bin_hdr("Reading", &info);
  1155.  
  1156.                 /* ensure the output files have no bogus UNIX characters */
  1157.                 unixify(base_fname);
  1158.                 mode_to_fname_suffix(mode, base_fname,
  1159.                         data_fname, &data_suffix, rsrc_fname, &rsrc_suffix);
  1160.  
  1161.                 /* always use suffix on write */
  1162.  
  1163.                 if (*data_fname) {
  1164.                         len = strlen(data_fname) + strlen(data_suffix);
  1165.                         if (len >= sizeof(data_fname))
  1166.                                 error("Error: generated data_fname would be too long", "");
  1167.                         strcat(data_fname, data_suffix);
  1168.                         data_file = mopen(data_fname, "", "w");
  1169.                 }
  1170.  
  1171.                 if (*rsrc_fname) {
  1172.                         len = strlen(rsrc_fname) + strlen(rsrc_suffix);
  1173.                         if (len >= sizeof(rsrc_fname))
  1174.                                 error("Error: generated rsrc_fname would be too long", "");
  1175.                         strcat(rsrc_fname, rsrc_suffix);
  1176.                         rsrc_file = mopen(rsrc_fname, "", "w");
  1177.                 }
  1178.  
  1179.                 /* process the data fork */
  1180.                 bcopy((char*)info.dlen, (char *) &temp, 4);
  1181.                 nchars = mac2long(temp);
  1182.                 extra_chars = 127 - (nchars + 127) % 128;
  1183.  
  1184.                 if (*data_fname) {
  1185.                 //FILE *fil=fopen(data_fname,"w");
  1186.  
  1187.  
  1188.                         if (translate_eol)
  1189.                                 while (nchars--) {
  1190.                                         (b = getc(binfile)) == EOF &&
  1191.                                                 error("Error: getc failed on binfile", "");
  1192.                                         if (b == CR)
  1193.                                                 b = LF;
  1194.  if (feof(data_file)) error("Error: putc failed on file","");
  1195.  putc((char)b, data_file);
  1196.                                 }
  1197.  
  1198.                         else
  1199.                                 while (nchars--) {
  1200.                                         (b = getc(binfile)) == EOF &&
  1201.                                                 error("Error: getc failed on binfile", "");
  1202.  if (feof(data_file)) error("Error: putc failed on file","");
  1203.  putc((char)b, data_file);
  1204.                                 }
  1205.                         mclose(&data_file, data_fname);
  1206.  
  1207.                 } else {
  1208.  
  1209.                         /* skip the actual data */
  1210.                         while (nchars--) {
  1211.                                 getc(binfile) == EOF &&
  1212.                                         error("Error: getc failed on binfile", "");
  1213.                         }
  1214.                 }
  1215.  
  1216.                 /* eat the padding to 128 byte boundary */
  1217.                 while (extra_chars--) {
  1218.                         getc(binfile) == EOF &&
  1219.                                 error("Error: getc failed on binfile", "");
  1220.                 }
  1221.  
  1222.                 /* process the rsrc fork */
  1223.  
  1224.                 bcopy((char*)info.rlen, (char *) &temp, 4);
  1225.                 nchars = mac2long(temp);
  1226.  
  1227.                 if (*rsrc_fname) {
  1228.                         while (nchars--) {
  1229.                                 (b = getc(binfile)) == EOF &&
  1230.                                         error("Error: getc failed on binfile", "");
  1231.                                 if (feof(rsrc_file)) error("Error: putc failed on file","");
  1232.                                 putc((char)b, rsrc_file);
  1233.                         }
  1234.                         mclose(&rsrc_file, rsrc_fname);
  1235.                 }
  1236.  
  1237.                 mclose(&binfile, "binfile");
  1238.         }
  1239. }
  1240.  
  1241. usage()
  1242. {
  1243.  fprintf(stderr,"Usage: %s { [option] ... name ...} ...\n",cmdname);
  1244.  fprintf(stderr,"version:\t%4.2f\ndefault:\t-xDqv\n\noption:\n",VERSION/100.);
  1245.  fprintf(stderr,"\t-x\tBinHex        .hqx  <-> MacBinary\n");
  1246.  fprintf(stderr,"\t-u\tText(trans)   .text <-> MacBinary\n");
  1247.  fprintf(stderr,"\t-h\tHost(as is)   .text <-> MacBinary\n");
  1248.  fprintf(stderr,"\t-d\tData          .data <-> MacBinary\n");
  1249.  fprintf(stderr,"\t-r\tResource      .rsrc <-> MacBinary\n");
  1250.  fprintf(stderr,"\t-b\tBoth    .data .rsrc <-> MacBinary\n\n");
  1251.  fprintf(stderr,"\t-D\tDownload       Other -> MacBinary\n");
  1252.  fprintf(stderr,"\t-U\tUpload     MacBinary -> Other\n\n");
  1253.  fprintf(stderr,"\t-p\tBinHex -> MacBinary => unpack PIT\n");
  1254.  fprintf(stderr,"\t-q\tdisable unpack PIT\n");
  1255.  fprintf(stderr,"\t-t\ttranslate end-of-line chars (useful with -b)\n\n");
  1256.  fprintf(stderr,"\t-I\tInformation only (does not write output files)\n");
  1257.  fprintf(stderr,"\t-P\tPipe output to stdout\n");
  1258.  fprintf(stderr,"\t-s\tsilent\n");
  1259.  fprintf(stderr,"\t-S\tSilent about ``Converting ... '' lines too\n");
  1260.  fprintf(stderr,"\t-v\tverbose\n");
  1261.  fprintf(stderr,"\t-V\tVerbose, includes debugging information\n");
  1262.  fprintf(stderr,"\t-VV\tVery Verbose, includes extra debugging information\n");
  1263.  fprintf(stderr,"\t-H\tdisable skip-legal-but-suspect-lines Heuristic\n\n");
  1264.  fprintf(stderr,"Environment:\n");
  1265.  fprintf(stderr,"\tMAC_FILETYPE  \tTEXT|????\tMac file type for Text|other\n");
  1266.  fprintf(stderr,"\tMAC_EDITOR    \tMACA|????\tMac creator (author) for Text|other\n");
  1267.  fprintf(stderr,"\tMAC_EXT       \t.bin     \textension for -D\n");
  1268.  fprintf(stderr,"\tMAC_DLOAD_DIR \t.        \tdirectory for -D\n");
  1269.  fprintf(stderr,"\tMAC_LINE_LIMIT\tnone     \tmaximum line length for -Ux\n");
  1270.  exit(1);
  1271. }
  1272.  
  1273.  
  1274. /* My FileIO routines, to enable clean implementation of info_only */
  1275. /* If info_only and open for write, inform user and use devnull instead */
  1276. /* If pipe_out and open for write, inform user and use stdout instead */
  1277. /* If trying to close devnull or stdout, don't */
  1278.  
  1279. FILE *
  1280. mopen(char *name, char *exten, char *type)
  1281. {
  1282.         FILE *result;
  1283.  
  1284.         switch (*type) {
  1285.         case 'r':
  1286.                 result = fopen(name, type);
  1287.                 if (result == NULL && *exten) {
  1288.                         /* see if adding the extension would help */
  1289.                         int len_root = strlen(name);
  1290.                         int len_ext = strlen(exten);
  1291.                         char *dotspot = name + len_root - len_ext;
  1292.                         if (strcmp(exten, dotspot)) {
  1293.                                 if (len_root + len_ext >= SYSNAMELEN)
  1294.                                         error("Error: generated file name would be too long", "");
  1295.                                 strcat(name, exten);
  1296.                                 result = fopen(name, type);
  1297.                         }
  1298.                 }
  1299.                 if (result == NULL)
  1300.                         error("Cannot open %s for read", name);
  1301.                 else {
  1302.                         fprintf(verbose, "\n%-15s%-27s\n",
  1303.                                 "Input file", name);
  1304.                         fflush(verbose);
  1305.                 }
  1306.                 break;
  1307.  
  1308.         case 'w':
  1309.                 if (info_only) {
  1310.                         fprintf(verbose, " %-14s%-27s -I (info only) specified\n",
  1311.                                 " No output to", name);
  1312.                         fflush(verbose);
  1313.                         result = devnull;
  1314.                 } else if (pipe_out) {
  1315.                         fprintf(verbose, " %-14s%-27s -P (pipe to stdout) specified\n",
  1316.                                 " stdout, vice", name);
  1317.                         fflush(verbose);
  1318.                         result = stdout;
  1319.                 } else {
  1320.                         result = fopen(name, type);
  1321.                         if (result == NULL)
  1322.                                 {error("Cannot open %s for write", name);}
  1323.                         else {
  1324.                                 fprintf(verbose, "%-15s%-27s\n",
  1325.                                         "Output file", name);
  1326.                                 fflush(verbose);
  1327.                         }
  1328.                 }
  1329.                 break;
  1330.  
  1331.         default:
  1332.                 fprintf(stderr, "%s: internal error in mopen -- exiting\n", cmdname);
  1333.                 exit(2);
  1334.  
  1335.         }
  1336.  
  1337.         return result;
  1338. }
  1339.  
  1340. mclose(FILE **stream_p, char *name)
  1341. {
  1342.         if (*stream_p && *stream_p != devnull && *stream_p != stdout) {
  1343.                 {
  1344.                 fclose(*stream_p);
  1345.                         }
  1346.         }
  1347.  
  1348.         *stream_p = (FILE *)0;
  1349. }
  1350.  
  1351. converting(byte *name, int len, byte *type, byte *auth)
  1352. {
  1353.         char buffer[SYSNAMELEN];
  1354.  
  1355.         /* make a copy to ensure null terminated */
  1356.         strncpy(buffer, (char*)name, len);
  1357.         buffer[len] = 0;
  1358.  
  1359.         fprintf(convert,
  1360.             "%-15s%-30s type = \"%4.4s\", author = \"%4.4s\"\n",
  1361.             info_only ? "Inspecting" : "Converting",
  1362.                  buffer, type, auth);
  1363.         fflush(convert);
  1364. }
  1365.  
  1366. print_bin_hdr(char *msg, info_header *hdr)
  1367. {
  1368.         ulong otime, xtime;
  1369.         long    dlen, rlen;
  1370.         bcopy((char*)hdr->ctim, (char*)&otime, 4);
  1371. #ifndef NDEBUG
  1372.         DEBUG && fprintf(Mydebug,
  1373.                 "DEBUG: verifying mac2unix/unix2mac: 0x%8lx, 0x%8lx\n",
  1374.                 otime, unix2mac(mac2unix(otime)));
  1375. #endif
  1376.  
  1377.         bcopy((char*)hdr->dlen, (char*)&dlen, 4);
  1378.         bcopy((char*)hdr->rlen, (char*)&rlen, 4);
  1379.  
  1380. #ifndef NDEBUG
  1381.         DEBUG && fprintf(Mydebug, "%s\n\tName     %.*s\n\tType     %.4s\n\tCreator  %.4s\n\tDataLen  %ld\n\tRsrcLen  %ld\n",
  1382.                 msg,
  1383.                 hdr->nlen, hdr->name,
  1384.                 hdr->type,
  1385.                 hdr->auth,
  1386.                 mac2long(dlen),
  1387.                 mac2long(rlen),
  1388.                 0);
  1389. #endif
  1390.  
  1391.         bcopy((char*)hdr->ctim, (char*)&otime, 4);
  1392.         xtime = mac2unix(otime);
  1393.  
  1394. #ifndef NDEBUG
  1395.         DEBUG && fprintf(Mydebug, "raw ctim: 0x%8lx, mac2unix: 0x%8lx\n",
  1396.                 otime, xtime);
  1397.         DEBUG && fflush(Mydebug);
  1398. #endif
  1399.  
  1400.         fprintf(verbose, "\tCreated  %s", ctime((time_t *)&xtime));
  1401.         fflush(verbose);
  1402.  
  1403.         bcopy((char*)hdr->mtim, (char*)&otime, 4);
  1404.         xtime = mac2unix(otime);
  1405. #ifndef NDEBUG
  1406.         DEBUG && fprintf(Mydebug, "raw mtim: 0x%8lx, mac2unix: 0x%8lx\n",
  1407.                 otime, xtime);
  1408.         DEBUG && fflush(Mydebug);
  1409. #endif
  1410.         fprintf(verbose, "\tModified %s", ctime((time_t *)&xtime));
  1411.         fflush(verbose);
  1412.  
  1413. }
  1414.