home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / files / archiver / rblharc / atari.c next >
C/C++ Source or Header  |  1993-07-08  |  4KB  |  209 lines

  1. #ifdef atarist
  2. #include <stdio.h>
  3. #include <osbind.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <memory.h>
  7. #include <ctype.h>
  8. #include <stddef.h>
  9. #include <support.h>
  10.  
  11. #define FALSE    0
  12. #define TRUE    1
  13. #define ISWILD(X)    ((X == '*')||(X == '?'))
  14.  
  15. /* extern char ** environ; */
  16. #ifdef __GNUC__
  17. size_t __DEFAULT_BUFSIZ__ = 32768L;
  18. long     _stksize = 24576L;
  19. #endif
  20.  
  21. char    **targv = (char **)NULL;
  22. int    targc = 0;
  23. char    dirbuf[256];
  24. int    wildcard = 0, desktop = 0;
  25. char     arcshpath[256];
  26. int     fullpath = TRUE;
  27.  
  28. static struct _dta statbuf;
  29. static char *cmd;
  30. /* static char * __junkenvironment = "\0\0\0\0"; */
  31.  
  32. static void expnd_args(char *s);
  33. static int handl_wild(char *s);
  34. static char *mkpathname(char *spec, char *file);
  35. static void add_targv(char *s);
  36.  
  37. main (argc, argv)
  38. int argc;
  39. char **argv;
  40. {
  41.     struct _dta *olddta;
  42.     int i;
  43.     char *temp;
  44.     
  45.     _malloczero(1);
  46.     olddta = (struct _dta *)Fgetdta ();
  47.     Fsetdta (&statbuf);
  48.     
  49.     if (argv[0][0] == '\0') 
  50.     {
  51.     add_targv("lharc");
  52.     desktop++;
  53. #if 0
  54.     environ = __junkenvironment; /* desktop doesn't have valid env */
  55. #endif
  56.     }
  57.     else
  58.     add_targv(argv[0]);
  59.     
  60.     cmd = argv[1];
  61.     
  62.     for (i = 1; i < argc; i++) 
  63.     {
  64.     /*
  65.      *  This prevents expnd_args from expanding directories.  We want
  66.      *  opendir in lharc.c to do this so that we get the proper dir
  67.       *  entries in the archive.
  68.      */
  69.     if (desktop) 
  70.     {
  71.         fullpath = FALSE;
  72.         if (temp = strstr (argv[i], "*.*")) 
  73.         {
  74.         wildcard++;
  75.         strncpy (arcshpath, argv[i], (strlen (argv[i]) - strlen (temp)) - 1);
  76.         }
  77.     }
  78.     
  79.     if (wildcard && desktop)
  80.     {
  81.         if ((strpbrk (argv[1], "AaMmUuCc")) && (strlen (arcshpath) > 3))
  82.         expnd_args (arcshpath);
  83.         else
  84.         expnd_args (argv[i]);
  85.     }
  86.     else
  87.         expnd_args (argv[i]);
  88.     }
  89.     
  90.     Fsetdta (olddta);
  91.     
  92.     add_targv("");                /* ensure space in targv */
  93.     targv[--targc] = NULL;    /* unfortunate unix semantics */
  94.     
  95.     getwd (dirbuf);
  96.     lzmain (targc, targv);
  97. }
  98.  
  99. /*
  100.  * Expand command line args
  101.  */
  102. static void expnd_args(s)
  103. register char *s;
  104. {
  105.     char next_arg[128];
  106.     register char *p;
  107.     register int contains_wild;
  108.     
  109.     while(*s != '\0')
  110.     {
  111.     p = next_arg;
  112.     while(isspace(*s)) 
  113.         s++; /* skip leading space */
  114.     if(*s != '\0')
  115.     {
  116.         contains_wild = FALSE;
  117.         while(!isspace(*s) && (*s != '\0'))
  118.         {
  119.         contains_wild |= ISWILD(*s);
  120.         *p++ = *s++;
  121.         }
  122.         *p = '\0';
  123.         
  124.         if(contains_wild)
  125.         {
  126.         if(handl_wild(next_arg))
  127.             return;
  128.         }
  129.         else
  130.         add_targv(next_arg);
  131.     } /* If */
  132.     } /* while */
  133.     
  134. }
  135.  
  136. /*
  137.  * expand wild card arguments. Return TRUE on error.
  138.  */
  139. static int handl_wild(s)
  140. char *s;
  141. {
  142.     char *path;
  143.     
  144.     if (strpbrk (cmd, "EeXx"))
  145.     return FALSE;
  146.     
  147.     path = strchr (cmd, 'z');
  148.     
  149.     if(Fsfirst(s, (path ? FA_DIR : 0)) != 0)
  150.     {
  151.     /* No match */
  152.     fprintf(stderr,"No Match for %s\n", s);
  153.     return TRUE;
  154.     }
  155.     if ((strcmp (statbuf.dta_name, ".") != 0) && (strcmp (statbuf.dta_name, "..") != 0))
  156.     add_targv(mkpathname (s, statbuf.dta_name));
  157.     
  158.     while(Fsnext() == 0)
  159.     {
  160.     if ((strcmp (statbuf.dta_name, ".") != 0) && (strcmp (statbuf.dta_name, "..") != 0))
  161.         add_targv(mkpathname (s, statbuf.dta_name));
  162.     }
  163.     
  164.     return FALSE;
  165. }
  166.  
  167. /*
  168.  * Given spec with a trailing wildcard and a base will name construct pathname
  169.  *
  170.  */
  171. static char *mkpathname(spec, file)
  172. register char *spec, *file;
  173. {
  174.     register char *p;
  175.     
  176.     if(((p = strrchr (spec, '\\')) == (char *)NULL))
  177.     /* no path name */
  178.     return file;
  179.     
  180.     while(*file != '\0')
  181.     *++p = *file++;
  182.     *++p = '\0';
  183.     
  184.     return spec;
  185. }
  186.  
  187. #define CHUNK_SIZE 16
  188.  
  189. static void add_targv(char *s)
  190. {
  191.     static size_t allocsize = 0;
  192.     static int avail = 0;
  193.     char temp[FILENAME_MAX];
  194.     
  195.     if(avail == 0)
  196.     { /* need more mem */
  197.     allocsize += (CHUNK_SIZE * sizeof(char **));
  198.     avail = CHUNK_SIZE;
  199.     targv = (char **)((targv == (char **)NULL) ? malloc(allocsize)
  200.               : realloc(targv, allocsize));
  201.     if(!targv)
  202.         error("out of memory");
  203.     }
  204.     dos2unx (s, temp);
  205.     targv[targc++] = strdup (temp);
  206.     avail--;
  207. }
  208. #endif /* atarist */
  209.