/*
   <p>Return the size of the currently open file.</p>

   <kbd>
   <b>environ - C programming support functions</b><br>
   Copyright (C) 2005  Martin C Gregorie
   
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Lesser GNU General Public License.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
  
   You should have received a copy of the Lesser GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   </kbd>
  
   <p>Contact Martin Gregorie at <i>martin@gregorie.org</i></p>
*/

#include <stdio.h>
#include "environ.h"

#ifdef OS9

#include <modes.h>

/*
   Return the size of the file <u>name</u> in bytes
   or -1 if the file is inaccessable.
*/
long filesize(name)
char*    name;
{
    int    f,size;
    
    f=open(name,S_IREAD & S_IOREAD);
    if (f == -1)
        return (long)f;    /* report failures with a negative reply */
        
    size = _gs_size(f);
    close(f);
    
    return (long)size;
}

#endif

#ifdef MSDOS
#include <stdio.h>
#include <sys/stat.h>

/*
   Return the size of the file <u>name</u> in bytes
   or -1 if the file is inaccessable.
*/
long filesize(name)
char*    name;
{
    struct stat statbuf;
    long        size;
    FILE        *f;

    f=fopen(name,"r");
    if (f == NULL)
        return (long)-1;    /* report failures with a negative reply */

    if (fstat(fileno(f), &statbuf))
        return (long)-1;
    fclose(f);
    size = statbuf.st_size;
    return (size);
}

#endif
#ifdef LINUX
#include <stdio.h>
#include <sys/stat.h>

/*
   Return the size of the file <u>name</u> in bytes
   or -1 if the file is inaccessable.
*/
long filesize(char *name)
{
    struct stat statbuf;
    long        size;
    FILE        *f;

    if (stat(name, &statbuf))
        return (long)-1;
    size = statbuf.st_size;
    return (size);
}

#endif

