/* $VER: ar files.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  (30.01.99) phx
 *       File created.
 */

#ifdef AMIGA
#include <dos/dos.h>
#include <proto/dos.h>
#define UNIXOFFSET 252457200  /* seconds from 1.1.70 to 1.1.78 */

#else /* UNIX */
#include <sys/types.h>
#include <sys/stat.h>

#endif
#include <string.h>
#include "ar.h"
#include "archive.h"



#ifdef AMIGA

void getfstat(struct ArObject *obj,char *name)
/* Amiga version */
{
  BPTR lock;
  static struct FileInfoBlock fib;
  /*     ^^ must be longword aligned - vbcc guarantees that */

  if (lock = Lock(name,ACCESS_READ)) {
    if (Examine(lock,&fib)) {
      uint16 m = 0;
      uint32 p;

      obj->time = UNIXOFFSET+(uint32)(fib.fib_Date.ds_Days*(60*60*24) +
                                      fib.fib_Date.ds_Minute*60 +
                                      fib.fib_Date.ds_Tick/TICKS_PER_SECOND);
      obj->uid = fib.fib_OwnerUID;
      obj->gid = fib.fib_OwnerGID;
      p = fib.fib_Protection;
      if (!(p & FIBF_READ))  m |= 0400;
      if (!(p & FIBF_WRITE))  m |= 0200;
      if (!(p & FIBF_EXECUTE))  m |= 0100;
      if (p & FIBF_GRP_READ)  m |= 0040;
      if (p & FIBF_GRP_WRITE)  m |= 0020;
      if (p & FIBF_GRP_EXECUTE)  m |= 0010;
      if (p & FIBF_OTR_READ)  m |= 0004;
      if (p & FIBF_OTR_WRITE)  m |= 0002;
      if (p & FIBF_OTR_EXECUTE)  m |= 0001;
      obj->mode = m;
    }
    UnLock(lock);
  }
}


char *filepart(char *path)
/* Amiga version */
{
  return ((char *)FilePart((STRPTR)path));
}

#else

void getfstat(struct ArObject *obj,char *name)
/* Unix version */
{
  struct stat sb;

  if (!stat(name,&sb)) {
    obj->time = (uint32)sb.st_mtime;
    obj->uid = (uint16)sb.st_uid;
    obj->gid = (uint16)sb.st_gid;
    obj->mode = (uint16)sb.st_mode;
  }
}


char *filepart(char *path)
/* Unix version */
{
  char *ind;

  return ((ind = strrchr(path, '/')) ? ind + 1 : path);
}

#endif
