home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / mcvertppc / mcvert.c < prev    next >
Text File  |  1998-04-27  |  46KB  |  1,468 lines

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