/* %Z%%M% %I% %E% */

/*
 * Copyright (c) 1991-2005 by Wayne C. Gramlich.
 * All rights reserved.
 */

/*
 * This file implements the status base type:
 */

#ifndef MEMORY_H
#include "memory.h"
#endif

#ifndef STHEADERS_H
#include "stheaders.h"
#endif

#ifndef UNIX_ASSERT_H
#include "unix_assert.h"
#endif

#ifndef UNIX_SYS_STAT_H
#include "unix_sys_stat.h"
#endif

typedef struct stat *Status;
typedef void *String;

extern module___object status__module__object;

Status status___initial = (Status)0;

static object___object status_initial_object = {
	"",				/* Package name */
	"status",			/* Type name */
	"??",				/* Object name */
	(type___reference *)0,		/* Type reference */
	(int *)0,			/* Pointer to status size */
	0,				/* Static needs count */
	(need___entry *)0,		/* Static needs */
	5,				/* Parameter needs count */
	(need___entry *)0,		/* Parameter needs */
	(void **)&status___initial,	/* Object pointer */
	(instantiation___object *)0	/* Instantiation list */
};
static object___object *object_list[1] = {
	&status_initial_object,
};

void
status__external__initialize(void)
{
    status__module__object.object_count = 1;
    status__module__object.objects = object_list;
}

extern String string__unix_string(Str);

/*
 * status__access_get(status)
 *	This routine will return the access mode bits of {status}.
 */
unsigned
status__access_get(
    Status status)
{
    assert(status != (Status)0);
    return status->st_mode;
}

/*
 * status__address_get(status)
 *	This routine will return the address of {status}.
 */
unsigned
status__address_get(
    Status status)
{
    return (unsigned)status;
}

/*
 * status__is_directory_get(status)
 *	This routine will return 1 if {status} is a directory and 0
 *	otherwise.
 */
int
status__is_directory_get(
    Status status)
{
    assert(status != (Status)0);
    return (S_ISDIR(status->st_mode) ? 1 : 0);
}

/*
 * status__is_pipe_get(status)
 *	This routine will return 1 if {status} is a pipe and 0
 *	otherwise.
 */
int
status__is_pipe_get(
    Status status)
{
    assert(status != (Status)0);
    return (S_ISFIFO(status->st_mode) ? 1 : 0);
}

/*
 * status__is_regular_file_get(status)
 *	This routine will return 1 if {status} is a regular file and 0
 *	otherwise.
 */
int
status__is_regular_file_get(
    Status status)
{
    assert(status != (Status)0);
    return (S_ISREG(status->st_mode) ? 1 : 0);
}

/*
 * status__is_symbolic_link_get(status)
 *	This routine will return 1 if {status} is a symbolic link and 0
 *	otherwise.
 */
int
status__is_symbolic_link_get(
    Status status)
{
#ifdef LINUX
    assert(status != (Status)0);
    return (S_ISLNK(status->st_mode) ? 1 : 0);
#endif /* LINUX */
#ifdef WINDOWS
    assert(0);
    return 0;
#endif /* WINDOWS */
}

/*
 * status__lookup(status, file_name)
 *	This routine will lookup the status associated with {file_name}
 *	and store it in {status}.  1 is returned if {file_name} does
 *	not exist; otherwise 0 is returned.
 */
int
status__lookup(
    Status status,
    String file_name)
{
    Str unix_file_name;

    assert(status != (Status)0);
    unix_file_name = string__unix_string(file_name);
    if (stat(unix_file_name, status) < 0) {
	status->st_mtime = 0;
	return 1;
    }
    return 0;
}

/*
 * status__modification_time_get(status)
 *	This routine will return the modification time in {status}.
 */
unsigned
status__modification_time_get(
    Status status)
{
    assert(status != (Status)0);
    return status->st_mtime;
}

/*
 * status__allocate()
 *	This routine will allocate and return a {Status} object.
 */
Status
status__allocate(void)
{
    Status status;

    status = memory_alloc(Status);
    return status;
}

/*
 * status__size_get(status)
 *	This routine will return the modification time in {status}.
 */
unsigned
status__size_get(
    Status status)
{
    assert(status != (Status)0);
    return status->st_size;
}


