home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 9 Archive / 09-Archive.zip / unzip532.zip / acorn / acorn.c next >
C/C++ Source or Header  |  1997-10-21  |  26KB  |  787 lines

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