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