/* $VER: ar amiga.c V0.1 (31.01.98)
 *
 * This file is part of ar, a portable archive maintanance
 * utility for normal and BSD-style archives.
 * Copyright (c) 1999  Frank Wille
 *
 * ar is freeware and part of the portable and retargetable ANSI C
 * compiler vbcc, copyright (c) 1995-99 by Volker Barthelmann.
 * ar may be freely redistributed as long as no modifications are
 * made and nothing is charged for it. Non-commercial usage is allowed
 * without any restrictions.
 * EVERY PRODUCT OR PROGRAM DERIVED DIRECTLY FROM MY SOURCE MAY NOT BE
 * SOLD COMMERCIALLY WITHOUT PERMISSION FROM THE AUTHOR.
 *
 *
 * v0.1  (31.01.99) phx
 *       First working version, which only supports 'q' (quick append)
 *       and 't' (table of contents), reads and writes normals and
 *       BSD-style archives. Symbol table will not be created!
 * v0.0  (31.01.99) phx
 *       File created.
 */

#ifdef AMIGA
#include <exec/types.h>
#include <dos/dosasl.h>
#include <dos/dosextens.h>
#include <proto/dos.h>
#include <stdlib.h>
#include <string.h>

#define NAMEBUF 256



static int pattmatch(int first,int nargs,char *args[],char **argv)
{
  struct AnchorPath *ap;
  char *s;
  int i,n;

  if (ap = (struct AnchorPath *)calloc(sizeof(struct AnchorPath)+NAMEBUF,1)) {
    ap->ap_Strlen = NAMEBUF;
    ap->ap_BreakBits = 0;
  }
  else
    return (0);

  for (i=n=first; i<nargs; i++) {
    if (!MatchFirst((STRPTR)args[i],ap)) {
      do {
        if (argv) {
          if (s = malloc(strlen(ap->ap_Buf)+1)) {
            strcpy(s,ap->ap_Buf);
            argv[n] = s;
          }
          else {
            free(ap);
            return (0);
          }
        }
        n++;
      }
      while (!MatchNext(ap));
    }
    else {
      /* no existing file name, just copy original string */
      if (argv)
        argv[n] = args[i];
      n++;
    }
  }
  free(ap);
  return (n);
}


char **expand_args(int *argc,int nargs,char *args[],int first)
/* do pattern matching on arguments, allocate new argv array */
/* 'first' is the index of the first argument to do patt.matching for */
{
  int i;
  char **argv;

  if (first > nargs)
    first = nargs;
  if (*argc = pattmatch(first,nargs,args,NULL)) {
    if (argv = malloc((*argc+1) * sizeof(char *))) {
      argv[*argc] = NULL;
      for (i=0; i<first; i++)
        argv[i] = args[i];
      if (pattmatch(first,nargs,args,argv))
        return (argv);
      else
        free(argv);
    }
  }

  /* expand_args failed, use original argument list */
  argv = args;
  *argc = nargs;
  return (argv);
}


#endif /* AMIGA */
