home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / disk / archive / nspark_1 / nspark-1.7.5 / misc.c < prev    next >
C/C++ Source or Header  |  1993-08-20  |  7KB  |  313 lines

  1. /*
  2.  * miscellaneous functions
  3.  *
  4.  * $Header: misc.c 1.12 92/12/22 $
  5.  * $Log:    misc.c,v $
  6.  * Revision 1.12  92/12/22  09:54:29  duplain
  7.  * Changed #include <malloc.h> to #include <stdlib.h> .
  8.  * 
  9.  * Revision 1.11  92/12/09  09:42:36  duplain
  10.  * Simplified append_type().  Changed append_type() to append lowercase types.
  11.  * 
  12.  * Revision 1.10  92/12/08  10:20:07  duplain
  13.  * Added append_type().
  14.  * 
  15.  * Revision 1.9  92/12/07  17:18:58  duplain
  16.  * reformatted source.
  17.  * 
  18.  * Revision 1.8  92/11/12  09:03:30  duplain
  19.  * Fixed bug with realloc() size in uplevel().
  20.  * 
  21.  * Revision 1.7  92/11/06  12:42:28  duplain
  22.  * Changed print_details() so it supports PC archive headers correctly.
  23.  * 
  24.  * Revision 1.6  92/11/04  16:56:14  duplain
  25.  * Added check for PC archive header in print_header().
  26.  * 
  27.  * Revision 1.5  92/10/09  18:06:58  duplain
  28.  * Added "+1" to malloc() call in riscos_path()... SCO UNIX was quiet right
  29.  * to core dump :-)
  30.  * 
  31.  * Revision 1.4  92/10/07  10:56:39  duplain
  32.  * Added check for SYSV2 when including <malloc.h>.  Made riscos_path() compile
  33.  * for non-RISCOS systems only.
  34.  * 
  35.  * Revision 1.3  92/10/06  12:12:29  duplain
  36.  * Removed reference to date->csecond in print_details().
  37.  * 
  38.  * Revision 1.2  92/09/30  10:26:58  duplain
  39.  * Fixed basename().  Added riscos_path().
  40.  * 
  41.  * Revision 1.1  92/09/29  18:02:21  duplain
  42.  * Initial revision
  43.  * 
  44.  */
  45.  
  46. #include <stdio.h>
  47. #include "spark.h"
  48. #include "cproto.h"
  49. #include "main.h"
  50.  
  51. #ifdef BSD42
  52. #include <strings.h>
  53. #else /* not 4.2BSD */
  54. #include <string.h>
  55. #endif /* 4.2BSD */
  56.  
  57. #if defined(BSD42) || defined(SYSV2)
  58. extern char *malloc P__((unsigned len));
  59. extern char *realloc P__((char *ptr, unsigned len));
  60. #else /* not 4.2BSD or SysV.2 */
  61. #include <string.h>
  62. #include <stdlib.h>
  63. #endif /* 4.2BSD or SysV.2 */
  64.  
  65. #include "date.h"
  66. #include "misc.h"
  67.  
  68. #ifdef UNIX
  69. static char rcsid[] = "$Header: misc.c 1.12 92/12/22 $";
  70. #endif /* UNIX */
  71.  
  72. /*
  73.  * return last element in pathname
  74.  */
  75. char *
  76. basename(s)
  77.     char *s;
  78. {
  79.     char *cptr = s + strlen(s);
  80.     while (cptr > s) {
  81.     if (*cptr == PATHSEP)
  82.         return(++cptr);
  83.     cptr--;
  84.     }
  85.     return (s);
  86. }
  87.  
  88. #if defined(RISCOS)
  89. #define DOTARC    "_arc"
  90. #define DOT    '_'
  91. #else /* not RISCOS */
  92. #define DOTARC    ".arc"
  93. #define DOT    '.'
  94. #endif /* RISCOS */
  95.  
  96. /*
  97.  * append ".arc" ("_arc" in RISCOS) to a string, if an extension doesn't
  98.  * already exist, and return the new string.
  99.  */
  100. char *
  101. name_dot_arc(s)
  102.     char *s;
  103. {
  104.     static char *newname = NULL;
  105.  
  106.     /*
  107.      * check that there's room for the extension
  108.      */
  109.     if (strlen(basename(s)) + sizeof(DOTARC)-1 > FILENAMELEN)
  110.     return (s);
  111.  
  112.     /*
  113.      * free previous allocation (hope it's finished with :-/)
  114.      */
  115.     if (newname)
  116.     free(newname);
  117.     newname = malloc(strlen(s) + sizeof(DOTARC));
  118.     if (!newname)
  119.     return (s);    /* don't complain */
  120.     strcpy(newname, s);
  121.     strcat(newname, DOTARC);
  122.     return (newname);
  123. }
  124.  
  125. /*
  126.  * turn a local-host pathname into a RISC OS path
  127.  */
  128.  
  129. #ifndef RISCOS
  130. char *
  131. riscos_path(s)
  132.     register char *s;
  133. {
  134.     static char *riscosname = NULL;
  135.     register char *cptr;
  136.  
  137.     if (riscosname)
  138.     free(riscosname);
  139.  
  140.     riscosname = malloc(strlen(s)+1);
  141.     if (!riscosname)
  142.     return (NULL);
  143.     for (cptr = riscosname; *s; s++, cptr++)
  144.     if (*s == PATHSEP)
  145.         *cptr = '.';
  146.     else
  147.         *cptr = *s;
  148.     *cptr = '\0';
  149.     return (riscosname);
  150. }
  151. #endif /* RISC OS */
  152.  
  153. static char *pathname = NULL;
  154. /*
  155.  * uplevel() and downlevel() maintain the pathname as directories are found
  156.  * within the archive
  157.  */
  158. char *
  159. uplevel()
  160. {
  161.     register char *cptr;
  162.     register olen, nlen;
  163.  
  164.     if (!pathname)
  165.     return (NULL);
  166.         
  167.     olen = strlen(pathname);
  168.     cptr = pathname + olen-1;
  169.     while (cptr > pathname)
  170.     if (*cptr == PATHSEP) {
  171.         *cptr = '\0';
  172.         break;
  173.     } else
  174.         cptr--;
  175.  
  176.     if (cptr == pathname) {
  177.     free(pathname);
  178.     pathname = NULL;
  179.     } else {
  180.     nlen = strlen(pathname);
  181.     if (nlen < olen)
  182.         pathname = realloc(pathname, nlen+1);
  183.     }
  184.     return (pathname);
  185. }
  186.  
  187. char *
  188. downlevel(filename)
  189.     char *filename;
  190. {
  191.     register len, flen;
  192.  
  193.     if (!pathname)
  194.     len = 0;
  195.     else
  196.     len = strlen(pathname);
  197.  
  198.     flen = strlen(filename);
  199.     if (!len) {
  200.     pathname = malloc(flen+1);
  201.     if (pathname)
  202.         strcpy(pathname, filename);
  203.     } else {
  204.     pathname = realloc(pathname, len + flen + 2);
  205.     if (pathname) {
  206.         strcat(pathname, PATHSEPSTR);
  207.         strcat(pathname, filename);
  208.     }
  209.     }
  210.     return (pathname);
  211. }
  212.          
  213. /*
  214.  * print archive file details (size, data and time)
  215.  */
  216. void
  217. print_details(header)
  218.     Header *header;
  219. {                                         
  220.     Date *date;
  221.  
  222.     if (!header)
  223.     return;
  224.  
  225.     if (header->comptype & ARCHPACK) {
  226.     /* Archimedes archive header */
  227.  
  228.     if ((header->load & (Word)0xfff00000) == (Word)0xfff00000) {
  229.         /* time stamp valid */
  230.         date = makedate(header);
  231.         printf("%8ld %02d-%s-%02d %02d:%02d:%02d  &%03x",
  232.            header->origlen, date->day,
  233.            monthname(date->month), date->year,
  234.            date->hour, date->minute, date->second,
  235.            (header->load >> 8) & 0xfff);
  236.     } else {
  237.         /* load/exec only */
  238.         printf("%8d &%08lx &%08lx ----", header->origlen,
  239.            header->load, header->exec);
  240.     }
  241.     } else {
  242.     /* PC archive header */
  243.     date = makedate(header);
  244.     printf("%8ld %02d-%s-%02d %02d:%02d:%02d  ----",
  245.            header->origlen, date->day, monthname(date->month),
  246.            date->year, date->hour, date->minute, date->second);
  247.     }
  248. }
  249.  
  250. /*
  251.  * Test if the given filename matches any of the names specified in the 
  252.  * command line "files list".  This function is also used to test if a given
  253.  * pathname is contained in the any of the names given in the "files list".
  254.  *
  255.  * Returns non-zero if filename matches, or no "files list" exists.
  256.  */
  257. int
  258. inlist(filename)
  259.     char *filename;
  260. {
  261.     register len = strlen(filename);
  262.     register char **filelist = files;
  263.  
  264.     if (!*filelist)
  265.     return (1);        /* no "files list" */
  266.  
  267.     while (*filelist)
  268.     if (strncmp(filename, *filelist++, len) == 0)
  269.         return (1);
  270.     return (0);
  271. }
  272.  
  273. /*
  274.  * append the file's type to the end if it's name
  275.  * (this function assumes that enough storage is available in the argument
  276.  *  "filename" to store the additional characters ",XXX")
  277.  */
  278. int
  279. append_type(header, filename)
  280.     Header *header;
  281.     char *filename;
  282. {
  283.     char append[sizeof(",xxx")];
  284.  
  285.     if ((header->load & (Word)0xfff00000) == (Word)0xfff00000) {
  286.     /* valid time-stamp */
  287.     sprintf(append, ",%03x", (header->load >> 8) & 0xfff);
  288.     strcat(filename, append);
  289.     return (0);
  290.     }
  291.     return (-1);
  292. }
  293.  
  294. #ifdef DEBUGGING
  295. /*
  296.  * print archive file header info
  297.  */
  298. void
  299. print_header(header)
  300.     Header *header;
  301. {
  302.     if (!header)
  303.     return;
  304.  
  305.     printf("comptype=0x%x name=%s complen=%lu date=0x%x time=0x%x\n",
  306.        header->comptype, header->name, header->complen, header->date,
  307.        header->time);
  308.     printf("crc=0x%x origlen=%lu load=0x%lx exec=0x%lx attr=0x%lx\n",
  309.        header->crc, header->origlen, header->load, header->exec,
  310.        header->attr);
  311. }
  312. #endif /* DEBUGGING */
  313.