
/*
 *  Amiga specific functions
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <time.h>


/* maxmium pathlength for AmigaDOS */

#define MAXPATHLEN (255) /* BSTR */


/* definitions for our chmod() replacement, which should
   the correct flags out of "(h)sparwed"
*/

#define H_BIT (7) /* Hidden  */
#define S_BIT (6) /* Script  */
#define P_BIT (5) /* Pure    */
#define A_BIT (4) /* Archive */
#define R_BIT (3) /* Read    */
#define W_BIT (2) /* Write   */
#define E_BIT (1) /* Execute */
#define D_BIT (0) /* Delete  */


/* Flags themselves */

#define H_VAL (1<<H_BIT) /* 128 = 2^7 */
#define S_VAL (1<<S_BIT) /*  64 = 2^6 */
#define P_VAL (1<<P_BIT) /*  32 = 2^5 */
#define A_VAL (1<<A_BIT) /*  16 = 2^4 */
#define R_VAL (1<<R_BIT) /*   8 = 2^3 */
#define W_VAL (1<<W_BIT) /*   4 = 2^2 */
#define E_VAL (1<<E_BIT) /*   2 = 2^1 */
#define D_VAL (1<<D_BIT) /*   1 = 2^0 */


/* Masks for High-Active, only specific bit not set (same as for low) */

#define H_SET (~(H_VAL))
#define S_SET (~(S_VAL))
#define P_SET (~(P_VAL))
#define A_SET (~(A_VAL))


/* Masks for Low-Active, only specific bit not set (same as for high) */

#define R_SET (~(R_VAL))
#define W_SET (~(W_VAL))
#define E_SET (~(E_VAL))
#define D_SET (~(D_VAL))

int sas_chmod(char *filename, int modes)
{
 unsigned long mask = R_VAL | W_VAL | D_VAL | E_VAL; /* clear; low-active */


 /* since the unix tar archives usually don't seem to have to correct
    S_I flags set, we now do just override our own chmod()
    replacement via always using standard "rwed" settings.
    Until someone finds a better solution
 */

 modes = S_IREAD | S_IWRITE | S_IEXECUTE | S_IDELETE;

 /* if(modes & S_IHIDDEN)   mask = mask|H_VAL; */
    if(modes & S_ISCRIPT)   mask = mask|S_VAL;
    if(modes & S_IPURE)     mask = mask|P_VAL;
    if(modes & S_IARCHIVE)  mask = mask|A_VAL;
    if(modes & S_IREAD)     mask = mask&R_SET;
    if(modes & S_IWRITE)    mask = mask&W_SET;
    if(modes & S_IEXECUTE)  mask = mask&E_SET;
    if(modes & S_IDELETE)   mask = mask&D_SET;

 /* result = */ SetProtection(filename, mask);

 return(0); /* should have worked */
}

char *getwd(char *buf)
{
    return(getcwd(buf, MAXPATHLEN));
}

int readlink(char *path, char *name, int max)
{
    return(-1);
}

int symlink(void)
{
    return(-1);
}

int utime(void)
{
    return(0);
}

int umask(void)
{
    return(0);
}

int fork(void)
{
    return(-1);
}

int execlp(void)
{
    return(-1);
}


int findgid(void)
{
    return(0);
}

int finduid(void)
{
    return(0);
}

int pipe(void)
{
    return(-1);
}

int dup(void)
{
    return(-1);
}

int setmode(void)
{
    return(0);
}

extern int wildmat(char *s, char *p);

int fnmatch (char *pattern, char *string, int flags)
{
 wildmat(string, pattern);
}

#include <sys/timeb.h>

void ftime(struct timeb *ftz)
{
 extern time_t timezone;

 (void)time(ftz->time);

 /* Set the timezone global. */
 tzset();

 ftz->timezone = (int) timezone / 60;
}
