Archive-Unzip-Burst

 view release on metacpan or  search on metacpan

unzip-6.0/amiga/stat.c  view on Meta::CPAN

#  include <sys/dir.h>               /* SAS/C dir function prototypes */
#  include <sys/types.h>
#  include <proto/exec.h>
#  include <proto/dos.h>
#endif

#ifndef SUCCESS
#  define SUCCESS (-1)
#  define FAILURE (0)
#endif


void close_leftover_open_dirs(void);    /* prototype */

static DIR *dir_cleanup_list = NULL;    /* for resource tracking */

/* CALL THIS WHEN HANDLING CTRL-C OR OTHER UNEXPECTED EXIT! */
void close_leftover_open_dirs(void)
{
    while (dir_cleanup_list)
        closedir(dir_cleanup_list);
}


unsigned short disk_not_mounted;

extern int stat(const char *file, struct stat *buf);

stat(file,buf)
const char *file;
struct stat *buf;
{

        struct FileInfoBlock *inf;
        BPTR lock;
        time_t ftime;
        struct tm local_tm;

        if( (lock = Lock((char *)file,SHARED_LOCK))==0 )
                /* file not found */
                return(-1);

        if( !(inf = (struct FileInfoBlock *)AllocMem(
                (long)sizeof(struct FileInfoBlock),MEMF_PUBLIC|MEMF_CLEAR)) )
        {
                UnLock(lock);
                return(-1);
        }

        if( Examine(lock,inf)==FAILURE )
        {
                FreeMem((char *)inf,(long)sizeof(*inf));
                UnLock(lock);
                return(-1);
        }

        /* fill in buf */
        buf->st_dev         =
        buf->st_nlink       =
        buf->st_uid         =
        buf->st_gid         =
        buf->st_rdev        = 0;
        buf->st_ino         = inf->fib_DiskKey;
        buf->st_blocks      = inf->fib_NumBlocks;
        buf->st_size        = inf->fib_Size;

        /* now the date.  AmigaDOS has weird datestamps---
         *      ds_Days is the number of days since 1-1-1978;
         *      however, as Unix wants date since 1-1-1970...
         */

        ftime =
                (inf->fib_Date.ds_Days * 86400 )                +
                (inf->fib_Date.ds_Minute * 60 )                 +
                (inf->fib_Date.ds_Tick / TICKS_PER_SECOND )     +
                (86400 * 8 * 365 )                              +
                (86400 * 2 );  /* two leap years */

        /* tzset(); */  /* this should be handled by mktime(), instead */
        /* ftime += timezone; */
        local_tm = *gmtime(&ftime);
        local_tm.tm_isdst = -1;
        ftime = mktime(&local_tm);

        buf->st_ctime =
        buf->st_atime =
        buf->st_mtime = ftime;

        buf->st_mode = (inf->fib_DirEntryType < 0 ? S_IFREG : S_IFDIR);

        /* lastly, throw in the protection bits */
        buf->st_mode |= ((inf->fib_Protection ^ 0xF) & 0xFF);

        FreeMem((char *)inf, (long)sizeof(*inf));
        UnLock((BPTR)lock);

        return(0);

}

int fstat(int handle, struct stat *buf)
{
    /* fake some reasonable values for stdin */
    buf->st_mode = (S_IREAD|S_IWRITE|S_IFREG);
    buf->st_size = -1;
    buf->st_mtime = time(&buf->st_mtime);
    return 0;
}


/* opendir(), readdir(), closedir(), rmdir(), and chmod() by Paul Kienitz. */

DIR *opendir(const char *path)
{
    DIR *dd = AllocMem(sizeof(DIR), MEMF_PUBLIC);
    if (!dd) return NULL;
    if (!(dd->d_parentlock = Lock((char *)path, MODE_OLDFILE))) {
        disk_not_mounted = IoErr() == ERROR_DEVICE_NOT_MOUNTED;
        FreeMem(dd, sizeof(DIR));
        return NULL;
    } else



( run in 0.630 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )