home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip540.zip / human68k / human68k.c < prev    next >
C/C++ Source or Header  |  1998-06-10  |  27KB  |  830 lines

  1. /*---------------------------------------------------------------------------
  2.  
  3.   human68k.c
  4.  
  5.   Human68K-specific routines for use with Info-ZIP's UnZip 5.1 and later.
  6.  
  7.   Contains:  do_wild()
  8.              mapattr()
  9.              mapname()
  10.              checkdir()
  11.              close_outfile()
  12.              stamp_file()                   (TIMESTAMP only)
  13.              version()
  14.              TwentyOne()
  15.              normalize_name()
  16.  
  17.   ---------------------------------------------------------------------------*/
  18.  
  19.  
  20. #include <dirent.h>
  21. #include <sys/dos.h>
  22. #include <sys/xunistd.h>
  23. #include <jstring.h>
  24. #define UNZIP_INTERNAL
  25. #include "unzip.h"
  26.  
  27.  
  28. static void normalize_name(char *);
  29.  
  30. static int created_dir;        /* used in mapname(), checkdir() */
  31. static int renamed_fullpath;   /* ditto */
  32.  
  33. #ifndef SFX
  34.  
  35. /**********************/
  36. /* Function do_wild() */
  37. /**********************/
  38.  
  39. char *do_wild(__G__ wildspec)
  40.     __GDEF
  41.     char *wildspec;         /* only used first time on a given dir */
  42. {
  43.     static DIR *dir = NULL;
  44.     static char *dirname, *wildname, matchname[FILNAMSIZ];
  45.     static int firstcall=TRUE, have_dirname, dirnamelen;
  46.     struct dirent *file;
  47.  
  48.  
  49.     /* Even when we're just returning wildspec, we *always* do so in
  50.      * matchname[]--calling routine is allowed to append four characters
  51.      * to the returned string, and wildspec may be a pointer to argv[].
  52.      */
  53.     if (firstcall) {        /* first call:  must initialize everything */
  54.         firstcall = FALSE;
  55.  
  56.         if (!iswild(wildspec)) {
  57.             strcpy(matchname, wildspec);
  58.             have_dirname = FALSE;
  59.             dir = NULL;
  60.             return matchname;
  61.         }
  62.  
  63.         /* break the wildspec into a directory part and a wildcard filename */
  64.         if ((wildname = strrchr(wildspec, '/')) == NULL) {
  65.             dirname = ".";
  66.             dirnamelen = 1;
  67.             have_dirname = FALSE;
  68.             wildname = wildspec;
  69.         } else {
  70.             ++wildname;     /* point at character after '/' */
  71.             dirnamelen = wildname - wildspec;
  72.             if ((dirname = (char *)malloc(dirnamelen+1)) == NULL) {
  73.                 Info(slide, 1, ((char *)slide,
  74.                   "warning:  cannot allocate wildcard buffers\n"));
  75.                 strcpy(matchname, wildspec);
  76.                 return matchname;   /* but maybe filespec was not a wildcard */
  77.             }
  78.             strncpy(dirname, wildspec, dirnamelen);
  79.             dirname[dirnamelen] = '\0';   /* terminate for strcpy below */
  80.             have_dirname = TRUE;
  81.         }
  82.  
  83.         if ((dir = opendir(dirname)) != NULL) {
  84.             while ((file = readdir(dir)) != NULL) {
  85.                 Trace((stderr, "do_wild:  readdir returns %s\n", file->d_name));
  86.                 if (file->d_name[0] == '.' && wildname[0] != '.')
  87.                     continue;  /* Unix:  '*' and '?' do not match leading dot */
  88.                 if (match(file->d_name, wildname, 0) &&  /* 0 == case sens. */
  89.                     /* skip "." and ".." directory entries */
  90.                     strcmp(file->d_name, ".") && strcmp(file->d_name, "..")) {
  91.                     Trace((stderr, "do_wild:  match() succeeds\n"));
  92.                     if (have_dirname) {
  93.                         strcpy(matchname, dirname);
  94.                         strcpy(matchname+dirnamelen, file->d_name);
  95.                     } else
  96.                         strcpy(matchname, file->d_name);
  97.                     return matchname;
  98.                 }
  99.             }
  100.             /* if we get to here directory is exhausted, so close it */
  101.             closedir(dir);
  102.             dir = NULL;
  103.         }
  104. #ifdef DEBUG
  105.         else {
  106.             Trace((stderr, "do_wild:  Opendir(%s) returns NULL\n", dirname));
  107.         }
  108. #endif /* DEBUG */
  109.  
  110.         /* return the raw wildspec in case that works (e.g., directory not
  111.          * searchable, but filespec was not wild and file is readable) */
  112.         strcpy(matchname, wildspec);
  113.         return matchname;
  114.     }
  115.  
  116.     /* last time through, might have failed opendir but returned raw wildspec */
  117.     if (dir == NULL) {
  118.         firstcall = TRUE;  /* nothing left to try--reset for new wildspec */
  119.         if (have_dirname)
  120.             free(dirname);
  121.         return (char *)NULL;
  122.     }
  123.  
  124.     /* If we've gotten this far, we've read and matched at least one entry
  125.      * successfully (in a previous call), so dirname has been copied into
  126.      * matchname already.
  127.      */
  128.     while ((file = readdir(dir)) != NULL) {
  129.         Trace((stderr, "do_wild:  readdir returns %s\n", file->d_name));
  130.         if (file->d_name[0] == '.' && wildname[0] != '.')
  131.             continue;   /* Unix:  '*' and '?' do not match leading dot */
  132.         if (match(file->d_name, wildname, 0)) {   /* 0 == don't ignore case */
  133.             Trace((stderr, "do_wild:  match() succeeds\n"));
  134.             if (have_dirname) {
  135.                 /* strcpy(matchname, dirname); */
  136.                 strcpy(matchname+dirnamelen, file->d_name);
  137.             } else
  138.                 strcpy(matchname, file->d_name);
  139.             return matchname;
  140.         }
  141.     }
  142.  
  143.     closedir(dir);     /* have read at least one dir entry; nothing left */
  144.     dir = NULL;
  145.     firstcall = TRUE;  /* reset for new wildspec */
  146.     if (have_dirname)
  147.         free(dirname);
  148.     return (char *)NULL;
  149.  
  150. } /* end function do_wild() */
  151.  
  152. #endif /* !SFX */
  153.  
  154.  
  155.  
  156.  
  157. /**********************/
  158. /* Function mapattr() */
  159. /**********************/
  160.  
  161. int mapattr(__G)
  162.     __GDEF
  163. {
  164.     ulg  tmp = G.crec.external_file_attributes;
  165.  
  166.     switch( G.pInfo->hostnum ) {
  167.         case UNIX_:
  168.         case VMS_:
  169.         case ACORN_:
  170.         case ATARI_:
  171.         case BEOS_:
  172.         case QDOS_:
  173.             G.pInfo->file_attr = _mode2dos(tmp >> 16);
  174.             break;
  175.         default:
  176.             /* set archive bit (file is not backed up) */
  177.             G.pInfo->file_attr =
  178.               (unsigned)(G.crec.external_file_attributes|32) & 0xff;
  179.             break;
  180.     }
  181.     return 0;
  182.  
  183. } /* end function mapattr() */
  184.  
  185.  
  186.  
  187.  
  188.  
  189. /**********************/
  190. /* Function mapname() */
  191. /**********************/
  192.                              /* return 0 if no error, 1 if caution (filename */
  193. int mapname(__G__ renamed)   /*  truncated), 2 if warning (skip file because */
  194.     __GDEF                   /*  dir doesn't exist), 3 if error (skip file), */
  195.     int renamed;             /*  or 10 if out of memory (skip file) */
  196. {                            /*  [also IZ_VOL_LABEL, IZ_CREATED_DIR] */
  197.     char pathcomp[FILNAMSIZ];    /* path-component buffer */
  198.     char *pp, *cp=(char *)NULL;  /* character pointers */
  199.     char *lastsemi=(char *)NULL; /* pointer to last semi-colon in pathcomp */
  200.     int quote = FALSE;           /* flags */
  201.     int error = 0;
  202.     register unsigned workch;    /* hold the character being tested */
  203.  
  204.  
  205. /*---------------------------------------------------------------------------
  206.     Initialize various pointers and counters and stuff.
  207.   ---------------------------------------------------------------------------*/
  208.  
  209.     if (G.pInfo->vollabel)
  210.         return IZ_VOL_LABEL;    /* can't set disk volume labels on Human68k */
  211.  
  212.     /* can create path as long as not just freshening, or if user told us */
  213.     G.create_dirs = (!uO.fflag || renamed);
  214.  
  215.     created_dir = FALSE;        /* not yet */
  216.  
  217.     /* user gave full pathname:  don't prepend rootpath */
  218.     renamed_fullpath = (renamed && (*filename == '/'));
  219.  
  220.     if (checkdir(__G__ (char *)NULL, INIT) == 10)
  221.         return 10;              /* initialize path buffer, unless no memory */
  222.  
  223.     *pathcomp = '\0';           /* initialize translation buffer */
  224.     pp = pathcomp;              /* point to translation buffer */
  225.     if (uO.jflag)               /* junking directories */
  226.         cp = (char *)strrchr(G.filename, '/');
  227.     if (cp == NULL)             /* no '/' or not junking dirs */
  228.         cp = G.filename;        /* point to internal zipfile-member pathname */
  229.     else
  230.         ++cp;                   /* point to start of last component of path */
  231.  
  232. /*---------------------------------------------------------------------------
  233.     Begin main loop through characters in filename.
  234.   ---------------------------------------------------------------------------*/
  235.  
  236.     while ((workch = (uch)*cp++) != 0) {
  237.         if (iskanji(workch)) {
  238.             *pp++ = (char)workch;
  239.             quote = TRUE;
  240.         } else if (quote) {                 /* if character quoted, */
  241.             *pp++ = (char)workch;    /*  include it literally */
  242.             quote = FALSE;
  243.         } else
  244.             switch (workch) {
  245.             case '/':             /* can assume -j flag not given */
  246.                 *pp = '\0';
  247.                 if ((error = checkdir(__G__ pathcomp, APPEND_DIR)) > 1)
  248.                     return error;
  249.                 pp = pathcomp;    /* reset conversion buffer for next piece */
  250.                 lastsemi = NULL;  /* leave directory semi-colons alone */
  251.                 break;
  252.  
  253.             case ';':             /* VMS version (or DEC-20 attrib?) */
  254.                 lastsemi = pp;         /* keep for now; remove VMS ";##" */
  255.                 *pp++ = (char)workch;  /*  later, if requested */
  256.                 break;
  257.  
  258.             case '\026':          /* control-V quote for special chars */
  259.                 quote = TRUE;     /* set flag for next character */
  260.                 break;
  261.  
  262.             case ' ':             /* change spaces to underscore under */
  263.                 *pp++ = '_';      /*  MTS; leave as spaces under Unix */
  264.                 break;
  265.  
  266.             default:
  267.                 /* allow European characters in filenames: */
  268.                 if (isprint(workch) || (128 <= workch && workch <= 254))
  269.                     *pp++ = (char)workch;
  270.             } /* end switch */
  271.  
  272.     } /* end while loop */
  273.  
  274.     *pp = '\0';                   /* done with pathcomp:  terminate it */
  275.  
  276.     /* if not saving them, remove VMS version numbers (appended ";###") */
  277.     if (!uO.V_flag && lastsemi) {
  278.         pp = lastsemi + 1;
  279.         while (isdigit((uch)(*pp)))
  280.             ++pp;
  281.         if (*pp == '\0')          /* only digits between ';' and end:  nuke */
  282.             *lastsemi = '\0';
  283.     }
  284.  
  285. /*---------------------------------------------------------------------------
  286.     Report if directory was created (and no file to create:  filename ended
  287.     in '/'), check name to be sure it exists, and combine path and name be-
  288.     fore exiting.
  289.   ---------------------------------------------------------------------------*/
  290.  
  291.     if (G.filename[strlen(G.filename) - 1] == '/') {
  292.         checkdir(__G__ G.filename, GETPATH);
  293.         if (created_dir) {
  294.             if (QCOND2) {
  295.                 Info(slide, 0, ((char *)slide, "   creating: %s\n",
  296.                   G.filename));
  297.             }
  298.             return IZ_CREATED_DIR;   /* set dir time (note trailing '/') */
  299.         }
  300.         return 2;   /* dir existed already; don't look for data to extract */
  301.     }
  302.  
  303.     if (*pathcomp == '\0') {
  304.         Info(slide, 1, ((char *)slide, "mapname:  conversion of %s failed\n",
  305.           G.filename));
  306.         return 3;
  307.     }
  308.  
  309.     checkdir(__G__ pathcomp, APPEND_NAME);  /* returns 1 if truncated: care? */
  310.     checkdir(__G__ G.filename, GETPATH);
  311.  
  312.     return error;
  313.  
  314. } /* end function mapname() */
  315.  
  316.  
  317.  
  318.  
  319.  
  320. /***********************/
  321. /* Function checkdir() */
  322. /***********************/
  323.  
  324. int checkdir(__G__ pathcomp, flag)
  325.     __GDEF
  326.     char *pathcomp;
  327.     int flag;
  328. /*
  329.  * returns:  1 - (on APPEND_NAME) truncated filename
  330.  *           2 - path doesn't exist, not allowed to create
  331.  *           3 - path doesn't exist, tried to create and failed; or
  332.  *               path exists and is not a directory, but is supposed to be
  333.  *           4 - path is too long
  334.  *          10 - can't allocate memory for filename buffers
  335.  */
  336. {
  337.     static int rootlen = 0;   /* length of rootpath */
  338.     static char *rootpath;    /* user's "extract-to" directory */
  339.     static char *buildpath;   /* full path (so far) to extracted file */
  340.     static char *end;         /* pointer to end of buildpath ('\0') */
  341.  
  342. #   define FN_MASK   7
  343. #   define FUNCTION  (flag & FN_MASK)
  344.  
  345.  
  346.  
  347. /*---------------------------------------------------------------------------
  348.     APPEND_DIR:  append the path component to the path being built and check
  349.     for its existence.  If doesn't exist and we are creating directories, do
  350.     so for this one; else signal success or error as appropriate.
  351.   ---------------------------------------------------------------------------*/
  352.  
  353.     if (FUNCTION == APPEND_DIR) {
  354.         int too_long = FALSE;
  355.         char *old_end = end;
  356.  
  357.         Trace((stderr, "appending dir segment [%s]\n", pathcomp));
  358.         while ((*end = *pathcomp++) != '\0')
  359.             ++end;
  360.         normalize_name(old_end);
  361.  
  362.         /* GRR:  could do better check, see if overrunning buffer as we go:
  363.          * check end-buildpath after each append, set warning variable if
  364.          * within 20 of FILNAMSIZ; then if var set, do careful check when
  365.          * appending.  Clear variable when begin new path. */
  366.  
  367.         if ((end-buildpath) > FILNAMSIZ-3)  /* need '/', one-char name, '\0' */
  368.             too_long = TRUE;                /* check if extracting directory? */
  369.         if (stat(buildpath, &G.statbuf))    /* path doesn't exist */
  370.         {
  371.             if (!G.create_dirs) { /* told not to create (freshening) */
  372.                 free(buildpath);
  373.                 return 2;         /* path doesn't exist:  nothing to do */
  374.             }
  375.             if (too_long) {
  376.                 Info(slide, 1, ((char *)slide,
  377.                   "checkdir error:  path too long: %s\n",
  378.                   buildpath));
  379.                 free(buildpath);
  380.                 return 4;         /* no room for filenames:  fatal */
  381.             }
  382.             if (MKDIR(buildpath, 0666) == -1) {   /* create the directory */
  383.                 Info(slide, 1, ((char *)slide,
  384.                   "checkdir error:  cannot create %s\n\
  385.                  unable to process %s.\n",
  386.                   buildpath, G.filename));
  387.                 free(buildpath);
  388.                 return 3;      /* path didn't exist, tried to create, failed */
  389.             }
  390.             created_dir = TRUE;
  391.         } else if (!S_ISDIR(G.statbuf.st_mode)) {
  392.             Info(slide, 1, ((char *)slide,
  393.               "checkdir error:  %s exists but is not directory\n\
  394.                  unable to process %s.\n",
  395.               buildpath, G.filename));
  396.             free(buildpath);
  397.             return 3;          /* path existed but wasn't dir */
  398.         }
  399.         if (too_long) {
  400.             Info(slide, 1, ((char *)slide,
  401.               "checkdir error:  path too long: %s\n",
  402.               buildpath));
  403.             free(buildpath);
  404.             return 4;         /* no room for filenames:  fatal */
  405.         }
  406.         *end++ = '/';
  407.         *end = '\0';
  408.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  409.         return 0;
  410.  
  411.     } /* end if (FUNCTION == APPEND_DIR) */
  412.  
  413. /*---------------------------------------------------------------------------
  414.     GETPATH:  copy full path to the string pointed at by pathcomp, and free
  415.     buildpath.
  416.   ---------------------------------------------------------------------------*/
  417.  
  418.     if (FUNCTION == GETPATH) {
  419.         strcpy(pathcomp, buildpath);
  420.         Trace((stderr, "getting and freeing path [%s]\n", pathcomp));
  421.         free(buildpath);
  422.         buildpath = end = (char *)NULL;
  423.         return 0;
  424.     }
  425.  
  426. /*---------------------------------------------------------------------------
  427.     APPEND_NAME:  assume the path component is the filename; append it and
  428.     return without checking for existence.
  429.   ---------------------------------------------------------------------------*/
  430.  
  431.     if (FUNCTION == APPEND_NAME) {
  432.         char *old_end = end;
  433.  
  434.         Trace((stderr, "appending filename [%s]\n", pathcomp));
  435.         while ((*end = *pathcomp++) != '\0') {
  436.             ++end;
  437.             normalize_name(old_end);
  438.             if ((end-buildpath) >= FILNAMSIZ) {
  439.                 *--end = '\0';
  440.                 Info(slide, 1, ((char *)slide,
  441.                   "checkdir warning:  path too long; truncating\n\
  442.                    %s\n                -> %s\n",
  443.                   G.filename, buildpath));
  444.                 return 1;   /* filename truncated */
  445.             }
  446.         }
  447.         Trace((stderr, "buildpath now = [%s]\n", buildpath));
  448.         return 0;  /* could check for existence here, prompt for new name... */
  449.     }
  450.  
  451. /*---------------------------------------------------------------------------
  452.     INIT:  allocate and initialize buffer space for the file currently being
  453.     extracted.  If file was renamed with an absolute path, don't prepend the
  454.     extract-to path.
  455.   ---------------------------------------------------------------------------*/
  456.  
  457. /* GRR:  for VMS and TOPS-20, add up to 13 to strlen */
  458.  
  459.     if (FUNCTION == INIT) {
  460.         Trace((stderr, "initializing buildpath to "));
  461.         if ((buildpath = (char *)malloc(strlen(G.filename)+rootlen+1)) ==
  462.             (char *)NULL)
  463.             return 10;
  464.         if ((rootlen > 0) && !renamed_fullpath) {
  465.             strcpy(buildpath, rootpath);
  466.             end = buildpath + rootlen;
  467.         } else {
  468.             *buildpath = '\0';
  469.             end = buildpath;
  470.         }
  471.         Trace((stderr, "[%s]\n", buildpath));
  472.         return 0;
  473.     }
  474.  
  475. /*---------------------------------------------------------------------------
  476.     ROOT:  if appropriate, store the path in rootpath and create it if neces-
  477.     sary; else assume it's a zipfile member and return.  This path segment
  478.     gets used in extracting all members from every zipfile specified on the
  479.     command line.
  480.   ---------------------------------------------------------------------------*/
  481.  
  482. #if (!defined(SFX) || defined(SFX_EXDIR))
  483.     if (FUNCTION == ROOT) {
  484.         Trace((stderr, "initializing root path to [%s]\n", pathcomp));
  485.         if (pathcomp == (char *)NULL) {
  486.             rootlen = 0;
  487.             return 0;
  488.         }
  489.         if ((rootlen = strlen(pathcomp)) > 0) {
  490.             int had_trailing_pathsep=FALSE;
  491.  
  492.             if (pathcomp[rootlen-1] == '/') {
  493.                 pathcomp[--rootlen] = '\0';
  494.                 had_trailing_pathsep = TRUE;
  495.             }
  496.             if (rootlen > 0 && (SSTAT(pathcomp, &G.statbuf) ||
  497.                 !S_ISDIR(G.statbuf.st_mode)))          /* path does not exist */
  498.             {
  499.                 if (!G.create_dirs /* || iswild(pathcomp) */ ) {
  500.                     rootlen = 0;
  501.                     return 2;   /* skip (or treat as stored file) */
  502.                 }
  503.                 /* create the directory (could add loop here to scan pathcomp
  504.                  * and create more than one level, but why really necessary?) */
  505.                 if (MKDIR(pathcomp, 0777) == -1) {
  506.                     Info(slide, 1, ((char *)slide,
  507.                       "checkdir:  cannot create extraction directory: %s\n",
  508.                       pathcomp));
  509.                     rootlen = 0;   /* path didn't exist, tried to create, and */
  510.                     return 3;  /* failed:  file exists, or 2+ levels required */
  511.                 }
  512.             }
  513.             if ((rootpath = (char *)malloc(rootlen+2)) == NULL) {
  514.                 rootlen = 0;
  515.                 return 10;
  516.             }
  517.             strcpy(rootpath, pathcomp);
  518.             rootpath[rootlen++] = '/';
  519.             rootpath[rootlen] = '\0';
  520.             Trace((stderr, "rootpath now = [%s]\n", rootpath));
  521.         }
  522.         return 0;
  523.     }
  524. #endif /* !SFX || SFX_EXDIR */
  525.  
  526. /*---------------------------------------------------------------------------
  527.     END:  free rootpath, immediately prior to program exit.
  528.   ---------------------------------------------------------------------------*/
  529.  
  530.     if (FUNCTION == END) {
  531.         Trace((stderr, "freeing rootpath\n"));
  532.         if (rootlen > 0) {
  533.             free(rootpath);
  534.             rootlen = 0;
  535.         }
  536.         return 0;
  537.     }
  538.  
  539.     return 99;  /* should never reach */
  540.  
  541. } /* end function checkdir() */
  542.  
  543.  
  544.  
  545.  
  546.  
  547. /****************************/
  548. /* Function close_outfile() */
  549. /****************************/
  550.  
  551. void close_outfile(__G)
  552.     __GDEF
  553. {
  554. #ifdef USE_EF_UT_TIME
  555.     iztimes z_utime;
  556.     struct tm *t;
  557.  
  558.     /* The following DOS date/time structure is machine-dependent as it
  559.      * assumes "little-endian" byte order.  For MSDOS-specific code, which
  560.      * is run on ix86 CPUs (or emulators), this assumption is valid; but
  561.      * care should be taken when using this code as template for other ports.
  562.      */
  563.     union {
  564.         ulg z_dostime;
  565.         struct {                /* date and time words */
  566.             union {             /* DOS file modification time word */
  567.                 ush ztime;
  568.                 struct {
  569.                     unsigned zt_se : 5;
  570.                     unsigned zt_mi : 6;
  571.                     unsigned zt_hr : 5;
  572.                 } _tf;
  573.             } _t;
  574.             union {             /* DOS file modification date word */
  575.                 ush zdate;
  576.                 struct {
  577.                     unsigned zd_dy : 5;
  578.                     unsigned zd_mo : 4;
  579.                     unsigned zd_yr : 7;
  580.                 } _df;
  581.             } _d;
  582.         } zt;
  583.     } dos_dt;
  584. #endif /* USE_EF_UT_TIME */
  585.  
  586.     if (uO.cflag) {
  587.         fclose(G.outfile);
  588.         return;
  589.     }
  590.  
  591. #ifdef USE_EF_UT_TIME
  592.     if (G.extra_field &&
  593. #ifdef IZ_CHECK_TZ
  594.         G.tz_is_valid &&
  595. #endif
  596.         (ef_scan_for_izux(G.extra_field, G.lrec.extra_field_length, 0,
  597.                           G.lrec.last_mod_dos_datetime, &z_utime, NULL)
  598.          & EB_UT_FL_MTIME))
  599.     {
  600.         TTrace((stderr, "close_outfile:  Unix e.f. modif. time = %ld\n",
  601.           z_utime.mtime));
  602.         /* round up (down if "up" overflows) to even seconds */
  603.         if (z_utime.mtime & 1)
  604.             z_utime.mtime = (z_utime.mtime + 1 > z_utime.mtime) ?
  605.                              z_utime.mtime + 1 : z_utime.mtime - 1;
  606.         TIMET_TO_NATIVE(z_utime.mtime)   /* NOP unless MSC 7.0 or Macintosh */
  607.         t = localtime(&(z_utime.mtime));
  608.     } else
  609.         t = (struct tm *)NULL;
  610.     if (t != (struct tm *)NULL) {
  611.         if (t->tm_year < 80) {
  612.             dos_dt.zt._t._tf.zt_se = 0;
  613.             dos_dt.zt._t._tf.zt_mi = 0;
  614.             dos_dt.zt._t._tf.zt_hr = 0;
  615.             dos_dt.zt._d._df.zd_dy = 1;
  616.             dos_dt.zt._d._df.zd_mo = 1;
  617.             dos_dt.zt._d._df.zd_yr = 0;
  618.         } else {
  619.             dos_dt.zt._t._tf.zt_se = t->tm_sec >> 1;
  620.             dos_dt.zt._t._tf.zt_mi = t->tm_min;
  621.             dos_dt.zt._t._tf.zt_hr = t->tm_hour;
  622.             dos_dt.zt._d._df.zd_dy = t->tm_mday;
  623.             dos_dt.zt._d._df.zd_mo = t->tm_mon + 1;
  624.             dos_dt.zt._d._df.zd_yr = t->tm_year - 80;
  625.         }
  626.     } else {
  627.         dos_dt.z_dostime = G.lrec.last_mod_dos_datetime;
  628.     }
  629.     _dos_filedate(fileno(G.outfile), dos_dt.z_dostime);
  630. #else /* !USE_EF_UT_TIME */
  631.     _dos_filedate(fileno(G.outfile), G.lrec.last_mod_dos_datetime);
  632. #endif /* ?USE_EF_UT_TIME */
  633.  
  634.     fclose(G.outfile);
  635.  
  636.     _dos_chmod(G.filename, G.pInfo->file_attr);
  637.  
  638. } /* end function close_outfile() */
  639.  
  640.  
  641.  
  642.  
  643.  
  644. #ifdef TIMESTAMP
  645.  
  646. /*************************/
  647. /* Function stamp_file() */
  648. /*************************/
  649.  
  650. int stamp_file(fname, modtime)
  651.     ZCONST char *fname;
  652.     time_t modtime;
  653. {
  654.     union {
  655.         ulg z_dostime;
  656.         struct {                /* date and time words */
  657.             union {             /* DOS file modification time word */
  658.                 ush ztime;
  659.                 struct {
  660.                     unsigned zt_se : 5;
  661.                     unsigned zt_mi : 6;
  662.                     unsigned zt_hr : 5;
  663.                 } _tf;
  664.             } _t;
  665.             union {             /* DOS file modification date word */
  666.                 ush zdate;
  667.                 struct {
  668.                     unsigned zd_dy : 5;
  669.                     unsigned zd_mo : 4;
  670.                     unsigned zd_yr : 7;
  671.                 } _df;
  672.             } _d;
  673.         } zt;
  674.     } dos_dt;
  675.     time_t t_even;
  676.     struct tm *t;
  677.     int fd;                             /* file handle */
  678.  
  679.     /* round up (down if "up" overflows) to even seconds */
  680.     t_even = ((modtime + 1 > modtime) ? modtime + 1 : modtime) & (~1);
  681.     TIMET_TO_NATIVE(t_even)             /* NOP unless MSC 7.0 or Macintosh */
  682.     t = localtime(&t_even);
  683.     if (t == (struct tm *)NULL)
  684.         return -1;                      /* time conversion error */
  685.     if (t->tm_year < 80) {
  686.         dos_dt.zt._t._tf.zt_se = 0;
  687.         dos_dt.zt._t._tf.zt_mi = 0;
  688.         dos_dt.zt._t._tf.zt_hr = 0;
  689.         dos_dt.zt._d._df.zd_dy = 1;
  690.         dos_dt.zt._d._df.zd_mo = 1;
  691.         dos_dt.zt._d._df.zd_yr = 0;
  692.     } else {
  693.         dos_dt.zt._t._tf.zt_se = t->tm_sec >> 1;
  694.         dos_dt.zt._t._tf.zt_mi = t->tm_min;
  695.         dos_dt.zt._t._tf.zt_hr = t->tm_hour;
  696.         dos_dt.zt._d._df.zd_dy = t->tm_mday;
  697.         dos_dt.zt._d._df.zd_mo = t->tm_mon + 1;
  698.         dos_dt.zt._d._df.zd_yr = t->tm_year - 80;
  699.     }
  700.     if (((fd = open((char *)fname, 0)) == -1) ||
  701.         (_dos_filedate(fileno(G.outfile), dos_dt.z_dostime)))
  702.     {
  703.         if (fd != -1)
  704.             close(fd);
  705.         return -1;
  706.     }
  707.     close(fd);
  708.     return 0;
  709.  
  710. } /* end function stamp_file() */
  711.  
  712. #endif /* TIMESTAMP */
  713.  
  714.  
  715.  
  716.  
  717. #ifndef SFX
  718.  
  719. /************************/
  720. /*  Function version()  */
  721. /************************/
  722.  
  723. void version(__G)
  724.     __GDEF
  725. {
  726.     int len;
  727. #if 0
  728.     char buf[40];
  729. #endif
  730.  
  731.     len = sprintf((char *)slide, LoadFarString(CompiledWith),
  732.  
  733. #ifdef __GNUC__
  734.       "gcc ", __VERSION__,
  735. #else
  736. #  if 0
  737.       "cc ", (sprintf(buf, " version %d", _RELEASE), buf),
  738. #  else
  739.       "unknown compiler", "",
  740. #  endif
  741. #endif
  742.  
  743.       "Human68k", " (X68000)",
  744.  
  745. #ifdef __DATE__
  746.       " on ", __DATE__
  747. #else
  748.       "", ""
  749. #endif
  750.       );
  751.  
  752.     (*G.message)((zvoid *)&G, slide, (ulg)len, 0);
  753.  
  754. } /* end function version() */
  755.  
  756. #endif /* !SFX */
  757.  
  758.  
  759.  
  760.  
  761.  
  762. /* Human68K-specific routines */
  763.  
  764. #define VALID_CHAR "&#()@_^{}!"
  765.  
  766. extern ulg TwentyOneOptions(void);
  767.  
  768. static int multi_period = 0;
  769. static int special_char = 0;
  770.  
  771. void
  772. InitTwentyOne(void)
  773. {
  774.     ulg stat;
  775.  
  776.     stat = TwentyOneOptions();
  777.     if (stat == 0 || stat == (unsigned long) -1) {
  778.         special_char = 0;
  779.         multi_period = 0;
  780.         return;
  781.     }
  782.     if (stat & (1UL << 29))
  783.         special_char = 1;
  784.     if (stat & (1UL << 28))
  785.         multi_period = 1;
  786. }
  787.  
  788. static void
  789. normalize_name(char *name)
  790. {
  791.     char *dot;
  792.     char *p;
  793.  
  794.     if (strlen(name) > 18) {    /* too long */
  795.         char base[18 + 1];
  796.         char ext[4 + 1];
  797.  
  798.         if ((dot = jstrrchr(name, '.')) != NULL)
  799.             *dot = '\0';
  800.         strncpy(base, name, 18);
  801.         base[18] = '\0';
  802.         if (dot) {
  803.             *dot = '.';
  804.             strncpy(ext, dot, 4);
  805.             ext[4] = '\0';
  806.         } else
  807.             *ext = '\0';
  808.         strcpy(name, base);
  809.         strcat(name, ext);
  810.     }
  811.     dot = NULL;
  812.     for (p = name; *p; p++) {
  813.         if (iskanji((unsigned char)*p) && p[1] != '\0')
  814.             p++;
  815.         else if (*p == '.') {
  816.             if (!multi_period) {
  817.                 dot = p;
  818.                 *p = '_';
  819.             }
  820.         } else if (!special_char && !isalnum (*p)
  821.                    && strchr(VALID_CHAR, *p) == NULL)
  822.             *p = '_';
  823.     }
  824.     if (dot != NULL) {
  825.         *dot = '.';
  826.         if (strlen(dot) > 4)
  827.             dot[4] = '\0';
  828.     }
  829. }
  830.